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 "public/platform/Platform.h"
12 #include "public/platform/WebUnitTestSupport.h"
13 #include "public/web/WebDocument.h"
14 #include "web/WebLocalFrameImpl.h"
15 #include "web/tests/FrameTestHelpers.h"
16 #include "web/tests/URLTestHelpers.h"
18 #include <gtest/gtest.h>
20 using namespace blink;
21 using blink::FrameTestHelpers::runPendingTasks;
25 class ImeRequestTrackingWebViewClient : public FrameTestHelpers::TestWebViewClient {
27 ImeRequestTrackingWebViewClient() :
32 // WebWidgetClient methods
33 virtual void showImeIfNeeded() OVERRIDE
41 m_imeRequestCount = 0;
46 return m_imeRequestCount;
50 int m_imeRequestCount;
53 class ImeOnFocusTest : public testing::Test {
56 : m_baseURL("http://www.test.com/")
60 virtual void TearDown()
62 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
66 void sendGestureTap(WebView*, IntPoint);
67 void focus(const WTF::AtomicString& element);
68 void runImeOnFocusTest(std::string fileName, int, IntPoint tapPoint = IntPoint(-1, -1), const WTF::AtomicString& focusElement = WTF::nullAtom);
70 std::string m_baseURL;
71 FrameTestHelpers::WebViewHelper m_webViewHelper;
72 RefPtrWillBePersistent<Document> m_document;
75 void ImeOnFocusTest::sendGestureTap(WebView* webView, IntPoint clientPoint)
77 WebGestureEvent webGestureEvent;
78 webGestureEvent.type = WebInputEvent::GestureTap;
79 webGestureEvent.x = clientPoint.x();
80 webGestureEvent.y = clientPoint.y();
81 webGestureEvent.globalX = clientPoint.x();
82 webGestureEvent.globalY = clientPoint.y();
83 webGestureEvent.data.tap.tapCount = 1;
84 webGestureEvent.data.tap.width = 10;
85 webGestureEvent.data.tap.height = 10;
87 webView->handleInputEvent(webGestureEvent);
88 FrameTestHelpers::runPendingTasks();
91 void ImeOnFocusTest::focus(const WTF::AtomicString& element)
93 m_document->body()->getElementById(element)->focus();
96 void ImeOnFocusTest::runImeOnFocusTest(std::string fileName, int expectedImeRequestCount, IntPoint tapPoint, const WTF::AtomicString& focusElement)
98 ImeRequestTrackingWebViewClient client;
99 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL), WebString::fromUTF8(fileName));
100 WebViewImpl* webView = m_webViewHelper.initialize(true, 0, &client);
101 m_webViewHelper.webView()->setPageScaleFactorLimits(1, 1);
102 webView->resize(WebSize(800, 1200));
103 FrameTestHelpers::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 (!focusElement.isNull())
115 EXPECT_EQ(expectedImeRequestCount, client.imeRequestCount());
117 m_webViewHelper.reset();
120 TEST_F(ImeOnFocusTest, OnLoad)
122 runImeOnFocusTest("ime-on-focus-on-load.html", 0);
125 TEST_F(ImeOnFocusTest, OnAutofocus)
127 runImeOnFocusTest("ime-on-focus-on-autofocus.html", 0);
130 TEST_F(ImeOnFocusTest, OnUserGesture)
132 runImeOnFocusTest("ime-on-focus-on-user-gesture.html", 1, IntPoint(50, 50));
135 TEST_F(ImeOnFocusTest, AfterFirstGesture)
137 runImeOnFocusTest("ime-on-focus-after-first-gesture.html", 1, IntPoint(50, 50), "input");
140 TEST_F(ImeOnFocusTest, AfterNavigationWithinPage)
142 runImeOnFocusTest("ime-on-focus-after-navigation-within-page.html", 1, IntPoint(50, 50), "input");