Chromium Code Reviews| Index: chrome/browser/ui/libgtk2ui/select_file_dialog_impl_test.cc | 
| diff --git a/chrome/browser/ui/libgtk2ui/select_file_dialog_impl_test.cc b/chrome/browser/ui/libgtk2ui/select_file_dialog_impl_test.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..62f2b0028b0e3d81301cd744036ff2306d3e06ae | 
| --- /dev/null | 
| +++ b/chrome/browser/ui/libgtk2ui/select_file_dialog_impl_test.cc | 
| @@ -0,0 +1,133 @@ | 
| +// Copyright 2015 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "base/run_loop.h" | 
| +#include "chrome/browser/ui/browser.h" | 
| +#include "chrome/browser/ui/browser_window.h" | 
| +#include "chrome/browser/ui/chrome_select_file_policy.h" | 
| +#include "chrome/browser/ui/libgtk2ui/select_file_dialog_impl_gtk2.h" | 
| 
 
sky
2015/11/06 21:04:10
This should be the first include.
 
joone
2015/11/09 20:50:07
It causes a #include order problem.
 
 | 
| +#include "chrome/browser/ui/view_ids.h" | 
| +#include "chrome/test/base/in_process_browser_test.h" | 
| +#include "chrome/test/base/interactive_test_utils.h" | 
| +#include "chrome/test/base/ui_test_utils.h" | 
| +#include "net/test/embedded_test_server/embedded_test_server.h" | 
| +#include "ui/shell_dialogs/select_file_dialog.h" | 
| +#include "ui/views/widget/widget.h" | 
| +#include "ui/views/widget/widget_observer.h" | 
| +#include "url/gurl.h" | 
| + | 
| + | 
| +const char kSimplePage[] = "/focus/page_with_focus.html"; | 
| + | 
| +class BrowserSelectFileDialogTest : public InProcessBrowserTest { | 
| 
 
sky
2015/11/06 21:04:10
Use a using statement.
 
joone
2015/11/09 20:50:07
Done.
 
 | 
| +}; | 
| + | 
| +// Spins a run loop until a Widget's activation reaches the desired state. | 
| +class WidgetActivationWaiter : public views::WidgetObserver { | 
| + public: | 
| + explicit WidgetActivationWaiter(views::Widget* widget, bool active) | 
| 
 
sky
2015/11/06 21:04:10
no explicit
 
joone
2015/11/09 20:50:06
Done.
 
 | 
| + : observed_(false), active_(active) { | 
| + widget->AddObserver(this); | 
| + EXPECT_NE(active, widget->IsActive()); | 
| + } | 
| + | 
| + void Wait() { | 
| + if (!observed_) | 
| + run_loop_.Run(); | 
| + } | 
| + | 
| + void OnWidgetActivationChanged(views::Widget* widget, bool active) override { | 
| 
 
sky
2015/11/06 21:04:10
prefix this with
// views::WidgetObserver:
and mov
 
joone
2015/11/09 20:50:07
Done.
 
 | 
| + if (active_ != active) | 
| + return; | 
| + | 
| + observed_ = true; | 
| + widget->RemoveObserver(this); | 
| + if (run_loop_.running()) | 
| + run_loop_.Quit(); | 
| + } | 
| + | 
| + private: | 
| + base::RunLoop run_loop_; | 
| + bool observed_; | 
| + bool active_; | 
| + | 
| + DISALLOW_COPY_AND_ASSIGN(WidgetActivationWaiter); | 
| +}; | 
| + | 
| +namespace libgtk2ui { | 
| + | 
| +class FilePicker: public ui::SelectFileDialog::Listener { | 
| 
 
sky
2015/11/06 21:04:09
Add description.
 
sky
2015/11/06 21:04:10
'FilePicker:' => 'FilePicker :'
 
joone
2015/11/09 20:50:07
Done.
 
joone
2015/11/09 20:50:07
Done.
 
 | 
| + public: | 
| + explicit FilePicker(BrowserWindow* window) { | 
| + select_file_dialog_ = ui::SelectFileDialog::Create( | 
| + this, new ChromeSelectFilePolicy(NULL)); | 
| 
 
sky
2015/11/06 21:04:09
nullptr.
 
joone
2015/11/09 20:50:07
Done.
 
 | 
| + | 
| + gfx::NativeWindow parent_window = window->GetNativeWindow(); | 
| + ui::SelectFileDialog::FileTypeInfo file_types; | 
| + file_types.support_drive = true; | 
| + const base::FilePath file_path; | 
| + select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE, | 
| + base::string16(), | 
| + file_path, | 
| + &file_types, | 
| + 0, | 
| + base::FilePath::StringType(), | 
| + parent_window, | 
| + NULL); | 
| + } | 
| + | 
| + ~FilePicker() override { | 
| + if (select_file_dialog_.get()) | 
| + select_file_dialog_->ListenerDestroyed(); | 
| + } | 
| + | 
| + void Close() { | 
| + SelectFileDialogImplGTK* file_dialog = | 
| + static_cast<SelectFileDialogImplGTK*>(select_file_dialog_.get()); | 
| + | 
| + while (file_dialog->dialogs_.begin() != file_dialog->dialogs_.end()) { | 
| 
 
sky
2015/11/06 21:04:09
nit: no {}
 
sky
2015/11/06 21:04:10
!dialogs_.empty().
 
joone
2015/11/09 20:50:07
Done.
 
joone
2015/11/09 20:50:07
Done.
 
 | 
| + gtk_widget_destroy(*(file_dialog->dialogs_.begin())); | 
| + } | 
| + } | 
| + | 
| + // SelectFileDialog::Listener implementation. | 
| + void FileSelected(const base::FilePath& path, | 
| + int index, | 
| + void* params) override {} | 
| + private: | 
| + // Dialog box used for opening and saving files. | 
| + scoped_refptr<ui::SelectFileDialog> select_file_dialog_; | 
| + | 
| + DISALLOW_COPY_AND_ASSIGN(FilePicker); | 
| +}; | 
| + | 
| +} // namespace libgtk2ui | 
| + | 
| +IN_PROC_BROWSER_TEST_F(BrowserSelectFileDialogTest, OpenCloseFileDialog) { | 
| 
 
sky
2015/11/06 21:04:10
Document what that test is asserting.
 
joone
2015/11/09 20:50:07
Done.
 
 | 
| + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 
| + ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 
| + | 
| + const GURL url = embedded_test_server()->GetURL(kSimplePage); | 
| + ui_test_utils::NavigateToURL(browser(), url); | 
| 
 
sky
2015/11/06 21:04:10
Why do you need to navigate?
 
joone
2015/11/09 20:50:07
Without the page loading, the test works fine so I
 
 | 
| + ASSERT_TRUE(browser()->window()->IsActive()); | 
| + | 
| + // Leaks in GtkFileChooserDialog. http://crbug.com/537468 | 
| + ANNOTATE_SCOPED_MEMORY_LEAK; | 
| + libgtk2ui::FilePicker file_picker(browser()->window()); | 
| + | 
| + gfx::NativeWindow window = browser()->window()->GetNativeWindow(); | 
| + views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window); | 
| + ASSERT_NE(nullptr, widget); | 
| + | 
| + // Run a nested loop until the browser window becomes inactive. | 
| 
 
sky
2015/11/06 21:04:10
Document why this is done.
 
joone
2015/11/09 20:50:07
Done.
 
 | 
| + WidgetActivationWaiter wait_inactive(widget, false); | 
| + wait_inactive.Wait(); | 
| + EXPECT_FALSE(browser()->window()->IsActive()); | 
| + file_picker.Close(); | 
| + | 
| + // Run a nested loop until the browser window becomes active. | 
| + WidgetActivationWaiter wait_active(widget, true); | 
| + wait_active.Wait(); | 
| + EXPECT_TRUE(browser()->window()->IsActive()); | 
| +} |