Commit 115004a2 authored by sauski's avatar sauski Committed by Commit Bot

HaTS Next: Resize dialog based on information pass from web contents

Introduces functionality to the HatsNextWebDialog to support resizing
of the dialog based on the size of the survey within the web contents.

Size information is passed from the HaTS Next JS library through to
native code by pushing URL fragments.

Bug: 1110888
Change-Id: I70d7268cbc10c2da9e34d7dec338180a975f059f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2274603
Commit-Queue: Theodore Olsauskas-Warren <sauski@google.com>
Reviewed-by: default avatarBret Sepulveda <bsep@chromium.org>
Cr-Commit-Position: refs/heads/master@{#797253}
parent c59b53c1
...@@ -190,6 +190,7 @@ class MockHatsNextWebDialog : public HatsNextWebDialog { ...@@ -190,6 +190,7 @@ class MockHatsNextWebDialog : public HatsNextWebDialog {
MOCK_METHOD0(ShowWidget, void()); MOCK_METHOD0(ShowWidget, void());
MOCK_METHOD0(CloseWidget, void()); MOCK_METHOD0(CloseWidget, void());
MOCK_METHOD1(UpdateWidgetSize, void(gfx::Size));
}; };
typedef InProcessBrowserTest HatsNextWebDialogBrowserTest; typedef InProcessBrowserTest HatsNextWebDialogBrowserTest;
...@@ -290,3 +291,36 @@ IN_PROC_BROWSER_TEST_F(HatsNextWebDialogBrowserTest, NewWebContents) { ...@@ -290,3 +291,36 @@ IN_PROC_BROWSER_TEST_F(HatsNextWebDialogBrowserTest, NewWebContents) {
GURL("http://foo.com"), GURL("http://foo.com"),
browser()->tab_strip_model()->GetActiveWebContents()->GetVisibleURL()); browser()->tab_strip_model()->GetActiveWebContents()->GetVisibleURL());
} }
IN_PROC_BROWSER_TEST_F(HatsNextWebDialogBrowserTest, DialogResize) {
ASSERT_TRUE(embedded_test_server()->Start());
auto* dialog = new MockHatsNextWebDialog(
browser(), "resize_for_testing",
embedded_test_server()->GetURL("/hats/hats_next_mock.html"),
base::TimeDelta::FromSeconds(100));
// Check that the dialog attempts to resize with the sizes defined in
// hats_next_mock.html.
base::RunLoop run_loop;
EXPECT_CALL(*dialog, UpdateWidgetSize(gfx::Size(123, 456)))
.WillOnce(testing::Invoke([&run_loop] { run_loop.Quit(); }));
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(HatsNextWebDialogBrowserTest, InvalidSize) {
ASSERT_TRUE(embedded_test_server()->Start());
// Check that providing a size which is too large results in the dialog being
// closed.
auto* dialog = new MockHatsNextWebDialog(
browser(), "invalid_size_for_testing",
embedded_test_server()->GetURL("/hats/hats_next_mock.html"),
base::TimeDelta::FromSeconds(100));
base::RunLoop run_loop;
EXPECT_CALL(*dialog, CloseWidget).WillOnce([&run_loop]() {
run_loop.Quit();
});
run_loop.Run();
}
...@@ -31,7 +31,8 @@ ...@@ -31,7 +31,8 @@
class HatsNextWebDialog::WebContentsDelegate class HatsNextWebDialog::WebContentsDelegate
: public content::WebContentsDelegate { : public content::WebContentsDelegate {
public: public:
explicit WebContentsDelegate(Browser* browser) : browser_(browser) {} explicit WebContentsDelegate(Browser* browser, HatsNextWebDialog* dialog)
: browser_(browser), dialog_(dialog) {}
bool IsWebContentsCreationOverridden( bool IsWebContentsCreationOverridden(
content::SiteInstance* source_site_instance, content::SiteInstance* source_site_instance,
...@@ -61,8 +62,23 @@ class HatsNextWebDialog::WebContentsDelegate ...@@ -61,8 +62,23 @@ class HatsNextWebDialog::WebContentsDelegate
return nullptr; return nullptr;
} }
void SetContentsBounds(content::WebContents* source,
const gfx::Rect& bounds) override {
// Check that the provided bounds do not exceed the dummy window size
// provided to the HaTS library by the wrapper website. These are defined
// in the website source at google3/chrome/hats/website/www/index.html.
if (bounds.width() > 800 || bounds.height() > 600) {
LOG(ERROR) << "Desired dimensions provided by contents exceed maximum"
<< "allowable.";
dialog_->CloseWidget();
return;
}
dialog_->UpdateWidgetSize(bounds.size());
}
private: private:
Browser* browser_; Browser* browser_;
HatsNextWebDialog* dialog_;
}; };
// A thin wrapper that forwards the reference part of the URL associated with // A thin wrapper that forwards the reference part of the URL associated with
...@@ -147,11 +163,7 @@ bool HatsNextWebDialog::HandleContextMenu( ...@@ -147,11 +163,7 @@ bool HatsNextWebDialog::HandleContextMenu(
} }
gfx::Size HatsNextWebDialog::CalculatePreferredSize() const { gfx::Size HatsNextWebDialog::CalculatePreferredSize() const {
// Default width/height of the dialog in screen size, these values are derived return size_;
// from the size of the HaTS HTML component displayed by this dialog.
constexpr int kDefaultHatsDialogWidth = 363;
constexpr int kDefaultHatsDialogHeight = 440;
return gfx::Size(kDefaultHatsDialogWidth, kDefaultHatsDialogHeight);
} }
void HatsNextWebDialog::OnProfileWillBeDestroyed(Profile* profile) { void HatsNextWebDialog::OnProfileWillBeDestroyed(Profile* profile) {
...@@ -183,12 +195,14 @@ HatsNextWebDialog::HatsNextWebDialog(Browser* browser, ...@@ -183,12 +195,14 @@ HatsNextWebDialog::HatsNextWebDialog(Browser* browser,
web_view_ = AddChildView(std::make_unique<views::WebDialogView>( web_view_ = AddChildView(std::make_unique<views::WebDialogView>(
otr_profile_, this, std::make_unique<ChromeWebContentsHandler>(), otr_profile_, this, std::make_unique<ChromeWebContentsHandler>(),
/* use_dialog_frame */ true)); /* use_dialog_frame */ true));
set_margins(gfx::Insets());
widget_ = views::BubbleDialogDelegateView::CreateBubble(this); widget_ = views::BubbleDialogDelegateView::CreateBubble(this);
web_contents_observer_ = web_contents_observer_ =
std::make_unique<WebContentsObserver>(web_view_->web_contents(), this); std::make_unique<WebContentsObserver>(web_view_->web_contents(), this);
web_contents_delegate_ = std::make_unique<WebContentsDelegate>(browser_); web_contents_delegate_ =
std::make_unique<WebContentsDelegate>(browser_, this);
web_view_->web_contents()->SetDelegate(web_contents_delegate_.get()); web_view_->web_contents()->SetDelegate(web_contents_delegate_.get());
loading_timer_.Start(FROM_HERE, timeout_, loading_timer_.Start(FROM_HERE, timeout_,
...@@ -233,6 +247,11 @@ void HatsNextWebDialog::CloseWidget() { ...@@ -233,6 +247,11 @@ void HatsNextWebDialog::CloseWidget() {
widget_->Close(); widget_->Close();
} }
void HatsNextWebDialog::UpdateWidgetSize(gfx::Size size) {
size_ = size;
SizeToContents();
}
bool HatsNextWebDialog::IsWaitingForSurveyForTesting() { bool HatsNextWebDialog::IsWaitingForSurveyForTesting() {
return loading_timer_.IsRunning(); return loading_timer_.IsRunning();
} }
...@@ -85,6 +85,10 @@ class HatsNextWebDialog : public ui::WebDialogDelegate, ...@@ -85,6 +85,10 @@ class HatsNextWebDialog : public ui::WebDialogDelegate,
// closed. Virtual to allow mocking in tests. // closed. Virtual to allow mocking in tests.
virtual void CloseWidget(); virtual void CloseWidget();
// Updates dialog size, provided via state update by the webpage. Virtual to
// allow mocking in tests.
virtual void UpdateWidgetSize(gfx::Size size);
// Returns whether the dialog is still waiting for the survey to load. // Returns whether the dialog is still waiting for the survey to load.
bool IsWaitingForSurveyForTesting(); bool IsWaitingForSurveyForTesting();
...@@ -100,6 +104,11 @@ class HatsNextWebDialog : public ui::WebDialogDelegate, ...@@ -100,6 +104,11 @@ class HatsNextWebDialog : public ui::WebDialogDelegate,
// The HaTS Next survey trigger ID that is provided to the HaTS webpage. // The HaTS Next survey trigger ID that is provided to the HaTS webpage.
const std::string& trigger_id_; const std::string& trigger_id_;
// The size of the dialog. Desired dimensions are provided by the site loaded
// in the web contents. Initialised to arbitrary non-zero value as creation
// of 0 sized windows is disallowed on OSX.
gfx::Size size_ = gfx::Size(10, 10);
views::WebDialogView* web_view_ = nullptr; views::WebDialogView* web_view_ = nullptr;
views::Widget* widget_ = nullptr; views::Widget* widget_ = nullptr;
......
...@@ -20,6 +20,14 @@ ...@@ -20,6 +20,14 @@
window.open('http://foo.com', '_blank'); window.open('http://foo.com', '_blank');
history.pushState('', '', '#loaded'); history.pushState('', '', '#loaded');
} }
if (params.get('trigger_id') == "resize_for_testing") {
window.resizeTo(123, 456);
}
if (params.get('trigger_id') == "invalid_size_for_testing") {
window.resizeTo(1000, 1000);
}
</script> </script>
</head> </head>
<body> <body>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment