aboutsummaryrefslogtreecommitdiff
path: root/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.cpp
blob: 3c8d06841300c250f85af7ae76a9dc4d6c0bcadb (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "mabstractscroller.h"
#include "mdeviceprofile.h"
#include "mrelocatorstyle.h"
#include "mscrollchain.h"
#include "minputmethodstate.h"
#include "minputwidgetrelocator.h"
#include "mkeyboardstatetracker.h"
#include "ut_minputwidgetrelocator.h"

#include <QGraphicsView>

namespace {
    QRect gLastTargetRect;
    int gScrollCallCount = 0;
    QPointer<QGraphicsWidget> gDummyWidget;

    // Stubbed style values. These are what subject sees as values read from MRelocatorStyle.
    qreal gStyleVerticalAnchorPosition = 0.0;
    qreal gStyleTopNoGoMargin = 0.0;
    qreal gStyleBottomNoGoMargin = 0.0;
}

Q_DECLARE_METATYPE(Ut_MInputWidgetRelocator::Operations);
Q_DECLARE_METATYPE(Ut_MInputWidgetRelocator::Zone);


class InputWidget : public QGraphicsWidget
{
public:
    InputWidget(QGraphicsItem *parent = 0)
        :QGraphicsWidget(parent)
    {
        setFlag(QGraphicsItem::ItemIsFocusable, true);
        setFlag(QGraphicsItem::ItemAcceptsInputMethod, true);
        resize(100, 20);
    }

    virtual ~InputWidget()
    {
    }

    QRect microFocusRect() const
    {
        return rect().toRect(); // only height is important at the moment
    }

protected:
    QVariant inputMethodQuery(Qt::InputMethodQuery query) const
    {
        if (query == Qt::ImMicroFocus) {
            return microFocusRect();
        }
        return QGraphicsWidget::inputMethodQuery(query);
    }
};

void Ut_MInputWidgetRelocator::initTestCase()
{
    qRegisterMetaType<Operations>("Operations");
    qRegisterMetaType<Zone>("Zone");

    static int argc = 1;
    static char *app_name[1] = { (char *) "./ut_minputwidgetrelocator" };
    MApplication::setLoadMInputContext(false);

    app = new MApplication(argc, app_name);

    scene = new QGraphicsScene(QRectF(QPointF(), MDeviceProfile::instance()->resolution()));
    view = new QGraphicsView(scene);

    QApplication::setActiveWindow(view);
    view->show();
    view->setFocus();
    QTest::qWaitForWindowShown(view);
    QTest::qWait(1000);
    QVERIFY(view->isVisible());

    rootElement = new QGraphicsWidget;
    rootElement->setFlag(QGraphicsItem::ItemHasNoContents, true);
    scene->addItem(rootElement);

    gDummyWidget = QPointer<QGraphicsWidget>(new QGraphicsWidget);
    scene->addItem(gDummyWidget);
}

void Ut_MInputWidgetRelocator::cleanupTestCase()
{
    delete scene;
    scene = 0;
    delete view;
    view = 0;
    delete app;
    app = 0;
}

void Ut_MInputWidgetRelocator::init()
{
    subject = new MInputWidgetRelocator(scene, rootElement, M::Landscape);
}

void Ut_MInputWidgetRelocator::cleanup()
{
    delete subject;
    subject = 0;
}

void Ut_MInputWidgetRelocator::testPostponeAndUpdate_data()
{
    QTest::addColumn<Operations>("operations");
    QTest::addColumn<int>("numberOfUpdates");

    // Operations RotationEnd, HwKbChange, and SipChange always sets an update pending.

    QTest::newRow("begin rotation") << (Operations() << RotationBegin) << 0;
    QTest::newRow("end rotation") << (Operations() << RotationEnd) << 1;
    QTest::newRow("begin scene window animation") << (Operations() << SceneWindowAppearing) << 0;
    QTest::newRow("begin and end scene window animation")
            << (Operations() << SceneWindowAppearing << SceneWindowAppeared) << 0;

    QTest::newRow("rotation during animation, animation not finished")
            << (Operations() << SceneWindowAppearing << RotationBegin << RotationEnd) << 0;
    QTest::newRow("rotation during animation, animation finished")
            << (Operations() << SceneWindowAppearing
                             << RotationBegin << RotationEnd
                             << SceneWindowAppeared) << 1;

    QTest::newRow("hwkb changed state") << (Operations() << HwKbChange) << 1;
    QTest::newRow("hwkb changed state but animating") << (Operations() << SceneWindowAppearing << HwKbChange) << 0;
    QTest::newRow("hwkb changed state but rotating") << (Operations() << RotationBegin << HwKbChange) << 0;

    QTest::newRow("sip changed") << (Operations() << SipChange) << 1;
    QTest::newRow("sip changed but animating") << (Operations() << SceneWindowAppearing << SipChange) << 0;
    QTest::newRow("sip changed but rotating") << (Operations() << RotationBegin << SipChange) << 0;

    QTest::newRow("sip changed, rotation finished but animation didn't")
            << (Operations() << RotationBegin << SceneWindowAppearing
                             << SipChange << RotationEnd) << 0;
    QTest::newRow("sip changed, rotation and animation finished")
            << (Operations() << RotationBegin << SceneWindowAppearing
                             << SipChange << RotationEnd << SceneWindowAppeared) << 1;
}

void Ut_MInputWidgetRelocator::testPostponeAndUpdate()
{
    QFETCH(Operations, operations);
    QFETCH(int, numberOfUpdates);

    const QRect sipRect(QPoint(0, 300), scene->sceneRect().size().toSize());
    MInputMethodState::instance()->setInputMethodArea(sipRect);

    // Only create input widget to be able to follow MScrollChain::run().
    InputWidget widget(rootElement);
    moveWidgetToZone(&widget, UpperNoGoZone, scene->sceneRect().toRect(), M::Landscape);
    widget.setPos(-100, -100); // Position somewhere beyond nogo zones so relocation takes place.
    widget.setFocus();

    gScrollCallCount = 0;

    foreach (Operation op, operations) {
        switch (op) {
        case RotationBegin:
            subject->handleRotationBegin();
            break;
        case RotationEnd:
            subject->handleRotationFinished(M::Landscape);
            break;
        case SceneWindowAppearing:
            subject->sceneWindowStateHasChanged(0, MSceneWindow::Appearing, MSceneWindow::Disappeared);
            break;
        case SceneWindowAppeared:
            subject->sceneWindowStateHasChanged(0, MSceneWindow::Appeared, MSceneWindow::Appearing);
            break;
        case SipChange:
            // Emit protected signal
            QVERIFY(QMetaObject::invokeMethod(MInputMethodState::instance(),
                                              "inputMethodAreaChanged",
                                              Qt::DirectConnection,
                                              Q_ARG(QRect, sipRect)));
            break;
        case HwKbChange:
            // Emit protected signal
            QVERIFY(QMetaObject::invokeMethod(MKeyboardStateTracker::instance(),
                                              "stateChanged",
                                              Qt::DirectConnection));
            break;
        }
    }

    QCOMPARE(gScrollCallCount, numberOfUpdates);
}

void Ut_MInputWidgetRelocator::testTargetPosition_data()
{
    QTest::addColumn<M::Orientation>("orientation");
    QTest::addColumn<QRect>("sipRect");
    QTest::addColumn<Zone>("initialZone");

    // Sip rectangle is given in scene coordinates.
    const QSize screenSize(MDeviceProfile::instance()->resolution());
    const QRect sipRect(0, screenSize.height() / 2,
                        screenSize.height() - (screenSize.height() / 2),
                        screenSize.width());

    QTest::newRow("no sip, widget at upper nogo zone")   << M::Landscape << QRect() << UpperNoGoZone;
    QTest::newRow("no sip, widget already visible")    << M::Landscape << QRect() << AllowedZone;
    QTest::newRow("no sip, widget at lower nogo zone")   << M::Landscape << QRect() << LowerNoGoZone;
    QTest::newRow("with sip, widget at upper nogo zone") << M::Landscape << sipRect << UpperNoGoZone;
    QTest::newRow("with sip, widget already visible")  << M::Landscape << sipRect << AllowedZone;
    QTest::newRow("with sip, widget at lower nogo zone") << M::Landscape << sipRect << LowerNoGoZone;

    QTest::newRow("PT, no sip, widget at upper nogo zone")   << M::Portrait << QRect() << UpperNoGoZone;
    QTest::newRow("PT, no sip, widget already visible")    << M::Portrait << QRect() << AllowedZone;
    QTest::newRow("PT, no sip, widget at lower nogo zone")   << M::Portrait << QRect() << LowerNoGoZone;
    QTest::newRow("PT, with sip, widget at upper nogo zone") << M::Portrait << sipRect << UpperNoGoZone;
    QTest::newRow("PT, with sip, widget already visible")  << M::Portrait << sipRect << AllowedZone;
    QTest::newRow("PT, with sip, widget at lower nogo zone") << M::Portrait << sipRect << LowerNoGoZone;
}

void Ut_MInputWidgetRelocator::testTargetPosition()
{
    QFETCH(M::Orientation, orientation);
    QFETCH(QRect, sipRect);
    QFETCH(Zone, initialZone);

    // Apply orientation
    subject->handleRotationBegin();
    subject->handleRotationFinished(orientation);

    updateStubbedStyleValues(orientation);

    MInputMethodState::instance()->setInputMethodArea(sipRect);
    QRect exposedContentRect(rootElement->mapRectFromScene(scene->sceneRect()).toRect());
    if (!sipRect.isNull()) {
        sipRect = rootElement->mapRectFromScene(sipRect).toRect();
        // Assume sip comes from bottom of screen.
        exposedContentRect.setBottom(sipRect.top());
    }

    InputWidget widget(rootElement);
    moveWidgetToZone(&widget, initialZone, exposedContentRect, orientation);
    widget.setFocus();

    // Set target rect of stub first as invalid.
    const QRect invalidRect(-10, -10, -10, -10);
    gLastTargetRect = invalidRect;

    subject->update();

    // Test two cases here:
    // 1) If widget was initially beyond nogo zones the new position should be at anchor point.
    // 2) If widget was already in allowed zone it should remain where it was.

    switch (initialZone) {
    case UpperNoGoZone:
    case LowerNoGoZone:
        QCOMPARE(static_cast<qreal>(gLastTargetRect.y()), verticalAnchorPosition(orientation));
        QVERIFY(allowedZone(exposedContentRect, orientation).contains(gLastTargetRect));
        break;
    case AllowedZone:
        // Already in allowed zone. Shouldn't have moved.
        QCOMPARE(gLastTargetRect, invalidRect);
        break;
    }
}

QRect Ut_MInputWidgetRelocator::allowedZone(const QRect &exposedContentRect, M::Orientation orientation) const
{
    return exposedContentRect.adjusted(0, topNoGoMargin(orientation),
                                       0, bottomNoGoMargin(orientation));
}

// Following values have been copied from real base theme style
// but they don't need to be precisely the same.
qreal Ut_MInputWidgetRelocator::verticalAnchorPosition(M::Orientation orientation) const
{
    if (orientation == M::Landscape) {
        // Could return different value when hardware keyboard open (150.0).
        return 40.0;
    } else {
        return 200;
    }
}

qreal Ut_MInputWidgetRelocator::topNoGoMargin(M::Orientation orientation) const
{
    if (orientation == M::Landscape) {
        return 40.0;
    } else {
        return 48.0;
    }
}

qreal Ut_MInputWidgetRelocator::bottomNoGoMargin(M::Orientation orientation) const
{
    if (orientation == M::Landscape) {
        return 40.0;
    } else {
        return 48.0;
    }
}

void Ut_MInputWidgetRelocator::moveWidgetToZone(InputWidget *widget,
                                                Ut_MInputWidgetRelocator::Zone zone,
                                                const QRect &exposedContentRect,
                                                M::Orientation orientation) const
{
    const QRect microRect(widget->microFocusRect());
    const QRect allowedRect(allowedZone(exposedContentRect, orientation));

    switch (zone) {
    case UpperNoGoZone:
        widget->setPos(widget->pos().x(), allowedRect.top() - microRect.bottom());
        break;
    case LowerNoGoZone:
        widget->setPos(widget->pos().x(), allowedRect.bottom() - microRect.top());
        break;
    case AllowedZone:
        widget->setPos(widget->pos().x(), allowedRect.center().y());
        break;
    }
}

void Ut_MInputWidgetRelocator::updateStubbedStyleValues(M::Orientation orientation) const
{
    gStyleVerticalAnchorPosition = verticalAnchorPosition(orientation);
    gStyleTopNoGoMargin = topNoGoMargin(orientation);
    gStyleBottomNoGoMargin = bottomNoGoMargin(orientation);
}

// Stubbing MScrollChain

MScrollChain::MScrollChain(const QGraphicsItem *, const QGraphicsItem *)
    : rootItem(0), scrollTarget(0)
{
}

MScrollChain::~MScrollChain()
{
}

int MScrollChain::count() const
{
    return 1;
}

const QGraphicsWidget *MScrollChain::widgetAt(int index) const
{
    Q_UNUSED(index);
    return gDummyWidget;
}

void MScrollChain::restore(const MScrollChain *excludeChain)
{
    Q_UNUSED(excludeChain);
}

void MScrollChain::addBottomUpScroll(const QRect &targetRect, const QPoint &originPoint, int index)
{
    gLastTargetRect = targetRect;
    Q_UNUSED(originPoint);
    Q_UNUSED(index);
}

void MScrollChain::addMinimalScroll(const QRect &localRect,
                                    int startingIndex,
                                    int untilIndex)
{
    Q_UNUSED(localRect);
    Q_UNUSED(startingIndex);
    Q_UNUSED(untilIndex);
}

void MScrollChain::applyScrolling()
{
    ++gScrollCallCount;
}

QTEST_APPLESS_MAIN(Ut_MInputWidgetRelocator)

// Stubbing MRelocatorStyle which is defined in stub mrelocatorstyle.h in current test directory.
qreal MRelocatorStyle::verticalAnchorPosition() const {return gStyleVerticalAnchorPosition;}
qreal MRelocatorStyle::topNoGoMargin() const {return gStyleTopNoGoMargin;}
qreal MRelocatorStyle::bottomNoGoMargin() const {return gStyleBottomNoGoMargin;}