Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 | |
|
sadrul
2015/11/17 02:10:15
- extra new line
joone
2015/11/17 23:16:22
Done.
| |
| 6 #include "base/run_loop.h" | |
| 7 #include "chrome/browser/ui/browser.h" | |
| 8 #include "chrome/browser/ui/browser_window.h" | |
| 9 #include "chrome/browser/ui/chrome_select_file_policy.h" | |
| 10 #include "chrome/browser/ui/libgtk2ui/select_file_dialog_impl_gtk2.h" | |
| 11 #include "chrome/browser/ui/view_ids.h" | |
| 12 #include "chrome/test/base/in_process_browser_test.h" | |
| 13 #include "chrome/test/base/interactive_test_utils.h" | |
| 14 #include "chrome/test/base/ui_test_utils.h" | |
| 15 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 16 #include "ui/shell_dialogs/select_file_dialog.h" | |
| 17 #include "ui/views/widget/widget.h" | |
| 18 #include "ui/views/widget/widget_observer.h" | |
| 19 | |
| 20 using BrowserSelectFileDialogTest = InProcessBrowserTest; | |
| 21 | |
| 22 // Spins a run loop until a Widget's activation reaches the desired state. | |
| 23 class WidgetActivationWaiter : public views::WidgetObserver { | |
| 24 public: | |
| 25 WidgetActivationWaiter(views::Widget* widget, bool active) | |
| 26 : observed_(false), active_(active) { | |
| 27 widget->AddObserver(this); | |
| 28 EXPECT_NE(active, widget->IsActive()); | |
| 29 } | |
| 30 | |
| 31 void Wait() { | |
| 32 if (!observed_) | |
| 33 run_loop_.Run(); | |
| 34 } | |
| 35 | |
| 36 private: | |
| 37 // views::WidgetObserver: | |
| 38 void OnWidgetActivationChanged(views::Widget* widget, bool active) override { | |
| 39 if (active_ != active) | |
| 40 return; | |
| 41 | |
| 42 observed_ = true; | |
| 43 widget->RemoveObserver(this); | |
| 44 if (run_loop_.running()) | |
| 45 run_loop_.Quit(); | |
| 46 } | |
| 47 | |
| 48 base::RunLoop run_loop_; | |
| 49 bool observed_; | |
| 50 bool active_; | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(WidgetActivationWaiter); | |
| 53 }; | |
| 54 | |
| 55 namespace libgtk2ui { | |
| 56 | |
| 57 // FilePicker opens a GtkFileChooser. | |
| 58 class FilePicker : public ui::SelectFileDialog::Listener { | |
| 59 public: | |
| 60 explicit FilePicker(BrowserWindow* window) { | |
| 61 select_file_dialog_ = ui::SelectFileDialog::Create( | |
| 62 this, new ChromeSelectFilePolicy(nullptr)); | |
| 63 | |
| 64 gfx::NativeWindow parent_window = window->GetNativeWindow(); | |
| 65 ui::SelectFileDialog::FileTypeInfo file_types; | |
| 66 file_types.support_drive = true; | |
| 67 const base::FilePath file_path; | |
| 68 select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE, | |
| 69 base::string16(), | |
| 70 file_path, | |
| 71 &file_types, | |
| 72 0, | |
| 73 base::FilePath::StringType(), | |
| 74 parent_window, | |
| 75 nullptr); | |
| 76 } | |
| 77 | |
| 78 ~FilePicker() override { | |
| 79 if (select_file_dialog_.get()) | |
|
sadrul
2015/11/17 02:10:15
When can |select_file_dialog_| be null?
joone
2015/11/17 23:16:22
There is no case where |select_file_dialog_| is nu
| |
| 80 select_file_dialog_->ListenerDestroyed(); | |
| 81 } | |
| 82 | |
| 83 void Close() { | |
| 84 SelectFileDialogImplGTK* file_dialog = | |
| 85 static_cast<SelectFileDialogImplGTK*>(select_file_dialog_.get()); | |
| 86 | |
| 87 | |
| 88 while (!file_dialog->dialogs_.empty()) | |
| 89 gtk_widget_destroy(*(file_dialog->dialogs_.begin())); | |
| 90 } | |
| 91 | |
| 92 // SelectFileDialog::Listener implementation. | |
| 93 void FileSelected(const base::FilePath& path, | |
| 94 int index, | |
| 95 void* params) override {} | |
| 96 private: | |
| 97 // Dialog box used for opening and saving files. | |
| 98 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(FilePicker); | |
| 101 }; | |
| 102 | |
| 103 } // namespace libgtk2ui | |
| 104 | |
| 105 // Test that the file-picker is opened and closed without memory leaks. | |
| 106 IN_PROC_BROWSER_TEST_F(BrowserSelectFileDialogTest, OpenCloseFileDialog) { | |
| 107 // Bring the native window to the foreground. Returns true on success. | |
| 108 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | |
| 109 // Initializes and waits until the test server is ready to accept requests. | |
| 110 ASSERT_TRUE(embedded_test_server()->Start()); | |
|
sadrul
2015/11/17 02:10:15
You don't need to start the test-server, right?
joone
2015/11/17 23:16:22
Right, we don't need to run it because the page lo
| |
| 111 ASSERT_TRUE(browser()->window()->IsActive()); | |
| 112 | |
| 113 // Leaks in GtkFileChooserDialog. http://crbug.com/537468 | |
| 114 ANNOTATE_SCOPED_MEMORY_LEAK; | |
| 115 libgtk2ui::FilePicker file_picker(browser()->window()); | |
| 116 | |
| 117 gfx::NativeWindow window = browser()->window()->GetNativeWindow(); | |
| 118 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window); | |
| 119 ASSERT_NE(nullptr, widget); | |
| 120 | |
| 121 // Run a nested loop until the browser window becomes inactive | |
| 122 // so that the file-picker can be active. | |
| 123 WidgetActivationWaiter wait_inactive(widget, false); | |
| 124 wait_inactive.Wait(); | |
| 125 EXPECT_FALSE(browser()->window()->IsActive()); | |
| 126 file_picker.Close(); | |
| 127 | |
| 128 // Run a nested loop until the browser window becomes active. | |
| 129 WidgetActivationWaiter wait_active(widget, true); | |
| 130 wait_active.Wait(); | |
| 131 EXPECT_TRUE(browser()->window()->IsActive()); | |
| 132 } | |
| OLD | NEW |