Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / tests / ImeOnFocusTest.cpp
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.
4
5 #include "config.h"
6
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"
17
18 #include <gtest/gtest.h>
19
20 using namespace blink;
21 using blink::FrameTestHelpers::runPendingTasks;
22
23 namespace {
24
25 class ImeRequestTrackingWebViewClient : public FrameTestHelpers::TestWebViewClient {
26 public:
27     ImeRequestTrackingWebViewClient() :
28         m_imeRequestCount(0)
29     {
30     }
31
32     // WebWidgetClient methods
33     virtual void showImeIfNeeded() OVERRIDE
34     {
35         ++m_imeRequestCount;
36     }
37
38     // Local methds
39     void reset()
40     {
41         m_imeRequestCount = 0;
42     }
43
44     int imeRequestCount()
45     {
46         return m_imeRequestCount;
47     }
48
49 private:
50     int m_imeRequestCount;
51 };
52
53 class ImeOnFocusTest : public testing::Test {
54 public:
55     ImeOnFocusTest()
56         : m_baseURL("http://www.test.com/")
57     {
58     }
59
60     virtual void TearDown()
61     {
62         Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
63     }
64
65 protected:
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);
69
70     std::string m_baseURL;
71     FrameTestHelpers::WebViewHelper m_webViewHelper;
72     RefPtrWillBePersistent<Document> m_document;
73 };
74
75 void ImeOnFocusTest::sendGestureTap(WebView* webView, IntPoint clientPoint)
76 {
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;
86
87     webView->handleInputEvent(webGestureEvent);
88     FrameTestHelpers::runPendingTasks();
89 }
90
91 void ImeOnFocusTest::focus(const WTF::AtomicString& element)
92 {
93     m_document->body()->getElementById(element)->focus();
94 }
95
96 void ImeOnFocusTest::runImeOnFocusTest(std::string fileName, int expectedImeRequestCount, IntPoint tapPoint, const WTF::AtomicString& focusElement)
97 {
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>();
105
106     if (!focusElement.isNull())
107         focus(focusElement);
108     EXPECT_EQ(0, client.imeRequestCount());
109
110     if (tapPoint.x() >= 0 && tapPoint.y() >= 0)
111         sendGestureTap(webView, tapPoint);
112
113     if (!focusElement.isNull())
114         focus(focusElement);
115     EXPECT_EQ(expectedImeRequestCount, client.imeRequestCount());
116
117     m_webViewHelper.reset();
118 }
119
120 TEST_F(ImeOnFocusTest, OnLoad)
121 {
122     runImeOnFocusTest("ime-on-focus-on-load.html", 0);
123 }
124
125 TEST_F(ImeOnFocusTest, OnAutofocus)
126 {
127     runImeOnFocusTest("ime-on-focus-on-autofocus.html", 0);
128 }
129
130 TEST_F(ImeOnFocusTest, OnUserGesture)
131 {
132     runImeOnFocusTest("ime-on-focus-on-user-gesture.html", 1, IntPoint(50, 50));
133 }
134
135 TEST_F(ImeOnFocusTest, AfterFirstGesture)
136 {
137     runImeOnFocusTest("ime-on-focus-after-first-gesture.html", 1, IntPoint(50, 50), "input");
138 }
139
140 TEST_F(ImeOnFocusTest, AfterNavigationWithinPage)
141 {
142     runImeOnFocusTest("ime-on-focus-after-navigation-within-page.html", 1, IntPoint(50, 50), "input");
143 }
144
145 }