~michael-sheldon/ubuntu-keyboard/fix-oxide-dismiss-test

« back to all changes in this revision

Viewing changes to tests/repeat-backspace/main.cpp

  • Committer: Thomas Moenicke
  • Date: 2013-07-19 12:05:07 UTC
  • Revision ID: thomas.moenicke@canonical.com-20130719120507-lzw5oq50xm567x0j
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of Maliit Plugins
 
3
 *
 
4
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
 
5
 *
 
6
 * Contact: Mohammad Anwari <Mohammad.Anwari@nokia.com>
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without modification,
 
9
 * are permitted provided that the following conditions are met:
 
10
 *
 
11
 * Redistributions of source code must retain the above copyright notice, this list
 
12
 * of conditions and the following disclaimer.
 
13
 * Redistributions in binary form must reproduce the above copyright notice, this list
 
14
 * of conditions and the following disclaimer in the documentation and/or other materials
 
15
 * provided with the distribution.
 
16
 * Neither the name of Nokia Corporation nor the names of its contributors may be
 
17
 * used to endorse or promote products derived from this software without specific
 
18
 * prior written permission.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 
21
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
22
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 
23
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
27
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
28
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 *
 
30
 */
 
31
 
 
32
#include "utils.h"
 
33
#include "models/key.h"
 
34
#include "models/text.h"
 
35
#include "logic/languagefeatures.h"
 
36
#include "logic/wordengine.h"
 
37
#include "plugin/editor.h"
 
38
 
 
39
#include <inputmethodhostprobe.h>
 
40
 
 
41
#include <QtCore>
 
42
#include <QtTest>
 
43
 
 
44
using namespace MaliitKeyboard;
 
45
 
 
46
typedef void (Editor::*Method)(const Key &key);
 
47
 
 
48
Q_DECLARE_METATYPE(Method);
 
49
 
 
50
// This test suite verifies how Editor implements auto-repeat for backspace key
 
51
class TestRepeatBackspace
 
52
    : public QObject
 
