summaryrefslogtreecommitdiffstats
path: root/tests/auto/declarative/qfxtextinput/tst_qfxtextinput.cpp
blob: 852a868d6ade6e051fe27043da80ec705fc29040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <qtest.h>
#include <QtDeclarative/qmlengine.h>
#include <QFile>
#include <QtDeclarative/qfxview.h>
#include <qfxtextinput.h>
#include <QDebug>

class tst_qfxtextinput : public QObject

{
    Q_OBJECT
public:
    tst_qfxtextinput();

private slots:
    void navigation();

private:
    void simulateKey(QFxView *, int key);
    QFxView *createView(const QString &filename);

    QmlEngine engine;
};

tst_qfxtextinput::tst_qfxtextinput()
{
}

/*
TextInput element should only handle left/right keys until the cursor reaches
the extent of the text, then they should ignore the keys.
*/
void tst_qfxtextinput::navigation()
{
    QFxView *canvas = createView(SRCDIR "/data/navigation.qml");
    canvas->execute();
    canvas->show();

    QVERIFY(canvas->root() != 0);

    QFxItem *input = qobject_cast<QFxItem *>(qvariant_cast<QObject *>(canvas->root()->property("myInput")));

    QVERIFY(input != 0);
    QVERIFY(input->hasFocus() == true);
    simulateKey(canvas, Qt::Key_Left);
    QVERIFY(input->hasFocus() == false);
    simulateKey(canvas, Qt::Key_Right);
    QVERIFY(input->hasFocus() == true);
    simulateKey(canvas, Qt::Key_Right);
    QVERIFY(input->hasFocus() == false);
    simulateKey(canvas, Qt::Key_Left);
    QVERIFY(input->hasFocus() == true);
}

void tst_qfxtextinput::simulateKey(QFxView *view, int key)
{
    QKeyEvent press(QKeyEvent::KeyPress, key, 0);
    QKeyEvent release(QKeyEvent::KeyRelease, key, 0);

    QApplication::sendEvent(view, &press);
    QApplication::sendEvent(view, &release);
}

QFxView *tst_qfxtextinput::createView(const QString &filename)
{
    QFxView *canvas = new QFxView(0);

    QFile file(filename);
    file.open(QFile::ReadOnly);
    QString xml = file.readAll();
    canvas->setQml(xml, filename);

    return canvas;
}

QTEST_MAIN(tst_qfxtextinput)

#include "tst_qfxtextinput.moc"