Rename all QWindow properties that have "window" in them
[profile/ivi/qtbase.git] / tests / auto / gui / kernel / qinputmethod / tst_qinputmethod.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 <qpa/qplatforminputcontext.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 isVisible();
114     void animating();
115     void keyboarRectangle();
116     void inputItemTransform();
117     void cursorRectangle();
118     void invokeAction();
119     void reset();
120     void commit();
121     void update();
122     void query();
123     void inputDirection();
124     void inputMethodAccepted();
125
126 private:
127     InputItem m_inputItem;
128     PlatformInputContext m_platformInputContext;
129 };
130
131 void tst_qinputmethod::initTestCase()
132 {
133     QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
134     inputMethodPrivate->testContext = &m_platformInputContext;
135 }
136
137 void tst_qinputmethod::isVisible()
138 {
139     QCOMPARE(qApp->inputMethod()->isVisible(), false);
140     qApp->inputMethod()->show();
141     QCOMPARE(qApp->inputMethod()->isVisible(), true);
142
143     qApp->inputMethod()->hide();
144     QCOMPARE(qApp->inputMethod()->isVisible(), false);
145
146     qApp->inputMethod()->setVisible(true);
147     QCOMPARE(qApp->inputMethod()->isVisible(), true);
148
149     qApp->inputMethod()->setVisible(false);
150     QCOMPARE(qApp->inputMethod()->isVisible(), false);
151 }
152
153 void tst_qinputmethod::animating()
154 {
155     QCOMPARE(qApp->inputMethod()->isAnimating(), false);
156
157     m_platformInputContext.m_animating = true;
158     QCOMPARE(qApp->inputMethod()->isAnimating(), true);
159
160     m_platformInputContext.m_animating = false;
161     QCOMPARE(qApp->inputMethod()->isAnimating(), false);
162
163     QSignalSpy spy(qApp->inputMethod(), SIGNAL(animatingChanged()));
164     m_platformInputContext.emitAnimatingChanged();
165     QCOMPARE(spy.count(), 1);
166 }
167
168 void tst_qinputmethod::keyboarRectangle()
169 {
170     QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF());
171
172     m_platformInputContext.m_keyboardRect = QRectF(10, 20, 30, 40);
173     QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF(10, 20, 30, 40));
174
175     QSignalSpy spy(qApp->inputMethod(), SIGNAL(keyboardRectangleChanged()));
176     m_platformInputContext.emitKeyboardRectChanged();
177     QCOMPARE(spy.count(), 1);
178 }
179
180 void tst_qinputmethod::inputItemTransform()
181 {
182     QCOMPARE(qApp->inputMethod()->inputItemTransform(), QTransform());
183     QSignalSpy spy(qApp->inputMethod(), SIGNAL(cursorRectangleChanged()));
184
185     QTransform transform;
186     transform.translate(10, 10);
187     transform.scale(2, 2);
188     transform.shear(2, 2);
189     qApp->inputMethod()->setInputItemTransform(transform);
190
191     QCOMPARE(qApp->inputMethod()->inputItemTransform(), transform);
192     QCOMPARE(spy.count(), 1);
193
194     // reset
195     qApp->inputMethod()->setInputItemTransform(QTransform());
196 }
197
198 void tst_qinputmethod::cursorRectangle()
199 {
200     QCOMPARE(qApp->inputMethod()->cursorRectangle(), QRectF());
201
202     DummyWindow window;
203     window.show();
204     QVERIFY(QTest::qWaitForWindowExposed(&window));
205     window.requestActivate();
206     QTRY_COMPARE(qApp->focusWindow(), &window);
207     window.setFocusObject(&m_inputItem);
208
209     QTransform transform;
210     transform.translate(10, 10);
211     transform.scale(2, 2);
212     transform.shear(2, 2);
213     qApp->inputMethod()->setInputItemTransform(transform);
214     QCOMPARE(qApp->inputMethod()->cursorRectangle(), transform.mapRect(QRectF(1, 2, 3, 4)));
215
216     m_inputItem.cursorRectangle = QRectF(1.5, 2, 1, 8);
217     QCOMPARE(qApp->inputMethod()->cursorRectangle(), transform.mapRect(QRectF(1.5, 2, 1, 8)));
218
219     // reset
220     m_inputItem.cursorRectangle = QRectF(1, 2, 3, 4);
221     qApp->inputMethod()->setInputItemTransform(QTransform());
222 }
223
224 void tst_qinputmethod::invokeAction()
225 {
226     QCOMPARE(m_platformInputContext.m_action, QInputMethod::Click);
227     QCOMPARE(m_platformInputContext.m_cursorPosition, 0);
228
229     qApp->inputMethod()->invokeAction(QInputMethod::ContextMenu, 5);
230     QCOMPARE(m_platformInputContext.m_action, QInputMethod::ContextMenu);
231     QCOMPARE(m_platformInputContext.m_cursorPosition, 5);
232 }
233
234 void tst_qinputmethod::reset()
235 {
236     QCOMPARE(m_platformInputContext.m_resetCallCount, 0);
237
238     qApp->inputMethod()->reset();
239     QCOMPARE(m_platformInputContext.m_resetCallCount, 1);
240
241     qApp->inputMethod()->reset();
242     QCOMPARE(m_platformInputContext.m_resetCallCount, 2);
243 }
244
245 void tst_qinputmethod::commit()
246 {
247     QCOMPARE(m_platformInputContext.m_commitCallCount, 0);
248
249     qApp->inputMethod()->commit();
250     QCOMPARE(m_platformInputContext.m_commitCallCount, 1);
251
252     qApp->inputMethod()->commit();
253     QCOMPARE(m_platformInputContext.m_commitCallCount, 2);
254 }
255
256 void tst_qinputmethod::update()
257 {
258     QCOMPARE(m_platformInputContext.m_updateCallCount, 0);
259     QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImhNone));
260
261     qApp->inputMethod()->update(Qt::ImQueryInput);
262     QCOMPARE(m_platformInputContext.m_updateCallCount, 1);
263     QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImQueryInput));
264
265     qApp->inputMethod()->update(Qt::ImQueryAll);
266     QCOMPARE(m_platformInputContext.m_updateCallCount, 2);
267     QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImQueryAll));
268
269     QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF(10, 20, 30, 40));
270 }
271
272 void tst_qinputmethod::query()
273 {
274     QInputMethodQueryEvent query(Qt::InputMethodQueries(Qt::ImPreferredLanguage | Qt::ImCursorRectangle));
275     QGuiApplication::sendEvent(&m_inputItem, &query);
276
277     QString language = query.value(Qt::ImPreferredLanguage).toString();
278     QCOMPARE(language, QString("English"));
279
280     QRect cursorRectangle = query.value(Qt::ImCursorRectangle).toRect();
281     QCOMPARE(cursorRectangle, QRect(1,2,3,4));
282 }
283
284 void tst_qinputmethod::inputDirection()
285 {
286     QCOMPARE(m_platformInputContext.m_inputDirectionCallCount, 0);
287     qApp->inputMethod()->inputDirection();
288     QCOMPARE(m_platformInputContext.m_inputDirectionCallCount, 1);
289
290     QCOMPARE(m_platformInputContext.m_localeCallCount, 0);
291     qApp->inputMethod()->locale();
292     QCOMPARE(m_platformInputContext.m_localeCallCount, 1);
293 }
294
295 void tst_qinputmethod::inputMethodAccepted()
296 {
297     InputItem disabledItem;
298     disabledItem.setEnabled(false);
299
300     DummyWindow window;
301     window.show();
302     QVERIFY(QTest::qWaitForWindowExposed(&window));
303     window.requestActivate();
304     QTRY_COMPARE(qApp->focusWindow(), &window);
305     window.setFocusObject(&disabledItem);
306
307     QCOMPARE(m_platformInputContext.inputMethodAccepted(), false);
308
309     window.setFocusObject(&m_inputItem);
310     QCOMPARE(m_platformInputContext.inputMethodAccepted(), true);
311 }
312
313 QTEST_MAIN(tst_qinputmethod)
314 #include "tst_qinputmethod.moc"