[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / constrained_window_views_browsertest.cc
blob5f5ac696913dd222b8ec4839df988a9a4f4f1235
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 "base/memory/scoped_ptr.h"
6 #include "chrome/browser/ui/browser.h"
7 #include "chrome/browser/ui/browser_commands.h"
8 #include "chrome/browser/ui/host_desktop.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/browser/ui/views/tab_modal_confirm_dialog_views.h"
11 #include "chrome/common/url_constants.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 "components/web_modal/web_contents_modal_dialog_host.h"
16 #include "components/web_modal/web_contents_modal_dialog_manager.h"
17 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/test/browser_test_utils.h"
20 #include "ui/base/accelerators/accelerator.h"
21 #include "ui/views/focus/focus_manager.h"
22 #include "ui/views/widget/widget.h"
24 #if defined(OS_WIN)
25 #include "base/win/windows_version.h"
26 #endif
28 namespace {
30 class TestDialog : public views::DialogDelegateView {
31 public:
32 TestDialog() { SetFocusable(true); }
33 virtual ~TestDialog() {}
35 virtual views::View* GetInitiallyFocusedView() OVERRIDE { return this; }
36 // Don't delete the delegate yet. Keep it around for inspection later.
37 virtual void DeleteDelegate() OVERRIDE {}
39 virtual ui::ModalType GetModalType() const OVERRIDE {
40 #if defined(USE_ASH)
41 return ui::MODAL_TYPE_CHILD;
42 #else
43 return views::WidgetDelegate::GetModalType();
44 #endif
47 private:
48 DISALLOW_COPY_AND_ASSIGN(TestDialog);
51 // A helper function to create and show a web contents modal dialog.
52 scoped_ptr<TestDialog> ShowModalDialog(content::WebContents* web_contents) {
53 scoped_ptr<TestDialog> dialog(new TestDialog());
54 views::Widget* window = views::Widget::CreateWindowAsFramelessChild(
55 dialog.get(), web_contents->GetNativeView());
56 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents)->
57 ShowModalDialog(window->GetNativeView());
58 return dialog.Pass();
61 } // namespace
63 typedef InProcessBrowserTest ConstrainedWindowViewTest;
65 // Tests the intial focus of tab-modal dialogs, the restoration of focus to the
66 // browser when they close, and that queued dialogs don't register themselves as
67 // accelerator targets until they are displayed.
68 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, FocusTest) {
69 content::WebContents* web_contents =
70 browser()->tab_strip_model()->GetActiveWebContents();
71 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
72 scoped_ptr<TestDialog> dialog1 = ShowModalDialog(web_contents);
74 // |dialog1| should be active and focused.
75 EXPECT_TRUE(dialog1->GetWidget()->IsVisible());
76 views::FocusManager* focus_manager = dialog1->GetWidget()->GetFocusManager();
77 EXPECT_EQ(dialog1->GetContentsView(), focus_manager->GetFocusedView());
79 // Create a second dialog. This will also be modal to |web_contents|, but will
80 // remain hidden since the |dialog1| is still showing.
81 scoped_ptr<TestDialog> dialog2 = ShowModalDialog(web_contents);
82 EXPECT_FALSE(dialog2->GetWidget()->IsVisible());
83 EXPECT_TRUE(dialog1->GetWidget()->IsVisible());
84 EXPECT_EQ(focus_manager, dialog2->GetWidget()->GetFocusManager());
85 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
86 EXPECT_EQ(dialog1->GetContentsView(), focus_manager->GetFocusedView());
88 // Pressing return should close |dialog1|.
89 EXPECT_TRUE(focus_manager->ProcessAccelerator(
90 ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)));
91 content::RunAllPendingInMessageLoop();
92 EXPECT_EQ(NULL, dialog1->GetWidget());
94 // |dialog2| should be visible and focused.
95 EXPECT_TRUE(dialog2->GetWidget()->IsVisible());
96 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
97 EXPECT_EQ(dialog2->GetContentsView(), focus_manager->GetFocusedView());
99 // Creating a new tab should take focus away from the other tab's dialog.
100 const int tab_with_dialog = browser()->tab_strip_model()->active_index();
101 chrome::NewTab(browser());
102 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
103 EXPECT_NE(dialog2->GetContentsView(), focus_manager->GetFocusedView());
105 // Activating the previous tab should bring focus to the dialog.
106 browser()->tab_strip_model()->ActivateTabAt(tab_with_dialog, false);
107 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
108 EXPECT_EQ(dialog2->GetContentsView(), focus_manager->GetFocusedView());
110 // Pressing enter again should close |dialog2|.
111 EXPECT_TRUE(focus_manager->ProcessAccelerator(
112 ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)));
113 content::RunAllPendingInMessageLoop();
114 EXPECT_EQ(NULL, dialog2->GetWidget());
115 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
118 // Tests that the tab-modal window is closed properly when its tab is closed.
119 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, TabCloseTest) {
120 scoped_ptr<TestDialog> dialog = ShowModalDialog(
121 browser()->tab_strip_model()->GetActiveWebContents());
122 EXPECT_TRUE(dialog->GetWidget()->IsVisible());
123 chrome::CloseTab(browser());
124 content::RunAllPendingInMessageLoop();
125 EXPECT_EQ(NULL, dialog->GetWidget());
128 // Tests that the tab-modal window is hidden when an other tab is selected and
129 // shown when its tab is selected again.
130 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, TabSwitchTest) {
131 scoped_ptr<TestDialog> dialog = ShowModalDialog(
132 browser()->tab_strip_model()->GetActiveWebContents());
133 EXPECT_TRUE(dialog->GetWidget()->IsVisible());
135 // Open a new tab. The tab-modal window should hide itself.
136 chrome::NewTab(browser());
137 EXPECT_FALSE(dialog->GetWidget()->IsVisible());
139 // Close the new tab. The tab-modal window should show itself again.
140 chrome::CloseTab(browser());
141 EXPECT_TRUE(dialog->GetWidget()->IsVisible());
143 // Close the original tab.
144 chrome::CloseTab(browser());
145 content::RunAllPendingInMessageLoop();
146 EXPECT_EQ(NULL, dialog->GetWidget());
149 // Tests that tab-modal dialogs follow tabs dragged between browser windows.
150 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, TabMoveTest) {
151 content::WebContents* web_contents =
152 browser()->tab_strip_model()->GetActiveWebContents();
153 scoped_ptr<TestDialog> dialog = ShowModalDialog(web_contents);
154 EXPECT_TRUE(dialog->GetWidget()->IsVisible());
156 // Move the tab to a second browser window; but first create another tab.
157 // That prevents the first browser window from closing when its tab is moved.
158 chrome::NewTab(browser());
159 browser()->tab_strip_model()->DetachWebContentsAt(
160 browser()->tab_strip_model()->GetIndexOfWebContents(web_contents));
161 Browser* browser2 = CreateBrowser(browser()->profile());
162 browser2->tab_strip_model()->AppendWebContents(web_contents, true);
163 EXPECT_TRUE(dialog->GetWidget()->IsVisible());
165 // Close the first browser.
166 chrome::CloseWindow(browser());
167 content::RunAllPendingInMessageLoop();
168 EXPECT_TRUE(dialog->GetWidget()->IsVisible());
170 // Close the dialog's browser window.
171 chrome::CloseTab(browser2);
172 content::RunAllPendingInMessageLoop();
173 EXPECT_EQ(NULL, dialog->GetWidget());
176 // Tests that the web contents navigates when backspace is pressed.
177 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, NavigationOnBackspace) {
178 content::WebContents* web_contents =
179 browser()->tab_strip_model()->GetActiveWebContents();
180 content::WaitForLoadStop(web_contents);
181 const GURL original_url = web_contents->GetURL();
182 EXPECT_NE(GURL(chrome::kChromeUIVersionURL), original_url);
183 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIVersionURL));
184 content::WaitForLoadStop(web_contents);
185 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL), web_contents->GetURL());
187 scoped_ptr<TestDialog> dialog = ShowModalDialog(web_contents);
188 EXPECT_TRUE(dialog->GetWidget()->IsVisible());
189 EXPECT_EQ(dialog->GetContentsView(),
190 dialog->GetWidget()->GetFocusManager()->GetFocusedView());
192 // Pressing backspace should navigate back and close the dialog.
193 EXPECT_TRUE(chrome::CanGoBack(browser()));
194 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_BACK,
195 false, false, false, false));
196 content::RunAllPendingInMessageLoop();
197 content::WaitForLoadStop(web_contents);
198 EXPECT_EQ(NULL, dialog->GetWidget());
199 EXPECT_EQ(original_url, web_contents->GetURL());
202 // Tests that the dialog closes when the escape key is pressed.
203 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, ClosesOnEscape) {
204 #if defined(OS_WIN)
205 // TODO(msw): The widget is not made NULL on XP. http://crbug.com/177482
206 if (base::win::GetVersion() < base::win::VERSION_VISTA)
207 return;
208 #endif
210 scoped_ptr<TestDialog> dialog = ShowModalDialog(
211 browser()->tab_strip_model()->GetActiveWebContents());
212 EXPECT_TRUE(dialog->GetWidget()->IsVisible());
213 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_ESCAPE,
214 false, false, false, false));
215 content::RunAllPendingInMessageLoop();
216 EXPECT_EQ(NULL, dialog->GetWidget());