1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/file_path.h" 6 #include "base/file_util.h" 7 #include "base/string16.h" 8 #include "base/values.h" 9 #include "chrome/browser/prefs/pref_service.h" 10 #include "chrome/browser/prefs/browser_prefs.h" 11 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/shell_dialogs.h" 13 #include "chrome/test/testing_browser_process_test.h" 14 #include "chrome/test/testing_pref_service.h" 15 #include "chrome/common/pref_names.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 18 class FileSelectionUser : public SelectFileDialog::Listener { 19 public: 20 FileSelectionUser() 21 : file_selection_initialisation_in_progress(false) { 22 } 23 24 ~FileSelectionUser() { 25 if (select_file_dialog_.get()) 26 select_file_dialog_->ListenerDestroyed(); 27 } 28 29 void StartFileSelection() { 30 CHECK(!select_file_dialog_.get()); 31 select_file_dialog_ = SelectFileDialog::Create(this); 32 33 const FilePath file_path; 34 const string16 title=string16(); 35 36 file_selection_initialisation_in_progress = true; 37 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_OPEN_FILE, 38 title, 39 file_path, 40 NULL, 41 0, 42 FILE_PATH_LITERAL(""), 43 NULL, 44 NULL, 45 NULL); 46 file_selection_initialisation_in_progress = false; 47 } 48 49 // SelectFileDialog::Listener implementation. 50 virtual void FileSelected(const FilePath& path, 51 int index, void* params){ 52 ASSERT_FALSE(file_selection_initialisation_in_progress); 53 } 54 virtual void MultiFilesSelected( 55 const std::vector<FilePath>& files, 56 void* params) { 57 ASSERT_FALSE(file_selection_initialisation_in_progress); 58 } 59 virtual void FileSelectionCanceled(void* params) { 60 ASSERT_FALSE(file_selection_initialisation_in_progress); 61 } 62 63 private: 64 scoped_refptr<SelectFileDialog> select_file_dialog_; 65 66 bool file_selection_initialisation_in_progress; 67 }; 68 69 typedef TestingBrowserProcessTest FileSelectionDialogTest; 70 71 // Tests if SelectFileDialog::SelectFile returns asynchronously with 72 // file-selection dialogs disabled by policy. 73 TEST_F(FileSelectionDialogTest, ExpectAsynchronousListenerCall) { 74 MessageLoopForUI message_loop; 75 BrowserThread ui_thread(BrowserThread::UI, &message_loop); 76 77 TestingPrefService test_local_state; 78 browser::RegisterLocalState(&test_local_state); 79 static_cast<TestingBrowserProcess*>(g_browser_process)-> 80 SetPrefService(&test_local_state); 81 82 FileSelectionUser* file_selection_user; 83 84 // Disallow file-selection dialogs. 85 test_local_state.SetManagedPref( 86 prefs::kAllowFileSelectionDialogs, 87 Value::CreateBooleanValue(false)); 88 89 file_selection_user = new FileSelectionUser(); 90 file_selection_user->StartFileSelection(); 91 delete file_selection_user; 92 93 static_cast<TestingBrowserProcess*>(g_browser_process)-> 94 SetPrefService(NULL); 95 } 96