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