53
{
 
54
    Q_OBJECT
 
55
 
 
56
private:
 
57
    QScopedPointer<Editor> editor;
 
58
    QScopedPointer<InputMethodHostProbe> host;
 
59
    EditorOptions options;
 
60
    int delay;
 
61
 
 
62
    Q_SLOT void initTestCase()
 
63
    {
 
64
        options.backspace_auto_repeat_delay = 50;
 
65
        options.backspace_auto_repeat_interval = 20;
 
66
 
 
67
        delay = qMax(options.backspace_auto_repeat_delay, options.backspace_auto_repeat_interval) + 10;
 
68
    }
 
69
 
 
70
    Q_SLOT void init()
 
71
    {
 
72
        editor.reset(new Editor(options, new Model::Text, new Logic::WordEngine, new Logic::LanguageFeatures));
 
73
        host.reset(new InputMethodHostProbe);
 
74
        editor->setHost(host.data());
 
75
    }
 
76
 
 
77
    Q_SLOT void cleanup()
 
78
    {
 
79
        editor.reset();
 
80
        host.reset();
 
81
    }
 
82
 
 
83
    /*
 
84
     * testClick verifies following scenario:
 
85
     * 1) press (or move finger into) backspace key
 
86
     * 2) release key without delay
 
87
     * At this point we should send exactly one key event
 
88
     * and auto-repeat should not be triggered
 
89
     */
 
90
    Q_SLOT void testClick_data()
 
91
    {
 
92
        QTest::addColumn<Method>("initiate");
 
93
 
 
94
        Method enter = &Editor::onKeyEntered;
 
95
        Method press = &Editor::onKeyPressed;
 
96
 
 
97
        QTest::newRow("enter") << enter;
 
98
        QTest::newRow("press") << press;
 
99
    }
 
100
 
 
101
    Q_SLOT void testClick()
 
102
    {
 
103
        QFETCH(Method, initiate);
 
104
 
 
105
        Key backspace;
 
106
        backspace.setAction(Key::ActionBackspace);
 
107
 
 
108
        (editor.data()->*initiate)(backspace);
 
109
        editor->onKeyReleased(backspace);
 
110
 
 
111
        QCOMPARE(host->keyEventCount(), 1);
 
112
        QCOMPARE(host->lastKeyEvent().type(), QEvent::KeyPress);
 
113
        QCOMPARE(host->lastKeyEvent().key(), int(Qt::Key_Backspace));
 
114
 
 
115
        QTest::qWait(delay);
 
116
 
 
117
        QCOMPARE(host->keyEventCount(), 1);
 
118
    }
 
119
 
 
120
    /*
 
121
     * testFastMove verifies following scenario:
 
122
     * 1) press (or move finger into) backspace key
 
123
     * 2) move finger to another key without delay
 
124
     * At this point we should not send key events at all.
 
125
     */
 
126
    Q_SLOT void testFastMove_data()
 
127
    {
 
128
        QTest::addColumn<Method>("initiate");
 
129
 
 
130
        Method enter = &Editor::onKeyEntered;
 
131
        Method press = &Editor::onKeyPressed;
 
132
 
 
133
        QTest::newRow("enter") << enter;
 
134
        QTest::newRow("press") << press;
 
135
    }
 
136
 
 
137
    Q_SLOT void testFastMove()
 
138
    {
 
139
        QFETCH(Method, initiate);
 
140
 
 
141
        Key backspace;
 
142
        backspace.setAction(Key::ActionBackspace);
 
143
 
 
144
        (editor.data()->*initiate)(backspace);
 
145
        editor->onKeyExited(backspace);
 
146
 
 
147
        QCOMPARE(host->keyEventCount(), 0);
 
148
 
 
149
        QTest::qWait(delay);
 
150
 
 
151
        QCOMPARE(host->keyEventCount(), 0);
 
152
    }
 
153
 
 
154
    /*
 
155
     * testRepeat verifies main scenario for auto repeat:
 
156
     * 1) press (or move finger into) backspace key
 
157
     * 2) wait a bit, so autorepeat starts
 
158
     * 3) we should send first key event now
 
159
     * 4) wait again
 
160
     * 5) auto-repeat send second event with different delay
 
161
     * 3) release (or move finger away from) backspace key
 
162
     * Auto-repeat should be stopped now and no new events will be sent
 
163
     * even after delay.
 
164
     */
 
165
    Q_SLOT void testRepeat_data()
 
166
    {
 
167
        QTest::addColumn<Method>("initiate");
 
168
        QTest::addColumn<Method>("finalize");
 
169
 
 
170
        Method enter = &Editor::onKeyEntered;
 
171
        Method exit = &Editor::onKeyExited;
 
172
        Method press = &Editor::onKeyPressed;
 
173
        Method release = &Editor::onKeyReleased;
 
174
 
 
175
        QTest::newRow("enter-exit")    << enter << exit;
 
176
        QTest::newRow("enter-release") << enter << release;
 
177
        QTest::newRow("press-release") << press << release;
 
178
        QTest::newRow("press-exit")    << press << exit;
 
179
    }
 
180
 
 
181
    Q_SLOT void testRepeat()
 
182
    {
 
183
        QFETCH(Method, initiate);
 
184
        QFETCH(Method, finalize);
 
185
 
 
186
        Key backspace;
 
187
        backspace.setAction(Key::ActionBackspace);
 
188
 
 
189
        (editor.data()->*initiate)(backspace);
 
190
 
 
191
        QCOMPARE(host->keyEventCount(), 0);
 
192
 
 
193
        TestUtils::waitForSignal(host.data(), SIGNAL(keyEventSent(QKeyEvent)));
 
194
        QCOMPARE(host->keyEventCount(), 1);
 
195
        QCOMPARE(host->lastKeyEvent().type(), QEvent::KeyPress);
 
196
        QCOMPARE(host->lastKeyEvent().key(), int(Qt::Key_Backspace));
 
197
 
 
198
        TestUtils::waitForSignal(host.data(), SIGNAL(keyEventSent(QKeyEvent)));
 
199
        QCOMPARE(host->keyEventCount(), 2);
 
200
        QCOMPARE(host->lastKeyEvent().type(), QEvent::KeyPress);
 
201
        QCOMPARE(host->lastKeyEvent().key(), int(Qt::Key_Backspace));
 
202
 
 
203
        (editor.data()->*finalize)(backspace);
 
204
 
 
205
        QTest::qWait(delay);
 
206
 
 
207
        QCOMPARE(host->keyEventCount(), 2);
 
208
    }
 
209
 
 
210
};
 
211
 
 
212
QTEST_MAIN(TestRepeatBackspace)
 
213
 
 
214
#include "main.moc"