| 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" |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 | 201 |
| 202 scoped_ptr<MockSelectFileDialogListener> second_listener_; | 202 scoped_ptr<MockSelectFileDialogListener> second_listener_; |
| 203 scoped_refptr<SelectFileDialogExtension> second_dialog_; | 203 scoped_refptr<SelectFileDialogExtension> second_dialog_; |
| 204 | 204 |
| 205 ScopedTempDir tmp_dir_; | 205 ScopedTempDir tmp_dir_; |
| 206 FilePath downloads_dir_; | 206 FilePath downloads_dir_; |
| 207 }; | 207 }; |
| 208 | 208 |
| 209 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, CreateAndDestroy) { | 209 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, CreateAndDestroy) { |
| 210 // Browser window must be up for us to test dialog window parent. | 210 // Browser window must be up for us to test dialog window parent. |
| 211 gfx::NativeWindow native_window = browser()->window()->GetNativeHandle(); | 211 gfx::NativeWindow native_window = browser()->window()->GetNativeWindow(); |
| 212 ASSERT_TRUE(native_window != NULL); | 212 ASSERT_TRUE(native_window != NULL); |
| 213 | 213 |
| 214 // Before we call SelectFile, dialog is not running/visible. | 214 // Before we call SelectFile, dialog is not running/visible. |
| 215 ASSERT_FALSE(dialog_->IsRunning(native_window)); | 215 ASSERT_FALSE(dialog_->IsRunning(native_window)); |
| 216 } | 216 } |
| 217 | 217 |
| 218 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, DestroyListener) { | 218 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, DestroyListener) { |
| 219 // Some users of SelectFileDialog destroy their listener before cleaning | 219 // Some users of SelectFileDialog destroy their listener before cleaning |
| 220 // up the dialog. Make sure we don't crash. | 220 // up the dialog. Make sure we don't crash. |
| 221 dialog_->ListenerDestroyed(); | 221 dialog_->ListenerDestroyed(); |
| 222 listener_.reset(); | 222 listener_.reset(); |
| 223 } | 223 } |
| 224 | 224 |
| 225 // TODO(jamescook): Add a test for selecting a file for an <input type='file'/> | 225 // TODO(jamescook): Add a test for selecting a file for an <input type='file'/> |
| 226 // page element, as that uses different memory management pathways. | 226 // page element, as that uses different memory management pathways. |
| 227 // crbug.com/98791 | 227 // crbug.com/98791 |
| 228 | 228 |
| 229 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, | 229 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, |
| 230 SelectFileAndCancel) { | 230 SelectFileAndCancel) { |
| 231 AddMountPoint(downloads_dir_); | 231 AddMountPoint(downloads_dir_); |
| 232 | 232 |
| 233 gfx::NativeWindow owning_window = browser()->window()->GetNativeHandle(); | 233 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); |
| 234 | 234 |
| 235 // FilePath() for default path. | 235 // FilePath() for default path. |
| 236 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, ""); | 236 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, ""); |
| 237 | 237 |
| 238 // Press cancel button. | 238 // Press cancel button. |
| 239 CloseDialog(DIALOG_BTN_CANCEL, owning_window); | 239 CloseDialog(DIALOG_BTN_CANCEL, owning_window); |
| 240 | 240 |
| 241 // Listener should have been informed of the cancellation. | 241 // Listener should have been informed of the cancellation. |
| 242 ASSERT_FALSE(listener_->file_selected()); | 242 ASSERT_FALSE(listener_->file_selected()); |
| 243 ASSERT_TRUE(listener_->canceled()); | 243 ASSERT_TRUE(listener_->canceled()); |
| 244 ASSERT_EQ(this, listener_->params()); | 244 ASSERT_EQ(this, listener_->params()); |
| 245 } | 245 } |
| 246 | 246 |
| 247 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, | 247 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, |
| 248 SelectFileAndOpen) { | 248 SelectFileAndOpen) { |
| 249 AddMountPoint(downloads_dir_); | 249 AddMountPoint(downloads_dir_); |
| 250 | 250 |
| 251 FilePath test_file = downloads_dir_.AppendASCII("file_manager_test.html"); | 251 FilePath test_file = downloads_dir_.AppendASCII("file_manager_test.html"); |
| 252 | 252 |
| 253 // Create an empty file to give us something to select. | 253 // Create an empty file to give us something to select. |
| 254 FILE* fp = file_util::OpenFile(test_file, "w"); | 254 FILE* fp = file_util::OpenFile(test_file, "w"); |
| 255 ASSERT_TRUE(fp != NULL); | 255 ASSERT_TRUE(fp != NULL); |
| 256 ASSERT_TRUE(file_util::CloseFile(fp)); | 256 ASSERT_TRUE(file_util::CloseFile(fp)); |
| 257 | 257 |
| 258 gfx::NativeWindow owning_window = browser()->window()->GetNativeHandle(); | 258 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); |
| 259 | 259 |
| 260 // 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 |
| 261 // 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 |
| 262 // waiting for chrome.test.sendMessage('selection-change-complete'). | 262 // waiting for chrome.test.sendMessage('selection-change-complete'). |
| 263 // 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 |
| 264 // 'selection-change-complete' before 'worker-initialized'. This is OK. | 264 // 'selection-change-complete' before 'worker-initialized'. This is OK. |
| 265 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, test_file, owning_window, | 265 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, test_file, owning_window, |
| 266 "selection-change-complete"); | 266 "selection-change-complete"); |
| 267 | 267 |
| 268 // Click open button. | 268 // Click open button. |
| 269 CloseDialog(DIALOG_BTN_OK, owning_window); | 269 CloseDialog(DIALOG_BTN_OK, owning_window); |
| 270 | 270 |
| 271 // Listener should have been informed that the file was opened. | 271 // Listener should have been informed that the file was opened. |
| 272 ASSERT_TRUE(listener_->file_selected()); | 272 ASSERT_TRUE(listener_->file_selected()); |
| 273 ASSERT_FALSE(listener_->canceled()); | 273 ASSERT_FALSE(listener_->canceled()); |
| 274 ASSERT_EQ(test_file, listener_->path()); | 274 ASSERT_EQ(test_file, listener_->path()); |
| 275 ASSERT_EQ(this, listener_->params()); | 275 ASSERT_EQ(this, listener_->params()); |
| 276 } | 276 } |
| 277 | 277 |
| 278 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, | 278 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, |
| 279 SelectFileAndSave) { | 279 SelectFileAndSave) { |
| 280 AddMountPoint(downloads_dir_); | 280 AddMountPoint(downloads_dir_); |
| 281 | 281 |
| 282 FilePath test_file = downloads_dir_.AppendASCII("file_manager_test.html"); | 282 FilePath test_file = downloads_dir_.AppendASCII("file_manager_test.html"); |
| 283 | 283 |
| 284 gfx::NativeWindow owning_window = browser()->window()->GetNativeHandle(); | 284 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); |
| 285 | 285 |
| 286 // Spawn a dialog to save a file, providing a suggested path. | 286 // Spawn a dialog to save a file, providing a suggested path. |
| 287 // Ensure "Save" button is enabled by waiting for notification from | 287 // Ensure "Save" button is enabled by waiting for notification from |
| 288 // chrome.test.sendMessage(). | 288 // chrome.test.sendMessage(). |
| 289 // 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 |
| 290 // 'directory-change-complete' before 'worker-initialized'. This is OK. | 290 // 'directory-change-complete' before 'worker-initialized'. This is OK. |
| 291 OpenDialog(SelectFileDialog::SELECT_SAVEAS_FILE, test_file, owning_window, | 291 OpenDialog(SelectFileDialog::SELECT_SAVEAS_FILE, test_file, owning_window, |
| 292 "directory-change-complete"); | 292 "directory-change-complete"); |
| 293 | 293 |
| 294 // Click save button. | 294 // Click save button. |
| 295 CloseDialog(DIALOG_BTN_OK, owning_window); | 295 CloseDialog(DIALOG_BTN_OK, owning_window); |
| 296 | 296 |
| 297 // Listener should have been informed that the file was selected. | 297 // Listener should have been informed that the file was selected. |
| 298 ASSERT_TRUE(listener_->file_selected()); | 298 ASSERT_TRUE(listener_->file_selected()); |
| 299 ASSERT_FALSE(listener_->canceled()); | 299 ASSERT_FALSE(listener_->canceled()); |
| 300 ASSERT_EQ(test_file, listener_->path()); | 300 ASSERT_EQ(test_file, listener_->path()); |
| 301 ASSERT_EQ(this, listener_->params()); | 301 ASSERT_EQ(this, listener_->params()); |
| 302 } | 302 } |
| 303 | 303 |
| 304 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, | 304 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, |
| 305 OpenSingletonTabAndCancel) { | 305 OpenSingletonTabAndCancel) { |
| 306 AddMountPoint(downloads_dir_); | 306 AddMountPoint(downloads_dir_); |
| 307 | 307 |
| 308 gfx::NativeWindow owning_window = browser()->window()->GetNativeHandle(); | 308 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); |
| 309 | 309 |
| 310 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, ""); | 310 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, ""); |
| 311 | 311 |
| 312 // Open a singleton tab in background. | 312 // Open a singleton tab in background. |
| 313 browser::NavigateParams p(browser(), GURL("www.google.com"), | 313 browser::NavigateParams p(browser(), GURL("www.google.com"), |
| 314 content::PAGE_TRANSITION_LINK); | 314 content::PAGE_TRANSITION_LINK); |
| 315 p.window_action = browser::NavigateParams::SHOW_WINDOW; | 315 p.window_action = browser::NavigateParams::SHOW_WINDOW; |
| 316 p.disposition = SINGLETON_TAB; | 316 p.disposition = SINGLETON_TAB; |
| 317 browser::Navigate(&p); | 317 browser::Navigate(&p); |
| 318 | 318 |
| 319 // Press cancel button. | 319 // Press cancel button. |
| 320 CloseDialog(DIALOG_BTN_CANCEL, owning_window); | 320 CloseDialog(DIALOG_BTN_CANCEL, owning_window); |
| 321 | 321 |
| 322 // Listener should have been informed of the cancellation. | 322 // Listener should have been informed of the cancellation. |
| 323 ASSERT_FALSE(listener_->file_selected()); | 323 ASSERT_FALSE(listener_->file_selected()); |
| 324 ASSERT_TRUE(listener_->canceled()); | 324 ASSERT_TRUE(listener_->canceled()); |
| 325 ASSERT_EQ(this, listener_->params()); | 325 ASSERT_EQ(this, listener_->params()); |
| 326 } | 326 } |
| 327 | 327 |
| 328 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, | 328 IN_PROC_BROWSER_TEST_F(SelectFileDialogExtensionBrowserTest, |
| 329 OpenTwoDialogs) { | 329 OpenTwoDialogs) { |
| 330 AddMountPoint(downloads_dir_); | 330 AddMountPoint(downloads_dir_); |
| 331 | 331 |
| 332 gfx::NativeWindow owning_window = browser()->window()->GetNativeHandle(); | 332 gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow(); |
| 333 | 333 |
| 334 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, ""); | 334 OpenDialog(SelectFileDialog::SELECT_OPEN_FILE, FilePath(), owning_window, ""); |
| 335 | 335 |
| 336 TryOpeningSecondDialog(owning_window); | 336 TryOpeningSecondDialog(owning_window); |
| 337 | 337 |
| 338 // Second dialog should not be running. | 338 // Second dialog should not be running. |
| 339 ASSERT_FALSE(second_dialog_->IsRunning(owning_window)); | 339 ASSERT_FALSE(second_dialog_->IsRunning(owning_window)); |
| 340 | 340 |
| 341 // Click cancel button. | 341 // Click cancel button. |
| 342 CloseDialog(DIALOG_BTN_CANCEL, owning_window); | 342 CloseDialog(DIALOG_BTN_CANCEL, owning_window); |
| 343 | 343 |
| 344 // Listener should have been informed of the cancellation. | 344 // Listener should have been informed of the cancellation. |
| 345 ASSERT_FALSE(listener_->file_selected()); | 345 ASSERT_FALSE(listener_->file_selected()); |
| 346 ASSERT_TRUE(listener_->canceled()); | 346 ASSERT_TRUE(listener_->canceled()); |
| 347 ASSERT_EQ(this, listener_->params()); | 347 ASSERT_EQ(this, listener_->params()); |
| 348 } | 348 } |
| OLD | NEW |