Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / constrained_web_dialog_ui_browsertest.cc
blobd9f4f96c279be5a1c82b23494bae1cbd9867a744
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
3 // found in the LICENSE file.
5 #include "base/callback.h"
6 #include "base/strings/stringprintf.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
12 #include "chrome/common/url_constants.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "components/web_modal/web_contents_modal_dialog_manager.h"
15 #include "content/public/browser/web_contents.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"
19 #include "ui/web_dialogs/test/test_web_dialog_delegate.h"
21 using content::WebContents;
22 using ui::WebDialogDelegate;
23 using web_modal::WebContentsModalDialogManager;
25 namespace {
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>";
34 bool IsEqualSizes(gfx::Size expected,
35 ConstrainedWebDialogDelegate* dialog_delegate) {
36 return expected == dialog_delegate->GetPreferredSize();
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);
43 #endif
45 class ConstrainedWebDialogBrowserTestObserver
46 : public content::WebContentsObserver {
47 public:
48 explicit ConstrainedWebDialogBrowserTestObserver(WebContents* contents)
49 : content::WebContentsObserver(contents),
50 contents_destroyed_(false) {
52 ~ConstrainedWebDialogBrowserTestObserver() override {}
54 bool contents_destroyed() { return contents_destroyed_; }
56 private:
57 void WebContentsDestroyed() override { contents_destroyed_ = true; }
59 bool contents_destroyed_;
62 } // namespace
64 class ConstrainedWebDialogBrowserTest : public InProcessBrowserTest {
65 public:
66 ConstrainedWebDialogBrowserTest() {}
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;
78 base::MessageLoop::current()->PostDelayedTask(
79 FROM_HERE,
80 base::MessageLoop::QuitClosure(),
81 base::TimeDelta::FromMilliseconds(20));
82 content::RunMessageLoop();
84 return true;
87 protected:
88 bool IsShowingWebContentsModalDialog(WebContents* web_contents) const {
89 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
90 WebContentsModalDialogManager::FromWebContents(web_contents);
91 return web_contents_modal_dialog_manager->IsDialogActive();
95 // Tests that opening/closing the constrained window won't crash it.
96 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, BasicTest) {
97 // The delegate deletes itself.
98 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate(
99 GURL(chrome::kChromeUIConstrainedHTMLTestURL));
100 WebContents* web_contents =
101 browser()->tab_strip_model()->GetActiveWebContents();
102 ASSERT_TRUE(web_contents);
104 ConstrainedWebDialogDelegate* dialog_delegate =
105 ShowConstrainedWebDialog(browser()->profile(), delegate, web_contents);
106 ASSERT_TRUE(dialog_delegate);
107 EXPECT_TRUE(dialog_delegate->GetNativeDialog());
108 EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents));
111 // Tests that ReleaseWebContentsOnDialogClose() works.
112 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest,
113 ReleaseWebContentsOnDialogClose) {
114 // The delegate deletes itself.
115 WebDialogDelegate* delegate = new ui::test::TestWebDialogDelegate(
116 GURL(chrome::kChromeUIConstrainedHTMLTestURL));
117 WebContents* web_contents =
118 browser()->tab_strip_model()->GetActiveWebContents();
119 ASSERT_TRUE(web_contents);
121 ConstrainedWebDialogDelegate* dialog_delegate =
122 ShowConstrainedWebDialog(browser()->profile(), delegate, web_contents);
123 ASSERT_TRUE(dialog_delegate);
124 scoped_ptr<WebContents> new_tab(dialog_delegate->GetWebContents());
125 ASSERT_TRUE(new_tab.get());
126 ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents));
128 ConstrainedWebDialogBrowserTestObserver observer(new_tab.get());
129 dialog_delegate->ReleaseWebContentsOnDialogClose();
130 dialog_delegate->OnDialogCloseFromWebUI();
132 ASSERT_FALSE(observer.contents_destroyed());
133 EXPECT_FALSE(IsShowingWebContentsModalDialog(web_contents));
134 new_tab.reset();
135 EXPECT_TRUE(observer.contents_destroyed());
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;
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;
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);
157 // Observes the next created WebContents.
158 content::TestNavigationObserver observer(NULL);
159 observer.StartWatchingNewWebContents();
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);
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());
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());
180 observer.Wait();
182 // Wait until the entire WebContents has loaded.
183 WaitForLoadStop(dialog_delegate->GetWebContents());
185 ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents));
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)));
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)));
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)));
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)));
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);
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));
234 // Wait until the entire WebContents has loaded.
235 WaitForLoadStop(dialog_delegate->GetWebContents());
237 gfx::Size initial_dialog_size;
238 delegate->GetDialogSize(&initial_dialog_size);
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());
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)));
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)));
260 #endif // !OS_MACOSX