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