Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4684)

Unified Diff: chrome/browser/ui/webui/constrained_web_dialog_ui_browsertest.cc

Issue 754953002: Enable AutoResize for Constrained Web Dialogs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move dialog border to constant Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a55e95fa91b9ab39df960116d27cf246fea412ea 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,7 @@
// 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/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
@@ -12,6 +13,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 +23,27 @@ using web_modal::WebContentsModalDialogManager;
namespace {
+static const char kTestDataURL[] = "data:text/html,<!doctype html>"
+ "<body></body>"
+ "<style>"
+ "body { height: 150px; width: 150px; }"
+ "</style>"
+ "<script>"
+ " function changeDimensions(width, height) {"
+ " window.document.body.style.width = width + 'px';"
+ " window.document.body.style.height = height + 'px';"
+ " }"
+ " document.title='ready';"
+ "</script>";
+
+// During auto-resizing, dialogs size to (WebContents size) + 16.
+static const int kDialogBorderSpace = 16;
+
+bool IsEqualSizes(gfx::Size expected,
+ ConstrainedWebDialogDelegate* dialog_delegate) {
+ return expected == dialog_delegate->GetPreferredSize();
+}
+
class ConstrainedWebDialogBrowserTestObserver
: public content::WebContentsObserver {
public:
@@ -43,6 +67,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 +104,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 +121,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 +136,134 @@ 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) {
+ // The delegate deletes itself.
+ 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.
+ 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 =
+ CreateConstrainedWebDialogWithAutoResize(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.
+ base::string16 ready_title(base::ASCIIToUTF16("ready"));
+ content::TitleWatcher watcher(dialog_delegate->GetWebContents(),
+ ready_title);
+ ignore_result(watcher.WaitAndGetTitle());
+
+ ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents));
+
+ // Resize to content's originally set dimensions.
+ EXPECT_TRUE(RunLoopUntil(base::Bind(
+ &IsEqualSizes,
+ 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.
+ dialog_delegate)));
+ ASSERT_EQ(gfx::Size(150 + kDialogBorderSpace, 150 + kDialogBorderSpace),
+ dialog_delegate->GetPreferredSize());
+
+ // Resize to dimensions within expected bounds.
+ EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(),
+ "changeDimensions(175, 175);"));
+ EXPECT_TRUE(RunLoopUntil(base::Bind(
+ &IsEqualSizes,
+ 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.
+ dialog_delegate)));
+ ASSERT_EQ(gfx::Size(175 + kDialogBorderSpace, 175 + kDialogBorderSpace),
+ dialog_delegate->GetPreferredSize());
+
+ // Resize to dimensions smaller than the minimum bounds.
+ EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(),
+ "changeDimensions(50, 50);"));
+ EXPECT_TRUE(RunLoopUntil(base::Bind(
+ &IsEqualSizes,
+ min_size,
+ dialog_delegate)));
+ ASSERT_EQ(min_size, dialog_delegate->GetPreferredSize());
+
+ // Resize to dimensions greater than the maximum bounds.
+ EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(),
+ "changeDimensions(250, 250);"));
+ EXPECT_TRUE(RunLoopUntil(base::Bind(
+ &IsEqualSizes,
+ max_size,
+ dialog_delegate)));
+ ASSERT_EQ(max_size, dialog_delegate->GetPreferredSize());
+}
+
+// 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.
+ base::string16 ready_title(base::ASCIIToUTF16("ready"));
+ content::TitleWatcher watcher(dialog_delegate->GetWebContents(),
+ ready_title);
+ ignore_result(watcher.WaitAndGetTitle());
+
+ 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(),
+ "changeDimensions(100, 100);"));
+ EXPECT_TRUE(RunLoopUntil(base::Bind(
+ &IsEqualSizes,
+ initial_dialog_size,
+ dialog_delegate)));
+ 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.
+
+ // Resize <body> to dimension larger than dialog.
+ EXPECT_TRUE(ExecuteScript(dialog_delegate->GetWebContents(),
+ "changeDimensions(500, 500);"));
+ EXPECT_TRUE(RunLoopUntil(base::Bind(
+ &IsEqualSizes,
+ initial_dialog_size,
+ dialog_delegate)));
+ 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.
+}
+#endif // !OS_MACOSX

Powered by Google App Engine
This is Rietveld 408576698