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
|
/*
* Copyright (C) 2013 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Pete Woods <pete.woods@canonical.com>
*/
#include <service/ItemStore.h>
#include <service/HardCodedSearchSettings.h>
#include <tests/unit/service/Mocks.h>
#include <string>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using namespace std;
using namespace testing;
using namespace hud::service;
using namespace hud::service::test;
namespace {
class TestItemStore: public Test {
protected:
TestItemStore() {
usageTracker.reset(new NiceMock<MockUsageTracker>());
searchSettings.reset(new HardCodedSearchSettings());
store.reset(new ItemStore("app-id", usageTracker, searchSettings));
}
/* Test a set of strings */
string search(const QString &query) {
QList<Result> results;
store->search(query, Query::EmptyBehaviour::SHOW_SUGGESTIONS, results);
QString result;
if (!results.isEmpty()) {
result = results.at(0).commandName();
}
return result.toStdString();
}
ItemStore::Ptr store;
QSharedPointer<MockUsageTracker> usageTracker;
QSharedPointer<HardCodedSearchSettings> searchSettings;
};
/* Ensure the base calculation works */
TEST_F(TestItemStore, DistanceSubfunction) {
QMenu root;
QMenu file("File");
file.addAction("Open");
file.addAction("New");
file.addAction("Print");
file.addAction("Print Preview");
root.addMenu(&file);
store->indexMenu(&root);
EXPECT_EQ("Print Preview", search("Print Pre"));
}
/* Ensure that we can handle some misspelling */
TEST_F(TestItemStore, DistanceMisspelll) {
QMenu root;
QMenu file("File");
file.addAction("Open");
file.addAction("New");
file.addAction("Print");
file.addAction("Print Preview");
root.addMenu(&file);
store->indexMenu(&root);
EXPECT_EQ("Print Preview", search("Prnt Pr"));
EXPECT_EQ("Print Preview", search("Print Preiw"));
EXPECT_EQ("Print Preview", search("Prnt Pr"));
}
/* Ensure that we can find print with short strings */
TEST_F(TestItemStore, DistancePrintIssues) {
QMenu root;
QMenu file("File");
file.addAction("New");
file.addAction("Open");
file.addAction("Print...");
root.addMenu(&file);
QMenu edit("Edit");
edit.addAction("Undo");
root.addMenu(&edit);
QMenu help("Help");
help.addAction("About");
help.addAction("Empty");
root.addMenu(&help);
store->indexMenu(&root);
EXPECT_EQ("Print...", search("Pr"));
EXPECT_EQ("Print...", search("Print"));
EXPECT_EQ("Print...", search("Print..."));
}
/* Not finished word yet */
TEST_F(TestItemStore, UnfinishedWord) {
QMenu root;
root.addAction("Open Terminal");
root.addAction("Open Tab");
store->indexMenu(&root);
EXPECT_EQ("Open Terminal", search("open ter"));
EXPECT_EQ("Open Terminal", search("open term"));
EXPECT_EQ("Open Terminal", search("open termi"));
EXPECT_EQ("Open Terminal", search("open termin"));
EXPECT_EQ("Open Terminal", search("open termina"));
EXPECT_EQ("Open Terminal", search("open terminal"));
}
/* Not finished word yet */
TEST_F(TestItemStore, UnfinishedWord2) {
QMenu root;
root.addAction("Change Topic");
store->indexMenu(&root);
EXPECT_EQ("Change Topic", search("cha"));
}
/* A variety of strings that should have predictable results */
TEST_F(TestItemStore, DistanceVariety) {
QMenu root;
QMenu date("Date");
date.addAction("House Cleaning");
root.addMenu(&date);
QMenu file("File");
file.addAction("Close Window");
root.addMenu(&file);
QMenu edit("Edit");
edit.addAction("Keyboard Shortcuts...");
root.addMenu(&edit);
QMenu vpn("VPN Configuration");
vpn.addAction("Configure VPN...");
QMenu network("Network");
network.addMenu(&vpn);
root.addMenu(&network);
store->indexMenu(&root);
EXPECT_EQ("House Cleaning", search("House"));
EXPECT_EQ("House Cleaning", search("House C"));
EXPECT_EQ("House Cleaning", search("House Cle"));
EXPECT_EQ("House Cleaning", search("House Clean"));
EXPECT_EQ("House Cleaning", search("Clean House"));
}
/* A variety of strings that should have predictable results */
TEST_F(TestItemStore, DistanceFrenchPref) {
QMenu root;
QMenu file("Fichier");
file.addAction("aperçu avant impression");
root.addMenu(&file);
QMenu connection("Connexion au réseau...");
root.addMenu(&connection);
QMenu edit("Edition");
edit.addAction("préférences");
root.addMenu(&edit);
store->indexMenu(&root);
EXPECT_EQ("préférences", search("préférences"));
EXPECT_EQ("préférences", search("pré"));
EXPECT_EQ("préférences", search("préf"));
EXPECT_EQ("préférences", search("préfé"));
EXPECT_EQ("préférences", search("pref"));
}
/* Check to make sure the returned hits are not dups and the
proper number */
TEST_F(TestItemStore, DistanceDups) {
QMenu root;
root.addAction("Inflated");
root.addAction("Confluated");
root.addAction("Sublimated");
root.addAction("Sadated");
root.addAction("Situated");
root.addAction("Infatuated");
store->indexMenu(&root);
EXPECT_EQ("Inflated", search("ted inf"));
}
/* Check to make sure 'Save' matches better than 'Save As...' for "save" */
TEST_F(TestItemStore, DistanceExtraTerms) {
QMenu root;
QMenu file("File");
file.addAction("Save As...");
file.addAction("Save");
root.addMenu(&file);
store->indexMenu(&root);
EXPECT_EQ("Save", search("save"));
}
TEST_F(TestItemStore, BlankSearchFrequentlyUsedItems) {
QMenu root;
QMenu file("&File");
file.addAction("&One");
file.addAction("&Two");
file.addAction("T&hree");
file.addAction("Fou&r");
root.addMenu(&file);
store->indexMenu(&root);
ON_CALL(*usageTracker,
usage(QString("app-id"), QString("File||One"))).WillByDefault(
Return(2));
ON_CALL(*usageTracker,
usage(QString("app-id"), QString("File||Two"))).WillByDefault(
Return(0));
ON_CALL(*usageTracker,
usage(QString("app-id"), QString("File||Three"))).WillByDefault(
Return(4));
ON_CALL(*usageTracker,
usage(QString("app-id"), QString("File||Four"))).WillByDefault(
Return(3));
QList<Result> results;
store->search("", Query::EmptyBehaviour::SHOW_SUGGESTIONS, results);
ASSERT_EQ(4, results.size());
EXPECT_EQ(QString("Three"), results.at(0).commandName());
EXPECT_EQ(QString("Four"), results.at(1).commandName());
EXPECT_EQ(QString("One"), results.at(2).commandName());
EXPECT_EQ(QString("Two"), results.at(3).commandName());
}
TEST_F(TestItemStore, BlankSearchNoSuggestions) {
QMenu root;
QMenu file("&File");
file.addAction("&One");
file.addAction("&Two");
file.addAction("T&hree");
file.addAction("Fou&r");
root.addMenu(&file);
store->indexMenu(&root);
ON_CALL(*usageTracker,
usage(QString("app-id"), QString("File||One"))).WillByDefault(
Return(2));
ON_CALL(*usageTracker,
usage(QString("app-id"), QString("File||Two"))).WillByDefault(
Return(0));
ON_CALL(*usageTracker,
usage(QString("app-id"), QString("File||Three"))).WillByDefault(
Return(4));
ON_CALL(*usageTracker,
usage(QString("app-id"), QString("File||Four"))).WillByDefault(
Return(3));
QList<Result> results;
store->search("", Query::EmptyBehaviour::NO_SUGGESTIONS, results);
ASSERT_TRUE(results.empty());
}
TEST_F(TestItemStore, ExecuteMarksHistory) {
QMenu root;
QMenu file("File");
file.addAction("Save As...");
file.addAction("Save");
root.addMenu(&file);
store->indexMenu(&root);
EXPECT_CALL(*usageTracker,
markUsage(QString("app-id"), QString("File||Save As...")));
store->execute(0);
}
TEST_F(TestItemStore, ChangeSearchSettings) {
QMenu root;
QMenu file("&File");
file.addAction("Apple");
file.addAction("Banana");
file.addAction("Can Cherry");
root.addMenu(&file);
store->indexMenu(&root);
EXPECT_EQ("Banana", search("Ban"));
searchSettings->setEndDropPenalty(100);
EXPECT_EQ("Can Cherry", search("Ban"));
}
} // namespace
|