This source file includes following definitions.
- CreateDialog
- file_index_
- selected
- file_index
- FileSelected
- dialog
- FileSelected
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/ui/views/select_file_dialog_extension.h"
#include "base/files/file_path.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/shell_dialogs/selected_file_info.h"
namespace {
const SelectFileDialogExtension::RoutingID kDefaultRoutingID =
SelectFileDialogExtension::RoutingID();
}
class SelectFileDialogExtensionTest : public testing::Test {
public:
SelectFileDialogExtensionTest() {}
virtual ~SelectFileDialogExtensionTest() {}
static SelectFileDialogExtension* CreateDialog(
ui::SelectFileDialog::Listener* listener) {
SelectFileDialogExtension* dialog = new SelectFileDialogExtension(listener,
NULL);
EXPECT_FALSE(SelectFileDialogExtension::PendingExists(kDefaultRoutingID));
dialog->AddPending(kDefaultRoutingID);
EXPECT_TRUE(SelectFileDialogExtension::PendingExists(kDefaultRoutingID));
return dialog;
}
private:
DISALLOW_COPY_AND_ASSIGN(SelectFileDialogExtensionTest);
};
class TestListener : public ui::SelectFileDialog::Listener {
public:
TestListener() : selected_(false), file_index_(-1) {}
virtual ~TestListener() {}
bool selected() const { return selected_; }
int file_index() const { return file_index_; }
virtual void FileSelected(const base::FilePath& path,
int index,
void* params) OVERRIDE {
selected_ = true;
file_index_ = index;
}
private:
bool selected_;
int file_index_;
DISALLOW_COPY_AND_ASSIGN(TestListener);
};
class SelfDeletingClient : public ui::SelectFileDialog::Listener {
public:
SelfDeletingClient() {
dialog_ = SelectFileDialogExtensionTest::CreateDialog(this);
}
virtual ~SelfDeletingClient() {
if (dialog_.get())
dialog_->ListenerDestroyed();
}
SelectFileDialogExtension* dialog() const { return dialog_.get(); }
virtual void FileSelected(const base::FilePath& path,
int index,
void* params) OVERRIDE {
delete this;
}
private:
scoped_refptr<SelectFileDialogExtension> dialog_;
};
TEST_F(SelectFileDialogExtensionTest, FileSelected) {
const int kFileIndex = 5;
scoped_ptr<TestListener> listener(new TestListener);
scoped_refptr<SelectFileDialogExtension> dialog =
CreateDialog(listener.get());
ui::SelectedFileInfo info;
SelectFileDialogExtension::OnFileSelected(kDefaultRoutingID, info,
kFileIndex);
dialog->ExtensionDialogClosing(NULL);
EXPECT_TRUE(listener->selected());
EXPECT_EQ(kFileIndex, listener->file_index());
}
TEST_F(SelectFileDialogExtensionTest, FileSelectionCanceled) {
scoped_ptr<TestListener> listener(new TestListener);
scoped_refptr<SelectFileDialogExtension> dialog =
CreateDialog(listener.get());
SelectFileDialogExtension::OnFileSelectionCanceled(kDefaultRoutingID);
dialog->ExtensionDialogClosing(NULL);
EXPECT_FALSE(listener->selected());
EXPECT_EQ(-1, listener->file_index());
}
TEST_F(SelectFileDialogExtensionTest, SelfDeleting) {
SelfDeletingClient* client = new SelfDeletingClient();
ui::SelectedFileInfo file_info;
SelectFileDialogExtension::OnFileSelected(kDefaultRoutingID, file_info, 0);
client->dialog()->ExtensionDialogClosing(NULL);
}