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 |
| 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, | |
|
msw
2014/12/09 20:34:41
Why does this need to be a ConstrainedWebDialogBro
apacible
2014/12/10 18:39:29
Done.
| |
| 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) { | |
|
msw
2014/12/09 20:34:41
This seems like a bad practice that may yield flak
apacible
2014/12/10 18:39:29
The dialog resizing is asynchronous, so we want to
miu
2014/12/10 19:28:22
To be clear, the resizing is initiated by the rend
msw
2014/12/11 01:13:57
I was hoping this could use something like WaitFor
apacible
2014/12/11 03:49:24
I looked into WaitForResizeComplete, but it's aura
| |
| 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( | |
|
msw
2014/12/09 20:34:41
nit: break line after assignment operator
apacible
2014/12/10 18:39:29
Done.
msw
2014/12/11 01:13:57
Ping (not actually done).
apacible
2014/12/11 03:49:24
Oops, sorry. Done.
| |
| 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 gfx::Size initial_dialog_size; | |
| 156 delegate->GetDialogSize(&initial_dialog_size); | |
| 157 | |
| 158 ConstrainedWebDialogDelegate* dialog_delegate = | |
| 159 CreateConstrainedWebDialogWithAutoResize(browser()->profile(), delegate, | |
| 160 web_contents, min_size, | |
| 161 max_size); | |
| 162 ASSERT_TRUE(dialog_delegate); | |
| 163 EXPECT_TRUE(dialog_delegate->GetNativeDialog()); | |
| 164 ASSERT_FALSE(IsShowingWebContentsModalDialog(web_contents)); | |
| 165 EXPECT_EQ(min_size, dialog_delegate->GetMinimumSize()); | |
| 166 EXPECT_EQ(max_size, dialog_delegate->GetMaximumSize()); | |
| 167 | |
| 168 // Check for initial sizing. Dialog was created as a 400x400 dialog. | |
| 169 EXPECT_EQ(gfx::Size(), web_contents->GetPreferredSize()); | |
| 170 ASSERT_EQ(initial_dialog_size, dialog_delegate->GetPreferredSize()); | |
| 171 | |
| 172 observer.Wait(); | |
| 173 | |
| 174 // Wait until the entire WebContents has loaded. | |
| 175 base::string16 ready_title(base::ASCIIToUTF16("ready")); | |
|
msw
2014/12/09 20:34:41
Is there no better pattern to observe webcontents
apacible
2014/12/10 18:39:29
I tried using WaitForLoadStop*, but it doesn't alw
msw
2014/12/11 01:13:57
There's probably some mechanism (maybe a test util
apacible
2014/12/11 03:49:24
Done. I moved the JS to a call in ExecuteScript an
| |
| 176 content::TitleWatcher watcher(dialog_delegate->GetWebContents(), | |
| 177 ready_title); | |
| 178 ignore_result(watcher.WaitAndGetTitle()); | |
| 179 | |
| 180 ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents)); | |
| 181 | |
| 182 // Dialog size to WebContents size + 16 for both width and height. | |
|
msw
2014/12/09 20:34:41
This seems fragile, can this value be determined w
apacible
2014/12/10 18:39:29
I haven't found a way to determine the value yet,
apacible
2014/12/10 20:32:58
Per offline conversation with msw -- made the dial
| |
| 183 // Resize to content's originally set dimensions. | |
| 184 EXPECT_TRUE(RunLoopUntil(base::Bind( | |
| 185 &ConstrainedWebDialogBrowserTest::IsEqualSizes, | |
| 186 base::Unretained(this), | |
| 187 gfx::Size(166, 166), // 150 + 16 | |
| 188 dialog_delegate))); | |
| 189 ASSERT_EQ(gfx::Size(166, 166), dialog_delegate->GetPreferredSize()); | |
| 190 | |
| 191 // Resize to dimensions within expected bounds. | |
| 192 EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(), | |
| 193 "changeDimensions(175, 175);")); | |
| 194 EXPECT_TRUE(RunLoopUntil(base::Bind( | |
| 195 &ConstrainedWebDialogBrowserTest::IsEqualSizes, | |
| 196 base::Unretained(this), | |
| 197 gfx::Size(191, 191), // 175 + 16 | |
| 198 dialog_delegate))); | |
| 199 ASSERT_EQ(gfx::Size(191, 191), dialog_delegate->GetPreferredSize()); | |
| 200 | |
| 201 // Resize to dimensions smaller than the minimum bounds. | |
| 202 EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(), | |
| 203 "changeDimensions(50, 50);")); | |
| 204 EXPECT_TRUE(RunLoopUntil(base::Bind( | |
| 205 &ConstrainedWebDialogBrowserTest::IsEqualSizes, | |
| 206 base::Unretained(this), | |
| 207 min_size, | |
| 208 dialog_delegate))); | |
| 209 ASSERT_EQ(min_size, dialog_delegate->GetPreferredSize()); | |
| 210 | |
| 211 // Resize to dimensions greater than the maximum bounds. | |
| 212 EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(), | |
| 213 "changeDimensions(250, 250);")); | |
| 214 EXPECT_TRUE(RunLoopUntil(base::Bind( | |
| 215 &ConstrainedWebDialogBrowserTest::IsEqualSizes, | |
| 216 base::Unretained(this), | |
| 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 std::string url = "data:text/html,<!doctype html>" | |
|
msw
2014/12/09 20:34:41
nit: make this a shared local/class static const c
apacible
2014/12/10 18:39:29
Done.
| |
| 226 "<body></body>" | |
| 227 "<style>" | |
| 228 "body { height: 150px; width: 150px; }" | |
| 229 "</style>" | |
| 230 "<script>" | |
| 231 " function changeDimensions(width, height) {" | |
| 232 " window.document.body.style.width = width + 'px';" | |
| 233 " window.document.body.style.height = height + 'px';" | |
| 234 " }" | |
| 235 " document.title='ready';" | |
| 236 "</script>"; | |
| 237 | |
| 238 // The delegate deletes itself. | |
| 239 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate( | |
| 240 GURL(url)); | |
| 241 WebContents* web_contents = | |
| 242 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 243 ASSERT_TRUE(web_contents); | |
| 244 | |
| 245 ConstrainedWebDialogDelegate* dialog_delegate = | |
| 246 ShowConstrainedWebDialog(browser()->profile(), delegate, web_contents); | |
| 247 ASSERT_TRUE(dialog_delegate); | |
| 248 EXPECT_TRUE(dialog_delegate->GetNativeDialog()); | |
| 249 EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents)); | |
| 250 | |
| 251 // Wait until the entire WebContents has loaded. | |
| 252 base::string16 ready_title(base::ASCIIToUTF16("ready")); | |
| 253 content::TitleWatcher watcher(dialog_delegate->GetWebContents(), | |
| 254 ready_title); | |
| 255 ignore_result(watcher.WaitAndGetTitle()); | |
| 256 | |
| 257 gfx::Size initial_dialog_size; | |
| 258 delegate->GetDialogSize(&initial_dialog_size); | |
| 259 | |
| 260 // Check for initial sizing. Dialog was created as a 400x400 dialog. | |
| 261 EXPECT_EQ(gfx::Size(), web_contents->GetPreferredSize()); | |
| 262 ASSERT_EQ(initial_dialog_size, dialog_delegate->GetPreferredSize()); | |
| 263 | |
| 264 // Resize <body> to dimension smaller than dialog. | |
| 265 EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(), | |
| 266 "changeDimensions(100, 100);")); | |
| 267 ASSERT_EQ(initial_dialog_size, dialog_delegate->GetPreferredSize()); | |
|
msw
2014/12/09 20:34:41
Shouldn't there theoretically be the same wait/cal
apacible
2014/12/10 18:39:29
Done.
| |
| 268 | |
| 269 // Resize <body> to dimension larger than dialog. | |
| 270 EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(), | |
| 271 "changeDimensions(500, 500);")); | |
| 272 ASSERT_EQ(initial_dialog_size, dialog_delegate->GetPreferredSize()); | |
| 273 } | |
| 274 #endif // !OS_MACOSX | |
| OLD | NEW |