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 "core/testing/URLTestHelpers.h"
12 #include "public/platform/Platform.h"
13 #include "public/platform/WebUnitTestSupport.h"
14 #include "public/web/WebDocument.h"
15 #include "web/WebLocalFrameImpl.h"
16 #include "web/tests/FrameTestHelpers.h"
18 #include <gtest/gtest.h>
20 using namespace blink;
21 using blink::FrameTestHelpers::runPendingTasks;
22 using blink::FrameTestHelpers::loadFrame;
23 using URLTestHelpers::registerMockedURLFromBaseURL;
27 class ImeRequestTrackingWebViewClient : public FrameTestHelpers::TestWebViewClient {
29 ImeRequestTrackingWebViewClient() :
34 // WebWidgetClient methods
35 virtual void showImeIfNeeded() OVERRIDE
43 m_imeRequestCount = 0;
48 return m_imeRequestCount;
52 int m_imeRequestCount;
55 class ImeOnFocusTest : public testing::Test {
58 : m_baseURL("http://www.test.com/")
62 virtual void TearDown()
64 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
68 void sendGestureTap(WebView*, IntPoint);
69 void focus(const WTF::AtomicString& element);
70 void runImeOnFocusTest(std::string fileName, int, IntPoint tapPoint = IntPoint(-1, -1), const WTF::AtomicString& focusElement = WTF::nullAtom, std::string frame = "");
72 std::string m_baseURL;
73 FrameTestHelpers::WebViewHelper m_webViewHelper;
74 RefPtrWillBePersistent<Document> m_document;
77 void ImeOnFocusTest::sendGestureTap(WebView* webView, IntPoint clientPoint)
79 WebGestureEvent webGestureEvent;
80 webGestureEvent.type = WebInputEvent::GestureTap;
81 webGestureEvent.x = clientPoint.x();
82 webGestureEvent.y = clientPoint.y();
83 webGestureEvent.globalX = clientPoint.x();
84 webGestureEvent.globalY = clientPoint.y();
85 webGestureEvent.data.tap.tapCount = 1;
86 webGestureEvent.data.tap.width = 10;
87 webGestureEvent.data.tap.height = 10;
89 webView->handleInputEvent(webGestureEvent);
90 FrameTestHelpers::runPendingTasks();
93 void ImeOnFocusTest::focus(const WTF::AtomicString& element)
95 m_document->body()->getElementById(element)->focus();
98 void ImeOnFocusTest::runImeOnFocusTest(std::string fileName, int expectedImeRequestCount, IntPoint tapPoint, const WTF::AtomicString& focusElement, std::string frame)
100 ImeRequestTrackingWebViewClient client;
101 registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL), WebString::fromUTF8(fileName));
102 WebViewImpl* webView = m_webViewHelper.initialize(true, 0, &client);
103 m_webViewHelper.webView()->setPageScaleFactorLimits(1, 1);
104 webView->resize(WebSize(800, 1200));
105 loadFrame(webView->mainFrame(), m_baseURL + fileName);
106 m_document = m_webViewHelper.webViewImpl()->mainFrameImpl()->document().unwrap<Document>();
108 if (!focusElement.isNull())
110 EXPECT_EQ(0, client.imeRequestCount());
112 if (tapPoint.x() >= 0 && tapPoint.y() >= 0)
113 sendGestureTap(webView, tapPoint);
115 if (!frame.empty()) {
116 registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL), WebString::fromUTF8(frame));
117 WebFrame* childFrame = webView->mainFrame()->firstChild();
118 loadFrame(childFrame, m_baseURL + frame);
121 if (!focusElement.isNull())
123 EXPECT_EQ(expectedImeRequestCount, client.imeRequestCount());
125 m_webViewHelper.reset();
128 TEST_F(ImeOnFocusTest, OnLoad)
130 runImeOnFocusTest("ime-on-focus-on-load.html", 0);
133 TEST_F(ImeOnFocusTest, OnAutofocus)
135 runImeOnFocusTest("ime-on-focus-on-autofocus.html", 0);
138 TEST_F(ImeOnFocusTest, OnUserGesture)
140 runImeOnFocusTest("ime-on-focus-on-user-gesture.html", 1, IntPoint(50, 50));
143 TEST_F(ImeOnFocusTest, AfterFirstGesture)
145 runImeOnFocusTest("ime-on-focus-after-first-gesture.html", 1, IntPoint(50, 50), "input");
148 TEST_F(ImeOnFocusTest, AfterNavigationWithinPage)
150 runImeOnFocusTest("ime-on-focus-after-navigation-within-page.html", 1, IntPoint(50, 50), "input");
153 TEST_F(ImeOnFocusTest, AfterFrameLoadOnGesture)
155 runImeOnFocusTest("ime-on-focus-after-frame-load-on-gesture.html", 1, IntPoint(50, 50), "input", "frame.html");