1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
44 #include <private/qguiapplication_p.h>
45 #include <private/qinputmethod_p.h>
46 #include <qpa/qplatforminputcontext.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();
116 void inputItemTransform();
117 void cursorRectangle();
123 void inputDirection();
124 void inputMethodAccepted();
127 InputItem m_inputItem;
128 PlatformInputContext m_platformInputContext;
131 void tst_qinputmethod::initTestCase()
133 QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
134 inputMethodPrivate->testContext = &m_platformInputContext;
137 void tst_qinputmethod::isVisible()
139 QCOMPARE(qApp->inputMethod()->isVisible(), false);
140 qApp->inputMethod()->show();
141 QCOMPARE(qApp->inputMethod()->isVisible(), true);
143 qApp->inputMethod()->hide();
144 QCOMPARE(qApp->inputMethod()->isVisible(), false);
146 qApp->inputMethod()->setVisible(true);
147 QCOMPARE(qApp->inputMethod()->isVisible(), true);
149 qApp->inputMethod()->setVisible(false);
150 QCOMPARE(qApp->inputMethod()->isVisible(), false);
153 void tst_qinputmethod::animating()
155 QCOMPARE(qApp->inputMethod()->isAnimating(), false);
157 m_platformInputContext.m_animating = true;
158 QCOMPARE(qApp->inputMethod()->isAnimating(), true);
160 m_platformInputContext.m_animating = false;
161 QCOMPARE(qApp->inputMethod()->isAnimating(), false);
163 QSignalSpy spy(qApp->inputMethod(), SIGNAL(animatingChanged()));
164 m_platformInputContext.emitAnimatingChanged();
165 QCOMPARE(spy.count(), 1);
168 void tst_qinputmethod::keyboarRectangle()
170 QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF());
172 m_platformInputContext.m_keyboardRect = QRectF(10, 20, 30, 40);
173 QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF(10, 20, 30, 40));
175 QSignalSpy spy(qApp->inputMethod(), SIGNAL(keyboardRectangleChanged()));
176 m_platformInputContext.emitKeyboardRectChanged();
177 QCOMPARE(spy.count(), 1);
180 void tst_qinputmethod::inputItemTransform()
182 QCOMPARE(qApp->inputMethod()->inputItemTransform(), QTransform());
183 QSignalSpy spy(qApp->inputMethod(), SIGNAL(cursorRectangleChanged()));
185 QTransform transform;
186 transform.translate(10, 10);
187 transform.scale(2, 2);
188 transform.shear(2, 2);
189 qApp->inputMethod()->setInputItemTransform(transform);
191 QCOMPARE(qApp->inputMethod()->inputItemTransform(), transform);
192 QCOMPARE(spy.count(), 1);
195 qApp->inputMethod()->setInputItemTransform(QTransform());
198 void tst_qinputmethod::cursorRectangle()
200 QCOMPARE(qApp->inputMethod()->cursorRectangle(), QRectF());
204 QVERIFY(QTest::qWaitForWindowExposed(&window));
205 window.requestActivate();
206 QTRY_COMPARE(qApp->focusWindow(), &window);
207 window.setFocusObject(&m_inputItem);
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)));
216 m_inputItem.cursorRectangle = QRectF(1.5, 2, 1, 8);
217 QCOMPARE(qApp->inputMethod()->cursorRectangle(), transform.mapRect(QRectF(1.5, 2, 1, 8)));
220 m_inputItem.cursorRectangle = QRectF(1, 2, 3, 4);
221 qApp->inputMethod()->setInputItemTransform(QTransform());
224 void tst_qinputmethod::invokeAction()
226 QCOMPARE(m_platformInputContext.m_action, QInputMethod::Click);
227 QCOMPARE(m_platformInputContext.m_cursorPosition, 0);
229 qApp->inputMethod()->invokeAction(QInputMethod::ContextMenu, 5);
230 QCOMPARE(m_platformInputContext.m_action, QInputMethod::ContextMenu);
231 QCOMPARE(m_platformInputContext.m_cursorPosition, 5);
234 void tst_qinputmethod::reset()
236 QCOMPARE(m_platformInputContext.m_resetCallCount, 0);
238 qApp->inputMethod()->reset();
239 QCOMPARE(m_platformInputContext.m_resetCallCount, 1);
241 qApp->inputMethod()->reset();
242 QCOMPARE(m_platformInputContext.m_resetCallCount, 2);
245 void tst_qinputmethod::commit()
247 QCOMPARE(m_platformInputContext.m_commitCallCount, 0);
249 qApp->inputMethod()->commit();
250 QCOMPARE(m_platformInputContext.m_commitCallCount, 1);
252 qApp->inputMethod()->commit();
253 QCOMPARE(m_platformInputContext.m_commitCallCount, 2);
256 void tst_qinputmethod::update()
258 QCOMPARE(m_platformInputContext.m_updateCallCount, 0);
259 QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImhNone));
261 qApp->inputMethod()->update(Qt::ImQueryInput);
262 QCOMPARE(m_platformInputContext.m_updateCallCount, 1);
263 QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImQueryInput));
265 qApp->inputMethod()->update(Qt::ImQueryAll);
266 QCOMPARE(m_platformInputContext.m_updateCallCount, 2);
267 QCOMPARE(int(m_platformInputContext.m_lastQueries), int(Qt::ImQueryAll));
269 QCOMPARE(qApp->inputMethod()->keyboardRectangle(), QRectF(10, 20, 30, 40));
272 void tst_qinputmethod::query()
274 QInputMethodQueryEvent query(Qt::InputMethodQueries(Qt::ImPreferredLanguage | Qt::ImCursorRectangle));
275 QGuiApplication::sendEvent(&m_inputItem, &query);
277 QString language = query.value(Qt::ImPreferredLanguage).toString();
278 QCOMPARE(language, QString("English"));
280 QRect cursorRectangle = query.value(Qt::ImCursorRectangle).toRect();
281 QCOMPARE(cursorRectangle, QRect(1,2,3,4));
284 void tst_qinputmethod::inputDirection()
286 QCOMPARE(m_platformInputContext.m_inputDirectionCallCount, 0);
287 qApp->inputMethod()->inputDirection();
288 QCOMPARE(m_platformInputContext.m_inputDirectionCallCount, 1);
290 QCOMPARE(m_platformInputContext.m_localeCallCount, 0);
291 qApp->inputMethod()->locale();
292 QCOMPARE(m_platformInputContext.m_localeCallCount, 1);
295 void tst_qinputmethod::inputMethodAccepted()
297 InputItem disabledItem;
298 disabledItem.setEnabled(false);
302 QVERIFY(QTest::qWaitForWindowExposed(&window));
303 window.requestActivate();
304 QTRY_COMPARE(qApp->focusWindow(), &window);
305 window.setFocusObject(&disabledItem);
307 QCOMPARE(m_platformInputContext.inputMethodAccepted(), false);
309 window.setFocusObject(&m_inputItem);
310 QCOMPARE(m_platformInputContext.inputMethodAccepted(), true);
313 QTEST_MAIN(tst_qinputmethod)
314 #include "tst_qinputmethod.moc"