| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/shelf/shelf_model.h" | 5 #include "ash/shelf/shelf_model.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "ash/shelf/shelf_model_observer.h" | 10 #include "ash/shelf/shelf_model_observer.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 DISALLOW_COPY_AND_ASSIGN(ShelfModelTest); | 90 DISALLOW_COPY_AND_ASSIGN(ShelfModelTest); |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 TEST_F(ShelfModelTest, BasicAssertions) { | 93 TEST_F(ShelfModelTest, BasicAssertions) { |
| 94 // Add an item. | 94 // Add an item. |
| 95 ShelfItem item1; | 95 ShelfItem item1; |
| 96 item1.id = ShelfID("item1"); | 96 item1.id = ShelfID("item1"); |
| 97 item1.type = TYPE_PINNED_APP; | 97 item1.type = TYPE_PINNED_APP; |
| 98 int index = model_->Add(item1); | 98 int index = model_->Add(item1); |
| 99 EXPECT_EQ(2, model_->item_count()); | 99 EXPECT_EQ(2, model_->item_count()); |
| 100 EXPECT_LE(0, model_->ItemIndexByID(item1.id)); |
| 101 EXPECT_NE(model_->items().end(), model_->ItemByID(item1.id)); |
| 100 EXPECT_EQ("added=1", observer_->StateStringAndClear()); | 102 EXPECT_EQ("added=1", observer_->StateStringAndClear()); |
| 101 | 103 |
| 102 // Change to a platform app item. | 104 // Change to a platform app item. |
| 103 ShelfID original_id = model_->items()[index].id; | |
| 104 item1.type = TYPE_APP; | 105 item1.type = TYPE_APP; |
| 105 model_->Set(index, item1); | 106 model_->Set(index, item1); |
| 106 EXPECT_EQ(original_id, model_->items()[index].id); | 107 EXPECT_EQ(item1.id, model_->items()[index].id); |
| 108 EXPECT_LE(0, model_->ItemIndexByID(item1.id)); |
| 109 EXPECT_NE(model_->items().end(), model_->ItemByID(item1.id)); |
| 107 EXPECT_EQ("changed=1", observer_->StateStringAndClear()); | 110 EXPECT_EQ("changed=1", observer_->StateStringAndClear()); |
| 108 EXPECT_EQ(TYPE_APP, model_->items()[index].type); | 111 EXPECT_EQ(TYPE_APP, model_->items()[index].type); |
| 109 | 112 |
| 110 // Remove the item. | 113 // Remove the item. |
| 111 model_->RemoveItemAt(index); | 114 model_->RemoveItemAt(index); |
| 112 EXPECT_EQ(1, model_->item_count()); | 115 EXPECT_EQ(1, model_->item_count()); |
| 116 EXPECT_EQ(-1, model_->ItemIndexByID(item1.id)); |
| 117 EXPECT_EQ(model_->items().end(), model_->ItemByID(item1.id)); |
| 113 EXPECT_EQ("removed=1", observer_->StateStringAndClear()); | 118 EXPECT_EQ("removed=1", observer_->StateStringAndClear()); |
| 114 | 119 |
| 115 // Add an app item. | 120 // Add an app item. |
| 116 ShelfItem item2; | 121 ShelfItem item2; |
| 117 item2.id = ShelfID("item2"); | 122 item2.id = ShelfID("item2"); |
| 118 item2.type = TYPE_PINNED_APP; | 123 item2.type = TYPE_PINNED_APP; |
| 119 index = model_->Add(item2); | 124 index = model_->Add(item2); |
| 120 observer_->StateStringAndClear(); | 125 EXPECT_EQ(2, model_->item_count()); |
| 126 EXPECT_LE(0, model_->ItemIndexByID(item2.id)); |
| 127 EXPECT_NE(model_->items().end(), model_->ItemByID(item2.id)); |
| 128 EXPECT_EQ("added=1", observer_->StateStringAndClear()); |
| 121 | 129 |
| 122 // Change the item type. | 130 // Change the item type. |
| 123 item2.type = TYPE_APP; | 131 item2.type = TYPE_APP; |
| 124 model_->Set(index, item2); | 132 model_->Set(index, item2); |
| 133 EXPECT_LE(0, model_->ItemIndexByID(item2.id)); |
| 134 EXPECT_NE(model_->items().end(), model_->ItemByID(item2.id)); |
| 125 EXPECT_EQ("changed=1", observer_->StateStringAndClear()); | 135 EXPECT_EQ("changed=1", observer_->StateStringAndClear()); |
| 126 EXPECT_EQ(TYPE_APP, model_->items()[index].type); | 136 EXPECT_EQ(TYPE_APP, model_->items()[index].type); |
| 127 | 137 |
| 128 // Add another item. | 138 // Add another item. |
| 129 ShelfItem item3; | 139 ShelfItem item3; |
| 130 item3.id = ShelfID("item3"); | 140 item3.id = ShelfID("item3"); |
| 131 item3.type = TYPE_PINNED_APP; | 141 item3.type = TYPE_PINNED_APP; |
| 132 model_->Add(item3); | 142 model_->Add(item3); |
| 133 observer_->StateStringAndClear(); | 143 EXPECT_EQ(3, model_->item_count()); |
| 144 EXPECT_LE(0, model_->ItemIndexByID(item3.id)); |
| 145 EXPECT_NE(model_->items().end(), model_->ItemByID(item3.id)); |
| 146 EXPECT_EQ("added=1", observer_->StateStringAndClear()); |
| 134 | 147 |
| 135 // Move the second to the first. | 148 // Move the second to the first. |
| 136 model_->Move(1, 0); | 149 model_->Move(1, 0); |
| 137 EXPECT_EQ("moved=1", observer_->StateStringAndClear()); | 150 EXPECT_EQ("moved=1", observer_->StateStringAndClear()); |
| 138 | 151 |
| 139 // And back. | 152 // And back. |
| 140 model_->Move(0, 1); | 153 model_->Move(0, 1); |
| 141 EXPECT_EQ("moved=1", observer_->StateStringAndClear()); | 154 EXPECT_EQ("moved=1", observer_->StateStringAndClear()); |
| 142 | 155 |
| 143 // Verifies all the items get unique ids. | 156 // Verifies all the items get unique ids. |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 int app3_index = model_->Add(item3); | 311 int app3_index = model_->Add(item3); |
| 299 EXPECT_EQ(3, app3_index); | 312 EXPECT_EQ(3, app3_index); |
| 300 | 313 |
| 301 // Unpinning an item moves it behind the shortcuts. | 314 // Unpinning an item moves it behind the shortcuts. |
| 302 EXPECT_EQ(item3.id, model_->items()[3].id); | 315 EXPECT_EQ(item3.id, model_->items()[3].id); |
| 303 item2.type = TYPE_APP; | 316 item2.type = TYPE_APP; |
| 304 model_->Set(app2_index, item2); | 317 model_->Set(app2_index, item2); |
| 305 EXPECT_EQ(item2.id, model_->items()[3].id); | 318 EXPECT_EQ(item2.id, model_->items()[3].id); |
| 306 } | 319 } |
| 307 | 320 |
| 308 // Test conversion between ShelfID and application [launch] ids. | 321 // Test getting the index of ShelfIDs as a check for item presence. |
| 309 TEST_F(ShelfModelTest, IdentifierConversion) { | 322 TEST_F(ShelfModelTest, ItemIndexByID) { |
| 310 const std::string app_id1("app_id1"); | 323 // Expect empty and unknown ids to return the invalid index -1. |
| 311 const std::string launch_id("launch_id"); | 324 EXPECT_EQ(-1, model_->ItemIndexByID(ShelfID())); |
| 325 EXPECT_EQ(-1, model_->ItemIndexByID(ShelfID("foo"))); |
| 326 EXPECT_EQ(-1, model_->ItemIndexByID(ShelfID("foo", "bar"))); |
| 312 | 327 |
| 313 // Expect empty ShelfIDs and app ids for input not found in the model. | 328 // Add an item and expect to get a valid index for its id. |
| 314 EXPECT_TRUE(model_->GetShelfIDForAppID(std::string()).IsNull()); | 329 ShelfItem item1; |
| 315 EXPECT_TRUE(model_->GetShelfIDForAppID(app_id1).IsNull()); | 330 item1.type = TYPE_PINNED_APP; |
| 316 EXPECT_TRUE( | 331 item1.id = ShelfID("app_id1", "launch_id1"); |
| 317 model_->GetShelfIDForAppIDAndLaunchID(app_id1, std::string()).IsNull()); | 332 const int index1 = model_->Add(item1); |
| 318 EXPECT_TRUE( | 333 EXPECT_EQ(index1, model_->ItemIndexByID(item1.id)); |
| 319 model_->GetShelfIDForAppIDAndLaunchID(app_id1, launch_id).IsNull()); | |
| 320 EXPECT_TRUE(model_->GetAppIDForShelfID(ShelfID()).empty()); | |
| 321 EXPECT_TRUE(model_->GetAppIDForShelfID(ShelfID("foo")).empty()); | |
| 322 | 334 |
| 323 // Add an example app with an app id and a launch id. | 335 // Add another item and expect to get another valid index for its id. |
| 324 ShelfItem item; | 336 ShelfItem item2; |
| 325 item.type = TYPE_PINNED_APP; | 337 item2.type = TYPE_APP; |
| 326 item.id = ShelfID(app_id1, launch_id); | 338 item2.id = ShelfID("app_id2", "launch_id2"); |
| 327 const int index = model_->Add(item); | 339 const int index2 = model_->Add(item2); |
| 340 EXPECT_EQ(index2, model_->ItemIndexByID(item2.id)); |
| 328 | 341 |
| 329 // Ensure the item ids can be found and converted as expected. | 342 // Removing the first item should yield an invalid index for that item. |
| 330 EXPECT_EQ(item.id, model_->GetShelfIDForAppID(app_id1)); | 343 model_->RemoveItemAt(index1); |
| 331 EXPECT_EQ(item.id, model_->GetShelfIDForAppIDAndLaunchID(app_id1, launch_id)); | 344 EXPECT_EQ(-1, model_->ItemIndexByID(item1.id)); |
| 332 EXPECT_NE(item.id, | 345 // The index of the second item should be decremented, but still valid. |
| 333 model_->GetShelfIDForAppIDAndLaunchID(app_id1, std::string())); | 346 EXPECT_EQ(index2 - 1, model_->ItemIndexByID(item2.id)); |
| 334 EXPECT_EQ(app_id1, model_->GetAppIDForShelfID(item.id)); | 347 EXPECT_LE(0, model_->ItemIndexByID(item2.id)); |
| 335 | |
| 336 // Removing the example app should again yield invalid ids. | |
| 337 model_->RemoveItemAt(index); | |
| 338 EXPECT_TRUE(model_->GetShelfIDForAppID(app_id1).IsNull()); | |
| 339 EXPECT_TRUE( | |
| 340 model_->GetShelfIDForAppIDAndLaunchID(app_id1, launch_id).IsNull()); | |
| 341 EXPECT_TRUE(model_->GetAppIDForShelfID(item.id).empty()); | |
| 342 | |
| 343 // Add an example app with a different app id and no launch id. | |
| 344 const std::string app_id2("app_id2"); | |
| 345 item.id = ShelfID(app_id2); | |
| 346 model_->Add(item); | |
| 347 | |
| 348 // Ensure the item ids can be found and converted as expected. | |
| 349 EXPECT_EQ(item.id, model_->GetShelfIDForAppID(app_id2)); | |
| 350 EXPECT_EQ(item.id, | |
| 351 model_->GetShelfIDForAppIDAndLaunchID(app_id2, std::string())); | |
| 352 EXPECT_NE(item.id, model_->GetShelfIDForAppIDAndLaunchID(app_id2, launch_id)); | |
| 353 EXPECT_EQ(app_id2, model_->GetAppIDForShelfID(item.id)); | |
| 354 } | 348 } |
| 355 | 349 |
| 356 // Test pinning and unpinning a closed app, and checking if it is pinned. | 350 // Test pinning and unpinning a closed app, and checking if it is pinned. |
| 357 TEST_F(ShelfModelTest, ClosedAppPinning) { | 351 TEST_F(ShelfModelTest, ClosedAppPinning) { |
| 358 const std::string app_id("app_id"); | 352 const std::string app_id("app_id"); |
| 359 | 353 |
| 360 // Check the initial state. | 354 // Check the initial state. |
| 361 EXPECT_FALSE(model_->IsAppPinned(app_id)); | 355 EXPECT_FALSE(model_->IsAppPinned(app_id)); |
| 362 EXPECT_EQ(1, model_->item_count()); | 356 EXPECT_EQ(1, model_->item_count()); |
| 363 | 357 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 | 424 |
| 431 // Unpinning the same app id again should have no change. | 425 // Unpinning the same app id again should have no change. |
| 432 model_->UnpinAppWithID(app_id); | 426 model_->UnpinAppWithID(app_id); |
| 433 EXPECT_FALSE(model_->IsAppPinned(app_id)); | 427 EXPECT_FALSE(model_->IsAppPinned(app_id)); |
| 434 EXPECT_EQ(2, model_->item_count()); | 428 EXPECT_EQ(2, model_->item_count()); |
| 435 EXPECT_EQ(TYPE_APP, model_->items()[index].type); | 429 EXPECT_EQ(TYPE_APP, model_->items()[index].type); |
| 436 EXPECT_EQ(item.id, model_->items()[index].id); | 430 EXPECT_EQ(item.id, model_->items()[index].id); |
| 437 } | 431 } |
| 438 | 432 |
| 439 } // namespace ash | 433 } // namespace ash |
| OLD | NEW |