This source file includes following definitions.
- params_
- file_selected
- canceled
- path
- params
- FileSelected
- FileSelectedWithExtraInfo
- MultiFilesSelected
- FileSelectionCanceled
- SetUp
- TearDown
- AddMountPoint
- CheckJavascriptErrors
- OpenDialog
- TryOpeningSecondDialog
- CloseDialog
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "chrome/browser/ui/views/select_file_dialog_extension.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/platform_thread.h"
#include "build/build_config.h"
#include "chrome/browser/chromeos/file_manager/volume_manager.h"
#include "chrome/browser/extensions/component_loader.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/test/test_utils.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#include "ui/shell_dialogs/selected_file_info.h"
using content::BrowserContext;
class MockSelectFileDialogListener : public ui::SelectFileDialog::Listener {
public:
MockSelectFileDialogListener()
: file_selected_(false),
canceled_(false),
params_(NULL) {
}
bool file_selected() const { return file_selected_; }
bool canceled() const { return canceled_; }
base::FilePath path() const { return path_; }
void* params() const { return params_; }
virtual void FileSelected(const base::FilePath& path,
int index,
void* params) OVERRIDE {
file_selected_ = true;
path_ = path;
params_ = params;
}
virtual void FileSelectedWithExtraInfo(
const ui::SelectedFileInfo& selected_file_info,
int index,
void* params) OVERRIDE {
FileSelected(selected_file_info.local_path, index, params);
}
virtual void MultiFilesSelected(
const std::vector<base::FilePath>& files, void* params) OVERRIDE {}
virtual void FileSelectionCanceled(void* params) OVERRIDE {
canceled_ = true;
params_ = params;
}
private:
bool file_selected_;
bool canceled_;
base::FilePath path_;
void* params_;
DISALLOW_COPY_AND_ASSIGN(MockSelectFileDialogListener);
};
class SelectFileDialogExtensionBrowserTest : public ExtensionBrowserTest {
public:
enum DialogButtonType {
DIALOG_BTN_OK,
DIALOG_BTN_CANCEL
};
virtual void SetUp() OVERRIDE {
extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
listener_.reset(new MockSelectFileDialogListener());
dialog_ = new SelectFileDialogExtension(listener_.get(), NULL);
base::FilePath tmp_path;
PathService::Get(base::DIR_TEMP, &tmp_path);
ASSERT_TRUE(tmp_dir_.CreateUniqueTempDirUnderPath(tmp_path));
downloads_dir_ = tmp_dir_.path().Append("Downloads");
base::CreateDirectory(downloads_dir_);
ExtensionBrowserTest::SetUp();
}
virtual void TearDown() OVERRIDE {
ExtensionBrowserTest::TearDown();
dialog_ = NULL;
listener_.reset();
second_dialog_ = NULL;
second_listener_.reset();
}
void AddMountPoint(const base::FilePath& path) {
EXPECT_TRUE(file_manager::VolumeManager::Get(
browser()->profile())->RegisterDownloadsDirectoryForTesting(path));
browser()->profile()->GetPrefs()->SetFilePath(
prefs::kDownloadDefaultDirectory, downloads_dir_);
}
void CheckJavascriptErrors() {
content::RenderFrameHost* host =
dialog_->GetRenderViewHost()->GetMainFrame();
scoped_ptr<base::Value> value =
content::ExecuteScriptAndGetValue(host, "window.JSErrorCount");
int js_error_count = 0;
ASSERT_TRUE(value->GetAsInteger(&js_error_count));
ASSERT_EQ(0, js_error_count);
}
void OpenDialog(ui::SelectFileDialog::Type dialog_type,
const base::FilePath& file_path,
const gfx::NativeWindow& owning_window,
const std::string& additional_message) {
ExtensionTestMessageListener init_listener("worker-initialized",
false );
scoped_ptr<ExtensionTestMessageListener> additional_listener;
if (!additional_message.empty()) {
additional_listener.reset(
new ExtensionTestMessageListener(additional_message, false));
}
dialog_->SelectFile(dialog_type,
base::string16() ,
file_path,
NULL ,
0 ,
FILE_PATH_LITERAL("") ,
owning_window,
this );
LOG(INFO) << "Waiting for JavaScript ready message.";
ASSERT_TRUE(init_listener.WaitUntilSatisfied());
if (additional_listener.get()) {
LOG(INFO) << "Waiting for JavaScript " << additional_message
<< " message.";
ASSERT_TRUE(additional_listener->WaitUntilSatisfied());
}
ASSERT_TRUE(dialog_->IsRunning(owning_window));
ASSERT_NO_FATAL_FAILURE(CheckJavascriptErrors());
}
void TryOpeningSecondDialog(const gfx::NativeWindow& owning_window) {
second_listener_.reset(new MockSelectFileDialogListener());
second_dialog_ = new SelectFileDialogExtension(second_listener_.get(),
NULL);
second_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE,
base::string16() ,
base::FilePath() ,
NULL ,
0 ,
FILE_PATH_LITERAL("") ,
owning_window,
this );
}
void CloseDialog(DialogButtonType button_type,
const gfx::NativeWindow& owning_window) {
content::WindowedNotificationObserver host_destroyed(
content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
content::NotificationService::AllSources());
content::RenderViewHost* host = dialog_->GetRenderViewHost();
std::string button_class =
(button_type == DIALOG_BTN_OK) ? ".button-panel .ok" :
".button-panel .cancel";
base::string16 script = base::ASCIIToUTF16(
"console.log(\'Test JavaScript injected.\');"
"document.querySelector(\'" + button_class + "\').click();");
host->GetMainFrame()->ExecuteJavaScript(script);
LOG(INFO) << "Waiting for window close notification.";
host_destroyed.Wait();
ASSERT_FALSE(dialog_->IsRunning(owning_window));
}
scoped_ptr<MockSelectFileDialogListener> listener_;
scoped_refptr<SelectFileDialogExtension> dialog_;
scoped_ptr<MockSelectFileDialogListener> second_listener_;
scoped_refptr<SelectFileDialogExtension> second_dialog_;
base::ScopedTempDir tmp_dir_;
base::FilePath downloads_dir_;
};
IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, CreateAndDestroy) {
gfx::NativeWindow native_window = browser()->window()->GetNativeWindow();
ASSERT_TRUE(native_window != NULL);
ASSERT_FALSE(dialog_->IsRunning(native_window));
}
IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, DestroyListener) {
dialog_->ListenerDestroyed();
listener_.reset();
}
IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest,
SelectFileAndCancel) {
AddMountPoint(downloads_dir_);
gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
base::FilePath(), owning_window, ""));
CloseDialog(DIALOG_BTN_CANCEL, owning_window);
ASSERT_FALSE(listener_->file_selected());
ASSERT_TRUE(listener_->canceled());
ASSERT_EQ(this, listener_->params());
}
IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest,
SelectFileAndOpen) {
AddMountPoint(downloads_dir_);
base::FilePath test_file =
downloads_dir_.AppendASCII("file_manager_test.html");
FILE* fp = base::OpenFile(test_file, "w");
ASSERT_TRUE(fp != NULL);
ASSERT_TRUE(base::CloseFile(fp));
gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
test_file, owning_window,
"selection-change-complete"));
CloseDialog(DIALOG_BTN_OK, owning_window);
ASSERT_TRUE(listener_->file_selected());
ASSERT_FALSE(listener_->canceled());
ASSERT_EQ(test_file, listener_->path());
ASSERT_EQ(this, listener_->params());
}
IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest,
SelectFileAndSave) {
AddMountPoint(downloads_dir_);
base::FilePath test_file =
downloads_dir_.AppendASCII("file_manager_test.html");
gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_SAVEAS_FILE,
test_file, owning_window,
"directory-change-complete"));
CloseDialog(DIALOG_BTN_OK, owning_window);
ASSERT_TRUE(listener_->file_selected());
ASSERT_FALSE(listener_->canceled());
ASSERT_EQ(test_file, listener_->path());
ASSERT_EQ(this, listener_->params());
}
IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest,
OpenSingletonTabAndCancel) {
AddMountPoint(downloads_dir_);
gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
base::FilePath(), owning_window, ""));
chrome::NavigateParams p(browser(), GURL("www.google.com"),
content::PAGE_TRANSITION_LINK);
p.window_action = chrome::NavigateParams::SHOW_WINDOW;
p.disposition = SINGLETON_TAB;
chrome::Navigate(&p);
CloseDialog(DIALOG_BTN_CANCEL, owning_window);
ASSERT_FALSE(listener_->file_selected());
ASSERT_TRUE(listener_->canceled());
ASSERT_EQ(this, listener_->params());
}
IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest,
OpenTwoDialogs) {
AddMountPoint(downloads_dir_);
gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
base::FilePath(), owning_window, ""));
TryOpeningSecondDialog(owning_window);
ASSERT_FALSE(second_dialog_->IsRunning(owning_window));
CloseDialog(DIALOG_BTN_CANCEL, owning_window);
ASSERT_FALSE(listener_->file_selected());
ASSERT_TRUE(listener_->canceled());
ASSERT_EQ(this, listener_->params());
}