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