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 "base/callback.h" |
5 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
6 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
7 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
9 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" | 10 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" |
10 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
11 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
12 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 13 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
14 #include "content/public/browser/web_contents_observer.h" | 15 #include "content/public/browser/web_contents_observer.h" |
| 16 #include "content/public/test/browser_test_utils.h" |
| 17 #include "content/public/test/test_navigation_observer.h" |
15 #include "ui/web_dialogs/test/test_web_dialog_delegate.h" | 18 #include "ui/web_dialogs/test/test_web_dialog_delegate.h" |
16 | 19 |
17 using content::WebContents; | 20 using content::WebContents; |
18 using ui::WebDialogDelegate; | 21 using ui::WebDialogDelegate; |
19 using web_modal::WebContentsModalDialogManager; | 22 using web_modal::WebContentsModalDialogManager; |
20 | 23 |
21 namespace { | 24 namespace { |
22 | 25 |
23 class ConstrainedWebDialogBrowserTestObserver | 26 class ConstrainedWebDialogBrowserTestObserver |
24 : public content::WebContentsObserver { | 27 : public content::WebContentsObserver { |
(...skipping 11 matching lines...) Expand all Loading... |
36 | 39 |
37 bool contents_destroyed_; | 40 bool contents_destroyed_; |
38 }; | 41 }; |
39 | 42 |
40 } // namespace | 43 } // namespace |
41 | 44 |
42 class ConstrainedWebDialogBrowserTest : public InProcessBrowserTest { | 45 class ConstrainedWebDialogBrowserTest : public InProcessBrowserTest { |
43 public: | 46 public: |
44 ConstrainedWebDialogBrowserTest() {} | 47 ConstrainedWebDialogBrowserTest() {} |
45 | 48 |
| 49 bool IsEqualSizes(gfx::Size expected, |
| 50 ConstrainedWebDialogDelegate* dialog_delegate) { |
| 51 return expected == dialog_delegate->GetPreferredSize(); |
| 52 } |
| 53 |
| 54 // Runs the current MessageLoop until |condition| is true or timeout. |
| 55 bool RunLoopUntil(const base::Callback<bool()>& condition) { |
| 56 const base::TimeTicks start_time = base::TimeTicks::Now(); |
| 57 while (!condition.Run()) { |
| 58 const base::TimeTicks current_time = base::TimeTicks::Now(); |
| 59 if (current_time - start_time > base::TimeDelta::FromSeconds(5)) { |
| 60 ADD_FAILURE() << "Condition not met within five seconds."; |
| 61 return false; |
| 62 } |
| 63 |
| 64 base::MessageLoop::current()->PostDelayedTask( |
| 65 FROM_HERE, |
| 66 base::MessageLoop::QuitClosure(), |
| 67 base::TimeDelta::FromMilliseconds(20)); |
| 68 content::RunMessageLoop(); |
| 69 } |
| 70 return true; |
| 71 } |
| 72 |
46 protected: | 73 protected: |
47 bool IsShowingWebContentsModalDialog(WebContents* web_contents) const { | 74 bool IsShowingWebContentsModalDialog(WebContents* web_contents) const { |
48 WebContentsModalDialogManager* web_contents_modal_dialog_manager = | 75 WebContentsModalDialogManager* web_contents_modal_dialog_manager = |
49 WebContentsModalDialogManager::FromWebContents(web_contents); | 76 WebContentsModalDialogManager::FromWebContents(web_contents); |
50 return web_contents_modal_dialog_manager->IsDialogActive(); | 77 return web_contents_modal_dialog_manager->IsDialogActive(); |
51 } | 78 } |
52 }; | 79 }; |
53 | 80 |
54 // Tests that opening/closing the constrained window won't crash it. | 81 // Tests that opening/closing the constrained window won't crash it. |
55 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, BasicTest) { | 82 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, BasicTest) { |
56 // The delegate deletes itself. | 83 // The delegate deletes itself. |
57 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate( | 84 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate( |
58 GURL(chrome::kChromeUIConstrainedHTMLTestURL)); | 85 GURL(chrome::kChromeUIConstrainedHTMLTestURL)); |
59 WebContents* web_contents = | 86 WebContents* web_contents = |
60 browser()->tab_strip_model()->GetActiveWebContents(); | 87 browser()->tab_strip_model()->GetActiveWebContents(); |
61 ASSERT_TRUE(web_contents); | 88 ASSERT_TRUE(web_contents); |
62 | 89 |
63 ConstrainedWebDialogDelegate* dialog_delegate = | 90 ConstrainedWebDialogDelegate* dialog_delegate = |
64 CreateConstrainedWebDialog(browser()->profile(), delegate, web_contents); | 91 ShowConstrainedWebDialog(browser()->profile(), delegate, web_contents); |
65 ASSERT_TRUE(dialog_delegate); | 92 ASSERT_TRUE(dialog_delegate); |
66 EXPECT_TRUE(dialog_delegate->GetNativeDialog()); | 93 EXPECT_TRUE(dialog_delegate->GetNativeDialog()); |
67 EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents)); | 94 EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents)); |
68 } | 95 } |
69 | 96 |
70 // Tests that ReleaseWebContentsOnDialogClose() works. | 97 // Tests that ReleaseWebContentsOnDialogClose() works. |
71 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, | 98 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, |
72 ReleaseWebContentsOnDialogClose) { | 99 ReleaseWebContentsOnDialogClose) { |
73 // The delegate deletes itself. | 100 // The delegate deletes itself. |
74 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate( | 101 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate( |
75 GURL(chrome::kChromeUIConstrainedHTMLTestURL)); | 102 GURL(chrome::kChromeUIConstrainedHTMLTestURL)); |
76 WebContents* web_contents = | 103 WebContents* web_contents = |
77 browser()->tab_strip_model()->GetActiveWebContents(); | 104 browser()->tab_strip_model()->GetActiveWebContents(); |
78 ASSERT_TRUE(web_contents); | 105 ASSERT_TRUE(web_contents); |
79 | 106 |
80 ConstrainedWebDialogDelegate* dialog_delegate = | 107 ConstrainedWebDialogDelegate* dialog_delegate = |
81 CreateConstrainedWebDialog(browser()->profile(), delegate, web_contents); | 108 ShowConstrainedWebDialog(browser()->profile(), delegate, web_contents); |
82 ASSERT_TRUE(dialog_delegate); | 109 ASSERT_TRUE(dialog_delegate); |
83 scoped_ptr<WebContents> new_tab(dialog_delegate->GetWebContents()); | 110 scoped_ptr<WebContents> new_tab(dialog_delegate->GetWebContents()); |
84 ASSERT_TRUE(new_tab.get()); | 111 ASSERT_TRUE(new_tab.get()); |
85 ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents)); | 112 ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents)); |
86 | 113 |
87 ConstrainedWebDialogBrowserTestObserver observer(new_tab.get()); | 114 ConstrainedWebDialogBrowserTestObserver observer(new_tab.get()); |
88 dialog_delegate->ReleaseWebContentsOnDialogClose(); | 115 dialog_delegate->ReleaseWebContentsOnDialogClose(); |
89 dialog_delegate->OnDialogCloseFromWebUI(); | 116 dialog_delegate->OnDialogCloseFromWebUI(); |
90 | 117 |
91 ASSERT_FALSE(observer.contents_destroyed()); | 118 ASSERT_FALSE(observer.contents_destroyed()); |
92 EXPECT_FALSE(IsShowingWebContentsModalDialog(web_contents)); | 119 EXPECT_FALSE(IsShowingWebContentsModalDialog(web_contents)); |
93 new_tab.reset(); | 120 new_tab.reset(); |
94 EXPECT_TRUE(observer.contents_destroyed()); | 121 EXPECT_TRUE(observer.contents_destroyed()); |
95 } | 122 } |
| 123 |
| 124 #if !defined(OS_MACOSX) |
| 125 // Tests that dialog autoresizes based on web contents when autoresizing |
| 126 // is enabled. |
| 127 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, |
| 128 ContentResizeInAutoResizingDialog) { |
| 129 std::string url = "data:text/html,<!doctype html>" |
| 130 "<body></body>" |
| 131 "<style>" |
| 132 "body { height: 150px; width: 150px; }" |
| 133 "</style>" |
| 134 "<script>" |
| 135 " function changeDimensions(width, height) {" |
| 136 " window.document.body.style.width = width + 'px';" |
| 137 " window.document.body.style.height = height + 'px';" |
| 138 " }" |
| 139 " document.title='ready';" |
| 140 "</script>"; |
| 141 |
| 142 // The delegate deletes itself. |
| 143 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate( |
| 144 GURL(url)); |
| 145 WebContents* web_contents = |
| 146 browser()->tab_strip_model()->GetActiveWebContents(); |
| 147 ASSERT_TRUE(web_contents); |
| 148 |
| 149 // Observes the next created WebContents. |
| 150 content::TestNavigationObserver observer(NULL); |
| 151 observer.StartWatchingNewWebContents(); |
| 152 |
| 153 gfx::Size min_size = gfx::Size(100, 100); |
| 154 gfx::Size max_size = gfx::Size(200, 200); |
| 155 ConstrainedWebDialogDelegate* dialog_delegate = |
| 156 CreateConstrainedWebDialogWithAutoResize(browser()->profile(), delegate, |
| 157 web_contents, min_size, |
| 158 max_size); |
| 159 ASSERT_TRUE(dialog_delegate); |
| 160 EXPECT_TRUE(dialog_delegate->GetNativeDialog()); |
| 161 ASSERT_FALSE(IsShowingWebContentsModalDialog(web_contents)); |
| 162 EXPECT_EQ(min_size, dialog_delegate->GetMinimumSize()); |
| 163 EXPECT_EQ(max_size, dialog_delegate->GetMaximumSize()); |
| 164 |
| 165 // Check for initial sizing. Dialog was created as a 400x400 dialog. |
| 166 EXPECT_EQ(gfx::Size(), web_contents->GetPreferredSize()); |
| 167 ASSERT_EQ(gfx::Size(400, 400), dialog_delegate->GetPreferredSize()); |
| 168 |
| 169 observer.Wait(); |
| 170 |
| 171 // Wait until the entire WebContents has loaded. |
| 172 base::string16 ready_title(base::ASCIIToUTF16("ready")); |
| 173 content::TitleWatcher watcher(dialog_delegate->GetWebContents(), |
| 174 ready_title); |
| 175 ignore_result(watcher.WaitAndGetTitle()); |
| 176 |
| 177 ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents)); |
| 178 |
| 179 // Resize to content's originally set dimensions. |
| 180 EXPECT_TRUE(RunLoopUntil(base::Bind( |
| 181 &ConstrainedWebDialogBrowserTest::IsEqualSizes, |
| 182 base::Unretained(this), |
| 183 gfx::Size(166, 166), |
| 184 dialog_delegate))); |
| 185 ASSERT_EQ(gfx::Size(166, 166), dialog_delegate->GetPreferredSize()); |
| 186 |
| 187 // Resize to dimensions within expected bounds. |
| 188 EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(), |
| 189 "changeDimensions(175, 175);")); |
| 190 EXPECT_TRUE(RunLoopUntil(base::Bind( |
| 191 &ConstrainedWebDialogBrowserTest::IsEqualSizes, |
| 192 base::Unretained(this), |
| 193 gfx::Size(191, 191), |
| 194 dialog_delegate))); |
| 195 ASSERT_EQ(gfx::Size(191, 191), dialog_delegate->GetPreferredSize()); |
| 196 |
| 197 // Resize to dimensions smaller than the minimum bounds. |
| 198 EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(), |
| 199 "changeDimensions(50, 50);")); |
| 200 EXPECT_TRUE(RunLoopUntil(base::Bind( |
| 201 &ConstrainedWebDialogBrowserTest::IsEqualSizes, |
| 202 base::Unretained(this), |
| 203 gfx::Size(100, 100), |
| 204 dialog_delegate))); |
| 205 ASSERT_EQ(gfx::Size(100, 100), dialog_delegate->GetPreferredSize()); |
| 206 |
| 207 // Resize to dimensions greater than the maximum bounds. |
| 208 EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(), |
| 209 "changeDimensions(250, 250);")); |
| 210 EXPECT_TRUE(RunLoopUntil(base::Bind( |
| 211 &ConstrainedWebDialogBrowserTest::IsEqualSizes, |
| 212 base::Unretained(this), |
| 213 gfx::Size(200, 200), |
| 214 dialog_delegate))); |
| 215 ASSERT_EQ(gfx::Size(200, 200), dialog_delegate->GetPreferredSize()); |
| 216 } |
| 217 |
| 218 // Tests that dialog does not autoresize when autoresizing is not enabled. |
| 219 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, |
| 220 ContentResizeInNonAutoResizingDialog) { |
| 221 std::string url = "data:text/html,<!doctype html>" |
| 222 "<body></body>" |
| 223 "<style>" |
| 224 "body { height: 150px; width: 150px; }" |
| 225 "</style>" |
| 226 "<script>" |
| 227 " function changeDimensions(width, height) {" |
| 228 " window.document.body.style.width = width + 'px';" |
| 229 " window.document.body.style.height = height + 'px';" |
| 230 " }" |
| 231 " document.title='ready';" |
| 232 "</script>"; |
| 233 |
| 234 // The delegate deletes itself. |
| 235 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate( |
| 236 GURL(url)); |
| 237 WebContents* web_contents = |
| 238 browser()->tab_strip_model()->GetActiveWebContents(); |
| 239 ASSERT_TRUE(web_contents); |
| 240 |
| 241 ConstrainedWebDialogDelegate* dialog_delegate = |
| 242 ShowConstrainedWebDialog(browser()->profile(), delegate, web_contents); |
| 243 ASSERT_TRUE(dialog_delegate); |
| 244 EXPECT_TRUE(dialog_delegate->GetNativeDialog()); |
| 245 EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents)); |
| 246 |
| 247 // Wait until the entire WebContents has loaded. |
| 248 base::string16 ready_title(base::ASCIIToUTF16("ready")); |
| 249 content::TitleWatcher watcher(dialog_delegate->GetWebContents(), |
| 250 ready_title); |
| 251 ignore_result(watcher.WaitAndGetTitle()); |
| 252 |
| 253 // Check for initial sizing. Dialog was created as a 400x400 dialog. |
| 254 EXPECT_EQ(gfx::Size(), web_contents->GetPreferredSize()); |
| 255 ASSERT_EQ(gfx::Size(400, 400), dialog_delegate->GetPreferredSize()); |
| 256 |
| 257 // Resize <body> to dimension smaller than dialog. |
| 258 EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(), |
| 259 "changeDimensions(100, 100);")); |
| 260 ASSERT_EQ(gfx::Size(400, 400), dialog_delegate->GetPreferredSize()); |
| 261 |
| 262 // Resize <body> to dimension larger than dialog. |
| 263 EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(), |
| 264 "changeDimensions(500, 500);")); |
| 265 ASSERT_EQ(gfx::Size(400, 400), dialog_delegate->GetPreferredSize()); |
| 266 } |
| 267 #endif |
OLD | NEW |