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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/chrome_select_file_policy.h"
7 #include "base/files/file_path.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string16.h"
11 #include "base/values.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/prefs/browser_prefs.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/scoped_testing_local_state.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "content/public/test/test_browser_thread.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/shell_dialogs/select_file_dialog.h"
23 // http://crbug.com/105200
24 #define MAYBE_ExpectAsynchronousListenerCall DISABLED_ExpectAsynchronousListenerCall
26 #define MAYBE_ExpectAsynchronousListenerCall ExpectAsynchronousListenerCall
29 using content::BrowserThread
;
33 class FileSelectionUser
: public ui::SelectFileDialog::Listener
{
36 : file_selection_initialisation_in_progress(false) {
39 virtual ~FileSelectionUser() {
40 if (select_file_dialog_
.get())
41 select_file_dialog_
->ListenerDestroyed();
44 void StartFileSelection() {
45 CHECK(!select_file_dialog_
.get());
46 select_file_dialog_
= ui::SelectFileDialog::Create(
47 this, new ChromeSelectFilePolicy(NULL
));
49 const base::FilePath file_path
;
50 const base::string16 title
= base::string16();
52 file_selection_initialisation_in_progress
= true;
53 select_file_dialog_
->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE
,
58 base::FilePath::StringType(),
61 file_selection_initialisation_in_progress
= false;
64 // ui::SelectFileDialog::Listener implementation.
65 virtual void FileSelected(const base::FilePath
& path
,
66 int index
, void* params
) OVERRIDE
{
67 ASSERT_FALSE(file_selection_initialisation_in_progress
);
69 virtual void MultiFilesSelected(
70 const std::vector
<base::FilePath
>& files
,
71 void* params
) OVERRIDE
{
72 ASSERT_FALSE(file_selection_initialisation_in_progress
);
74 virtual void FileSelectionCanceled(void* params
) OVERRIDE
{
75 ASSERT_FALSE(file_selection_initialisation_in_progress
);
79 scoped_refptr
<ui::SelectFileDialog
> select_file_dialog_
;
81 bool file_selection_initialisation_in_progress
;
86 typedef testing::Test ChromeSelectFilePolicyTest
;
88 // Tests if SelectFileDialog::SelectFile returns asynchronously with
89 // file-selection dialogs disabled by policy.
90 TEST_F(ChromeSelectFilePolicyTest
, MAYBE_ExpectAsynchronousListenerCall
) {
91 base::MessageLoopForUI message_loop
;
92 content::TestBrowserThread
ui_thread(BrowserThread::UI
, &message_loop
);
94 ScopedTestingLocalState
local_state(
95 TestingBrowserProcess::GetGlobal());
97 scoped_ptr
<FileSelectionUser
> file_selection_user(new FileSelectionUser());
99 // Disallow file-selection dialogs.
100 local_state
.Get()->SetManagedPref(prefs::kAllowFileSelectionDialogs
,
101 new base::FundamentalValue(false));
103 file_selection_user
->StartFileSelection();