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
|
/*
* Copyright 2012 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Renato Araujo Oliveira Filho <renato@canonical.com>
* Olivier Tilloy <olivier.tilloy@canonical.com>
*/
#include "qmenumodel.h"
extern "C" {
#include <gio/gio.h>
}
#include <QtTest>
class TestModel : public QMenuModel
{
Q_OBJECT
public:
TestModel() : QMenuModel(0)
{
GMenu *menu3 = g_menu_new();
g_menu_append(menu3, "menu4", NULL);
g_menu_append(menu3, "menu5", NULL);
g_menu_append(menu3, "menu6", NULL);
GMenu *menu = g_menu_new();
g_menu_append(menu, "menu0", NULL);
g_menu_append(menu, "menu1", NULL);
g_menu_append(menu, "menu2", NULL);
g_menu_append_section(menu, "menu3", G_MENU_MODEL(menu3));
setMenuModel(G_MENU_MODEL(menu));
m_menus << menu << menu3;
}
void removeItem(int section, int index)
{
GMenu *menu = m_menus[section];
g_menu_remove(menu, index);
}
void insertItem(int section, int index, const QString &label)
{
GMenu *menu = m_menus[section];
g_menu_insert(menu, index, label.toUtf8().data(), NULL);
}
QList<int> cacheIndexes() const
{
QList<int> indexes = cache().keys();
qSort(indexes);
return indexes;
}
private:
QList<GMenu*> m_menus;
};
class CacheTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
// Verify that normal menu items are not cached (only sub-menus are)
void testCacheContents()
{
TestModel menu;
QVERIFY(menu.cacheIndexes().isEmpty());
menu.data(menu.index(1), QMenuModel::Label);
QVERIFY(menu.cacheIndexes().isEmpty());
menu.data(menu.index(2), QMenuModel::Action);
QVERIFY(menu.cacheIndexes().isEmpty());
}
// Verify that the link attribute always returns the same cached menu
void testStaticMenuCache()
{
TestModel menu;
QModelIndex index = menu.index(3);
QVariant data = menu.data(index, QMenuModel::LinkSection);
QCOMPARE(menu.cacheIndexes(), QList<int>() << 3);
QVariant data2 = menu.data(index, QMenuModel::LinkSection);
QCOMPARE(menu.cacheIndexes(), QList<int>() << 3);
QVERIFY(data.value<QObject*>() == data2.value<QObject*>());
QMenuModel *section = qvariant_cast<QMenuModel*>(data);
index = section->index(1);
data = menu.data(index, QMenuModel::LinkSection);
data2 = menu.data(index, QMenuModel::LinkSection);
QCOMPARE(menu.cacheIndexes(), QList<int>() << 3);
QVERIFY(data.value<QObject*>() == data2.value<QObject*>());
}
// Verify that the cache is correctly updated after inserting a new item
void testInsertItems()
{
TestModel menu;
QModelIndex index = menu.index(3);
QVariant data = menu.data(index, QMenuModel::LinkSection);
QCOMPARE(menu.cacheIndexes(), QList<int>() << 3);
menu.insertItem(0, 4, "newMenu");
QCOMPARE(menu.cacheIndexes(), QList<int>() << 3);
menu.insertItem(0, 1, "newMenu");
QCOMPARE(menu.cacheIndexes(), QList<int>() << 4);
index = menu.index(4);
QVariant data2 = menu.data(index, QMenuModel::LinkSection);
QCOMPARE(menu.cacheIndexes(), QList<int>() << 4);
QVERIFY(data.value<QObject*>() == data2.value<QObject*>());
}
// Verify that the cache is correctly updated after removing an item that wasn’t cached
void testRemoveNonCachedItem()
{
TestModel menu;
QModelIndex index = menu.index(3);
QVariant data = menu.data(index, QMenuModel::LinkSection);
QCOMPARE(menu.cacheIndexes(), QList<int>() << 3);
menu.removeItem(0, 1);
QCOMPARE(menu.cacheIndexes(), QList<int>() << 2);
index = menu.index(2);
QVariant data2 = menu.data(index, QMenuModel::LinkSection);
QCOMPARE(menu.cacheIndexes(), QList<int>() << 2);
QVERIFY(data.value<QObject*>() == data2.value<QObject*>());
}
// Verify that the cache is correctly updated after removing a cached item
void testRemoveCachedItem()
{
TestModel menu;
QModelIndex index = menu.index(3);
QVariant data = menu.data(index, QMenuModel::LinkSection);
QCOMPARE(menu.cacheIndexes(), QList<int>() << 3);
menu.removeItem(0, 3);
QVERIFY(menu.cacheIndexes().isEmpty());
}
// Verify that the cache is correctly updated after multiple insertions and removals
void testMultiplesUpdates()
{
TestModel menu;
QVERIFY(menu.cacheIndexes().isEmpty());
menu.data(menu.index(3), QMenuModel::LinkSection);
QCOMPARE(menu.cacheIndexes(), QList<int>() << 3);
menu.insertItem(0, 1, "newMenu");
menu.insertItem(0, 2, "newMenu");
menu.insertItem(0, 6, "newMenu");
menu.insertItem(0, 3, "newMenu");
menu.insertItem(0, 7, "newMenu");
QCOMPARE(menu.cacheIndexes(), QList<int>() << 6);
menu.removeItem(0, 4);
menu.removeItem(0, 6);
menu.removeItem(0, 2);
QCOMPARE(menu.cacheIndexes(), QList<int>() << 4);
menu.removeItem(0, 4);
QVERIFY(menu.cacheIndexes().isEmpty());
}
};
QTEST_MAIN(CacheTest)
#include "cachetest.moc"
|