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