aboutsummaryrefslogtreecommitdiff
path: root/tests/ut_mcombobox/ut_mcombobox.cpp
blob: dd779da75c6611acfb80b992e3aec3510a2d632f (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/***************************************************************************
**
** 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 <QTest>
#include <QSignalSpy>
#include <QList>
#include <QObject>
#include <QGraphicsSceneMouseEvent>
#include <QModelIndex>
#include <MApplication>
#include <QStandardItemModel>
#include <QVariant>
#include <QGraphicsLinearLayout>
#include <MImageWidget>
#include <MLabel>
#include <MWindow>
#include <MScene>
#include <MSceneManager>
#include <MPopupList>
#include <MLocale>

#include "ut_mcombobox.h"
#include "mcancelevent.h"
#include "mcomboboxview.h"
#include "mcomboboxview_p.h"
#include "mlayout.h"
#include "mgridlayoutpolicy.h"

MApplication *app;
MWindow *win;

void Ut_MComboBox::initTestCase()
{
    static int argc = 1;
    static char *argv[1] = { (char *) "./ut_mcombobox" };
    app = new MApplication(argc, argv);

    // make sure that we only have engineering English loaded:
    MLocale locale;
    locale.removeTrCatalog("libmeegotouch"); // removes libmeegotouch.qm and libmeegotouch_<localeName>.qm
    locale.removeTrCatalog("common"); // removes common.qm and common_<localeName>.qm
    locale.installTrCatalog("libmeegotouch.qm"); // installs libmeegotouch.qm (engineering English)
    MLocale::setDefault(locale);

    win = new MWindow();
    win->setSceneManager(new MSceneManager());
}

void Ut_MComboBox::cleanupTestCase()
{
    delete win;
    win = NULL;
    delete app;
    app = NULL;
}

void Ut_MComboBox::init()
{
    m_combobox = new MComboBox();
}

void Ut_MComboBox::cleanup()
{
    if (m_combobox)
        delete m_combobox;
    m_combobox = NULL;
}

void Ut_MComboBox::testItemModel()
{
    QStringList buffer;
    int total = 10;
    for (int i = 0; i < total; i++)
        buffer << "Item" + QString::number(i);

    // test StandardItemModel
    m_combobox->addItems(buffer);

    QAbstractItemModel *itemModel = m_combobox->itemModel();
    QStandardItemModel *smodel = qobject_cast<QStandardItemModel *>(itemModel);
    QVERIFY(smodel != NULL);

    // test use model from outside
    QStringListModel model;
    model.setStringList(buffer);

    m_combobox->setItemModel(&model);
    QCOMPARE(&model, m_combobox->itemModel());
    QCOMPARE(m_combobox->count(), total);
}

void Ut_MComboBox::testActions()
{
    QStringList buffer;
    int total = 10;
    for (int i = 0; i < total; i++)
        buffer << "Item" + QString::number(i);

    // test add items
    m_combobox->addItems(buffer);
    QCOMPARE(m_combobox->count(), 10);

    QAbstractItemModel *itemModel = m_combobox->itemModel();
    QCOMPARE(itemModel->rowCount(), total);

    m_combobox->clear();
    QCOMPARE(m_combobox->count(), 0);

    m_combobox->addItem("Just a test");
    QCOMPARE(m_combobox->count(), 1);

    m_combobox->addItem("Icon", "Just a test");
    QCOMPARE(m_combobox->count(), 2);

    // test remove item
    m_combobox->removeItem(0);
    QCOMPARE(m_combobox->count(), 1);

    m_combobox->removeItem(0);
    QCOMPARE(m_combobox->count(), 0);

    // test insert item
    m_combobox->insertItem(0, "Just a test");
    QCOMPARE(m_combobox->count(), 1);

    m_combobox->insertItem(0, "Icon", "Just a test");
    QCOMPARE(m_combobox->count(), 2);

    m_combobox->clear();
    m_combobox->insertItems(0, buffer);
    QCOMPARE(m_combobox->count(), total);
}

void Ut_MComboBox::testCurrentIndex()
{
    QStringList buffer;
    int total = 10;
    for (int i = 0; i < total; i++)
        buffer << "Item" + QString::number(i);

    m_combobox->addItems(buffer);

    QSignalSpy spy1(m_combobox, SIGNAL(currentIndexChanged(int)));
    QSignalSpy spy2(m_combobox, SIGNAL(currentIndexChanged(QString)));

    m_combobox->setCurrentIndex(0);
    QCOMPARE(m_combobox->currentIndex(), 0);
    QCOMPARE(m_combobox->currentText(), QString("Item0"));

    QCOMPARE(spy1.count(), 1);
    QCOMPARE(spy2.count(), 1);

    QList<QVariant> arguments1 = spy1.takeFirst();
    QVERIFY(arguments1.at(0).type() == QVariant::Int);
    QVERIFY(arguments1.at(0).toInt() == 0);

    QList<QVariant> arguments2 = spy2.takeFirst();
    QVERIFY(arguments2.at(0).type() == QVariant::String);
    QCOMPARE(arguments2.at(0).toString(), QString("Item0"));
}

void Ut_MComboBox::testFunctions()
{
    QString value = "test";
    m_combobox->setIconID(value);
    QCOMPARE(m_combobox->iconID(), value);

    m_combobox->setTitle(value);
    QCOMPARE(m_combobox->title(), value);

    m_combobox->setIconVisible(false);
    QCOMPARE(m_combobox->isIconVisible(), false);

    m_combobox->setIconVisible(true);
    QCOMPARE(m_combobox->isIconVisible(), true);

    m_combobox->insertItem(0, "Just a test");
    m_combobox->setItemText(0, value);
    QCOMPARE(m_combobox->itemText(0), value);

    m_combobox->setItemIconID(0, value);
    QCOMPARE(m_combobox->itemIconID(0), value);
}

void Ut_MComboBox::testIconVisibility()
{
    MComboBoxView *view = (MComboBoxView *)m_combobox->view();
    MComboBoxViewPrivate *viewPrivate = view->d_func();

    m_combobox->setTitle("Title");
    view->updateData(QList<const char *>() << MComboBoxModel::Title);
    QCOMPARE(viewPrivate->button->layoutGrid->policy(), viewPrivate->button->_basicSubtitlePolicy);
    QCOMPARE(viewPrivate->button->titleWidget()->text(), QString("Title"));

    m_combobox->setIconID("Icon-music");
    view->updateData(QList<const char *>() << MComboBoxModel::IconID);
    QCOMPARE(viewPrivate->button->layoutGrid->policy(), viewPrivate->button->_iconSubtitlePolicy);
    QCOMPARE(viewPrivate->button->iconWidget()->image(), QString("Icon-music"));

    m_combobox->setIconVisible(false);
    view->updateData(QList<const char *>() << MComboBoxModel::IconVisible);
    QCOMPARE(viewPrivate->button->layoutGrid->policy(), viewPrivate->button->_basicSubtitlePolicy);
    QCOMPARE(viewPrivate->button->titleWidget()->text(), QString("Title"));
    QCOMPARE(viewPrivate->button->iconWidget()->image(), QString("Icon-music"));

    m_combobox->setIconVisible(true);
    view->updateData(QList<const char *>() << MComboBoxModel::IconVisible);
    QCOMPARE(viewPrivate->button->layoutGrid->policy(), viewPrivate->button->_iconSubtitlePolicy);
    QCOMPARE(viewPrivate->button->iconWidget()->image(), QString("Icon-music"));
}


void Ut_MComboBox::testClickSlot()
{
    win->scene()->addItem(m_combobox);

    MComboBoxView *view = (MComboBoxView *)m_combobox->view();
    MComboBoxViewPrivate *viewPrivate = view->d_func();

    m_combobox->addItem("Item1");
    m_combobox->addItem("Item2");

    m_combobox->click();

    QVERIFY(viewPrivate->popuplist != 0);
    QVERIFY(viewPrivate->popuplist->isVisible());

    win->scene()->removeItem(m_combobox);
}

void Ut_MComboBox::testBuiltinModel()
{
    MComboBoxView *view = (MComboBoxView *)m_combobox->view();
    MComboBoxViewPrivate *viewPrivate = view->d_func();
    QSignalSpy spy(m_combobox, SIGNAL(currentIndexChanged(int)));

    m_combobox->addItems(QStringList() << "item0" << "item1" << "item2" << "item3");

    // select item2
    m_combobox->setCurrentIndex(2);
    QCOMPARE(m_combobox->currentIndex(), 2);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), QString("item2"));
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.takeLast()[0], QVariant(2));
    // remove item1, index should change
    m_combobox->removeItem(1);
    QCOMPARE(m_combobox->currentIndex(), 1);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), QString("item2"));
    // remove current item
    m_combobox->removeItem(1);
    QCOMPARE(m_combobox->currentIndex(), -1);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), qtTrId("xx_ComboBoxSubtitle"));
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.takeLast()[0], QVariant(-1));
    // check that after removing items everything looks ok
    QCOMPARE(m_combobox->count(), 2);
    m_combobox->setCurrentIndex(1);
    QCOMPARE(m_combobox->currentIndex(), 1);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), QString("item3"));
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.takeLast()[0], QVariant(1));
    // add one item
    m_combobox->insertItem(0, QString("itemX"));
    QCOMPARE(m_combobox->currentIndex(), 2);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), QString("item3"));
    QCOMPARE(spy.count(), 0);

    // change current item text
    m_combobox->setItemText(2, QString("beef"));
    QCOMPARE(m_combobox->currentIndex(), 2);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), QString("beef"));
}

void Ut_MComboBox::testModelSwitching()
{
    MComboBoxView *view = (MComboBoxView *)m_combobox->view();
    MComboBoxViewPrivate *viewPrivate = view->d_func();
    QSignalSpy spy(m_combobox, SIGNAL(currentIndexChanged(int)));

    m_combobox->addItems(QStringList() << "item0" << "item1" << "item2" << "item3");

    m_combobox->setCurrentIndex(2);
    QCOMPARE(m_combobox->currentIndex(), 2);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), QString("item2"));
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.takeLast()[0], QVariant(2));

    QStringListModel *strListModel = new QStringListModel();
    strListModel->setStringList(QStringList() << "foo0" << "foo1" << "foo2" << "foo3");
    m_combobox->setItemModel(strListModel);

    // model changed selection should reset
    QCOMPARE(m_combobox->currentIndex(), -1);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), qtTrId("xx_ComboBoxSubtitle"));
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.takeLast()[0], QVariant(-1));

    // currentIndexChanged signals should not be emitted
    m_combobox->insertItems(0, QStringList() << "bar1" << "bar2" << "bar3");
    QCOMPARE(spy.count(), 0);
}

void Ut_MComboBox::testStringListModel()
{
    MComboBoxView *view = (MComboBoxView *)m_combobox->view();
    MComboBoxViewPrivate *viewPrivate = view->d_func();

    QStringListModel *strListModel = new QStringListModel();
    strListModel->setStringList(QStringList() << "foo0" << "foo1" << "foo2" << "foo3");
    m_combobox->setItemModel(strListModel);

    // select item 2
    m_combobox->setCurrentIndex(2);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), QString("foo2"));
    // remove item1, index should change
    strListModel->removeRow(1);
    QCOMPARE(m_combobox->currentIndex(), 1);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), QString("foo2"));
    // remove current item
    strListModel->removeRow(1);
    QCOMPARE(m_combobox->currentIndex(), -1);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), qtTrId("xx_ComboBoxSubtitle"));
    // check that after removing items everything looks ok
    QCOMPARE(m_combobox->count(), 2);
    m_combobox->setCurrentIndex(1);
    QCOMPARE(m_combobox->currentIndex(), 1);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), QString("foo3"));
    // add one item
    strListModel->insertRow(0);
    strListModel->setData(strListModel->index(0, 0), QString("fooX"), Qt::DisplayRole);
    QCOMPARE(m_combobox->currentIndex(), 2);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), QString("foo3"));

    // change current item text
    strListModel->setData(strListModel->index(2, 0), QString("lamb"), Qt::DisplayRole);
    QCOMPARE(m_combobox->currentIndex(), 2);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), QString("lamb"));
}

void Ut_MComboBox::testStringListModelSetStringList()
{
    MComboBoxView *view = (MComboBoxView *)m_combobox->view();
    MComboBoxViewPrivate *viewPrivate = view->d_func();
    QSignalSpy spy(m_combobox, SIGNAL(currentIndexChanged(int)));

    QStringListModel *strListModel = new QStringListModel();
    strListModel->setStringList(QStringList() << "foo0" << "foo1" << "foo2" << "foo3");
    m_combobox->setItemModel(strListModel);

    // select foo2
    m_combobox->setCurrentIndex(2);
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.takeFirst()[0], QVariant(2));
    // change string list completely
    strListModel->setStringList(QStringList() << "bar0" << "bar1" << "bar2" << "bar3");
    QCOMPARE(m_combobox->currentIndex(), -1);
    QCOMPARE(viewPrivate->button->subtitleWidget()->text(), qtTrId("xx_ComboBoxSubtitle"));
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.takeFirst()[0], QVariant(-1));
}

void Ut_MComboBox::testActivatedSignal()
{
    MComboBoxView *view = (MComboBoxView *)m_combobox->view();
    MComboBoxViewPrivate *viewPrivate = view->d_func();
    m_combobox->addItems(QStringList() << "foo0" << "foo1" << "foo2" << "foo3");
    m_combobox->click();

    QSignalSpy activatedIntSpy(m_combobox, SIGNAL(activated(int)));
    QSignalSpy activatedQStringSpy(m_combobox, SIGNAL(activated(QString)));
    QSignalSpy currentItemChangedIntSpy(m_combobox, SIGNAL(currentIndexChanged(int)));
    QSignalSpy currentItemChangedQStringSpy(m_combobox, SIGNAL(currentIndexChanged(QString)));

    // click first item
    viewPrivate->popuplist->click(m_combobox->itemModel()->index(1, 0));

    QCOMPARE(activatedIntSpy.count(), 1);
    QCOMPARE(activatedIntSpy.last()[0].toInt(), 1);
    QCOMPARE(activatedQStringSpy.count(), 1);
    QCOMPARE(activatedQStringSpy.last()[0].toString(), QString("foo1"));

    QCOMPARE(currentItemChangedIntSpy.count(), 1);
    QCOMPARE(currentItemChangedIntSpy.last()[0].toInt(), 1);
    QCOMPARE(currentItemChangedQStringSpy.count(), 1);
    QCOMPARE(currentItemChangedQStringSpy.last()[0].toString(), QString("foo1"));

    // click first item again, currentItemChanged signal should not be emited
    viewPrivate->popuplist->click(m_combobox->itemModel()->index(1, 0));

    QCOMPARE(activatedIntSpy.count(), 2);
    QCOMPARE(activatedIntSpy.last()[0].toInt(), 1);
    QCOMPARE(activatedQStringSpy.count(), 2);
    QCOMPARE(activatedQStringSpy.last()[0].toString(), QString("foo1"));

    QCOMPARE(currentItemChangedIntSpy.count(), 1);
    QCOMPARE(currentItemChangedQStringSpy.count(), 1);

    // click second item
    viewPrivate->popuplist->click(m_combobox->itemModel()->index(2, 0));

    QCOMPARE(activatedIntSpy.count(), 3);
    QCOMPARE(activatedIntSpy.last()[0].toInt(), 2);
    QCOMPARE(activatedQStringSpy.count(), 3);
    QCOMPARE(activatedQStringSpy.last()[0].toString(), QString("foo2"));

    QCOMPARE(currentItemChangedIntSpy.count(), 2);
    QCOMPARE(currentItemChangedIntSpy.last()[0].toInt(), 2);
    QCOMPARE(currentItemChangedQStringSpy.count(), 2);
    QCOMPARE(currentItemChangedQStringSpy.last()[0].toString(), QString("foo2"));
}

void Ut_MComboBox::testSetCurrentIndex()
{
    win->scene()->addItem(m_combobox);

    MComboBoxView *view = (MComboBoxView *)m_combobox->view();
    MComboBoxViewPrivate *viewPrivate = view->d_func();

    QSignalSpy spy(m_combobox, SIGNAL(currentIndexChanged(int)));

    m_combobox->addItem("text1");
    m_combobox->setCurrentIndex(0);
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.takeFirst()[0].toInt(), 0);

    m_combobox->addItem("text2");
    m_combobox->click();
    viewPrivate->popuplist->click(m_combobox->itemModel()->index(1, 0));
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.takeFirst()[0].toInt(), 1);

    win->scene()->removeItem(m_combobox);
}

QTEST_APPLESS_MAIN(Ut_MComboBox)