Chromium Code Reviews| 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 // During auto-resizing, dialogs size to (WebContents size) + 16. | |
| 34 static const int kDialogBorderSpace = 16; | |
| 35 | |
| 36 // Expected dialog sizes after auto-resizing. | |
| 37 static const int initial_size = 150 + kDialogBorderSpace; | |
|
msw
2014/12/11 23:30:59
nit: move these into the scope of ContentResizeInA
apacible
2014/12/12 23:02:09
Done.
| |
| 38 static const int new_size = 175 + kDialogBorderSpace; | |
| 39 | |
| 40 bool IsEqualSizes(gfx::Size expected, | |
| 41 ConstrainedWebDialogDelegate* dialog_delegate) { | |
| 42 return expected == dialog_delegate->GetPreferredSize(); | |
| 43 } | |
| 44 | |
| 45 std::string GetChangeDimensionsScript(int dimension) { | |
| 46 return base::StringPrintf("window.document.body.style.width = %d + 'px';" | |
| 47 "window.document.body.style.height = %d + 'px';", dimension, dimension); | |
| 48 } | |
| 49 | |
| 23 class ConstrainedWebDialogBrowserTestObserver | 50 class ConstrainedWebDialogBrowserTestObserver |
| 24 : public content::WebContentsObserver { | 51 : public content::WebContentsObserver { |
| 25 public: | 52 public: |
| 26 explicit ConstrainedWebDialogBrowserTestObserver(WebContents* contents) | 53 explicit ConstrainedWebDialogBrowserTestObserver(WebContents* contents) |
| 27 : content::WebContentsObserver(contents), | 54 : content::WebContentsObserver(contents), |
| 28 contents_destroyed_(false) { | 55 contents_destroyed_(false) { |
| 29 } | 56 } |
| 30 ~ConstrainedWebDialogBrowserTestObserver() override {} | 57 ~ConstrainedWebDialogBrowserTestObserver() override {} |
| 31 | 58 |
| 32 bool contents_destroyed() { return contents_destroyed_; } | 59 bool contents_destroyed() { return contents_destroyed_; } |
| 33 | 60 |
| 34 private: | 61 private: |
| 35 void WebContentsDestroyed() override { contents_destroyed_ = true; } | 62 void WebContentsDestroyed() override { contents_destroyed_ = true; } |
| 36 | 63 |
| 37 bool contents_destroyed_; | 64 bool contents_destroyed_; |
| 38 }; | 65 }; |
| 39 | 66 |
| 40 } // namespace | 67 } // namespace |
| 41 | 68 |
| 42 class ConstrainedWebDialogBrowserTest : public InProcessBrowserTest { | 69 class ConstrainedWebDialogBrowserTest : public InProcessBrowserTest { |
| 43 public: | 70 public: |
| 44 ConstrainedWebDialogBrowserTest() {} | 71 ConstrainedWebDialogBrowserTest() {} |
| 45 | 72 |
| 73 // Runs the current MessageLoop until |condition| is true or timeout. | |
| 74 bool RunLoopUntil(const base::Callback<bool()>& condition) { | |
| 75 const base::TimeTicks start_time = base::TimeTicks::Now(); | |
| 76 while (!condition.Run()) { | |
| 77 const base::TimeTicks current_time = base::TimeTicks::Now(); | |
| 78 if (current_time - start_time > base::TimeDelta::FromSeconds(5)) { | |
| 79 ADD_FAILURE() << "Condition not met within five seconds."; | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 base::MessageLoop::current()->PostDelayedTask( | |
| 84 FROM_HERE, | |
| 85 base::MessageLoop::QuitClosure(), | |
| 86 base::TimeDelta::FromMilliseconds(20)); | |
| 87 content::RunMessageLoop(); | |
| 88 } | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 46 protected: | 92 protected: |
| 47 bool IsShowingWebContentsModalDialog(WebContents* web_contents) const { | 93 bool IsShowingWebContentsModalDialog(WebContents* web_contents) const { |
| 48 WebContentsModalDialogManager* web_contents_modal_dialog_manager = | 94 WebContentsModalDialogManager* web_contents_modal_dialog_manager = |
| 49 WebContentsModalDialogManager::FromWebContents(web_contents); | 95 WebContentsModalDialogManager::FromWebContents(web_contents); |
| 50 return web_contents_modal_dialog_manager->IsDialogActive(); | 96 return web_contents_modal_dialog_manager->IsDialogActive(); |
| 51 } | 97 } |
| 52 }; | 98 }; |
| 53 | 99 |
| 54 // Tests that opening/closing the constrained window won't crash it. | 100 // Tests that opening/closing the constrained window won't crash it. |
| 55 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, BasicTest) { | 101 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, BasicTest) { |
| 56 // The delegate deletes itself. | 102 // The delegate deletes itself. |
| 57 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate( | 103 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate( |
| 58 GURL(chrome::kChromeUIConstrainedHTMLTestURL)); | 104 GURL(chrome::kChromeUIConstrainedHTMLTestURL)); |
| 59 WebContents* web_contents = | 105 WebContents* web_contents = |
| 60 browser()->tab_strip_model()->GetActiveWebContents(); | 106 browser()->tab_strip_model()->GetActiveWebContents(); |
| 61 ASSERT_TRUE(web_contents); | 107 ASSERT_TRUE(web_contents); |
| 62 | 108 |
| 63 ConstrainedWebDialogDelegate* dialog_delegate = | 109 ConstrainedWebDialogDelegate* dialog_delegate = |
| 64 CreateConstrainedWebDialog(browser()->profile(), delegate, web_contents); | 110 ShowConstrainedWebDialog(browser()->profile(), delegate, web_contents); |
| 65 ASSERT_TRUE(dialog_delegate); | 111 ASSERT_TRUE(dialog_delegate); |
| 66 EXPECT_TRUE(dialog_delegate->GetNativeDialog()); | 112 EXPECT_TRUE(dialog_delegate->GetNativeDialog()); |
| 67 EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents)); | 113 EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents)); |
| 68 } | 114 } |
| 69 | 115 |
| 70 // Tests that ReleaseWebContentsOnDialogClose() works. | 116 // Tests that ReleaseWebContentsOnDialogClose() works. |
| 71 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, | 117 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, |
| 72 ReleaseWebContentsOnDialogClose) { | 118 ReleaseWebContentsOnDialogClose) { |
| 73 // The delegate deletes itself. | 119 // The delegate deletes itself. |
| 74 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate( | 120 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate( |
| 75 GURL(chrome::kChromeUIConstrainedHTMLTestURL)); | 121 GURL(chrome::kChromeUIConstrainedHTMLTestURL)); |
| 76 WebContents* web_contents = | 122 WebContents* web_contents = |
| 77 browser()->tab_strip_model()->GetActiveWebContents(); | 123 browser()->tab_strip_model()->GetActiveWebContents(); |
| 78 ASSERT_TRUE(web_contents); | 124 ASSERT_TRUE(web_contents); |
| 79 | 125 |
| 80 ConstrainedWebDialogDelegate* dialog_delegate = | 126 ConstrainedWebDialogDelegate* dialog_delegate = |
| 81 CreateConstrainedWebDialog(browser()->profile(), delegate, web_contents); | 127 ShowConstrainedWebDialog(browser()->profile(), delegate, web_contents); |
| 82 ASSERT_TRUE(dialog_delegate); | 128 ASSERT_TRUE(dialog_delegate); |
| 83 scoped_ptr<WebContents> new_tab(dialog_delegate->GetWebContents()); | 129 scoped_ptr<WebContents> new_tab(dialog_delegate->GetWebContents()); |
| 84 ASSERT_TRUE(new_tab.get()); | 130 ASSERT_TRUE(new_tab.get()); |
| 85 ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents)); | 131 ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents)); |
| 86 | 132 |
| 87 ConstrainedWebDialogBrowserTestObserver observer(new_tab.get()); | 133 ConstrainedWebDialogBrowserTestObserver observer(new_tab.get()); |
| 88 dialog_delegate->ReleaseWebContentsOnDialogClose(); | 134 dialog_delegate->ReleaseWebContentsOnDialogClose(); |
| 89 dialog_delegate->OnDialogCloseFromWebUI(); | 135 dialog_delegate->OnDialogCloseFromWebUI(); |
| 90 | 136 |
| 91 ASSERT_FALSE(observer.contents_destroyed()); | 137 ASSERT_FALSE(observer.contents_destroyed()); |
| 92 EXPECT_FALSE(IsShowingWebContentsModalDialog(web_contents)); | 138 EXPECT_FALSE(IsShowingWebContentsModalDialog(web_contents)); |
| 93 new_tab.reset(); | 139 new_tab.reset(); |
| 94 EXPECT_TRUE(observer.contents_destroyed()); | 140 EXPECT_TRUE(observer.contents_destroyed()); |
| 95 } | 141 } |
| 142 | |
| 143 #if !defined(OS_MACOSX) | |
| 144 // Tests that dialog autoresizes based on web contents when autoresizing | |
| 145 // is enabled. | |
| 146 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, | |
| 147 ContentResizeInAutoResizingDialog) { | |
| 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 CreateConstrainedWebDialogWithAutoResize(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)); | |
|
msw
2014/12/11 23:30:59
q: Where is the call to actually show the dialog?
apacible
2014/12/12 23:02:09
The dialog is shown when the WebContents load (L18
msw
2014/12/12 23:17:35
Hmm, it's odd that this code only ever calls creat
| |
| 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 |