summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ui/views/controls/webview/webview.cc8
-rw-r--r--ui/views/controls/webview/webview.h1
-rw-r--r--ui/views/controls/webview/webview_unittest.cc69
3 files changed, 78 insertions, 0 deletions
diff --git a/ui/views/controls/webview/webview.cc b/ui/views/controls/webview/webview.cc
index 80ece01..deb98a4 100644
--- a/ui/views/controls/webview/webview.cc
+++ b/ui/views/controls/webview/webview.cc
@@ -199,6 +199,14 @@ bool WebView::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) {
return web_contents() && !web_contents()->IsCrashed();
}
+bool WebView::OnMousePressed(const ui::MouseEvent& event) {
+ if (event.IsOnlyLeftMouseButton() && HitTestPoint(event.location())) {
+ RequestFocus();
+ return true;
+ }
+ return View::OnMousePressed(event);
+}
+
void WebView::OnFocus() {
if (web_contents())
web_contents()->Focus();
diff --git a/ui/views/controls/webview/webview.h b/ui/views/controls/webview/webview.h
index e691e67..65fd347 100644
--- a/ui/views/controls/webview/webview.h
+++ b/ui/views/controls/webview/webview.h
@@ -103,6 +103,7 @@ class WEBVIEW_EXPORT WebView : public View,
void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) override;
bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) override;
+ bool OnMousePressed(const ui::MouseEvent& event) override;
void OnFocus() override;
void AboutToRequestFocusFromTabTraversal(bool reverse) override;
void GetAccessibleState(ui::AXViewState* state) override;
diff --git a/ui/views/controls/webview/webview_unittest.cc b/ui/views/controls/webview/webview_unittest.cc
index 7bc68c2..f94a9f9 100644
--- a/ui/views/controls/webview/webview_unittest.cc
+++ b/ui/views/controls/webview/webview_unittest.cc
@@ -12,6 +12,7 @@
#include "content/public/test/web_contents_tester.h"
#include "content/test/test_content_browser_client.h"
#include "ui/aura/window.h"
+#include "ui/events/event.h"
#include "ui/views/controls/native/native_view_host.h"
#include "ui/views/test/test_views_delegate.h"
#include "ui/views/test/widget_test.h"
@@ -364,4 +365,72 @@ TEST_F(WebViewUnitTest, EmbeddedFullscreenDuringScreenCapture_Switching) {
EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder()->bounds());
}
+// Tests that clicking anywhere within the bounds of WebView, and either outside
+// or inside the bounds of its child NativeViewHost, causes WebView to gain
+// focus.
+TEST_F(WebViewUnitTest, EmbeddedFullscreenDuringScreenCapture_ClickToFocus) {
+ // For this test, add another View that can take focus away from WebView.
+ web_view()->SetBoundsRect(gfx::Rect(0, 0, 100, 90));
+ views::View* const something_to_focus = new views::View();
+ something_to_focus->SetBoundsRect(gfx::Rect(0, 90, 100, 10));
+ something_to_focus->SetFocusable(true);
+ top_level_widget()->GetContentsView()->AddChildView(something_to_focus);
+
+ web_view()->SetEmbedFullscreenWidgetMode(true);
+ ASSERT_EQ(1, web_view()->child_count());
+
+ const scoped_ptr<content::WebContents> web_contents(CreateWebContents());
+ WebViewTestWebContentsDelegate delegate;
+ web_contents->SetDelegate(&delegate);
+ web_view()->SetWebContents(web_contents.get());
+
+ // Begin screen capture of the WebContents and then enter fullscreen mode.
+ // The holder should be centered within WebView and sized to match the capture
+ // size.
+ const gfx::Size capture_size(64, 48);
+ web_contents->IncrementCapturerCount(capture_size);
+ delegate.set_is_fullscreened(true);
+ static_cast<content::WebContentsObserver*>(web_view())->
+ DidToggleFullscreenModeForTab(true);
+ EXPECT_EQ(gfx::Rect(18, 21, 64, 48), holder()->bounds());
+
+ // Focus the other widget.
+ something_to_focus->RequestFocus();
+ EXPECT_FALSE(web_view()->HasFocus());
+ EXPECT_FALSE(holder()->HasFocus());
+ EXPECT_TRUE(something_to_focus->HasFocus());
+
+ // Send mouse press event to WebView outside the bounds of the holder, and
+ // confirm WebView took focus.
+ const ui::MouseEvent click_outside_holder(ui::ET_MOUSE_PRESSED,
+ gfx::Point(1, 1),
+ gfx::Point(), // Immaterial.
+ ui::EF_LEFT_MOUSE_BUTTON,
+ 0);
+ EXPECT_TRUE(static_cast<views::View*>(web_view())->
+ OnMousePressed(click_outside_holder));
+ EXPECT_TRUE(web_view()->HasFocus());
+ EXPECT_FALSE(holder()->HasFocus());
+ EXPECT_FALSE(something_to_focus->HasFocus());
+
+ // Focus the other widget again.
+ something_to_focus->RequestFocus();
+ EXPECT_FALSE(web_view()->HasFocus());
+ EXPECT_FALSE(holder()->HasFocus());
+ EXPECT_TRUE(something_to_focus->HasFocus());
+
+ // Send a mouse press event within the bounds of the holder, and confirm the
+ // WebView took focus.
+ const ui::MouseEvent click_inside_holder(ui::ET_MOUSE_PRESSED,
+ web_view()->bounds().CenterPoint(),
+ gfx::Point(), // Immaterial.
+ ui::EF_LEFT_MOUSE_BUTTON,
+ 0);
+ EXPECT_TRUE(static_cast<views::View*>(web_view())->
+ OnMousePressed(click_inside_holder));
+ EXPECT_TRUE(web_view()->HasFocus());
+ EXPECT_FALSE(holder()->HasFocus());
+ EXPECT_FALSE(something_to_focus->HasFocus());
+}
+
} // namespace views