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
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/shelf/shelf_model.h"
#include <set>
#include <string>
#include "ash/ash_switches.h"
#include "ash/shelf/shelf_model_observer.h"
#include "base/command_line.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()
: added_count_(0),
removed_count_(0),
changed_count_(0),
moved_count_(0) {
}
// 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);
added_count_ = removed_count_ = changed_count_ = moved_count_ = 0;
return result;
}
// ShelfModelObserver overrides:
virtual void ShelfItemAdded(int index) OVERRIDE {
added_count_++;
}
virtual void ShelfItemRemoved(int index, LauncherID id) OVERRIDE {
removed_count_++;
}
virtual void ShelfItemChanged(int index,
const LauncherItem& old_item) OVERRIDE {
changed_count_++;
}
virtual void ShelfItemMoved(int start_index, int target_index) OVERRIDE {
moved_count_++;
}
virtual void ShelfStatusChanged() OVERRIDE {
}
private:
void AddToResult(const std::string& format, int count, std::string* result) {
if (!count)
return;
if (!result->empty())
*result += " ";
*result += base::StringPrintf(format.c_str(), count);
}
int added_count_;
int removed_count_;
int changed_count_;
int moved_count_;
DISALLOW_COPY_AND_ASSIGN(TestShelfModelObserver);
};
} // namespace
class ShelfModelTest : public testing::Test {
public:
ShelfModelTest() {}
virtual ~ShelfModelTest() {}
virtual void SetUp() {
model_.reset(new ShelfModel);
observer_.reset(new TestShelfModelObserver);
EXPECT_EQ(0, model_->item_count());
LauncherItem item;
item.type = TYPE_APP_LIST;
model_->Add(item);
EXPECT_EQ(1, model_->item_count());
model_->AddObserver(observer_.get());
}
virtual void TearDown() {
observer_.reset();
model_.reset();
}
scoped_ptr<ShelfModel> model_;
scoped_ptr<TestShelfModelObserver> observer_;
private:
DISALLOW_COPY_AND_ASSIGN(ShelfModelTest);
};
TEST_F(ShelfModelTest, BasicAssertions) {
// Add an item.
LauncherItem item;
item.type = TYPE_APP_SHORTCUT;
int index = model_->Add(item);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ("added=1", observer_->StateStringAndClear());
// Change to a platform app item.
LauncherID original_id = model_->items()[index].id;
item.type = TYPE_PLATFORM_APP;
model_->Set(index, item);
EXPECT_EQ(original_id, model_->items()[index].id);
EXPECT_EQ("changed=1", observer_->StateStringAndClear());
EXPECT_EQ(TYPE_PLATFORM_APP, model_->items()[index].type);
// Remove the item.
model_->RemoveItemAt(index);
EXPECT_EQ(1, model_->item_count());
EXPECT_EQ("removed=1", observer_->StateStringAndClear());
// Add an app item.
item.type = TYPE_APP_SHORTCUT;
index = model_->Add(item);
observer_->StateStringAndClear();
// Change everything.
model_->Set(index, item);
EXPECT_EQ("changed=1", observer_->StateStringAndClear());
EXPECT_EQ(TYPE_APP_SHORTCUT, model_->items()[index].type);
// Add another item.
item.type = TYPE_APP_SHORTCUT;
model_->Add(item);
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<LauncherID> 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 browser short cut at index 1.
LauncherItem browser_shortcut;
browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
int browser_shortcut_index = model_->Add(browser_shortcut);
EXPECT_EQ(1, browser_shortcut_index);
// platform app items should be after browser shortcut.
LauncherItem item;
item.type = TYPE_PLATFORM_APP;
int platform_app_index1 = model_->Add(item);
EXPECT_EQ(2, platform_app_index1);
// Add another platform app item, it should follow first.
int platform_app_index2 = model_->Add(item);
EXPECT_EQ(3, platform_app_index2);
// APP_SHORTCUT priority is higher than PLATFORM_APP but same as
// BROWSER_SHORTCUT. So APP_SHORTCUT is located after BROWSER_SHORCUT.
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index1 = model_->Add(item);
EXPECT_EQ(2, app_shortcut_index1);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index2 = model_->Add(item);
EXPECT_EQ(3, app_shortcut_index2);
// Check that AddAt() figures out the correct indexes for app shortcuts.
// APP_SHORTCUT and BROWSER_SHORTCUT has the same weight.
// So APP_SHORTCUT is located at index 0. And, BROWSER_SHORTCUT is located at
// index 1.
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index3 = model_->AddAt(1, item);
EXPECT_EQ(1, app_shortcut_index3);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index4 = model_->AddAt(6, item);
EXPECT_EQ(5, app_shortcut_index4);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index5 = model_->AddAt(3, item);
EXPECT_EQ(3, app_shortcut_index5);
// Before there are any panels, no icons should be right aligned.
EXPECT_EQ(model_->item_count(), model_->FirstPanelIndex());
// Check that AddAt() figures out the correct indexes for platform apps and
// panels.
item.type = TYPE_PLATFORM_APP;
int platform_app_index3 = model_->AddAt(3, item);
EXPECT_EQ(7, platform_app_index3);
item.type = TYPE_APP_PANEL;
int app_panel_index1 = model_->AddAt(2, item);
EXPECT_EQ(10, app_panel_index1);
item.type = TYPE_PLATFORM_APP;
int platform_app_index4 = model_->AddAt(11, item);
EXPECT_EQ(10, platform_app_index4);
item.type = TYPE_APP_PANEL;
int app_panel_index2 = model_->AddAt(12, item);
EXPECT_EQ(12, app_panel_index2);
item.type = TYPE_PLATFORM_APP;
int platform_app_index5 = model_->AddAt(7, item);
EXPECT_EQ(7, platform_app_index5);
item.type = TYPE_APP_PANEL;
int app_panel_index3 = model_->AddAt(13, item);
EXPECT_EQ(13, app_panel_index3);
// Right aligned index should be the first app panel index.
EXPECT_EQ(12, model_->FirstPanelIndex());
EXPECT_EQ(TYPE_BROWSER_SHORTCUT, model_->items()[2].type);
EXPECT_EQ(TYPE_APP_LIST, model_->items()[0].type);
}
// Test that the indexes for the running applications are properly determined
// when the first running application is a windowed application.
TEST_F(ShelfModelTest, FirstRunningAppIndexUsingWindowedAppFirst) {
// Insert the browser shortcut at index 1 and check that the running
// application index would be behind it.
LauncherItem item;
item.type = TYPE_BROWSER_SHORTCUT;
EXPECT_EQ(1, model_->Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
// Insert a panel application at the end and check that the running
// application index would be at / before the application panel.
item.type = TYPE_APP_PANEL;
EXPECT_EQ(2, model_->Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
// Insert an application shortcut and make sure that the running application
// index would be behind it.
item.type = TYPE_APP_SHORTCUT;
EXPECT_EQ(2, model_->Add(item));
EXPECT_EQ(3, model_->FirstRunningAppIndex());
// Insert different running application shortcuts - but first a windowed
// application - and make sure that the same index gets returned.
item.type = TYPE_WINDOWED_APP;
int running_app_index = model_->Add(item);
EXPECT_EQ(3, running_app_index);
EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex());
item.type = TYPE_PLATFORM_APP;
EXPECT_EQ(running_app_index + 1, model_->Add(item));
EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex());
}
// Test that the indexes for the running applications are properly determined
// when the first running application is a platform application.
TEST_F(ShelfModelTest, FirstRunningAppIndexUsingPlatformAppFirst) {
// Insert the browser shortcut at index 1 and check that the running
// application index would be behind it.
LauncherItem item;
item.type = TYPE_BROWSER_SHORTCUT;
EXPECT_EQ(1, model_->Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
// Insert a panel application at the end and check that the running
// application index would be at / before the application panel.
item.type = TYPE_APP_PANEL;
EXPECT_EQ(2, model_->Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
// Insert an application shortcut and make sure that the running application
// index would be behind it.
item.type = TYPE_APP_SHORTCUT;
EXPECT_EQ(2, model_->Add(item));
EXPECT_EQ(3, model_->FirstRunningAppIndex());
// Insert different running application shortcuts - but first a platfom
// application - and make sure that the same index gets returned.
item.type = TYPE_PLATFORM_APP;
int running_app_index = model_->Add(item);
EXPECT_EQ(3, running_app_index);
EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex());
item.type = TYPE_WINDOWED_APP;
EXPECT_EQ(running_app_index + 1, model_->Add(item));
EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex());
}
// Assertions around where items are added.
TEST_F(ShelfModelTest, AddIndicesForLegacyShelfLayout) {
CommandLine::ForCurrentProcess()->AppendSwitch(
ash::switches::kAshDisableAlternateShelfLayout);
// Insert browser short cut at index 0.
LauncherItem browser_shortcut;
browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
int browser_shortcut_index = model_->Add(browser_shortcut);
EXPECT_EQ(0, browser_shortcut_index);
// platform app items should be after browser shortcut.
LauncherItem item;
item.type = TYPE_PLATFORM_APP;
int platform_app_index1 = model_->Add(item);
EXPECT_EQ(1, platform_app_index1);
// Add another platform app item, it should follow first.
int platform_app_index2 = model_->Add(item);
EXPECT_EQ(2, platform_app_index2);
// APP_SHORTCUT priority is higher than PLATFORM_APP but same as
// BROWSER_SHORTCUT. So APP_SHORTCUT is located after BROWSER_SHORCUT.
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index1 = model_->Add(item);
EXPECT_EQ(1, app_shortcut_index1);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index2 = model_->Add(item);
EXPECT_EQ(2, app_shortcut_index2);
// Check that AddAt() figures out the correct indexes for app shortcuts.
// APP_SHORTCUT and BROWSER_SHORTCUT has the same weight.
// So APP_SHORTCUT is located at index 0. And, BROWSER_SHORTCUT is located at
// index 1.
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index3 = model_->AddAt(0, item);
EXPECT_EQ(0, app_shortcut_index3);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index4 = model_->AddAt(5, item);
EXPECT_EQ(4, app_shortcut_index4);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index5 = model_->AddAt(2, item);
EXPECT_EQ(2, app_shortcut_index5);
// Before there are any panels, no icons should be right aligned.
EXPECT_EQ(model_->item_count(), model_->FirstPanelIndex());
// Check that AddAt() figures out the correct indexes for platform apps and
// panels.
item.type = TYPE_PLATFORM_APP;
int platform_app_index3 = model_->AddAt(2, item);
EXPECT_EQ(6, platform_app_index3);
item.type = TYPE_APP_PANEL;
int app_panel_index1 = model_->AddAt(2, item);
EXPECT_EQ(10, app_panel_index1);
item.type = TYPE_PLATFORM_APP;
int platform_app_index4 = model_->AddAt(11, item);
EXPECT_EQ(9, platform_app_index4);
item.type = TYPE_APP_PANEL;
int app_panel_index2 = model_->AddAt(12, item);
EXPECT_EQ(12, app_panel_index2);
item.type = TYPE_PLATFORM_APP;
int platform_app_index5 = model_->AddAt(7, item);
EXPECT_EQ(7, platform_app_index5);
item.type = TYPE_APP_PANEL;
int app_panel_index3 = model_->AddAt(13, item);
EXPECT_EQ(13, app_panel_index3);
// Right aligned index should be the first app panel index.
EXPECT_EQ(12, model_->FirstPanelIndex());
EXPECT_EQ(TYPE_BROWSER_SHORTCUT, model_->items()[1].type);
EXPECT_EQ(TYPE_APP_LIST, model_->items()[model_->FirstPanelIndex() - 1].type);
}
// Assertions around id generation and usage.
TEST_F(ShelfModelTest, LauncherIDTests) {
// Get the next to use ID counter.
LauncherID id = model_->next_id();
// Calling this function multiple times does not change the returned ID.
EXPECT_EQ(model_->next_id(), id);
// Check that when we reserve a value it will be the previously retrieved ID,
// but it will not change the item count and retrieving the next ID should
// produce something new.
EXPECT_EQ(model_->reserve_external_id(), id);
EXPECT_EQ(1, model_->item_count());
LauncherID id2 = model_->next_id();
EXPECT_NE(id2, id);
// Adding another item to the list should also produce a new ID.
LauncherItem item;
item.type = TYPE_PLATFORM_APP;
model_->Add(item);
EXPECT_NE(model_->next_id(), id2);
}
// This verifies that converting an existing item into a lower weight category
// (e.g. shortcut to running but not pinned app) will move it to the proper
// location. See crbug.com/248769.
TEST_F(ShelfModelTest, CorrectMoveItemsWhenStateChange) {
// The first item is the app list and last item is the browser.
LauncherItem browser_shortcut;
browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
int browser_shortcut_index = model_->Add(browser_shortcut);
EXPECT_EQ(TYPE_APP_LIST, model_->items()[0].type);
EXPECT_EQ(1, browser_shortcut_index);
// Add three shortcuts. They should all be moved between the two.
LauncherItem item;
item.type = TYPE_APP_SHORTCUT;
int app1_index = model_->Add(item);
EXPECT_EQ(2, app1_index);
int app2_index = model_->Add(item);
EXPECT_EQ(3, app2_index);
int app3_index = model_->Add(item);
EXPECT_EQ(4, app3_index);
// Now change the type of the second item and make sure that it is moving
// behind the shortcuts.
item.type = TYPE_PLATFORM_APP;
model_->Set(app2_index, item);
// The item should have moved in front of the app launcher.
EXPECT_EQ(TYPE_PLATFORM_APP, model_->items()[4].type);
}
TEST_F(ShelfModelTest, CorrectMoveItemsWhenStateChangeForLegacyShelfLayout) {
CommandLine::ForCurrentProcess()->AppendSwitch(
ash::switches::kAshDisableAlternateShelfLayout);
// The first item is the browser and the second item is app list.
LauncherItem browser_shortcut;
browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
int browser_shortcut_index = model_->Add(browser_shortcut);
EXPECT_EQ(0, browser_shortcut_index);
EXPECT_EQ(TYPE_APP_LIST, model_->items()[1].type);
// Add three shortcuts. They should all be moved between the two.
LauncherItem item;
item.type = TYPE_APP_SHORTCUT;
int app1_index = model_->Add(item);
EXPECT_EQ(1, app1_index);
int app2_index = model_->Add(item);
EXPECT_EQ(2, app2_index);
int app3_index = model_->Add(item);
EXPECT_EQ(3, app3_index);
// Now change the type of the second item and make sure that it is moving
// behind the shortcuts.
item.type = TYPE_PLATFORM_APP;
model_->Set(app2_index, item);
// The item should have moved in front of the app launcher.
EXPECT_EQ(TYPE_PLATFORM_APP, model_->items()[3].type);
}
} // namespace ash
|