1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
44 #include <private/qguiapplication_p.h>
45 #include <private/qinputmethod_p.h>
46 #include <qplatforminputcontext_qpa.h>
47 #include "../../../shared/platforminputcontext.h"
49 class InputItem : public QObject
53 InputItem() : cursorRectangle(1, 2, 3, 4), m_enabled(true) {}
55 bool event(QEvent *event)
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();
72 void setEnabled(bool enabled) {
73 if (enabled != m_enabled) {
75 qApp->inputMethod()->update(Qt::ImEnabled);
79 QRectF cursorRectangle;
80 Qt::InputMethodQueries m_lastQueries;
85 class DummyWindow : public QWindow
88 DummyWindow() : m_focusObject(0) {}
90 virtual QObject *focusObject() const
95 void setFocusObject(QObject *object)
97 m_focusObject = object;
98 emit focusObjectChanged(object);
101 QObject *m_focusObject;
105 class tst_qinputmethod : public QObject
109 tst_qinputmethod() {}
110 virtual ~tst_qinputmethod() {}
115 void keyboarRectangle();
117 void inputItemTransform();
118 void cursorRectangle();
124 void inputDirection();
126 InputItem m_inputItem;
127 PlatformInputContext m_platformInputContext;
130 void tst_qinputmethod::initTestCase()
132 QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
133 inputMethodPrivate->testContext = &m_platformInputContext;
136 void tst_qinputmethod::visible()
138 QCOMPARE(qApp->inputMethod()->visible(), false);
139 qApp->inputMethod()->show();
140 QCOMPARE(qApp->inputMethod()->visible(), true);
142 qApp->inputMethod()->hide();
143 QCOMPARE(qApp->inputMethod()->visible(), false);
145 qApp->inputMethod()->setVisible(true);
146 QCOMPARE(qApp->inputMethod()->visible(), true);
148 qApp->inputMethod()->setVisible(false);
149 QCOMPARE(qApp->inputMethod()->visible(), false);
152 void tst_qinputmethod::animating()
154 QCOMPARE(qApp->inputMethod()->isAnimating(), false);
156 m_platformInputContext.m_animating = true;
157 QCOMPARE(qApp->inputMethod()->isAnimating(), true);
159 m_platformInputContext.m_animating = false;
160 QCOMPARE(qApp->inputMethod()->isAnimating(), false);
162 QSignalSpy spy(qApp->inputMethod(), SIGNAL(animatingChanged()));
163 m_platformInputContext.emitAnimatingChanged();
164 QCOMPARE(spy.count(), 1);
167 void tst_qinputmethod::keyboarRectangle()
169 QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF());
171 m_platformInputContext.m_keyboardRect = QRectF(10, 20, 30, 40);
172 QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF(10, 20, 30, 40));
174 QSignalSpy spy(qApp->inputMethod(), SIGNAL(keyboardRectangleChanged()));
175 m_platformInputContext.emitKeyboardRectChanged();
176 QCOMPARE(spy.count(), 1);
179 void tst_qinputmethod::inputItem()
181 QVERIFY(!qApp->inputMethod()->inputItem());
182 QSignalSpy spy(qApp->inputMethod(), SIGNAL(inputItemChanged()));
184 qApp->inputMethod()->setInputItem(&m_inputItem);
186 QCOMPARE(qApp->inputMethod()->inputItem(), &m_inputItem);
187 QCOMPARE(spy.count(), 1);
190 qApp->inputMethod()->setInputItem(0);
193 void tst_qinputmethod::inputItemTransform()
195 QCOMPARE(qApp->inputMethod()->inputItemTransform(), QTransform());
196 QSignalSpy spy(qApp->inputMethod(), SIGNAL(cursorRectangleChanged()));
198 QTransform transform;
199 transform.translate(10, 10);
200 transform.scale(2, 2);
201 transform.shear(2, 2);
202 qApp->inputMethod()->setInputItemTransform(transform);
204 QCOMPARE(qApp->inputMethod()->inputItemTransform(), transform);
205 QCOMPARE(spy.count(), 1);
208 qApp->inputMethod()->setInputItemTransform(QTransform());
211 void tst_qinputmethod::cursorRectangle()
213 QCOMPARE(qApp->inputMethod()->cursorRectangle(), QRectF());
217 QTest::qWaitForWindowShown(&window);
218 window.requestActivateWindow();
219 QTRY_COMPARE(qApp->focusWindow(), &window);
220 window.setFocusObject(&m_inputItem);
222 QTransform transform;
223 transform.translate(10, 10);
224 transform.scale(2, 2);
225 transform.shear(2, 2);
226 qApp->inputMethod()->setInputItemTransform(transform);
228 QCOMPARE(qApp->inputMethod()->cursorRectangle(), transform.mapRect(QRectF(1, 2, 3, 4)));
230 m_inputItem.cursorRectangle = QRectF(1.5, 2, 1, 8);
231 QCOMPARE(qApp->inputMethod()->cursorRectangle(), transform.mapRect(QRectF(1.5, 2, 1, 8)));
234 m_inputItem.cursorRectangle = QRectF(1, 2, 3, 4);
235 qApp->inputMethod()->setInputItemTransform(QTransform());
238 void tst_qinputmethod::invokeAction()
240 QCOMPARE(m_platformInputContext.m_action, QInputMethod::Click);
241 QCOMPARE(m_platformInputContext.m_cursorPosition, 0);
243 qApp->inputMethod()->invokeAction(QInputMethod::ContextMenu, 5);
244 QCOMPARE(m_platformInputContext.m_action, QInputMethod::ContextMenu);
245 QCOMPARE(m_platformInputContext.m_cursorPosition, 5);
248 void tst_qinputmethod::reset()
250 QCOMPARE(m_platformInputContext.m_resetCallCount, 0);
252 qApp->inputMethod()->reset();
253 QCOMPARE(m_platformInputContext.m_resetCallCount, 1);
255 qApp->inputMethod()->reset();
256 QCOMPARE(m_platformInputContext.m_resetCallCount, 2);
259 void tst_qinputmethod::commit()
261 QCOMPARE(m_platformInputContext.m_commitCallCount, 0);
263 qApp->inputMethod()->commit();
264 QCOMPARE(m_platformInputContext.m_commitCallCount, 1);
266 qApp->inputMethod()->commit();
267 QCOMPARE(m_platformInputContext.m_commitCallCount, 2);
270 void tst_qinputmethod::update()
274 QTest::qWaitForWindowShown(&window);
275 window.requestActivateWindow();
276 QTRY_COMPARE(qApp->focusWindow(), &window);
277 window.setFocusObject(&m_inputItem);
279 QCOMPARE(m_platformInputContext.m_updateCallCount, 0);
280 QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImhNone));
282 qApp->inputMethod()->update(Qt::ImQueryInput);
283 QCOMPARE(m_platformInputContext.m_updateCallCount, 1);
284 QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImQueryInput));
286 qApp->inputMethod()->update(Qt::ImQueryAll);
287 QCOMPARE(m_platformInputContext.m_updateCallCount, 2);
288 QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImQueryAll));
290 QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF(10, 20, 30, 40));
293 void tst_qinputmethod::query()
295 QInputMethodQueryEvent query(Qt::InputMethodQueries(Qt::ImPreferredLanguage | Qt::ImCursorRectangle));
296 QGuiApplication::sendEvent(&m_inputItem, &query);
298 QString language = query.value(Qt::ImPreferredLanguage).toString();
299 QCOMPARE(language, QString("English"));
301 QRect cursorRectangle = query.value(Qt::ImCursorRectangle).toRect();
302 QCOMPARE(cursorRectangle, QRect(1,2,3,4));
305 void tst_qinputmethod::inputDirection()
307 QCOMPARE(m_platformInputContext.m_inputDirectionCallCount, 0);
308 qApp->inputMethod()->inputDirection();
309 QCOMPARE(m_platformInputContext.m_inputDirectionCallCount, 1);
311 QCOMPARE(m_platformInputContext.m_localeCallCount, 0);
312 qApp->inputMethod()->locale();
313 QCOMPARE(m_platformInputContext.m_localeCallCount, 1);
316 QTEST_MAIN(tst_qinputmethod)
317 #include "tst_qinputmethod.moc"