OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/select_file_dialog_extension.h" | 5 #include "chrome/browser/ui/views/select_file_dialog_extension.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
13 #include "base/utf_string_conversions.h" // ASCIIToUTF16 | 13 #include "base/utf_string_conversions.h" // ASCIIToUTF16 |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 #include "chrome/browser/extensions/extension_browsertest.h" | 15 #include "chrome/browser/extensions/extension_browsertest.h" |
16 #include "chrome/browser/extensions/extension_test_message_listener.h" | 16 #include "chrome/browser/extensions/extension_test_message_listener.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
19 #include "chrome/browser/ui/browser_navigator.h" | 19 #include "chrome/browser/ui/browser_navigator.h" |
20 #include "chrome/browser/ui/browser_window.h" | 20 #include "chrome/browser/ui/browser_window.h" |
21 #include "chrome/browser/ui/select_file_dialog.h" | |
22 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
23 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
24 #include "content/public/browser/notification_types.h" | 23 #include "content/public/browser/notification_types.h" |
25 #include "content/public/browser/render_view_host.h" | 24 #include "content/public/browser/render_view_host.h" |
26 #include "content/public/test/test_utils.h" | 25 #include "content/public/test/test_utils.h" |
| 26 #include "ui/base/dialogs/select_file_dialog.h" |
27 #include "ui/base/dialogs/selected_file_info.h" | 27 #include "ui/base/dialogs/selected_file_info.h" |
28 #include "webkit/fileapi/file_system_context.h" | 28 #include "webkit/fileapi/file_system_context.h" |
29 #include "webkit/fileapi/file_system_mount_point_provider.h" | 29 #include "webkit/fileapi/file_system_mount_point_provider.h" |
30 | 30 |
31 using content::BrowserContext; | 31 using content::BrowserContext; |
32 | 32 |
33 // Mock listener used by test below. | 33 // Mock listener used by test below. |
34 class MockSelectFileDialogListener : public SelectFileDialog::Listener { | 34 class MockSelectFileDialogListener : public ui::SelectFileDialog::Listener { |
35 public: | 35 public: |
36 MockSelectFileDialogListener() | 36 MockSelectFileDialogListener() |
37 : file_selected_(false), | 37 : file_selected_(false), |
38 canceled_(false), | 38 canceled_(false), |
39 params_(NULL) { | 39 params_(NULL) { |
40 } | 40 } |
41 | 41 |
42 bool file_selected() const { return file_selected_; } | 42 bool file_selected() const { return file_selected_; } |
43 bool canceled() const { return canceled_; } | 43 bool canceled() const { return canceled_; } |
44 FilePath path() const { return path_; } | 44 FilePath path() const { return path_; } |
45 void* params() const { return params_; } | 45 void* params() const { return params_; } |
46 | 46 |
47 // SelectFileDialog::Listener implementation. | 47 // ui::SelectFileDialog::Listener implementation. |
48 virtual void FileSelected(const FilePath& path, | 48 virtual void FileSelected(const FilePath& path, |
49 int index, | 49 int index, |
50 void* params) OVERRIDE { | 50 void* params) OVERRIDE { |
51 file_selected_ = true; | 51 file_selected_ = true; |
52 path_ = path; | 52 path_ = path; |
53 params_ = params; | 53 params_ = params; |
54 } | 54 } |
55 virtual void FileSelectedWithExtraInfo( | 55 virtual void FileSelectedWithExtraInfo( |
56 const ui::SelectedFileInfo& selected_file_info, | 56 const ui::SelectedFileInfo& selected_file_info, |
57 int index, | 57 int index, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 } | 110 } |
111 | 111 |
112 // Creates a file system mount point for a directory. | 112 // Creates a file system mount point for a directory. |
113 void AddMountPoint(const FilePath& path) { | 113 void AddMountPoint(const FilePath& path) { |
114 fileapi::ExternalFileSystemMountPointProvider* provider = | 114 fileapi::ExternalFileSystemMountPointProvider* provider = |
115 BrowserContext::GetFileSystemContext(browser()->profile())-> | 115 BrowserContext::GetFileSystemContext(browser()->profile())-> |
116 external_provider(); | 116 external_provider(); |
117 provider->AddLocalMountPoint(path); | 117 provider->AddLocalMountPoint(path); |
118 } | 118 } |
119 | 119 |
120 void OpenDialog(SelectFileDialog::Type dialog_type, | 120 void OpenDialog(ui::SelectFileDialog::Type dialog_type, |
121 const FilePath& file_path, | 121 const FilePath& file_path, |
122 const gfx::NativeWindow& owning_window, | 122 const gfx::NativeWindow& owning_window, |
123 const std::string& additional_message) { | 123 const std::string& additional_message) { |
124 // Spawn a dialog to open a file. The dialog will signal that it is ready | 124 // Spawn a dialog to open a file. The dialog will signal that it is ready |
125 // via chrome.test.sendMessage() in the extension JavaScript. | 125 // via chrome.test.sendMessage() in the extension JavaScript. |
126 ExtensionTestMessageListener init_listener("worker-initialized", | 126 ExtensionTestMessageListener init_listener("worker-initialized", |
127 false /* will_reply */); | 127 false /* will_reply */); |
128 | 128 |
129 scoped_ptr<ExtensionTestMessageListener> additional_listener; | 129 scoped_ptr<ExtensionTestMessageListener> additional_listener; |
130 if (!additional_message.empty()) { | 130 if (!additional_message.empty()) { |
(...skipping 23 matching lines...) Expand all Loading... |
154 ASSERT_TRUE(dialog_->IsRunning(owning_window)); | 154 ASSERT_TRUE(dialog_->IsRunning(owning_window)); |
155 } | 155 } |
156 | 156 |
157 void TryOpeningSecondDialog(const gfx::NativeWindow& owning_window) { | 157 void TryOpeningSecondDialog(const gfx::NativeWindow& owning_window) { |
158 second_listener_.reset(new MockSelectFileDialogListener()); | 158 second_listener_.reset(new MockSelectFileDialogListener()); |
159 second_dialog_ = new SelectFileDialogExtension(second_listener_.get(), | 159 second_dialog_ = new SelectFileDialogExtension(second_listener_.get(), |
160 NULL); | 160 NULL); |
161 | 161 |
162 // At the moment we don't really care about dialog type, but we have to put | 162 // At the moment we don't really care about dialog type, but we have to put |
163 // some dialog type. | 163 // some dialog type. |
164 second_dialog_->SelectFile(SelectFileDialog::SELECT_OPEN_FILE, | 164 second_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE, |
165 string16() /* title */, | 165 string16() /* title */, |
166 FilePath() /* default_path */, | 166 FilePath() /* default_path */, |
167 NULL /* file_types */, | 167 NULL /* file_types */, |
168 0 /* file_type_index */, | 168 0 /* file_type_index */, |
169 FILE_PATH_LITERAL("") /* default_extension */, | 169 FILE_PATH_LITERAL("") /* default_extension */, |
170 owning_window, | 170 owning_window, |
171 this /* params */); | 171 this /* params */); |
172 } | 172 } |
173 | 173 |
174 void CloseDialog(DialogButtonType button_type, | 174 void CloseDialog(DialogButtonType button_type, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 // page element, as that uses different memory management pathways. | 225 // page element, as that uses different memory management pathways. |
226 // crbug.com/98791 | 226 // crbug.com/98791 |
227 | 227 |
228 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, | 228 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, |
229 SelectFileAndCancel) { | 229 SelectFileAndCancel) { |
230 AddMountPoint(downloads_dir_); | 230 AddMountPoint(downloads_dir_); |
231 | 231 |
232 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); | 232 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); |
233 | 233 |
234 // FilePath() for default path. | 234 // FilePath() for default path. |
235 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, ""); | 235 OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, |
| 236 ""); |
236 | 237 |
237 // Press cancel button. | 238 // Press cancel button. |
238 CloseDialog(DIALOG_BTN_CANCEL, owning_window); | 239 CloseDialog(DIALOG_BTN_CANCEL, owning_window); |
239 | 240 |
240 // Listener should have been informed of the cancellation. | 241 // Listener should have been informed of the cancellation. |
241 ASSERT_FALSE(listener_->file_selected()); | 242 ASSERT_FALSE(listener_->file_selected()); |
242 ASSERT_TRUE(listener_->canceled()); | 243 ASSERT_TRUE(listener_->canceled()); |
243 ASSERT_EQ(this, listener_->params()); | 244 ASSERT_EQ(this, listener_->params()); |
244 } | 245 } |
245 | 246 |
246 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, | 247 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, |
247 SelectFileAndOpen) { | 248 SelectFileAndOpen) { |
248 AddMountPoint(downloads_dir_); | 249 AddMountPoint(downloads_dir_); |
249 | 250 |
250 FilePath test_file = downloads_dir_.AppendASCII("file_manager_test.html"); | 251 FilePath test_file = downloads_dir_.AppendASCII("file_manager_test.html"); |
251 | 252 |
252 // Create an empty file to give us something to select. | 253 // Create an empty file to give us something to select. |
253 FILE* fp = file_util::OpenFile(test_file, "w"); | 254 FILE* fp = file_util::OpenFile(test_file, "w"); |
254 ASSERT_TRUE(fp != NULL); | 255 ASSERT_TRUE(fp != NULL); |
255 ASSERT_TRUE(file_util::CloseFile(fp)); | 256 ASSERT_TRUE(file_util::CloseFile(fp)); |
256 | 257 |
257 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); | 258 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); |
258 | 259 |
259 // Spawn a dialog to open a file. Provide the path to the file so the dialog | 260 // Spawn a dialog to open a file. Provide the path to the file so the dialog |
260 // will automatically select it. Ensure that the OK button is enabled by | 261 // will automatically select it. Ensure that the OK button is enabled by |
261 // waiting for chrome.test.sendMessage('selection-change-complete'). | 262 // waiting for chrome.test.sendMessage('selection-change-complete'). |
262 // The extension starts a Web Worker to read file metadata, so it may send | 263 // The extension starts a Web Worker to read file metadata, so it may send |
263 // 'selection-change-complete' before 'worker-initialized'. This is OK. | 264 // 'selection-change-complete' before 'worker-initialized'. This is OK. |
264 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, test_file, owning_window, | 265 OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE, test_file, owning_window, |
265 "selection-change-complete"); | 266 "selection-change-complete"); |
266 | 267 |
267 // Click open button. | 268 // Click open button. |
268 CloseDialog(DIALOG_BTN_OK, owning_window); | 269 CloseDialog(DIALOG_BTN_OK, owning_window); |
269 | 270 |
270 // Listener should have been informed that the file was opened. | 271 // Listener should have been informed that the file was opened. |
271 ASSERT_TRUE(listener_->file_selected()); | 272 ASSERT_TRUE(listener_->file_selected()); |
272 ASSERT_FALSE(listener_->canceled()); | 273 ASSERT_FALSE(listener_->canceled()); |
273 ASSERT_EQ(test_file, listener_->path()); | 274 ASSERT_EQ(test_file, listener_->path()); |
274 ASSERT_EQ(this, listener_->params()); | 275 ASSERT_EQ(this, listener_->params()); |
275 } | 276 } |
276 | 277 |
277 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, | 278 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, |
278 SelectFileAndSave) { | 279 SelectFileAndSave) { |
279 AddMountPoint(downloads_dir_); | 280 AddMountPoint(downloads_dir_); |
280 | 281 |
281 FilePath test_file = downloads_dir_.AppendASCII("file_manager_test.html"); | 282 FilePath test_file = downloads_dir_.AppendASCII("file_manager_test.html"); |
282 | 283 |
283 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); | 284 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); |
284 | 285 |
285 // Spawn a dialog to save a file, providing a suggested path. | 286 // Spawn a dialog to save a file, providing a suggested path. |
286 // Ensure "Save" button is enabled by waiting for notification from | 287 // Ensure "Save" button is enabled by waiting for notification from |
287 // chrome.test.sendMessage(). | 288 // chrome.test.sendMessage(). |
288 // The extension starts a Web Worker to read file metadata, so it may send | 289 // The extension starts a Web Worker to read file metadata, so it may send |
289 // 'directory-change-complete' before 'worker-initialized'. This is OK. | 290 // 'directory-change-complete' before 'worker-initialized'. This is OK. |
290 OpenDialog(SelectFileDialog::SELECT_SAVEAS_FILE, test_file, owning_window, | 291 OpenDialog(ui::SelectFileDialog::SELECT_SAVEAS_FILE, test_file, |
291 "directory-change-complete"); | 292 owning_window, "directory-change-complete"); |
292 | 293 |
293 // Click save button. | 294 // Click save button. |
294 CloseDialog(DIALOG_BTN_OK, owning_window); | 295 CloseDialog(DIALOG_BTN_OK, owning_window); |
295 | 296 |
296 // Listener should have been informed that the file was selected. | 297 // Listener should have been informed that the file was selected. |
297 ASSERT_TRUE(listener_->file_selected()); | 298 ASSERT_TRUE(listener_->file_selected()); |
298 ASSERT_FALSE(listener_->canceled()); | 299 ASSERT_FALSE(listener_->canceled()); |
299 ASSERT_EQ(test_file, listener_->path()); | 300 ASSERT_EQ(test_file, listener_->path()); |
300 ASSERT_EQ(this, listener_->params()); | 301 ASSERT_EQ(this, listener_->params()); |
301 } | 302 } |
302 | 303 |
303 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, | 304 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, |
304 OpenSingletonTabAndCancel) { | 305 OpenSingletonTabAndCancel) { |
305 AddMountPoint(downloads_dir_); | 306 AddMountPoint(downloads_dir_); |
306 | 307 |
307 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); | 308 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); |
308 | 309 |
309 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, ""); | 310 OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, |
| 311 ""); |
310 | 312 |
311 // Open a singleton tab in background. | 313 // Open a singleton tab in background. |
312 chrome::NavigateParams p(browser(), GURL("www.google.com"), | 314 chrome::NavigateParams p(browser(), GURL("www.google.com"), |
313 content::PAGE_TRANSITION_LINK); | 315 content::PAGE_TRANSITION_LINK); |
314 p.window_action = chrome::NavigateParams::SHOW_WINDOW; | 316 p.window_action = chrome::NavigateParams::SHOW_WINDOW; |
315 p.disposition = SINGLETON_TAB; | 317 p.disposition = SINGLETON_TAB; |
316 chrome::Navigate(&p); | 318 chrome::Navigate(&p); |
317 | 319 |
318 // Press cancel button. | 320 // Press cancel button. |
319 CloseDialog(DIALOG_BTN_CANCEL, owning_window); | 321 CloseDialog(DIALOG_BTN_CANCEL, owning_window); |
320 | 322 |
321 // Listener should have been informed of the cancellation. | 323 // Listener should have been informed of the cancellation. |
322 ASSERT_FALSE(listener_->file_selected()); | 324 ASSERT_FALSE(listener_->file_selected()); |
323 ASSERT_TRUE(listener_->canceled()); | 325 ASSERT_TRUE(listener_->canceled()); |
324 ASSERT_EQ(this, listener_->params()); | 326 ASSERT_EQ(this, listener_->params()); |
325 } | 327 } |
326 | 328 |
327 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, | 329 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, |
328 OpenTwoDialogs) { | 330 OpenTwoDialogs) { |
329 AddMountPoint(downloads_dir_); | 331 AddMountPoint(downloads_dir_); |
330 | 332 |
331 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); | 333 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); |
332 | 334 |
333 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, ""); | 335 OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, |
| 336 ""); |
334 | 337 |
335 TryOpeningSecondDialog(owning_window); | 338 TryOpeningSecondDialog(owning_window); |
336 | 339 |
337 // Second dialog should not be running. | 340 // Second dialog should not be running. |
338 ASSERT_FALSE(second_dialog_->IsRunning(owning_window)); | 341 ASSERT_FALSE(second_dialog_->IsRunning(owning_window)); |
339 | 342 |
340 // Click cancel button. | 343 // Click cancel button. |
341 CloseDialog(DIALOG_BTN_CANCEL, owning_window); | 344 CloseDialog(DIALOG_BTN_CANCEL, owning_window); |
342 | 345 |
343 // Listener should have been informed of the cancellation. | 346 // Listener should have been informed of the cancellation. |
344 ASSERT_FALSE(listener_->file_selected()); | 347 ASSERT_FALSE(listener_->file_selected()); |
345 ASSERT_TRUE(listener_->canceled()); | 348 ASSERT_TRUE(listener_->canceled()); |
346 ASSERT_EQ(this, listener_->params()); | 349 ASSERT_EQ(this, listener_->params()); |
347 } | 350 } |
OLD | NEW |