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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
ash / public / cpp / shelf_model_unittest.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/public/cpp/shelf_model.h"
#include <memory>
#include <set>
#include <string>
#include "ash/public/cpp/shelf_model_observer.h"
#include "ash/public/cpp/test/test_shelf_item_delegate.h"
#include "base/strings/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace {
// ShelfModelObserver implementation that tracks what message are invoked.
class TestShelfModelObserver : public ShelfModelObserver {
public:
TestShelfModelObserver() = default;
TestShelfModelObserver(const TestShelfModelObserver&) = delete;
TestShelfModelObserver& operator=(const TestShelfModelObserver&) = delete;
// Returns a string description of the changes that have occurred since this
// was last invoked. Resets state to initial state.
std::string StateStringAndClear() {
std::string result;
AddToResult("added=%d", added_count_, &result);
AddToResult("removed=%d", removed_count_, &result);
AddToResult("changed=%d", changed_count_, &result);
AddToResult("moved=%d", moved_count_, &result);
AddToResult("delegate_changed=%d", delegate_changed_count_, &result);
added_count_ = removed_count_ = changed_count_ = moved_count_ =
delegate_changed_count_ = 0;
return result;
}
// ShelfModelObserver overrides:
void ShelfItemAdded(int) override { added_count_++; }
void ShelfItemRemoved(int, const ShelfItem&) override { removed_count_++; }
void ShelfItemChanged(int, const ShelfItem&) override { changed_count_++; }
void ShelfItemMoved(int, int) override { moved_count_++; }
void ShelfItemDelegateChanged(const ShelfID&,
ShelfItemDelegate*,
ShelfItemDelegate*) override {
delegate_changed_count_++;
}
private:
void AddToResult(const std::string& format, int count, std::string* result) {
if (!count)
return;
if (!result->empty())
*result += " ";
*result += base::StringPrintfNonConstexpr(format.c_str(), count);
}
int added_count_ = 0;
int removed_count_ = 0;
int changed_count_ = 0;
int moved_count_ = 0;
int delegate_changed_count_ = 0;
};
} // namespace
class ShelfModelTest : public testing::Test {
public:
ShelfModelTest() = default;
ShelfModelTest(const ShelfModelTest&) = delete;
ShelfModelTest& operator=(const ShelfModelTest&) = delete;
~ShelfModelTest() override = default;
void SetUp() override {
model_ = std::make_unique<ShelfModel>();
observer_ = std::make_unique<TestShelfModelObserver>();
model_->AddObserver(observer_.get());
}
void TearDown() override {
observer_.reset();
model_.reset();
}
// Helper function for simplifying adding items to the shelf.
int Add(const ShelfItem& item) {
return model_->Add(item, std::make_unique<TestShelfItemDelegate>(item.id));
}
int AddAt(int index, const ShelfItem& item) {
return model_->AddAt(index, item,
std::make_unique<TestShelfItemDelegate>(item.id));
}
std::unique_ptr<ShelfModel> model_;
std::unique_ptr<TestShelfModelObserver> observer_;
};
TEST_F(ShelfModelTest, BasicAssertions) {
// Add an item.
ShelfItem item1;
item1.id = ShelfID("item1");
item1.type = TYPE_PINNED_APP;
int index = Add(item1);
EXPECT_EQ(1, model_->item_count());
EXPECT_LE(0, model_->ItemIndexByID(item1.id));
EXPECT_TRUE(model_->ItemByID(item1.id));
EXPECT_EQ("added=1", observer_->StateStringAndClear());
// Change to a platform app item.
item1.type = TYPE_APP;
model_->Set(index, item1);
EXPECT_EQ(item1.id, model_->items()[index].id);
EXPECT_LE(0, model_->ItemIndexByID(item1.id));
EXPECT_TRUE(model_->ItemByID(item1.id));
EXPECT_EQ("changed=1", observer_->StateStringAndClear());
EXPECT_EQ(TYPE_APP, model_->items()[index].type);
// Remove the item.
model_->RemoveItemAt(index);
EXPECT_EQ(0, model_->item_count());
EXPECT_EQ(-1, model_->ItemIndexByID(item1.id));
EXPECT_FALSE(model_->ItemByID(item1.id));
EXPECT_EQ("removed=1", observer_->StateStringAndClear());
// Add an app item.
ShelfItem item2;
item2.id = ShelfID("item2");
item2.type = TYPE_PINNED_APP;
index = Add(item2);
EXPECT_EQ(1, model_->item_count());
EXPECT_LE(0, model_->ItemIndexByID(item2.id));
EXPECT_TRUE(model_->ItemByID(item2.id));
EXPECT_EQ("added=1", observer_->StateStringAndClear());
// Change the item type.
item2.type = TYPE_APP;
model_->Set(index, item2);
EXPECT_LE(0, model_->ItemIndexByID(item2.id));
EXPECT_TRUE(model_->ItemByID(item2.id));
EXPECT_EQ("changed=1", observer_->StateStringAndClear());
EXPECT_EQ(TYPE_APP, model_->items()[index].type);
// Add another item.
ShelfItem item3;
item3.id = ShelfID("item3");
item3.type = TYPE_PINNED_APP;
Add(item3);
EXPECT_EQ(2, model_->item_count());
EXPECT_LE(0, model_->ItemIndexByID(item3.id));
EXPECT_TRUE(model_->ItemByID(item3.id));
EXPECT_EQ("added=1", observer_->StateStringAndClear());
// Move the second to the first.
model_->Move(1, 0);
EXPECT_EQ("moved=1", observer_->StateStringAndClear());
// And back.
model_->Move(0, 1);
EXPECT_EQ("moved=1", observer_->StateStringAndClear());
// Verifies all the items get unique ids.
std::set<ShelfID> ids;
for (int i = 0; i < model_->item_count(); ++i)
ids.insert(model_->items()[i].id);
EXPECT_EQ(model_->item_count(), static_cast<int>(ids.size()));
}
// Assertions around where items are added.
TEST_F(ShelfModelTest, AddIndices) {
// Insert a browser shortcut, like Chrome does, it should be added at index 0.
ShelfItem browser_shortcut;
browser_shortcut.id = ShelfID("browser");
browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
EXPECT_EQ(0, Add(browser_shortcut));
// App items should be after the browser shortcut.
ShelfItem item;
item.type = TYPE_APP;
item.id = ShelfID("id1");
int platform_app_index1 = Add(item);
EXPECT_EQ(1, platform_app_index1);
// Add another platform app item, it should follow first.
item.id = ShelfID("id2");
int platform_app_index2 = Add(item);
EXPECT_EQ(2, platform_app_index2);
// TYPE_PINNED_APP priority is higher than TYPE_APP but same as
// TYPE_BROWSER_SHORTCUT. So TYPE_PINNED_APP is located after
// TYPE_BROWSER_SHORTCUT.
item.type = TYPE_PINNED_APP;
item.id = ShelfID("id3");
int app_shortcut_index1 = Add(item);
EXPECT_EQ(1, app_shortcut_index1);
item.type = TYPE_PINNED_APP;
item.id = ShelfID("id4");
int app_shortcut_index2 = Add(item);
EXPECT_EQ(2, app_shortcut_index2);
// Check that AddAt() figures out the correct indexes for app
// shortcuts. TYPE_PINNED_APP and TYPE_BROWSER_SHORTCUT has the same weight.
// So TYPE_PINNED_APP is located at index 0. And, TYPE_BROWSER_SHORTCUT is
// located at index 1.
item.type = TYPE_PINNED_APP;
item.id = ShelfID("id5");
int app_shortcut_index3 = AddAt(0, item);
EXPECT_EQ(0, app_shortcut_index3);
item.type = TYPE_PINNED_APP;
item.id = ShelfID("id6");
int app_shortcut_index4 = AddAt(5, item);
EXPECT_EQ(4, app_shortcut_index4);
item.type = TYPE_PINNED_APP;
item.id = ShelfID("id7");
int app_shortcut_index5 = AddAt(1, item);
EXPECT_EQ(1, app_shortcut_index5);
// Check that AddAt() figures out the correct indexes for apps.
item.type = TYPE_APP;
item.id = ShelfID("id8");
int platform_app_index3 = AddAt(2, item);
EXPECT_EQ(6, platform_app_index3);
item.type = TYPE_APP;
item.id = ShelfID("id9");
int platform_app_index4 = AddAt(6, item);
EXPECT_EQ(6, platform_app_index4);
EXPECT_EQ(TYPE_BROWSER_SHORTCUT, model_->items()[2].type);
// TYPE_UNPINNED_BROWSER_SHORTCUT icons should behave similar to
// unpinned apps.
item.type = TYPE_UNPINNED_BROWSER_SHORTCUT;
item.id = ShelfID("unpinned_browser");
int unpinned_browser_index = AddAt(2, item);
EXPECT_EQ(6, unpinned_browser_index);
}
// Test that the indexes for the running applications are properly determined.
TEST_F(ShelfModelTest, FirstRunningAppIndex) {
// Insert the browser shortcut at index 0 and check that the running
// application index would be behind it.
ShelfItem item;
item.id = ShelfID("browser");
item.type = TYPE_BROWSER_SHORTCUT;
EXPECT_EQ(0, Add(item));
EXPECT_EQ(1, model_->FirstRunningAppIndex());
// Insert an application shortcut and make sure that the running application
// index would be behind it.
item.type = TYPE_PINNED_APP;
item.id = ShelfID("pinned app");
EXPECT_EQ(1, Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
// Insert a two app items and check the first running app index.
item.type = TYPE_APP;
item.id = ShelfID("app1");
EXPECT_EQ(2, Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
item.id = ShelfID("app2");
EXPECT_EQ(3, Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
item.type = TYPE_UNPINNED_BROWSER_SHORTCUT;
item.id = ShelfID("unpinned browser");
EXPECT_EQ(4, Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
}
// Test item reordering on type/weight (eg. pinning) changes. crbug.com/248769.
TEST_F(ShelfModelTest, ReorderOnTypeChanges) {
// Add three pinned items.
ShelfItem item1;
item1.type = TYPE_PINNED_APP;
item1.id = ShelfID("id1");
int app1_index = Add(item1);
EXPECT_EQ(0, app1_index);
ShelfItem item2;
item2.type = TYPE_PINNED_APP;
item2.id = ShelfID("id2");
int app2_index = Add(item2);
EXPECT_EQ(1, app2_index);
ShelfItem item3;
item3.type = TYPE_PINNED_APP;
item3.id = ShelfID("id3");
int app3_index = Add(item3);
EXPECT_EQ(2, app3_index);
// Unpinning an item moves it behind the shortcuts.
EXPECT_EQ(item3.id, model_->items()[2].id);
item2.type = TYPE_APP;
model_->Set(app2_index, item2);
EXPECT_EQ(item2.id, model_->items()[2].id);
}
// Test getting the index of ShelfIDs as a check for item presence.
TEST_F(ShelfModelTest, ItemIndexByID) {
// Expect empty and unknown ids to return the invalid index -1.
EXPECT_EQ(-1, model_->ItemIndexByID(ShelfID()));
EXPECT_EQ(-1, model_->ItemIndexByID(ShelfID("foo")));
EXPECT_EQ(-1, model_->ItemIndexByID(ShelfID("foo", "bar")));
// Add an item and expect to get a valid index for its id.
ShelfItem item1;
item1.type = TYPE_PINNED_APP;
item1.id = ShelfID("app_id1", "launch_id1");
const int index1 = Add(item1);
EXPECT_EQ(index1, model_->ItemIndexByID(item1.id));
// Add another item and expect to get another valid index for its id.
ShelfItem item2;
item2.type = TYPE_APP;
item2.id = ShelfID("app_id2", "launch_id2");
const int index2 = Add(item2);
EXPECT_EQ(index2, model_->ItemIndexByID(item2.id));
// Removing the first item should yield an invalid index for that item.
model_->RemoveItemAt(index1);
EXPECT_EQ(-1, model_->ItemIndexByID(item1.id));
// The index of the second item should be decremented, but still valid.
EXPECT_EQ(index2 - 1, model_->ItemIndexByID(item2.id));
EXPECT_LE(0, model_->ItemIndexByID(item2.id));
}
// Test pinning and unpinning a closed app, and checking if it is pinned.
TEST_F(ShelfModelTest, ClosedAppPinning) {
const std::string app_id("app_id");
// Check the initial state.
EXPECT_FALSE(model_->IsAppPinned(app_id));
EXPECT_EQ(0, model_->item_count());
// Pinning a previously unknown app should add an item.
ShelfItem item;
item.id = ShelfID(app_id);
item.type = TYPE_PINNED_APP;
model_->Add(item, std::make_unique<TestShelfItemDelegate>(item.id));
EXPECT_TRUE(model_->IsAppPinned(app_id));
EXPECT_EQ(1, model_->item_count());
EXPECT_EQ(TYPE_PINNED_APP, model_->items()[0].type);
EXPECT_EQ(app_id, model_->items()[0].id.app_id);
// Pinning the same app id again should have no change.
model_->PinExistingItemWithID(app_id);
EXPECT_TRUE(model_->IsAppPinned(app_id));
EXPECT_EQ(1, model_->item_count());
EXPECT_EQ(TYPE_PINNED_APP, model_->items()[0].type);
EXPECT_EQ(app_id, model_->items()[0].id.app_id);
// Unpinning the app should remove the item.
model_->UnpinAppWithID(app_id);
EXPECT_FALSE(model_->IsAppPinned(app_id));
EXPECT_EQ(0, model_->item_count());
// Unpinning the same app id again should have no change.
model_->UnpinAppWithID(app_id);
EXPECT_FALSE(model_->IsAppPinned(app_id));
EXPECT_EQ(0, model_->item_count());
}
// Test pinning and unpinning a running app, and checking if it is pinned.
TEST_F(ShelfModelTest, RunningAppPinning) {
const std::string app_id("app_id");
// Check the initial state.
EXPECT_FALSE(model_->IsAppPinned(app_id));
EXPECT_EQ(0, model_->item_count());
// Add an example running app.
ShelfItem item;
item.type = TYPE_APP;
item.status = STATUS_RUNNING;
item.id = ShelfID(app_id);
const int index = Add(item);
// The item should be added but not pinned.
EXPECT_FALSE(model_->IsAppPinned(app_id));
EXPECT_EQ(1, model_->item_count());
EXPECT_EQ(TYPE_APP, model_->items()[index].type);
EXPECT_EQ(item.id, model_->items()[index].id);
// Pinning the item should just change its type.
model_->PinExistingItemWithID(app_id);
EXPECT_TRUE(model_->IsAppPinned(app_id));
EXPECT_EQ(1, model_->item_count());
EXPECT_EQ(TYPE_PINNED_APP, model_->items()[index].type);
EXPECT_EQ(item.id, model_->items()[index].id);
// Pinning the same app id again should have no change.
model_->PinExistingItemWithID(app_id);
EXPECT_TRUE(model_->IsAppPinned(app_id));
EXPECT_EQ(1, model_->item_count());
EXPECT_EQ(TYPE_PINNED_APP, model_->items()[index].type);
EXPECT_EQ(item.id, model_->items()[index].id);
// Unpinning the app should leave the item unpinnned but running.
model_->UnpinAppWithID(app_id);
EXPECT_FALSE(model_->IsAppPinned(app_id));
EXPECT_EQ(1, model_->item_count());
EXPECT_EQ(TYPE_APP, model_->items()[index].type);
EXPECT_EQ(item.id, model_->items()[index].id);
// Unpinning the same app id again should have no change.
model_->UnpinAppWithID(app_id);
EXPECT_FALSE(model_->IsAppPinned(app_id));
EXPECT_EQ(1, model_->item_count());
EXPECT_EQ(TYPE_APP, model_->items()[index].type);
EXPECT_EQ(item.id, model_->items()[index].id);
}
// Tests that apps are updated properly when notifications are added or removed.
TEST_F(ShelfModelTest, AddRemoveNotification) {
const std::string app_id("app_id");
// Add an example running app.
ShelfItem item;
item.type = TYPE_APP;
item.status = STATUS_RUNNING;
item.id = ShelfID(app_id);
const int index = Add(item);
EXPECT_FALSE(model_->items()[index].has_notification);
// Update to add a notification for the app.
model_->UpdateItemNotification(app_id, true /* has_badge */);
EXPECT_TRUE(model_->items()[index].has_notification);
// Update to remove the notification for the app.
model_->UpdateItemNotification(app_id, false /* has_badge */);
EXPECT_FALSE(model_->items()[index].has_notification);
}
// Test that RemoveItemAndTakeShelfItemDelegate has the same effect as
// RemoveItemAt and returns the correct delegate.
TEST_F(ShelfModelTest, RemoveItemAndTakeShelfItemDelegate) {
// Add an item.
ShelfItem item1;
item1.id = ShelfID("item1");
item1.type = TYPE_PINNED_APP;
Add(item1);
EXPECT_EQ(1, model_->item_count());
EXPECT_LE(0, model_->ItemIndexByID(item1.id));
EXPECT_TRUE(model_->ItemByID(item1.id));
EXPECT_EQ("added=1", observer_->StateStringAndClear());
// Set item delegate.
auto* delegate = new TestShelfItemDelegate(item1.id);
model_->ReplaceShelfItemDelegate(
item1.id, std::unique_ptr<ShelfItemDelegate>(delegate));
EXPECT_EQ("delegate_changed=1", observer_->StateStringAndClear());
// Remove the item.
auto taken_delegate = model_->RemoveItemAndTakeShelfItemDelegate(item1.id);
EXPECT_EQ(0, model_->item_count());
EXPECT_EQ(-1, model_->ItemIndexByID(item1.id));
EXPECT_FALSE(model_->ItemByID(item1.id));
EXPECT_EQ("removed=1", observer_->StateStringAndClear());
EXPECT_EQ(delegate, taken_delegate.get());
}
} // namespace ash