| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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/shelf/shelf_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 void ShelfItemAdded(int index) override { added_count_++; } | |
| 44 void ShelfItemRemoved(int index, ShelfID id) override { removed_count_++; } | |
| 45 void ShelfItemChanged(int index, const ShelfItem& old_item) override { | |
| 46 changed_count_++; | |
| 47 } | |
| 48 void ShelfItemMoved(int start_index, int target_index) override { | |
| 49 moved_count_++; | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 void AddToResult(const std::string& format, int count, std::string* result) { | |
| 54 if (!count) | |
| 55 return; | |
| 56 if (!result->empty()) | |
| 57 *result += " "; | |
| 58 *result += base::StringPrintf(format.c_str(), count); | |
| 59 } | |
| 60 | |
| 61 int added_count_; | |
| 62 int removed_count_; | |
| 63 int changed_count_; | |
| 64 int moved_count_; | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(TestShelfModelObserver); | |
| 67 }; | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 class ShelfModelTest : public testing::Test { | |
| 72 public: | |
| 73 ShelfModelTest() {} | |
| 74 ~ShelfModelTest() override {} | |
| 75 | |
| 76 void SetUp() override { | |
| 77 model_.reset(new ShelfModel); | |
| 78 observer_.reset(new TestShelfModelObserver); | |
| 79 EXPECT_EQ(0, model_->item_count()); | |
| 80 | |
| 81 ShelfItem item; | |
| 82 item.type = TYPE_APP_LIST; | |
| 83 model_->Add(item); | |
| 84 EXPECT_EQ(1, model_->item_count()); | |
| 85 | |
| 86 model_->AddObserver(observer_.get()); | |
| 87 } | |
| 88 | |
| 89 void TearDown() override { | |
| 90 observer_.reset(); | |
| 91 model_.reset(); | |
| 92 } | |
| 93 | |
| 94 std::unique_ptr<ShelfModel> model_; | |
| 95 std::unique_ptr<TestShelfModelObserver> observer_; | |
| 96 | |
| 97 private: | |
| 98 DISALLOW_COPY_AND_ASSIGN(ShelfModelTest); | |
| 99 }; | |
| 100 | |
| 101 TEST_F(ShelfModelTest, BasicAssertions) { | |
| 102 // Add an item. | |
| 103 ShelfItem item; | |
| 104 item.type = TYPE_APP_SHORTCUT; | |
| 105 int index = model_->Add(item); | |
| 106 EXPECT_EQ(2, model_->item_count()); | |
| 107 EXPECT_EQ("added=1", observer_->StateStringAndClear()); | |
| 108 | |
| 109 // Change to a platform app item. | |
| 110 ShelfID original_id = model_->items()[index].id; | |
| 111 item.type = TYPE_PLATFORM_APP; | |
| 112 model_->Set(index, item); | |
| 113 EXPECT_EQ(original_id, model_->items()[index].id); | |
| 114 EXPECT_EQ("changed=1", observer_->StateStringAndClear()); | |
| 115 EXPECT_EQ(TYPE_PLATFORM_APP, model_->items()[index].type); | |
| 116 | |
| 117 // Remove the item. | |
| 118 model_->RemoveItemAt(index); | |
| 119 EXPECT_EQ(1, model_->item_count()); | |
| 120 EXPECT_EQ("removed=1", observer_->StateStringAndClear()); | |
| 121 | |
| 122 // Add an app item. | |
| 123 item.type = TYPE_APP_SHORTCUT; | |
| 124 index = model_->Add(item); | |
| 125 observer_->StateStringAndClear(); | |
| 126 | |
| 127 // Change everything. | |
| 128 model_->Set(index, item); | |
| 129 EXPECT_EQ("changed=1", observer_->StateStringAndClear()); | |
| 130 EXPECT_EQ(TYPE_APP_SHORTCUT, model_->items()[index].type); | |
| 131 | |
| 132 // Add another item. | |
| 133 item.type = TYPE_APP_SHORTCUT; | |
| 134 model_->Add(item); | |
| 135 observer_->StateStringAndClear(); | |
| 136 | |
| 137 // Move the second to the first. | |
| 138 model_->Move(1, 0); | |
| 139 EXPECT_EQ("moved=1", observer_->StateStringAndClear()); | |
| 140 | |
| 141 // And back. | |
| 142 model_->Move(0, 1); | |
| 143 EXPECT_EQ("moved=1", observer_->StateStringAndClear()); | |
| 144 | |
| 145 // Verifies all the items get unique ids. | |
| 146 std::set<ShelfID> ids; | |
| 147 for (int i = 0; i < model_->item_count(); ++i) | |
| 148 ids.insert(model_->items()[i].id); | |
| 149 EXPECT_EQ(model_->item_count(), static_cast<int>(ids.size())); | |
| 150 } | |
| 151 | |
| 152 // Assertions around where items are added. | |
| 153 TEST_F(ShelfModelTest, AddIndices) { | |
| 154 // Insert browser short cut at index 1. | |
| 155 ShelfItem browser_shortcut; | |
| 156 browser_shortcut.type = TYPE_BROWSER_SHORTCUT; | |
| 157 int browser_shortcut_index = model_->Add(browser_shortcut); | |
| 158 EXPECT_EQ(1, browser_shortcut_index); | |
| 159 | |
| 160 // platform app items should be after browser shortcut. | |
| 161 ShelfItem item; | |
| 162 item.type = TYPE_PLATFORM_APP; | |
| 163 int platform_app_index1 = model_->Add(item); | |
| 164 EXPECT_EQ(2, platform_app_index1); | |
| 165 | |
| 166 // Add another platform app item, it should follow first. | |
| 167 int platform_app_index2 = model_->Add(item); | |
| 168 EXPECT_EQ(3, platform_app_index2); | |
| 169 | |
| 170 // APP_SHORTCUT priority is higher than PLATFORM_APP but same as | |
| 171 // BROWSER_SHORTCUT. So APP_SHORTCUT is located after BROWSER_SHORCUT. | |
| 172 item.type = TYPE_APP_SHORTCUT; | |
| 173 int app_shortcut_index1 = model_->Add(item); | |
| 174 EXPECT_EQ(2, app_shortcut_index1); | |
| 175 | |
| 176 item.type = TYPE_APP_SHORTCUT; | |
| 177 int app_shortcut_index2 = model_->Add(item); | |
| 178 EXPECT_EQ(3, app_shortcut_index2); | |
| 179 | |
| 180 // Check that AddAt() figures out the correct indexes for app shortcuts. | |
| 181 // APP_SHORTCUT and BROWSER_SHORTCUT has the same weight. | |
| 182 // So APP_SHORTCUT is located at index 0. And, BROWSER_SHORTCUT is located at | |
| 183 // index 1. | |
| 184 item.type = TYPE_APP_SHORTCUT; | |
| 185 int app_shortcut_index3 = model_->AddAt(1, item); | |
| 186 EXPECT_EQ(1, app_shortcut_index3); | |
| 187 | |
| 188 item.type = TYPE_APP_SHORTCUT; | |
| 189 int app_shortcut_index4 = model_->AddAt(6, item); | |
| 190 EXPECT_EQ(5, app_shortcut_index4); | |
| 191 | |
| 192 item.type = TYPE_APP_SHORTCUT; | |
| 193 int app_shortcut_index5 = model_->AddAt(3, item); | |
| 194 EXPECT_EQ(3, app_shortcut_index5); | |
| 195 | |
| 196 // Before there are any panels, no icons should be right aligned. | |
| 197 EXPECT_EQ(model_->item_count(), model_->FirstPanelIndex()); | |
| 198 | |
| 199 // Check that AddAt() figures out the correct indexes for platform apps and | |
| 200 // panels. | |
| 201 item.type = TYPE_PLATFORM_APP; | |
| 202 int platform_app_index3 = model_->AddAt(3, item); | |
| 203 EXPECT_EQ(7, platform_app_index3); | |
| 204 | |
| 205 item.type = TYPE_APP_PANEL; | |
| 206 int app_panel_index1 = model_->AddAt(2, item); | |
| 207 EXPECT_EQ(10, app_panel_index1); | |
| 208 | |
| 209 item.type = TYPE_PLATFORM_APP; | |
| 210 int platform_app_index4 = model_->AddAt(11, item); | |
| 211 EXPECT_EQ(10, platform_app_index4); | |
| 212 | |
| 213 item.type = TYPE_APP_PANEL; | |
| 214 int app_panel_index2 = model_->AddAt(12, item); | |
| 215 EXPECT_EQ(12, app_panel_index2); | |
| 216 | |
| 217 item.type = TYPE_PLATFORM_APP; | |
| 218 int platform_app_index5 = model_->AddAt(7, item); | |
| 219 EXPECT_EQ(7, platform_app_index5); | |
| 220 | |
| 221 item.type = TYPE_APP_PANEL; | |
| 222 int app_panel_index3 = model_->AddAt(13, item); | |
| 223 EXPECT_EQ(13, app_panel_index3); | |
| 224 | |
| 225 // Right aligned index should be the first app panel index. | |
| 226 EXPECT_EQ(12, model_->FirstPanelIndex()); | |
| 227 | |
| 228 EXPECT_EQ(TYPE_BROWSER_SHORTCUT, model_->items()[2].type); | |
| 229 EXPECT_EQ(TYPE_APP_LIST, model_->items()[0].type); | |
| 230 } | |
| 231 | |
| 232 // Test that the indexes for the running applications are properly determined | |
| 233 // when the first running application is a windowed application. | |
| 234 TEST_F(ShelfModelTest, FirstRunningAppIndexUsingWindowedAppFirst) { | |
| 235 // Insert the browser shortcut at index 1 and check that the running | |
| 236 // application index would be behind it. | |
| 237 ShelfItem item; | |
| 238 item.type = TYPE_BROWSER_SHORTCUT; | |
| 239 EXPECT_EQ(1, model_->Add(item)); | |
| 240 EXPECT_EQ(2, model_->FirstRunningAppIndex()); | |
| 241 | |
| 242 // Insert a panel application at the end and check that the running | |
| 243 // application index would be at / before the application panel. | |
| 244 item.type = TYPE_APP_PANEL; | |
| 245 EXPECT_EQ(2, model_->Add(item)); | |
| 246 EXPECT_EQ(2, model_->FirstRunningAppIndex()); | |
| 247 | |
| 248 // Insert an application shortcut and make sure that the running application | |
| 249 // index would be behind it. | |
| 250 item.type = TYPE_APP_SHORTCUT; | |
| 251 EXPECT_EQ(2, model_->Add(item)); | |
| 252 EXPECT_EQ(3, model_->FirstRunningAppIndex()); | |
| 253 | |
| 254 // Insert different running application shortcuts - but first a windowed | |
| 255 // application - and make sure that the same index gets returned. | |
| 256 item.type = TYPE_WINDOWED_APP; | |
| 257 int running_app_index = model_->Add(item); | |
| 258 EXPECT_EQ(3, running_app_index); | |
| 259 EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex()); | |
| 260 item.type = TYPE_PLATFORM_APP; | |
| 261 EXPECT_EQ(running_app_index + 1, model_->Add(item)); | |
| 262 EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex()); | |
| 263 } | |
| 264 | |
| 265 // Test that the indexes for the running applications are properly determined | |
| 266 // when the first running application is a platform application. | |
| 267 TEST_F(ShelfModelTest, FirstRunningAppIndexUsingPlatformAppFirst) { | |
| 268 // Insert the browser shortcut at index 1 and check that the running | |
| 269 // application index would be behind it. | |
| 270 ShelfItem item; | |
| 271 item.type = TYPE_BROWSER_SHORTCUT; | |
| 272 EXPECT_EQ(1, model_->Add(item)); | |
| 273 EXPECT_EQ(2, model_->FirstRunningAppIndex()); | |
| 274 | |
| 275 // Insert a panel application at the end and check that the running | |
| 276 // application index would be at / before the application panel. | |
| 277 item.type = TYPE_APP_PANEL; | |
| 278 EXPECT_EQ(2, model_->Add(item)); | |
| 279 EXPECT_EQ(2, model_->FirstRunningAppIndex()); | |
| 280 | |
| 281 // Insert an application shortcut and make sure that the running application | |
| 282 // index would be behind it. | |
| 283 item.type = TYPE_APP_SHORTCUT; | |
| 284 EXPECT_EQ(2, model_->Add(item)); | |
| 285 EXPECT_EQ(3, model_->FirstRunningAppIndex()); | |
| 286 | |
| 287 // Insert different running application shortcuts - but first a platfom | |
| 288 // application - and make sure that the same index gets returned. | |
| 289 item.type = TYPE_PLATFORM_APP; | |
| 290 int running_app_index = model_->Add(item); | |
| 291 EXPECT_EQ(3, running_app_index); | |
| 292 EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex()); | |
| 293 item.type = TYPE_WINDOWED_APP; | |
| 294 EXPECT_EQ(running_app_index + 1, model_->Add(item)); | |
| 295 EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex()); | |
| 296 } | |
| 297 | |
| 298 // Assertions around id generation and usage. | |
| 299 TEST_F(ShelfModelTest, ShelfIDTests) { | |
| 300 // Get the next to use ID counter. | |
| 301 ShelfID id = model_->next_id(); | |
| 302 | |
| 303 // Calling this function multiple times does not change the returned ID. | |
| 304 EXPECT_EQ(model_->next_id(), id); | |
| 305 | |
| 306 // Check that when we reserve a value it will be the previously retrieved ID, | |
| 307 // but it will not change the item count and retrieving the next ID should | |
| 308 // produce something new. | |
| 309 EXPECT_EQ(model_->reserve_external_id(), id); | |
| 310 EXPECT_EQ(1, model_->item_count()); | |
| 311 ShelfID id2 = model_->next_id(); | |
| 312 EXPECT_NE(id2, id); | |
| 313 | |
| 314 // Adding another item to the list should also produce a new ID. | |
| 315 ShelfItem item; | |
| 316 item.type = TYPE_PLATFORM_APP; | |
| 317 model_->Add(item); | |
| 318 EXPECT_NE(model_->next_id(), id2); | |
| 319 } | |
| 320 | |
| 321 // This verifies that converting an existing item into a lower weight category | |
| 322 // (e.g. shortcut to running but not pinned app) will move it to the proper | |
| 323 // location. See crbug.com/248769. | |
| 324 TEST_F(ShelfModelTest, CorrectMoveItemsWhenStateChange) { | |
| 325 // The first item is the app list and last item is the browser. | |
| 326 ShelfItem browser_shortcut; | |
| 327 browser_shortcut.type = TYPE_BROWSER_SHORTCUT; | |
| 328 int browser_shortcut_index = model_->Add(browser_shortcut); | |
| 329 EXPECT_EQ(TYPE_APP_LIST, model_->items()[0].type); | |
| 330 EXPECT_EQ(1, browser_shortcut_index); | |
| 331 | |
| 332 // Add three shortcuts. They should all be moved between the two. | |
| 333 ShelfItem item; | |
| 334 item.type = TYPE_APP_SHORTCUT; | |
| 335 int app1_index = model_->Add(item); | |
| 336 EXPECT_EQ(2, app1_index); | |
| 337 int app2_index = model_->Add(item); | |
| 338 EXPECT_EQ(3, app2_index); | |
| 339 int app3_index = model_->Add(item); | |
| 340 EXPECT_EQ(4, app3_index); | |
| 341 | |
| 342 // Now change the type of the second item and make sure that it is moving | |
| 343 // behind the shortcuts. | |
| 344 item.type = TYPE_PLATFORM_APP; | |
| 345 model_->Set(app2_index, item); | |
| 346 | |
| 347 // The item should have moved in front of the app launcher. | |
| 348 EXPECT_EQ(TYPE_PLATFORM_APP, model_->items()[4].type); | |
| 349 } | |
| 350 | |
| 351 } // namespace ash | |
| OLD | NEW |