OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/constrained_window/constrained_window_views.h" | 5 #include <memory> |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "build/build_config.h" | 8 #include "build/build_config.h" |
10 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/browser_commands.h" | 10 #include "chrome/browser/ui/browser_commands.h" |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
13 #include "chrome/browser/ui/views/tab_modal_confirm_dialog_views.h" | 12 #include "chrome/browser/ui/views/tab_modal_confirm_dialog_views.h" |
14 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
15 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
16 #include "chrome/test/base/interactive_test_utils.h" | 15 #include "chrome/test/base/interactive_test_utils.h" |
17 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
| 17 #include "components/constrained_window/constrained_window_views.h" |
18 #include "components/web_modal/web_contents_modal_dialog_host.h" | 18 #include "components/web_modal/web_contents_modal_dialog_host.h" |
19 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 19 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
20 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" | 20 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" |
21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
22 #include "content/public/test/browser_test_utils.h" | 22 #include "content/public/test/browser_test_utils.h" |
23 #include "ui/base/accelerators/accelerator.h" | 23 #include "ui/base/accelerators/accelerator.h" |
24 #include "ui/views/focus/focus_manager.h" | 24 #include "ui/views/focus/focus_manager.h" |
25 #include "ui/views/widget/widget.h" | 25 #include "ui/views/widget/widget.h" |
26 | 26 |
27 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
(...skipping 11 matching lines...) Expand all Loading... |
39 // Don't delete the delegate yet. Keep it around for inspection later. | 39 // Don't delete the delegate yet. Keep it around for inspection later. |
40 void DeleteDelegate() override {} | 40 void DeleteDelegate() override {} |
41 | 41 |
42 ui::ModalType GetModalType() const override { return ui::MODAL_TYPE_CHILD; } | 42 ui::ModalType GetModalType() const override { return ui::MODAL_TYPE_CHILD; } |
43 | 43 |
44 private: | 44 private: |
45 DISALLOW_COPY_AND_ASSIGN(TestDialog); | 45 DISALLOW_COPY_AND_ASSIGN(TestDialog); |
46 }; | 46 }; |
47 | 47 |
48 // A helper function to create and show a web contents modal dialog. | 48 // A helper function to create and show a web contents modal dialog. |
49 scoped_ptr<TestDialog> ShowModalDialog(content::WebContents* web_contents) { | 49 std::unique_ptr<TestDialog> ShowModalDialog( |
50 scoped_ptr<TestDialog> dialog(new TestDialog()); | 50 content::WebContents* web_contents) { |
| 51 std::unique_ptr<TestDialog> dialog(new TestDialog()); |
51 constrained_window::ShowWebModalDialogViews(dialog.get(), web_contents); | 52 constrained_window::ShowWebModalDialogViews(dialog.get(), web_contents); |
52 return dialog; | 53 return dialog; |
53 } | 54 } |
54 | 55 |
55 } // namespace | 56 } // namespace |
56 | 57 |
57 typedef InProcessBrowserTest ConstrainedWindowViewTest; | 58 typedef InProcessBrowserTest ConstrainedWindowViewTest; |
58 | 59 |
59 // Tests the intial focus of tab-modal dialogs, the restoration of focus to the | 60 // Tests the intial focus of tab-modal dialogs, the restoration of focus to the |
60 // browser when they close, and that queued dialogs don't register themselves as | 61 // browser when they close, and that queued dialogs don't register themselves as |
61 // accelerator targets until they are displayed. | 62 // accelerator targets until they are displayed. |
62 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, FocusTest) { | 63 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, FocusTest) { |
63 content::WebContents* web_contents = | 64 content::WebContents* web_contents = |
64 browser()->tab_strip_model()->GetActiveWebContents(); | 65 browser()->tab_strip_model()->GetActiveWebContents(); |
65 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | 66 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); |
66 scoped_ptr<TestDialog> dialog1 = ShowModalDialog(web_contents); | 67 std::unique_ptr<TestDialog> dialog1 = ShowModalDialog(web_contents); |
67 | 68 |
68 // |dialog1| should be active and focused. | 69 // |dialog1| should be active and focused. |
69 EXPECT_TRUE(dialog1->GetWidget()->IsVisible()); | 70 EXPECT_TRUE(dialog1->GetWidget()->IsVisible()); |
70 views::FocusManager* focus_manager = dialog1->GetWidget()->GetFocusManager(); | 71 views::FocusManager* focus_manager = dialog1->GetWidget()->GetFocusManager(); |
71 EXPECT_EQ(dialog1->GetContentsView(), focus_manager->GetFocusedView()); | 72 EXPECT_EQ(dialog1->GetContentsView(), focus_manager->GetFocusedView()); |
72 | 73 |
73 // Create a second dialog. This will also be modal to |web_contents|, but will | 74 // Create a second dialog. This will also be modal to |web_contents|, but will |
74 // remain hidden since the |dialog1| is still showing. | 75 // remain hidden since the |dialog1| is still showing. |
75 scoped_ptr<TestDialog> dialog2 = ShowModalDialog(web_contents); | 76 std::unique_ptr<TestDialog> dialog2 = ShowModalDialog(web_contents); |
76 EXPECT_FALSE(dialog2->GetWidget()->IsVisible()); | 77 EXPECT_FALSE(dialog2->GetWidget()->IsVisible()); |
77 EXPECT_TRUE(dialog1->GetWidget()->IsVisible()); | 78 EXPECT_TRUE(dialog1->GetWidget()->IsVisible()); |
78 EXPECT_EQ(focus_manager, dialog2->GetWidget()->GetFocusManager()); | 79 EXPECT_EQ(focus_manager, dialog2->GetWidget()->GetFocusManager()); |
79 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | 80 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); |
80 EXPECT_EQ(dialog1->GetContentsView(), focus_manager->GetFocusedView()); | 81 EXPECT_EQ(dialog1->GetContentsView(), focus_manager->GetFocusedView()); |
81 | 82 |
82 // Pressing return should close |dialog1|. | 83 // Pressing return should close |dialog1|. |
83 EXPECT_TRUE(focus_manager->ProcessAccelerator( | 84 EXPECT_TRUE(focus_manager->ProcessAccelerator( |
84 ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE))); | 85 ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE))); |
85 content::RunAllPendingInMessageLoop(); | 86 content::RunAllPendingInMessageLoop(); |
(...skipping 18 matching lines...) Expand all Loading... |
104 // Pressing enter again should close |dialog2|. | 105 // Pressing enter again should close |dialog2|. |
105 EXPECT_TRUE(focus_manager->ProcessAccelerator( | 106 EXPECT_TRUE(focus_manager->ProcessAccelerator( |
106 ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE))); | 107 ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE))); |
107 content::RunAllPendingInMessageLoop(); | 108 content::RunAllPendingInMessageLoop(); |
108 EXPECT_EQ(NULL, dialog2->GetWidget()); | 109 EXPECT_EQ(NULL, dialog2->GetWidget()); |
109 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); | 110 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); |
110 } | 111 } |
111 | 112 |
112 // Tests that the tab-modal window is closed properly when its tab is closed. | 113 // Tests that the tab-modal window is closed properly when its tab is closed. |
113 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, TabCloseTest) { | 114 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, TabCloseTest) { |
114 scoped_ptr<TestDialog> dialog = ShowModalDialog( | 115 std::unique_ptr<TestDialog> dialog = |
115 browser()->tab_strip_model()->GetActiveWebContents()); | 116 ShowModalDialog(browser()->tab_strip_model()->GetActiveWebContents()); |
116 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); | 117 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); |
117 chrome::CloseTab(browser()); | 118 chrome::CloseTab(browser()); |
118 content::RunAllPendingInMessageLoop(); | 119 content::RunAllPendingInMessageLoop(); |
119 EXPECT_EQ(NULL, dialog->GetWidget()); | 120 EXPECT_EQ(NULL, dialog->GetWidget()); |
120 } | 121 } |
121 | 122 |
122 // Tests that the tab-modal window is hidden when an other tab is selected and | 123 // Tests that the tab-modal window is hidden when an other tab is selected and |
123 // shown when its tab is selected again. | 124 // shown when its tab is selected again. |
124 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, TabSwitchTest) { | 125 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, TabSwitchTest) { |
125 scoped_ptr<TestDialog> dialog = ShowModalDialog( | 126 std::unique_ptr<TestDialog> dialog = |
126 browser()->tab_strip_model()->GetActiveWebContents()); | 127 ShowModalDialog(browser()->tab_strip_model()->GetActiveWebContents()); |
127 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); | 128 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); |
128 | 129 |
129 // Open a new tab. The tab-modal window should hide itself. | 130 // Open a new tab. The tab-modal window should hide itself. |
130 chrome::NewTab(browser()); | 131 chrome::NewTab(browser()); |
131 EXPECT_FALSE(dialog->GetWidget()->IsVisible()); | 132 EXPECT_FALSE(dialog->GetWidget()->IsVisible()); |
132 | 133 |
133 // Close the new tab. The tab-modal window should show itself again. | 134 // Close the new tab. The tab-modal window should show itself again. |
134 chrome::CloseTab(browser()); | 135 chrome::CloseTab(browser()); |
135 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); | 136 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); |
136 | 137 |
137 // Close the original tab. | 138 // Close the original tab. |
138 chrome::CloseTab(browser()); | 139 chrome::CloseTab(browser()); |
139 content::RunAllPendingInMessageLoop(); | 140 content::RunAllPendingInMessageLoop(); |
140 EXPECT_EQ(NULL, dialog->GetWidget()); | 141 EXPECT_EQ(NULL, dialog->GetWidget()); |
141 } | 142 } |
142 | 143 |
143 // Tests that tab-modal dialogs follow tabs dragged between browser windows. | 144 // Tests that tab-modal dialogs follow tabs dragged between browser windows. |
144 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, TabMoveTest) { | 145 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, TabMoveTest) { |
145 content::WebContents* web_contents = | 146 content::WebContents* web_contents = |
146 browser()->tab_strip_model()->GetActiveWebContents(); | 147 browser()->tab_strip_model()->GetActiveWebContents(); |
147 scoped_ptr<TestDialog> dialog = ShowModalDialog(web_contents); | 148 std::unique_ptr<TestDialog> dialog = ShowModalDialog(web_contents); |
148 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); | 149 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); |
149 | 150 |
150 // Move the tab to a second browser window; but first create another tab. | 151 // Move the tab to a second browser window; but first create another tab. |
151 // That prevents the first browser window from closing when its tab is moved. | 152 // That prevents the first browser window from closing when its tab is moved. |
152 chrome::NewTab(browser()); | 153 chrome::NewTab(browser()); |
153 browser()->tab_strip_model()->DetachWebContentsAt( | 154 browser()->tab_strip_model()->DetachWebContentsAt( |
154 browser()->tab_strip_model()->GetIndexOfWebContents(web_contents)); | 155 browser()->tab_strip_model()->GetIndexOfWebContents(web_contents)); |
155 Browser* browser2 = CreateBrowser(browser()->profile()); | 156 Browser* browser2 = CreateBrowser(browser()->profile()); |
156 browser2->tab_strip_model()->AppendWebContents(web_contents, true); | 157 browser2->tab_strip_model()->AppendWebContents(web_contents, true); |
157 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); | 158 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); |
(...skipping 13 matching lines...) Expand all Loading... |
171 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, NavigationOnBackspace) { | 172 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, NavigationOnBackspace) { |
172 content::WebContents* web_contents = | 173 content::WebContents* web_contents = |
173 browser()->tab_strip_model()->GetActiveWebContents(); | 174 browser()->tab_strip_model()->GetActiveWebContents(); |
174 content::WaitForLoadStop(web_contents); | 175 content::WaitForLoadStop(web_contents); |
175 const GURL original_url = web_contents->GetURL(); | 176 const GURL original_url = web_contents->GetURL(); |
176 EXPECT_NE(GURL(chrome::kChromeUIVersionURL), original_url); | 177 EXPECT_NE(GURL(chrome::kChromeUIVersionURL), original_url); |
177 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIVersionURL)); | 178 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIVersionURL)); |
178 content::WaitForLoadStop(web_contents); | 179 content::WaitForLoadStop(web_contents); |
179 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL), web_contents->GetURL()); | 180 EXPECT_EQ(GURL(chrome::kChromeUIVersionURL), web_contents->GetURL()); |
180 | 181 |
181 scoped_ptr<TestDialog> dialog = ShowModalDialog(web_contents); | 182 std::unique_ptr<TestDialog> dialog = ShowModalDialog(web_contents); |
182 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); | 183 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); |
183 EXPECT_EQ(dialog->GetContentsView(), | 184 EXPECT_EQ(dialog->GetContentsView(), |
184 dialog->GetWidget()->GetFocusManager()->GetFocusedView()); | 185 dialog->GetWidget()->GetFocusManager()->GetFocusedView()); |
185 | 186 |
186 // Pressing backspace should navigate back and close the dialog. | 187 // Pressing backspace should navigate back and close the dialog. |
187 EXPECT_TRUE(chrome::CanGoBack(browser())); | 188 EXPECT_TRUE(chrome::CanGoBack(browser())); |
188 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_BACK, | 189 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_BACK, |
189 false, false, false, false)); | 190 false, false, false, false)); |
190 content::RunAllPendingInMessageLoop(); | 191 content::RunAllPendingInMessageLoop(); |
191 content::WaitForLoadStop(web_contents); | 192 content::WaitForLoadStop(web_contents); |
192 EXPECT_EQ(NULL, dialog->GetWidget()); | 193 EXPECT_EQ(NULL, dialog->GetWidget()); |
193 EXPECT_EQ(original_url, web_contents->GetURL()); | 194 EXPECT_EQ(original_url, web_contents->GetURL()); |
194 } | 195 } |
195 | 196 |
196 // Tests that the dialog closes when the escape key is pressed. | 197 // Tests that the dialog closes when the escape key is pressed. |
197 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, ClosesOnEscape) { | 198 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, ClosesOnEscape) { |
198 #if defined(OS_WIN) | 199 #if defined(OS_WIN) |
199 // TODO(msw): The widget is not made NULL on XP. http://crbug.com/177482 | 200 // TODO(msw): The widget is not made NULL on XP. http://crbug.com/177482 |
200 if (base::win::GetVersion() < base::win::VERSION_VISTA) | 201 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
201 return; | 202 return; |
202 #endif | 203 #endif |
203 | 204 |
204 scoped_ptr<TestDialog> dialog = ShowModalDialog( | 205 std::unique_ptr<TestDialog> dialog = |
205 browser()->tab_strip_model()->GetActiveWebContents()); | 206 ShowModalDialog(browser()->tab_strip_model()->GetActiveWebContents()); |
206 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); | 207 EXPECT_TRUE(dialog->GetWidget()->IsVisible()); |
207 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_ESCAPE, | 208 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_ESCAPE, |
208 false, false, false, false)); | 209 false, false, false, false)); |
209 content::RunAllPendingInMessageLoop(); | 210 content::RunAllPendingInMessageLoop(); |
210 EXPECT_EQ(NULL, dialog->GetWidget()); | 211 EXPECT_EQ(NULL, dialog->GetWidget()); |
211 } | 212 } |
OLD | NEW |