summaryrefslogtreecommitdiff
path: root/client/test/testcomboboxsearch.cc
blob: 564f5c1bf4c26bd25404bfe18820484c876c4909 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
#include <QtTest/QtTest>
#include "util.h"
#include "combobox.h"
#include <QAbstractItemView>
#include <QCompleter>
#include <QComboBox>

static QString xml = 
	"<combobox name=\"mycombobox\" type=\"search\">\n"
	"  <item value=\"item1\" caption=\"Item 1\"/>\n"
	"  <item value=\"item3\" caption=\"Item 3\"/>\n"
	"  <item value=\"item2\" caption=\"Item 2\"/>\n"
	"  <item value=\"thingy\" caption=\"Thingy\"/>\n"
	"  <item value=\"bob\" caption=\"Bob\"/>\n"
	"</combobox>\n";

static QString xml_default = 
	"<combobox name=\"mycombobox\" type=\"search\" value=\"item2\">\n"
	"  <item value=\"item1\" caption=\"Item 1\"/>\n"
	"  <item value=\"item3\" caption=\"Item 3\"/>\n"
	"  <item value=\"item2\" caption=\"Item 2\"/>\n"
	"  <item value=\"thingy\" caption=\"Thingy\"/>\n"
	"  <item value=\"bob\" caption=\"Bob\"/>\n"
	"</combobox>\n";

class TestComboBoxSearch: public QObject
{
Q_OBJECT
private slots:
  void creation() { TEST_CREATION(ComboBox); }
  void disable() { TEST_DISABLE(ComboBox); }

  void searchFullItem()
	{
		QDomDocument doc;	doc.setContent(xml);
    QDomElement e = doc.documentElement();
    ComboBox cmb(e, createMacroWindow());

		// Full item search
    QTest::keyClicks(cmb.qwidget(), "Item 2");
    QCOMPARE(cmb.value(), QString("item2"));
  }

  void searchPrefix()
	{
		QDomDocument doc;	doc.setContent(xml);
    QDomElement e = doc.documentElement();
    ComboBox cmb(e, createMacroWindow());

    QComboBox *qcmb = (QComboBox *)cmb.qwidget();
		// Item prefix search
    QTest::keyClicks(cmb.qwidget(), "T");
		QCOMPARE(qcmb->completer()->currentCompletion(), QString("Thingy"));

    QTest::keyClicks(cmb.qwidget(), qcmb->completer()->currentCompletion());
    QCOMPARE(cmb.value(), QString("thingy"));
  }

  void searchNegative()
	{
		QDomDocument doc;	doc.setContent(xml);
    QDomElement e = doc.documentElement();
    ComboBox cmb(e, createMacroWindow());

		// Negative search
    QTest::keyClicks(cmb.qwidget(), "A");
    QCOMPARE(cmb.value(), QString(""));
  }

  void arrowSelect()
	{
		QDomDocument doc;	doc.setContent(xml);
    QDomElement e = doc.documentElement();
    ComboBox cmb(e, createMacroWindow());
    QTest::keyPress(cmb.qwidget(), Qt::Key_Down);
    QTest::keyPress(cmb.qwidget(), Qt::Key_Down);
    QTest::keyPress(cmb.qwidget(), Qt::Key_Enter);
    QCOMPARE(cmb.value(), QString("item3"));
  }
	/* // It is set in MacroWindow generation .. not directly in the Widget.
  void defaultValue()
	{
		QDomDocument doc;	doc.setContent(xml_default);
    QDomElement e = doc.documentElement();
    ComboBox cmb(e, NULL);
    QCOMPARE(cmb.value(), QString("item2"));
  }
	*/
	void changeEmitUser()
	{
		QDomDocument doc;	doc.setContent(xml);
    QDomElement e = doc.documentElement();
    ComboBox cmb(e, createMacroWindow());
		QSignalSpy spy(&cmb, SIGNAL(wasChanged()));
    QTest::keyClicks(cmb.qwidget(), "I");
		QCOMPARE(spy.count(), 1);
	}

	void changeEmitSystem()
	{
		QDomDocument doc;	doc.setContent(xml);
    QDomElement e = doc.documentElement();
    ComboBox cmb(e, createMacroWindow());
		QSignalSpy spy(&cmb, SIGNAL(wasChanged()));
		cmb.setValue("some value", "pentominos");
		QCOMPARE(spy.count(), 1);
	}

	void changeNoEmitSystem()
	{
		QDomDocument doc;	doc.setContent(xml);
    QDomElement e = doc.documentElement();
    ComboBox cmb(e, createMacroWindow());
		QSignalSpy spy(&cmb, SIGNAL(wasChanged()));
		cmb.setValue("some value", "pracro");
		QCOMPARE(spy.count(), 0);
	}
};

QTEST_MAIN(TestComboBoxSearch)
#include "testcomboboxsearch.moc"