c906ebaabe5fa0916c62fec9d7dd2b8001a4240c
[profile/ivi/qtbase.git] / tests / auto / gui / kernel / qinputmethod / tst_qinputmethod.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43
44 #include <private/qguiapplication_p.h>
45 #include <private/qinputmethod_p.h>
46 #include <qplatforminputcontext_qpa.h>
47 #include "../../../shared/platforminputcontext.h"
48
49 class InputItem : public QObject
50 {
51     Q_OBJECT
52 public:
53     InputItem() : cursorRectangle(1, 2, 3, 4), m_enabled(true) {}
54
55     bool event(QEvent *event)
56     {
57         if (event->type() == QEvent::InputMethodQuery) {
58             QInputMethodQueryEvent *query = static_cast<QInputMethodQueryEvent *>(event);
59             if (query->queries() & Qt::ImEnabled)
60                 query->setValue(Qt::ImEnabled, m_enabled);
61             if (query->queries() & Qt::ImCursorRectangle)
62                 query->setValue(Qt::ImCursorRectangle, cursorRectangle);
63             if (query->queries() & Qt::ImPreferredLanguage)
64                 query->setValue(Qt::ImPreferredLanguage, QString("English"));
65             m_lastQueries = query->queries();
66             query->accept();
67             return true;
68         }
69         return false;
70     }
71
72     void setEnabled(bool enabled) {
73         if (enabled != m_enabled) {
74             m_enabled = enabled;
75             qApp->inputMethod()->update(Qt::ImEnabled);
76         }
77     }
78
79     QRectF cursorRectangle;
80     Qt::InputMethodQueries m_lastQueries;
81     bool m_enabled;
82 };
83
84
85 class DummyWindow : public QWindow
86 {
87 public:
88     DummyWindow() : m_focusObject(0) {}
89
90     virtual QObject *focusObject() const
91     {
92         return m_focusObject;
93     }
94
95     void setFocusObject(QObject *object)
96     {
97         m_focusObject = object;
98         emit focusObjectChanged(object);
99     }
100
101     QObject *m_focusObject;
102 };
103
104
105 class tst_qinputmethod : public QObject
106 {
107     Q_OBJECT
108 public:
109     tst_qinputmethod() {}
110     virtual ~tst_qinputmethod() {}
111 private slots:
112     void initTestCase();
113     void visible();
114     void animating();
115     void keyboarRectangle();
116     void inputItem();
117     void inputItemTransform();
118     void cursorRectangle();
119     void invokeAction();
120     void reset();
121     void commit();
122     void update();
123     void query();
124     void inputDirection();
125 private:
126     InputItem m_inputItem;
127     PlatformInputContext m_platformInputContext;
128 };
129
130 void tst_qinputmethod::initTestCase()
131 {
132     QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
133     inputMethodPrivate->testContext = &m_platformInputContext;
134 }
135
136 void tst_qinputmethod::visible()
137 {
138     QCOMPARE(qApp->inputMethod()->visible(), false);
139     qApp->inputMethod()->show();
140     QCOMPARE(qApp->inputMethod()->visible(), true);
141
142     qApp->inputMethod()->hide();
143     QCOMPARE(qApp->inputMethod()->visible(), false);
144
145     qApp->inputMethod()->setVisible(true);
146     QCOMPARE(qApp->inputMethod()->visible(), true);
147
148     qApp->inputMethod()->setVisible(false);
149     QCOMPARE(qApp->inputMethod()->visible(), false);
150 }
151
152 void tst_qinputmethod::animating()
153 {
154     QCOMPARE(qApp->inputMethod()->isAnimating(), false);
155
156     m_platformInputContext.m_animating = true;
157     QCOMPARE(qApp->inputMethod()->isAnimating(), true);
158
159     m_platformInputContext.m_animating = false;
160     QCOMPARE(qApp->inputMethod()->isAnimating(), false);
161
162     QSignalSpy spy(qApp->inputMethod(), SIGNAL(animatingChanged()));
163     m_platformInputContext.emitAnimatingChanged();
164     QCOMPARE(spy.count(), 1);
165 }
166
167 void tst_qinputmethod::keyboarRectangle()
168 {
169     QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF());
170
171     m_platformInputContext.m_keyboardRect = QRectF(10, 20, 30, 40);
172     QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF(10, 20, 30, 40));
173
174     QSignalSpy spy(qApp->inputMethod(), SIGNAL(keyboardRectangleChanged()));
175     m_platformInputContext.emitKeyboardRectChanged();
176     QCOMPARE(spy.count(), 1);
177 }
178
179 void tst_qinputmethod::inputItem()
180 {
181     QVERIFY(!qApp->inputMethod()->inputItem());
182     QSignalSpy spy(qApp->inputMethod(), SIGNAL(inputItemChanged()));
183
184     qApp->inputMethod()->setInputItem(&m_inputItem);
185
186     QCOMPARE(qApp->inputMethod()->inputItem(), &m_inputItem);
187     QCOMPARE(spy.count(), 1);
188
189     // reset
190     qApp->inputMethod()->setInputItem(0);
191 }
192
193 void tst_qinputmethod::inputItemTransform()
194 {
195     QCOMPARE(qApp->inputMethod()->inputItemTransform(), QTransform());
196     QSignalSpy spy(qApp->inputMethod(), SIGNAL(cursorRectangleChanged()));
197
198     QTransform transform;
199     transform.translate(10, 10);
200     transform.scale(2, 2);
201     transform.shear(2, 2);
202     qApp->inputMethod()->setInputItemTransform(transform);
203
204     QCOMPARE(qApp->inputMethod()->inputItemTransform(), transform);
205     QCOMPARE(spy.count(), 1);
206
207     // reset
208     qApp->inputMethod()->setInputItemTransform(QTransform());
209 }
210
211 void tst_qinputmethod::cursorRectangle()
212 {
213     QCOMPARE(qApp->inputMethod()->cursorRectangle(), QRectF());
214
215     DummyWindow window;
216     window.show();
217     QTest::qWaitForWindowShown(&window);
218     window.requestActivateWindow();
219     QTRY_COMPARE(qApp->focusWindow(), &window);
220     window.setFocusObject(&m_inputItem);
221
222     QTransform transform;
223     transform.translate(10, 10);
224     transform.scale(2, 2);
225     transform.shear(2, 2);
226     qApp->inputMethod()->setInputItemTransform(transform);
227
228     QCOMPARE(qApp->inputMethod()->cursorRectangle(), transform.mapRect(QRectF(1, 2, 3, 4)));
229
230     m_inputItem.cursorRectangle = QRectF(1.5, 2, 1, 8);
231     QCOMPARE(qApp->inputMethod()->cursorRectangle(), transform.mapRect(QRectF(1.5, 2, 1, 8)));
232
233     // reset
234     m_inputItem.cursorRectangle = QRectF(1, 2, 3, 4);
235     qApp->inputMethod()->setInputItemTransform(QTransform());
236 }
237
238 void tst_qinputmethod::invokeAction()
239 {
240     QCOMPARE(m_platformInputContext.m_action, QInputMethod::Click);
241     QCOMPARE(m_platformInputContext.m_cursorPosition, 0);
242
243     qApp->inputMethod()->invokeAction(QInputMethod::ContextMenu, 5);
244     QCOMPARE(m_platformInputContext.m_action, QInputMethod::ContextMenu);
245     QCOMPARE(m_platformInputContext.m_cursorPosition, 5);
246 }
247
248 void tst_qinputmethod::reset()
249 {
250     QCOMPARE(m_platformInputContext.m_resetCallCount, 0);
251
252     qApp->inputMethod()->reset();
253     QCOMPARE(m_platformInputContext.m_resetCallCount, 1);
254
255     qApp->inputMethod()->reset();
256     QCOMPARE(m_platformInputContext.m_resetCallCount, 2);
257 }
258
259 void tst_qinputmethod::commit()
260 {
261     QCOMPARE(m_platformInputContext.m_commitCallCount, 0);
262
263     qApp->inputMethod()->commit();
264     QCOMPARE(m_platformInputContext.m_commitCallCount, 1);
265
266     qApp->inputMethod()->commit();
267     QCOMPARE(m_platformInputContext.m_commitCallCount, 2);
268 }
269
270 void tst_qinputmethod::update()
271 {
272     DummyWindow window;
273     window.show();
274     QTest::qWaitForWindowShown(&window);
275     window.requestActivateWindow();
276     QTRY_COMPARE(qApp->focusWindow(), &window);
277     window.setFocusObject(&m_inputItem);
278
279     QCOMPARE(m_platformInputContext.m_updateCallCount, 0);
280     QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImhNone));
281
282     qApp->inputMethod()->update(Qt::ImQueryInput);
283     QCOMPARE(m_platformInputContext.m_updateCallCount, 1);
284     QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImQueryInput));
285
286     qApp->inputMethod()->update(Qt::ImQueryAll);
287     QCOMPARE(m_platformInputContext.m_updateCallCount, 2);
288     QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImQueryAll));
289
290     QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF(10, 20, 30, 40));
291 }
292
293 void tst_qinputmethod::query()
294 {
295     QInputMethodQueryEvent query(Qt::InputMethodQueries(Qt::ImPreferredLanguage | Qt::ImCursorRectangle));
296     QGuiApplication::sendEvent(&m_inputItem, &query);
297
298     QString language = query.value(Qt::ImPreferredLanguage).toString();
299     QCOMPARE(language, QString("English"));
300
301     QRect cursorRectangle = query.value(Qt::ImCursorRectangle).toRect();
302     QCOMPARE(cursorRectangle, QRect(1,2,3,4));
303 }
304
305 void tst_qinputmethod::inputDirection()
306 {
307     QCOMPARE(m_platformInputContext.m_inputDirectionCallCount, 0);
308     qApp->inputMethod()->inputDirection();
309     QCOMPARE(m_platformInputContext.m_inputDirectionCallCount, 1);
310
311     QCOMPARE(m_platformInputContext.m_localeCallCount, 0);
312     qApp->inputMethod()->locale();
313     QCOMPARE(m_platformInputContext.m_localeCallCount, 1);
314 }
315
316 QTEST_MAIN(tst_qinputmethod)
317 #include "tst_qinputmethod.moc"