1
/* * This file is part of Maliit framework *
3
* Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
* Contact: maliit-discuss@lists.maliit.org
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License version 2.1 as published by the Free Software Foundation
11
* and appearing in the file LICENSE.LGPL included in the packaging
15
#include <QtCore/QString>
16
#include <QtTest/QtTest>
17
#include <QApplication>
18
#include <QScopedPointer>
20
#include <maliit/inputmethod.h>
22
#include "gui-utils.h"
24
using Maliit::InputMethod;
25
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
26
using MaliitTestUtils::EventSpyInputContext;
30
const QRect DefaultArea;
31
const QRect TestArea(QPoint(100, 100), QSize(100, 100));
33
const Maliit::OrientationAngle DefaultAngle(Maliit::Angle0);
35
void resetInputMethodToDefault() {
36
InputMethod::instance()->setArea(DefaultArea);
37
InputMethod::instance()->setOrientationAngle(DefaultAngle);
41
class Ut_Maliit_InputMethod : public QObject
47
void cleanupTestCase();
48
void testDefaultArea();
49
void testDefaultOrientationAngle();
51
void testSetArea_data();
52
void testStartOrientationAngleChange();
53
void testSetOrientationAngle();
54
void testSetOrientationAngleSignal();
55
void testSetOrientationAngleSignal_data();
56
void testEmitKeyPress();
57
void testEmitKeyRelease();
58
void testRequestInputMethodPanel();
59
void testCloseInputMethodPanel();
62
void Ut_Maliit_InputMethod::initTestCase()
66
void Ut_Maliit_InputMethod::cleanupTestCase()
70
void Ut_Maliit_InputMethod::testDefaultArea()
72
QCOMPARE(InputMethod::instance()->area(), DefaultArea);
75
void Ut_Maliit_InputMethod::testDefaultOrientationAngle()
77
QCOMPARE(InputMethod::instance()->orientationAngle(), DefaultAngle);
80
void Ut_Maliit_InputMethod::testSetArea()
82
resetInputMethodToDefault();
84
QFETCH(QRect, expectedArea);
85
QFETCH(int, areaChangedCount);
87
QSignalSpy areaChanged(InputMethod::instance(), SIGNAL(areaChanged(QRect)));
88
InputMethod::instance()->setArea(area);
90
QCOMPARE(InputMethod::instance()->area(), expectedArea);
91
QCOMPARE(areaChanged.size(), areaChangedCount);
92
if (areaChanged.size() > 0)
93
QCOMPARE(areaChanged.first().at(0).toRect(), expectedArea);
96
void Ut_Maliit_InputMethod::testSetArea_data()
98
QTest::addColumn<QRect>("area");
99
QTest::addColumn<QRect>("expectedArea");
100
QTest::addColumn<int>("areaChangedCount");
102
QTest::newRow("change to different area") << TestArea << TestArea << 1;
103
QTest::newRow("change to same area (should not Q_EMIT a signal)") << DefaultArea << DefaultArea << 0;
106
void Ut_Maliit_InputMethod::testStartOrientationAngleChange()
108
resetInputMethodToDefault();
109
InputMethod::instance()->startOrientationAngleChange(Maliit::Angle180);
111
QCOMPARE(InputMethod::instance()->orientationAngle(), Maliit::Angle180);
114
void Ut_Maliit_InputMethod::testSetOrientationAngle()
116
resetInputMethodToDefault();
117
InputMethod::instance()->setOrientationAngle(Maliit::Angle180);
119
QCOMPARE(InputMethod::instance()->orientationAngle(), Maliit::Angle180);
122
void Ut_Maliit_InputMethod::testSetOrientationAngleSignal()
124
resetInputMethodToDefault();
125
QFETCH(Maliit::OrientationAngle, startAngle);
126
QFETCH(Maliit::OrientationAngle, setAngle);
127
QFETCH(Maliit::OrientationAngle, expectedAngle);
128
QFETCH(int, aboutToChangeCount);
129
QFETCH(int, changedCount);
131
QSignalSpy aboutToChange(InputMethod::instance(), SIGNAL(orientationAngleAboutToChange(Maliit::OrientationAngle)));
132
QSignalSpy changed(InputMethod::instance(), SIGNAL(orientationAngleChanged(Maliit::OrientationAngle)));
134
InputMethod::instance()->startOrientationAngleChange(startAngle);
135
InputMethod::instance()->setOrientationAngle(setAngle);
137
QCOMPARE(InputMethod::instance()->orientationAngle(), expectedAngle);
138
QCOMPARE(aboutToChange.size(), aboutToChangeCount);
139
QCOMPARE(changed.size(), changedCount);
142
void Ut_Maliit_InputMethod::testSetOrientationAngleSignal_data()
144
QTest::addColumn<Maliit::OrientationAngle>("startAngle");
145
QTest::addColumn<Maliit::OrientationAngle>("setAngle");
146
QTest::addColumn<Maliit::OrientationAngle>("expectedAngle");
147
QTest::addColumn<int>("aboutToChangeCount");
148
QTest::addColumn<int>("changedCount");
150
QTest::newRow("startAngle and setAngle to the same angle") << Maliit::Angle90 << Maliit::Angle90 << Maliit::Angle90 << 1 << 1;
151
QTest::newRow("just setAngle to an angle") << DefaultAngle << Maliit::Angle90 << Maliit::Angle90 << 0 << 1;
152
QTest::newRow("startAngle and setAngle to different angles") << Maliit::Angle90 << Maliit::Angle180 << Maliit::Angle180 << 1 << 1;
153
QTest::newRow("startAngle and setAngle to the default angle") << DefaultAngle << DefaultAngle << DefaultAngle << 0 << 0;
156
void Ut_Maliit_InputMethod::testEmitKeyPress()
158
const QKeyEvent eventA(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier, QString::fromLatin1("a"));
159
const QKeyEvent eventZ(QEvent::KeyPress, Qt::Key_Z, Qt::ShiftModifier, QString::fromLatin1("Z"));
160
QSignalSpy keyPress(InputMethod::instance(), SIGNAL(keyPress(QKeyEvent)));
162
InputMethod::instance()->emitKeyPress(eventA);
163
InputMethod::instance()->emitKeyPress(eventZ);
165
QCOMPARE(keyPress.size(), 2);
168
void Ut_Maliit_InputMethod::testEmitKeyRelease()
170
const QKeyEvent eventA(QEvent::KeyRelease, Qt::Key_A, Qt::NoModifier, QString::fromLatin1("a"));
171
const QKeyEvent eventZ(QEvent::KeyRelease, Qt::Key_Z, Qt::ShiftModifier, QString::fromLatin1("Z"));
172
QSignalSpy keyRelease(InputMethod::instance(), SIGNAL(keyRelease(QKeyEvent)));
174
InputMethod::instance()->emitKeyRelease(eventA);
175
InputMethod::instance()->emitKeyRelease(eventZ);
177
QCOMPARE(keyRelease.size(), 2);
180
void Ut_Maliit_InputMethod::testRequestInputMethodPanel()
182
// API not available on Qt 5 anymore
183
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
184
EventSpyInputContext<QEvent::Type> *spy = new EventSpyInputContext<QEvent::Type>(std::mem_fun(&QEvent::type));
185
qApp->setInputContext(spy);
187
Maliit::requestInputMethodPanel();
189
QVERIFY(spy->size() == 1);
190
QCOMPARE(spy->at(0), QEvent::RequestSoftwareInputPanel);
194
void Ut_Maliit_InputMethod::testCloseInputMethodPanel()
196
// API not available on Qt 5 anymore
197
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
199
EventSpyInputContext<QEvent::Type> *spy = new EventSpyInputContext<QEvent::Type>(std::mem_fun(&QEvent::type));
200
qApp->setInputContext(spy);
202
Maliit::closeInputMethodPanel();
204
QVERIFY(spy->size() == 1);
205
QCOMPARE(spy->at(0), QEvent::CloseSoftwareInputPanel);
209
QTEST_MAIN(Ut_Maliit_InputMethod)
211
#include "ut_maliit_inputmethod.moc"