1 // Copyright 2014 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.
7 #include "core/dom/Document.h"
8 #include "core/dom/Element.h"
9 #include "core/dom/Node.h"
10 #include "core/html/HTMLElement.h"
11 #include "platform/testing/URLTestHelpers.h"
12 #include "platform/testing/UnitTestHelpers.h"
13 #include "public/platform/Platform.h"
14 #include "public/platform/WebUnitTestSupport.h"
15 #include "public/web/WebDocument.h"
16 #include "web/WebLocalFrameImpl.h"
17 #include "web/tests/FrameTestHelpers.h"
18 #include <gtest/gtest.h>
20 using blink::FrameTestHelpers::loadFrame
;
21 using blink::testing::runPendingTasks
;
22 using blink::URLTestHelpers::registerMockedURLFromBaseURL
;
26 class ImeRequestTrackingWebViewClient
: public FrameTestHelpers::TestWebViewClient
{
28 ImeRequestTrackingWebViewClient() :
33 // WebWidgetClient methods
34 void showImeIfNeeded() override
42 m_imeRequestCount
= 0;
47 return m_imeRequestCount
;
51 int m_imeRequestCount
;
54 class ImeOnFocusTest
: public ::testing::Test
{
57 : m_baseURL("http://www.test.com/")
61 void TearDown() override
63 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
67 void sendGestureTap(WebView
*, IntPoint
);
68 void focus(const AtomicString
& element
);
69 void runImeOnFocusTest(std::string fileName
, int, IntPoint tapPoint
= IntPoint(-1, -1), const AtomicString
& focusElement
= nullAtom
, std::string frame
= "");
71 std::string m_baseURL
;
72 FrameTestHelpers::WebViewHelper m_webViewHelper
;
73 RefPtrWillBePersistent
<Document
> m_document
;
76 void ImeOnFocusTest::sendGestureTap(WebView
* webView
, IntPoint clientPoint
)
78 WebGestureEvent webGestureEvent
;
79 webGestureEvent
.type
= WebInputEvent::GestureTap
;
80 webGestureEvent
.x
= clientPoint
.x();
81 webGestureEvent
.y
= clientPoint
.y();
82 webGestureEvent
.globalX
= clientPoint
.x();
83 webGestureEvent
.globalY
= clientPoint
.y();
84 webGestureEvent
.data
.tap
.tapCount
= 1;
85 webGestureEvent
.data
.tap
.width
= 10;
86 webGestureEvent
.data
.tap
.height
= 10;
88 webView
->handleInputEvent(webGestureEvent
);
92 void ImeOnFocusTest::focus(const AtomicString
& element
)
94 m_document
->body()->getElementById(element
)->focus();
97 void ImeOnFocusTest::runImeOnFocusTest(std::string fileName
, int expectedImeRequestCount
, IntPoint tapPoint
, const AtomicString
& focusElement
, std::string frame
)
99 ImeRequestTrackingWebViewClient client
;
100 registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
), WebString::fromUTF8(fileName
));
101 WebViewImpl
* webView
= m_webViewHelper
.initialize(true, 0, &client
);
102 webView
->resize(WebSize(800, 1200));
103 loadFrame(webView
->mainFrame(), m_baseURL
+ fileName
);
104 m_document
= m_webViewHelper
.webViewImpl()->mainFrameImpl()->document().unwrap
<Document
>();
106 if (!focusElement
.isNull())
108 EXPECT_EQ(0, client
.imeRequestCount());
110 if (tapPoint
.x() >= 0 && tapPoint
.y() >= 0)
111 sendGestureTap(webView
, tapPoint
);
113 if (!frame
.empty()) {
114 registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
), WebString::fromUTF8(frame
));
115 WebFrame
* childFrame
= webView
->mainFrame()->firstChild();
116 loadFrame(childFrame
, m_baseURL
+ frame
);
119 if (!focusElement
.isNull())
121 EXPECT_EQ(expectedImeRequestCount
, client
.imeRequestCount());
123 m_webViewHelper
.reset();
126 TEST_F(ImeOnFocusTest
, OnLoad
)
128 runImeOnFocusTest("ime-on-focus-on-load.html", 0);
131 TEST_F(ImeOnFocusTest
, OnAutofocus
)
133 runImeOnFocusTest("ime-on-focus-on-autofocus.html", 0);
136 TEST_F(ImeOnFocusTest
, OnUserGesture
)
138 runImeOnFocusTest("ime-on-focus-on-user-gesture.html", 1, IntPoint(50, 50));
141 TEST_F(ImeOnFocusTest
, AfterFirstGesture
)
143 runImeOnFocusTest("ime-on-focus-after-first-gesture.html", 1, IntPoint(50, 50), "input");
146 TEST_F(ImeOnFocusTest
, AfterNavigationWithinPage
)
148 runImeOnFocusTest("ime-on-focus-after-navigation-within-page.html", 1, IntPoint(50, 50), "input");
151 TEST_F(ImeOnFocusTest
, AfterFrameLoadOnGesture
)
153 runImeOnFocusTest("ime-on-focus-after-frame-load-on-gesture.html", 1, IntPoint(50, 50), "input", "frame.html");