| Index: chrome/browser/ui/webui/constrained_web_dialog_ui_browsertest.cc
|
| diff --git a/chrome/browser/ui/webui/constrained_web_dialog_ui_browsertest.cc b/chrome/browser/ui/webui/constrained_web_dialog_ui_browsertest.cc
|
| index 5570984094b8f76ba6d46db87a6e54f4e11814d3..d9f4f96c279be5a1c82b23494bae1cbd9867a744 100644
|
| --- a/chrome/browser/ui/webui/constrained_web_dialog_ui_browsertest.cc
|
| +++ b/chrome/browser/ui/webui/constrained_web_dialog_ui_browsertest.cc
|
| @@ -2,6 +2,8 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| +#include "base/callback.h"
|
| +#include "base/strings/stringprintf.h"
|
| #include "base/strings/utf_string_conversions.h"
|
| #include "chrome/browser/profiles/profile.h"
|
| #include "chrome/browser/ui/browser.h"
|
| @@ -12,6 +14,8 @@
|
| #include "components/web_modal/web_contents_modal_dialog_manager.h"
|
| #include "content/public/browser/web_contents.h"
|
| #include "content/public/browser/web_contents_observer.h"
|
| +#include "content/public/test/browser_test_utils.h"
|
| +#include "content/public/test/test_navigation_observer.h"
|
| #include "ui/web_dialogs/test/test_web_dialog_delegate.h"
|
|
|
| using content::WebContents;
|
| @@ -20,6 +24,24 @@ using web_modal::WebContentsModalDialogManager;
|
|
|
| namespace {
|
|
|
| +#if !defined(OS_MACOSX)
|
| +static const char kTestDataURL[] = "data:text/html,<!doctype html>"
|
| + "<body></body>"
|
| + "<style>"
|
| + "body { height: 150px; width: 150px; }"
|
| + "</style>";
|
| +
|
| +bool IsEqualSizes(gfx::Size expected,
|
| + ConstrainedWebDialogDelegate* dialog_delegate) {
|
| + return expected == dialog_delegate->GetPreferredSize();
|
| +}
|
| +
|
| +std::string GetChangeDimensionsScript(int dimension) {
|
| + return base::StringPrintf("window.document.body.style.width = %d + 'px';"
|
| + "window.document.body.style.height = %d + 'px';", dimension, dimension);
|
| +}
|
| +#endif
|
| +
|
| class ConstrainedWebDialogBrowserTestObserver
|
| : public content::WebContentsObserver {
|
| public:
|
| @@ -43,6 +65,25 @@ class ConstrainedWebDialogBrowserTest : public InProcessBrowserTest {
|
| public:
|
| ConstrainedWebDialogBrowserTest() {}
|
|
|
| + // Runs the current MessageLoop until |condition| is true or timeout.
|
| + bool RunLoopUntil(const base::Callback<bool()>& condition) {
|
| + const base::TimeTicks start_time = base::TimeTicks::Now();
|
| + while (!condition.Run()) {
|
| + const base::TimeTicks current_time = base::TimeTicks::Now();
|
| + if (current_time - start_time > base::TimeDelta::FromSeconds(5)) {
|
| + ADD_FAILURE() << "Condition not met within five seconds.";
|
| + return false;
|
| + }
|
| +
|
| + base::MessageLoop::current()->PostDelayedTask(
|
| + FROM_HERE,
|
| + base::MessageLoop::QuitClosure(),
|
| + base::TimeDelta::FromMilliseconds(20));
|
| + content::RunMessageLoop();
|
| + }
|
| + return true;
|
| + }
|
| +
|
| protected:
|
| bool IsShowingWebContentsModalDialog(WebContents* web_contents) const {
|
| WebContentsModalDialogManager* web_contents_modal_dialog_manager =
|
| @@ -61,7 +102,7 @@ IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest, BasicTest) {
|
| ASSERT_TRUE(web_contents);
|
|
|
| ConstrainedWebDialogDelegate* dialog_delegate =
|
| - CreateConstrainedWebDialog(browser()->profile(), delegate, web_contents);
|
| + ShowConstrainedWebDialog(browser()->profile(), delegate, web_contents);
|
| ASSERT_TRUE(dialog_delegate);
|
| EXPECT_TRUE(dialog_delegate->GetNativeDialog());
|
| EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents));
|
| @@ -78,7 +119,7 @@ IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest,
|
| ASSERT_TRUE(web_contents);
|
|
|
| ConstrainedWebDialogDelegate* dialog_delegate =
|
| - CreateConstrainedWebDialog(browser()->profile(), delegate, web_contents);
|
| + ShowConstrainedWebDialog(browser()->profile(), delegate, web_contents);
|
| ASSERT_TRUE(dialog_delegate);
|
| scoped_ptr<WebContents> new_tab(dialog_delegate->GetWebContents());
|
| ASSERT_TRUE(new_tab.get());
|
| @@ -93,3 +134,127 @@ IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest,
|
| new_tab.reset();
|
| EXPECT_TRUE(observer.contents_destroyed());
|
| }
|
| +
|
| +#if !defined(OS_MACOSX)
|
| +// Tests that dialog autoresizes based on web contents when autoresizing
|
| +// is enabled.
|
| +IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest,
|
| + ContentResizeInAutoResizingDialog) {
|
| + // During auto-resizing, dialogs size to (WebContents size) + 16.
|
| + const int dialog_border_space = 16;
|
| +
|
| + // Expected dialog sizes after auto-resizing.
|
| + const int initial_size = 150 + dialog_border_space;
|
| + const int new_size = 175 + dialog_border_space;
|
| +
|
| + // The delegate deletes itself.
|
| + WebDialogDelegate* delegate =
|
| + new ui::test::TestWebDialogDelegate(GURL(kTestDataURL));
|
| + WebContents* web_contents =
|
| + browser()->tab_strip_model()->GetActiveWebContents();
|
| + ASSERT_TRUE(web_contents);
|
| +
|
| + // Observes the next created WebContents.
|
| + content::TestNavigationObserver observer(NULL);
|
| + observer.StartWatchingNewWebContents();
|
| +
|
| + gfx::Size min_size = gfx::Size(100, 100);
|
| + gfx::Size max_size = gfx::Size(200, 200);
|
| + gfx::Size initial_dialog_size;
|
| + delegate->GetDialogSize(&initial_dialog_size);
|
| +
|
| + ConstrainedWebDialogDelegate* dialog_delegate =
|
| + ShowConstrainedWebDialogWithAutoResize(browser()->profile(), delegate,
|
| + web_contents, min_size,
|
| + max_size);
|
| + ASSERT_TRUE(dialog_delegate);
|
| + EXPECT_TRUE(dialog_delegate->GetNativeDialog());
|
| + ASSERT_FALSE(IsShowingWebContentsModalDialog(web_contents));
|
| + EXPECT_EQ(min_size, dialog_delegate->GetMinimumSize());
|
| + EXPECT_EQ(max_size, dialog_delegate->GetMaximumSize());
|
| +
|
| + // Check for initial sizing. Dialog was created as a 400x400 dialog.
|
| + EXPECT_EQ(gfx::Size(), web_contents->GetPreferredSize());
|
| + ASSERT_EQ(initial_dialog_size, dialog_delegate->GetPreferredSize());
|
| +
|
| + observer.Wait();
|
| +
|
| + // Wait until the entire WebContents has loaded.
|
| + WaitForLoadStop(dialog_delegate->GetWebContents());
|
| +
|
| + ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents));
|
| +
|
| + // Resize to content's originally set dimensions.
|
| + ASSERT_TRUE(RunLoopUntil(base::Bind(
|
| + &IsEqualSizes,
|
| + gfx::Size(initial_size, initial_size),
|
| + dialog_delegate)));
|
| +
|
| + // Resize to dimensions within expected bounds.
|
| + EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(),
|
| + GetChangeDimensionsScript(175)));
|
| + ASSERT_TRUE(RunLoopUntil(base::Bind(
|
| + &IsEqualSizes,
|
| + gfx::Size(new_size, new_size),
|
| + dialog_delegate)));
|
| +
|
| + // Resize to dimensions smaller than the minimum bounds.
|
| + EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(),
|
| + GetChangeDimensionsScript(50)));
|
| + ASSERT_TRUE(RunLoopUntil(base::Bind(
|
| + &IsEqualSizes,
|
| + min_size,
|
| + dialog_delegate)));
|
| +
|
| + // Resize to dimensions greater than the maximum bounds.
|
| + EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(),
|
| + GetChangeDimensionsScript(250)));
|
| + ASSERT_TRUE(RunLoopUntil(base::Bind(
|
| + &IsEqualSizes,
|
| + max_size,
|
| + dialog_delegate)));
|
| +}
|
| +
|
| +// Tests that dialog does not autoresize when autoresizing is not enabled.
|
| +IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest,
|
| + ContentResizeInNonAutoResizingDialog) {
|
| + // The delegate deletes itself.
|
| + WebDialogDelegate* delegate =
|
| + new ui::test::TestWebDialogDelegate(GURL(kTestDataURL));
|
| + WebContents* web_contents =
|
| + browser()->tab_strip_model()->GetActiveWebContents();
|
| + ASSERT_TRUE(web_contents);
|
| +
|
| + ConstrainedWebDialogDelegate* dialog_delegate =
|
| + ShowConstrainedWebDialog(browser()->profile(), delegate, web_contents);
|
| + ASSERT_TRUE(dialog_delegate);
|
| + EXPECT_TRUE(dialog_delegate->GetNativeDialog());
|
| + EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents));
|
| +
|
| + // Wait until the entire WebContents has loaded.
|
| + WaitForLoadStop(dialog_delegate->GetWebContents());
|
| +
|
| + gfx::Size initial_dialog_size;
|
| + delegate->GetDialogSize(&initial_dialog_size);
|
| +
|
| + // Check for initial sizing. Dialog was created as a 400x400 dialog.
|
| + EXPECT_EQ(gfx::Size(), web_contents->GetPreferredSize());
|
| + ASSERT_EQ(initial_dialog_size, dialog_delegate->GetPreferredSize());
|
| +
|
| + // Resize <body> to dimension smaller than dialog.
|
| + EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(),
|
| + GetChangeDimensionsScript(100)));
|
| + ASSERT_TRUE(RunLoopUntil(base::Bind(
|
| + &IsEqualSizes,
|
| + initial_dialog_size,
|
| + dialog_delegate)));
|
| +
|
| + // Resize <body> to dimension larger than dialog.
|
| + EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(),
|
| + GetChangeDimensionsScript(500)));
|
| + ASSERT_TRUE(RunLoopUntil(base::Bind(
|
| + &IsEqualSizes,
|
| + initial_dialog_size,
|
| + dialog_delegate)));
|
| +}
|
| +#endif // !OS_MACOSX
|
|
|