OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/launcher/launcher_model.h" | |
6 | |
7 #include <set> | |
8 #include <string> | |
9 | |
10 #include "ash/ash_switches.h" | |
11 #include "ash/shelf/shelf_model_observer.h" | |
12 #include "base/command_line.h" | |
13 #include "base/strings/stringprintf.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace ash { | |
17 | |
18 namespace { | |
19 | |
20 // ShelfModelObserver implementation that tracks what message are invoked. | |
21 class TestShelfModelObserver : public ShelfModelObserver { | |
22 public: | |
23 TestShelfModelObserver() | |
24 : added_count_(0), | |
25 removed_count_(0), | |
26 changed_count_(0), | |
27 moved_count_(0) { | |
28 } | |
29 | |
30 // Returns a string description of the changes that have occurred since this | |
31 // was last invoked. Resets state to initial state. | |
32 std::string StateStringAndClear() { | |
33 std::string result; | |
34 AddToResult("added=%d", added_count_, &result); | |
35 AddToResult("removed=%d", removed_count_, &result); | |
36 AddToResult("changed=%d", changed_count_, &result); | |
37 AddToResult("moved=%d", moved_count_, &result); | |
38 added_count_ = removed_count_ = changed_count_ = moved_count_ = 0; | |
39 return result; | |
40 } | |
41 | |
42 // ShelfModelObserver overrides: | |
43 virtual void ShelfItemAdded(int index) OVERRIDE { | |
44 added_count_++; | |
45 } | |
46 virtual void ShelfItemRemoved(int index, LauncherID id) OVERRIDE { | |
47 removed_count_++; | |
48 } | |
49 virtual void ShelfItemChanged(int index, | |
50 const LauncherItem& old_item) OVERRIDE { | |
51 changed_count_++; | |
52 } | |
53 virtual void ShelfItemMoved(int start_index, int target_index) OVERRIDE { | |
54 moved_count_++; | |
55 } | |
56 virtual void ShelfStatusChanged() OVERRIDE { | |
57 } | |
58 | |
59 private: | |
60 void AddToResult(const std::string& format, int count, std::string* result) { | |
61 if (!count) | |
62 return; | |
63 if (!result->empty()) | |
64 *result += " "; | |
65 *result += base::StringPrintf(format.c_str(), count); | |
66 } | |
67 | |
68 int added_count_; | |
69 int removed_count_; | |
70 int changed_count_; | |
71 int moved_count_; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(TestShelfModelObserver); | |
74 }; | |
75 | |
76 } // namespace | |
77 | |
78 class LauncherModelTest : public testing::Test { | |
79 public: | |
80 LauncherModelTest() {} | |
81 virtual ~LauncherModelTest() {} | |
82 | |
83 virtual void SetUp() { | |
84 model_.reset(new LauncherModel); | |
85 observer_.reset(new TestShelfModelObserver); | |
86 EXPECT_EQ(0, model_->item_count()); | |
87 | |
88 LauncherItem item; | |
89 item.type = TYPE_APP_LIST; | |
90 model_->Add(item); | |
91 EXPECT_EQ(1, model_->item_count()); | |
92 | |
93 model_->AddObserver(observer_.get()); | |
94 } | |
95 | |
96 virtual void TearDown() { | |
97 observer_.reset(); | |
98 model_.reset(); | |
99 } | |
100 | |
101 scoped_ptr<LauncherModel> model_; | |
102 scoped_ptr<TestShelfModelObserver> observer_; | |
103 | |
104 private: | |
105 DISALLOW_COPY_AND_ASSIGN(LauncherModelTest); | |
106 }; | |
107 | |
108 TEST_F(LauncherModelTest, BasicAssertions) { | |
109 // Add an item. | |
110 LauncherItem item; | |
111 item.type = TYPE_APP_SHORTCUT; | |
112 int index = model_->Add(item); | |
113 EXPECT_EQ(2, model_->item_count()); | |
114 EXPECT_EQ("added=1", observer_->StateStringAndClear()); | |
115 | |
116 // Change to a platform app item. | |
117 LauncherID original_id = model_->items()[index].id; | |
118 item.type = TYPE_PLATFORM_APP; | |
119 model_->Set(index, item); | |
120 EXPECT_EQ(original_id, model_->items()[index].id); | |
121 EXPECT_EQ("changed=1", observer_->StateStringAndClear()); | |
122 EXPECT_EQ(TYPE_PLATFORM_APP, model_->items()[index].type); | |
123 | |
124 // Remove the item. | |
125 model_->RemoveItemAt(index); | |
126 EXPECT_EQ(1, model_->item_count()); | |
127 EXPECT_EQ("removed=1", observer_->StateStringAndClear()); | |
128 | |
129 // Add an app item. | |
130 item.type = TYPE_APP_SHORTCUT; | |
131 index = model_->Add(item); | |
132 observer_->StateStringAndClear(); | |
133 | |
134 // Change everything. | |
135 model_->Set(index, item); | |
136 EXPECT_EQ("changed=1", observer_->StateStringAndClear()); | |
137 EXPECT_EQ(TYPE_APP_SHORTCUT, model_->items()[index].type); | |
138 | |
139 // Add another item. | |
140 item.type = TYPE_APP_SHORTCUT; | |
141 model_->Add(item); | |
142 observer_->StateStringAndClear(); | |
143 | |
144 // Move the second to the first. | |
145 model_->Move(1, 0); | |
146 EXPECT_EQ("moved=1", observer_->StateStringAndClear()); | |
147 | |
148 // And back. | |
149 model_->Move(0, 1); | |
150 EXPECT_EQ("moved=1", observer_->StateStringAndClear()); | |
151 | |
152 // Verifies all the items get unique ids. | |
153 std::set<LauncherID> ids; | |
154 for (int i = 0; i < model_->item_count(); ++i) | |
155 ids.insert(model_->items()[i].id); | |
156 EXPECT_EQ(model_->item_count(), static_cast<int>(ids.size())); | |
157 } | |
158 | |
159 // Assertions around where items are added. | |
160 TEST_F(LauncherModelTest, AddIndices) { | |
161 // Insert browser short cut at index 1. | |
162 LauncherItem browser_shortcut; | |
163 browser_shortcut.type = TYPE_BROWSER_SHORTCUT; | |
164 int browser_shortcut_index = model_->Add(browser_shortcut); | |
165 EXPECT_EQ(1, browser_shortcut_index); | |
166 | |
167 // platform app items should be after browser shortcut. | |
168 LauncherItem item; | |
169 item.type = TYPE_PLATFORM_APP; | |
170 int platform_app_index1 = model_->Add(item); | |
171 EXPECT_EQ(2, platform_app_index1); | |
172 | |
173 // Add another platform app item, it should follow first. | |
174 int platform_app_index2 = model_->Add(item); | |
175 EXPECT_EQ(3, platform_app_index2); | |
176 | |
177 // APP_SHORTCUT priority is higher than PLATFORM_APP but same as | |
178 // BROWSER_SHORTCUT. So APP_SHORTCUT is located after BROWSER_SHORCUT. | |
179 item.type = TYPE_APP_SHORTCUT; | |
180 int app_shortcut_index1 = model_->Add(item); | |
181 EXPECT_EQ(2, app_shortcut_index1); | |
182 | |
183 item.type = TYPE_APP_SHORTCUT; | |
184 int app_shortcut_index2 = model_->Add(item); | |
185 EXPECT_EQ(3, app_shortcut_index2); | |
186 | |
187 // Check that AddAt() figures out the correct indexes for app shortcuts. | |
188 // APP_SHORTCUT and BROWSER_SHORTCUT has the same weight. | |
189 // So APP_SHORTCUT is located at index 0. And, BROWSER_SHORTCUT is located at | |
190 // index 1. | |
191 item.type = TYPE_APP_SHORTCUT; | |
192 int app_shortcut_index3 = model_->AddAt(1, item); | |
193 EXPECT_EQ(1, app_shortcut_index3); | |
194 | |
195 item.type = TYPE_APP_SHORTCUT; | |
196 int app_shortcut_index4 = model_->AddAt(6, item); | |
197 EXPECT_EQ(5, app_shortcut_index4); | |
198 | |
199 item.type = TYPE_APP_SHORTCUT; | |
200 int app_shortcut_index5 = model_->AddAt(3, item); | |
201 EXPECT_EQ(3, app_shortcut_index5); | |
202 | |
203 // Before there are any panels, no icons should be right aligned. | |
204 EXPECT_EQ(model_->item_count(), model_->FirstPanelIndex()); | |
205 | |
206 // Check that AddAt() figures out the correct indexes for platform apps and | |
207 // panels. | |
208 item.type = TYPE_PLATFORM_APP; | |
209 int platform_app_index3 = model_->AddAt(3, item); | |
210 EXPECT_EQ(7, platform_app_index3); | |
211 | |
212 item.type = TYPE_APP_PANEL; | |
213 int app_panel_index1 = model_->AddAt(2, item); | |
214 EXPECT_EQ(10, app_panel_index1); | |
215 | |
216 item.type = TYPE_PLATFORM_APP; | |
217 int platform_app_index4 = model_->AddAt(11, item); | |
218 EXPECT_EQ(10, platform_app_index4); | |
219 | |
220 item.type = TYPE_APP_PANEL; | |
221 int app_panel_index2 = model_->AddAt(12, item); | |
222 EXPECT_EQ(12, app_panel_index2); | |
223 | |
224 item.type = TYPE_PLATFORM_APP; | |
225 int platform_app_index5 = model_->AddAt(7, item); | |
226 EXPECT_EQ(7, platform_app_index5); | |
227 | |
228 item.type = TYPE_APP_PANEL; | |
229 int app_panel_index3 = model_->AddAt(13, item); | |
230 EXPECT_EQ(13, app_panel_index3); | |
231 | |
232 // Right aligned index should be the first app panel index. | |
233 EXPECT_EQ(12, model_->FirstPanelIndex()); | |
234 | |
235 EXPECT_EQ(TYPE_BROWSER_SHORTCUT, model_->items()[2].type); | |
236 EXPECT_EQ(TYPE_APP_LIST, model_->items()[0].type); | |
237 } | |
238 | |
239 // Assertions around where items are added. | |
240 TEST_F(LauncherModelTest, AddIndicesForLegacyShelfLayout) { | |
241 CommandLine::ForCurrentProcess()->AppendSwitch( | |
242 ash::switches::kAshDisableAlternateShelfLayout); | |
243 | |
244 // Insert browser short cut at index 0. | |
245 LauncherItem browser_shortcut; | |
246 browser_shortcut.type = TYPE_BROWSER_SHORTCUT; | |
247 int browser_shortcut_index = model_->Add(browser_shortcut); | |
248 EXPECT_EQ(0, browser_shortcut_index); | |
249 | |
250 // platform app items should be after browser shortcut. | |
251 LauncherItem item; | |
252 item.type = TYPE_PLATFORM_APP; | |
253 int platform_app_index1 = model_->Add(item); | |
254 EXPECT_EQ(1, platform_app_index1); | |
255 | |
256 // Add another platform app item, it should follow first. | |
257 int platform_app_index2 = model_->Add(item); | |
258 EXPECT_EQ(2, platform_app_index2); | |
259 | |
260 // APP_SHORTCUT priority is higher than PLATFORM_APP but same as | |
261 // BROWSER_SHORTCUT. So APP_SHORTCUT is located after BROWSER_SHORCUT. | |
262 item.type = TYPE_APP_SHORTCUT; | |
263 int app_shortcut_index1 = model_->Add(item); | |
264 EXPECT_EQ(1, app_shortcut_index1); | |
265 | |
266 item.type = TYPE_APP_SHORTCUT; | |
267 int app_shortcut_index2 = model_->Add(item); | |
268 EXPECT_EQ(2, app_shortcut_index2); | |
269 | |
270 // Check that AddAt() figures out the correct indexes for app shortcuts. | |
271 // APP_SHORTCUT and BROWSER_SHORTCUT has the same weight. | |
272 // So APP_SHORTCUT is located at index 0. And, BROWSER_SHORTCUT is located at | |
273 // index 1. | |
274 item.type = TYPE_APP_SHORTCUT; | |
275 int app_shortcut_index3 = model_->AddAt(0, item); | |
276 EXPECT_EQ(0, app_shortcut_index3); | |
277 | |
278 item.type = TYPE_APP_SHORTCUT; | |
279 int app_shortcut_index4 = model_->AddAt(5, item); | |
280 EXPECT_EQ(4, app_shortcut_index4); | |
281 | |
282 item.type = TYPE_APP_SHORTCUT; | |
283 int app_shortcut_index5 = model_->AddAt(2, item); | |
284 EXPECT_EQ(2, app_shortcut_index5); | |
285 | |
286 // Before there are any panels, no icons should be right aligned. | |
287 EXPECT_EQ(model_->item_count(), model_->FirstPanelIndex()); | |
288 | |
289 // Check that AddAt() figures out the correct indexes for platform apps and | |
290 // panels. | |
291 item.type = TYPE_PLATFORM_APP; | |
292 int platform_app_index3 = model_->AddAt(2, item); | |
293 EXPECT_EQ(6, platform_app_index3); | |
294 | |
295 item.type = TYPE_APP_PANEL; | |
296 int app_panel_index1 = model_->AddAt(2, item); | |
297 EXPECT_EQ(10, app_panel_index1); | |
298 | |
299 item.type = TYPE_PLATFORM_APP; | |
300 int platform_app_index4 = model_->AddAt(11, item); | |
301 EXPECT_EQ(9, platform_app_index4); | |
302 | |
303 item.type = TYPE_APP_PANEL; | |
304 int app_panel_index2 = model_->AddAt(12, item); | |
305 EXPECT_EQ(12, app_panel_index2); | |
306 | |
307 item.type = TYPE_PLATFORM_APP; | |
308 int platform_app_index5 = model_->AddAt(7, item); | |
309 EXPECT_EQ(7, platform_app_index5); | |
310 | |
311 item.type = TYPE_APP_PANEL; | |
312 int app_panel_index3 = model_->AddAt(13, item); | |
313 EXPECT_EQ(13, app_panel_index3); | |
314 | |
315 // Right aligned index should be the first app panel index. | |
316 EXPECT_EQ(12, model_->FirstPanelIndex()); | |
317 | |
318 EXPECT_EQ(TYPE_BROWSER_SHORTCUT, model_->items()[1].type); | |
319 EXPECT_EQ(TYPE_APP_LIST, model_->items()[model_->FirstPanelIndex() - 1].type); | |
320 } | |
321 | |
322 // Assertions around id generation and usage. | |
323 TEST_F(LauncherModelTest, LauncherIDTests) { | |
324 // Get the next to use ID counter. | |
325 LauncherID id = model_->next_id(); | |
326 | |
327 // Calling this function multiple times does not change the returned ID. | |
328 EXPECT_EQ(model_->next_id(), id); | |
329 | |
330 // Check that when we reserve a value it will be the previously retrieved ID, | |
331 // but it will not change the item count and retrieving the next ID should | |
332 // produce something new. | |
333 EXPECT_EQ(model_->reserve_external_id(), id); | |
334 EXPECT_EQ(1, model_->item_count()); | |
335 LauncherID id2 = model_->next_id(); | |
336 EXPECT_NE(id2, id); | |
337 | |
338 // Adding another item to the list should also produce a new ID. | |
339 LauncherItem item; | |
340 item.type = TYPE_PLATFORM_APP; | |
341 model_->Add(item); | |
342 EXPECT_NE(model_->next_id(), id2); | |
343 } | |
344 | |
345 // This verifies that converting an existing item into a lower weight category | |
346 // (e.g. shortcut to running but not pinned app) will move it to the proper | |
347 // location. See crbug.com/248769. | |
348 TEST_F(LauncherModelTest, CorrectMoveItemsWhenStateChange) { | |
349 // The first item is the app list and last item is the browser. | |
350 LauncherItem browser_shortcut; | |
351 browser_shortcut.type = TYPE_BROWSER_SHORTCUT; | |
352 int browser_shortcut_index = model_->Add(browser_shortcut); | |
353 EXPECT_EQ(TYPE_APP_LIST, model_->items()[0].type); | |
354 EXPECT_EQ(1, browser_shortcut_index); | |
355 | |
356 // Add three shortcuts. They should all be moved between the two. | |
357 LauncherItem item; | |
358 item.type = TYPE_APP_SHORTCUT; | |
359 int app1_index = model_->Add(item); | |
360 EXPECT_EQ(2, app1_index); | |
361 int app2_index = model_->Add(item); | |
362 EXPECT_EQ(3, app2_index); | |
363 int app3_index = model_->Add(item); | |
364 EXPECT_EQ(4, app3_index); | |
365 | |
366 // Now change the type of the second item and make sure that it is moving | |
367 // behind the shortcuts. | |
368 item.type = TYPE_PLATFORM_APP; | |
369 model_->Set(app2_index, item); | |
370 | |
371 // The item should have moved in front of the app launcher. | |
372 EXPECT_EQ(TYPE_PLATFORM_APP, model_->items()[4].type); | |
373 } | |
374 | |
375 TEST_F(LauncherModelTest, CorrectMoveItemsWhenStateChangeForLegacyShelfLayout) { | |
376 CommandLine::ForCurrentProcess()->AppendSwitch( | |
377 ash::switches::kAshDisableAlternateShelfLayout); | |
378 | |
379 // The first item is the browser and the second item is app list. | |
380 LauncherItem browser_shortcut; | |
381 browser_shortcut.type = TYPE_BROWSER_SHORTCUT; | |
382 int browser_shortcut_index = model_->Add(browser_shortcut); | |
383 EXPECT_EQ(0, browser_shortcut_index); | |
384 EXPECT_EQ(TYPE_APP_LIST, model_->items()[1].type); | |
385 | |
386 // Add three shortcuts. They should all be moved between the two. | |
387 LauncherItem item; | |
388 item.type = TYPE_APP_SHORTCUT; | |
389 int app1_index = model_->Add(item); | |
390 EXPECT_EQ(1, app1_index); | |
391 int app2_index = model_->Add(item); | |
392 EXPECT_EQ(2, app2_index); | |
393 int app3_index = model_->Add(item); | |
394 EXPECT_EQ(3, app3_index); | |
395 | |
396 // Now change the type of the second item and make sure that it is moving | |
397 // behind the shortcuts. | |
398 item.type = TYPE_PLATFORM_APP; | |
399 model_->Set(app2_index, item); | |
400 | |
401 // The item should have moved in front of the app launcher. | |
402 EXPECT_EQ(TYPE_PLATFORM_APP, model_->items()[3].type); | |
403 } | |
404 | |
405 } // namespace ash | |
OLD | NEW |