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