Chromium Code Reviews| 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 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 } | 248 } |
| 249 | 249 |
| 250 // Assertions around id generation and usage. | 250 // Assertions around id generation and usage. |
| 251 TEST_F(ShelfModelTest, ShelfIDTests) { | 251 TEST_F(ShelfModelTest, ShelfIDTests) { |
| 252 // Get the next to use ID counter. | 252 // Get the next to use ID counter. |
| 253 ShelfID id = model_->next_id(); | 253 ShelfID id = model_->next_id(); |
| 254 | 254 |
| 255 // Calling this function multiple times does not change the returned ID. | 255 // Calling this function multiple times does not change the returned ID. |
| 256 EXPECT_EQ(model_->next_id(), id); | 256 EXPECT_EQ(model_->next_id(), id); |
| 257 | 257 |
| 258 // Check that when we reserve a value it will be the previously retrieved ID, | 258 // Adding another item to the list should produce a new ID. |
| 259 // but it will not change the item count and retrieving the next ID should | |
| 260 // produce something new. | |
| 261 EXPECT_EQ(model_->reserve_external_id(), id); | |
| 262 EXPECT_EQ(1, model_->item_count()); | |
| 263 ShelfID id2 = model_->next_id(); | |
| 264 EXPECT_NE(id2, id); | |
| 265 | |
| 266 // Adding another item to the list should also produce a new ID. | |
| 267 ShelfItem item; | 259 ShelfItem item; |
| 268 item.type = TYPE_APP; | 260 item.type = TYPE_APP; |
| 269 model_->Add(item); | 261 model_->Add(item); |
| 270 EXPECT_NE(model_->next_id(), id2); | 262 EXPECT_NE(model_->next_id(), id); |
| 271 } | 263 } |
| 272 | 264 |
| 273 // This verifies that converting an existing item into a lower weight category | 265 // This verifies that converting an existing item into a lower weight category |
| 274 // (e.g. shortcut to running but not pinned app) will move it to the proper | 266 // (e.g. shortcut to running but not pinned app) will move it to the proper |
| 275 // location. See crbug.com/248769. | 267 // location. See crbug.com/248769. |
| 276 TEST_F(ShelfModelTest, CorrectMoveItemsWhenStateChange) { | 268 TEST_F(ShelfModelTest, CorrectMoveItemsWhenStateChange) { |
| 277 // The first item is the app list and last item is the browser. | 269 // The first item is the app list and last item is the browser. |
| 278 ShelfItem browser_shortcut; | 270 ShelfItem browser_shortcut; |
| 279 browser_shortcut.type = TYPE_BROWSER_SHORTCUT; | 271 browser_shortcut.type = TYPE_BROWSER_SHORTCUT; |
| 280 int browser_shortcut_index = model_->Add(browser_shortcut); | 272 int browser_shortcut_index = model_->Add(browser_shortcut); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 293 | 285 |
| 294 // Now change the type of the second item and make sure that it is moving | 286 // Now change the type of the second item and make sure that it is moving |
| 295 // behind the shortcuts. | 287 // behind the shortcuts. |
| 296 item.type = TYPE_APP; | 288 item.type = TYPE_APP; |
| 297 model_->Set(app2_index, item); | 289 model_->Set(app2_index, item); |
| 298 | 290 |
| 299 // The item should have moved in front of the app launcher. | 291 // The item should have moved in front of the app launcher. |
| 300 EXPECT_EQ(TYPE_APP, model_->items()[4].type); | 292 EXPECT_EQ(TYPE_APP, model_->items()[4].type); |
| 301 } | 293 } |
| 302 | 294 |
| 295 // Test conversion between ShelfID and application [launch] ids. | |
| 296 TEST_F(ShelfModelTest, IdentifierConversion) { | |
| 297 const std::string app_id("app_id"); | |
| 298 const std::string launch_id("launch_id"); | |
| 299 const ShelfID unknown_shelf_id = 123; | |
| 300 | |
| 301 // Expect kInvalidShelfID and empty app ids for input not found in the model. | |
| 302 EXPECT_EQ(kInvalidShelfID, model_->GetShelfIDForAppID(std::string())); | |
| 303 EXPECT_EQ(kInvalidShelfID, model_->GetShelfIDForAppID(app_id)); | |
| 304 EXPECT_EQ(kInvalidShelfID, | |
| 305 model_->GetShelfIDForAppIDAndLaunchID(app_id, std::string())); | |
| 306 EXPECT_EQ(kInvalidShelfID, | |
| 307 model_->GetShelfIDForAppIDAndLaunchID(app_id, launch_id)); | |
| 308 EXPECT_TRUE(model_->GetAppIDForShelfID(kInvalidShelfID).empty()); | |
| 309 EXPECT_TRUE(model_->GetAppIDForShelfID(unknown_shelf_id).empty()); | |
| 310 | |
| 311 // Add an example app with an app id and a launch id. | |
| 312 ShelfItem item; | |
| 313 item.type = TYPE_PINNED_APP; | |
| 314 item.app_launch_id = AppLaunchId(app_id, launch_id); | |
| 315 const ShelfID assigned_shelf_id = model_->next_id(); | |
| 316 const int index = model_->Add(item); | |
| 317 | |
| 318 // Ensure the item ids can be found and converted as expected. | |
| 319 EXPECT_NE(kInvalidShelfID, assigned_shelf_id); | |
| 320 EXPECT_EQ(assigned_shelf_id, model_->GetShelfIDForAppID(app_id)); | |
| 321 EXPECT_EQ(assigned_shelf_id, | |
| 322 model_->GetShelfIDForAppIDAndLaunchID(app_id, launch_id)); | |
| 323 EXPECT_EQ(app_id, model_->GetAppIDForShelfID(assigned_shelf_id)); | |
| 324 | |
| 325 // Removing the example app should again yield invalid ids. | |
| 326 model_->RemoveItemAt(index); | |
| 327 EXPECT_EQ(kInvalidShelfID, model_->GetShelfIDForAppID(app_id)); | |
| 328 EXPECT_EQ(kInvalidShelfID, | |
| 329 model_->GetShelfIDForAppIDAndLaunchID(app_id, launch_id)); | |
| 330 EXPECT_TRUE(model_->GetAppIDForShelfID(assigned_shelf_id).empty()); | |
| 331 | |
| 332 // Add an example app with a different app id and no launch id. | |
| 333 const std::string app_id1("app_id1"); | |
|
James Cook
2017/04/19 21:17:54
nit: maybe app_id2 for the second one (and/or app_
msw
2017/04/19 21:33:18
Done.
| |
| 334 item.app_launch_id = AppLaunchId(app_id1); | |
| 335 const ShelfID assigned_shelf_id1 = model_->next_id(); | |
| 336 model_->Add(item); | |
| 337 | |
| 338 // Ensure the item ids can be found and converted as expected. | |
| 339 EXPECT_NE(kInvalidShelfID, assigned_shelf_id1); | |
| 340 EXPECT_NE(assigned_shelf_id, assigned_shelf_id1); | |
| 341 EXPECT_EQ(assigned_shelf_id1, model_->GetShelfIDForAppID(app_id1)); | |
| 342 EXPECT_EQ(assigned_shelf_id1, | |
| 343 model_->GetShelfIDForAppIDAndLaunchID(app_id1, std::string())); | |
| 344 EXPECT_EQ(kInvalidShelfID, | |
| 345 model_->GetShelfIDForAppIDAndLaunchID(app_id1, launch_id)); | |
| 346 EXPECT_EQ(app_id1, model_->GetAppIDForShelfID(assigned_shelf_id1)); | |
| 347 } | |
| 348 | |
| 349 // Test pinning and unpinning a closed app, and checking if it is pinned. | |
| 350 TEST_F(ShelfModelTest, ClosedAppPinning) { | |
| 351 const std::string app_id("app_id"); | |
| 352 | |
| 353 // Check the initial state. | |
| 354 EXPECT_FALSE(model_->IsAppPinned(app_id)); | |
| 355 EXPECT_EQ(1, model_->item_count()); | |
| 356 | |
| 357 // Pinning a previously unknown app should add an item. | |
| 358 model_->PinAppWithID(app_id); | |
| 359 EXPECT_TRUE(model_->IsAppPinned(app_id)); | |
| 360 EXPECT_EQ(2, model_->item_count()); | |
| 361 EXPECT_EQ(TYPE_PINNED_APP, model_->items()[1].type); | |
| 362 EXPECT_EQ(app_id, model_->items()[1].app_launch_id.app_id()); | |
| 363 | |
| 364 // Pinning the same app id again should have no change. | |
| 365 model_->PinAppWithID(app_id); | |
| 366 EXPECT_TRUE(model_->IsAppPinned(app_id)); | |
| 367 EXPECT_EQ(2, model_->item_count()); | |
| 368 EXPECT_EQ(TYPE_PINNED_APP, model_->items()[1].type); | |
| 369 EXPECT_EQ(app_id, model_->items()[1].app_launch_id.app_id()); | |
| 370 | |
| 371 // Unpinning the app should remove the item. | |
| 372 model_->UnpinAppWithID(app_id); | |
| 373 EXPECT_FALSE(model_->IsAppPinned(app_id)); | |
| 374 EXPECT_EQ(1, model_->item_count()); | |
| 375 | |
| 376 // Unpinning the same app id again should have no change. | |
| 377 model_->UnpinAppWithID(app_id); | |
| 378 EXPECT_FALSE(model_->IsAppPinned(app_id)); | |
| 379 EXPECT_EQ(1, model_->item_count()); | |
| 380 } | |
| 381 | |
| 382 // Test pinning and unpinning a running app, and checking if it is pinned. | |
| 383 TEST_F(ShelfModelTest, RunningAppPinning) { | |
| 384 const std::string app_id("app_id"); | |
| 385 | |
| 386 // Check the initial state. | |
| 387 EXPECT_FALSE(model_->IsAppPinned(app_id)); | |
| 388 EXPECT_EQ(1, model_->item_count()); | |
| 389 | |
| 390 // Add an example running app. | |
| 391 ShelfItem item; | |
| 392 item.type = TYPE_APP; | |
| 393 item.status = STATUS_RUNNING; | |
| 394 item.app_launch_id = AppLaunchId(app_id); | |
| 395 const ShelfID assigned_shelf_id = model_->next_id(); | |
| 396 const int index = model_->Add(item); | |
| 397 | |
| 398 // The item should be added but not pinned. | |
| 399 EXPECT_FALSE(model_->IsAppPinned(app_id)); | |
| 400 EXPECT_EQ(2, model_->item_count()); | |
| 401 EXPECT_EQ(TYPE_APP, model_->items()[index].type); | |
| 402 EXPECT_EQ(app_id, model_->items()[index].app_launch_id.app_id()); | |
| 403 EXPECT_EQ(assigned_shelf_id, model_->items()[index].id); | |
| 404 | |
| 405 // Pinning the item should just change its type. | |
| 406 model_->PinAppWithID(app_id); | |
| 407 EXPECT_TRUE(model_->IsAppPinned(app_id)); | |
| 408 EXPECT_EQ(2, model_->item_count()); | |
| 409 EXPECT_EQ(TYPE_PINNED_APP, model_->items()[index].type); | |
| 410 EXPECT_EQ(app_id, model_->items()[index].app_launch_id.app_id()); | |
| 411 EXPECT_EQ(assigned_shelf_id, model_->items()[index].id); | |
| 412 | |
| 413 // Pinning the same app id again should have no change. | |
| 414 model_->PinAppWithID(app_id); | |
| 415 EXPECT_TRUE(model_->IsAppPinned(app_id)); | |
| 416 EXPECT_EQ(2, model_->item_count()); | |
| 417 EXPECT_EQ(TYPE_PINNED_APP, model_->items()[index].type); | |
| 418 EXPECT_EQ(app_id, model_->items()[index].app_launch_id.app_id()); | |
| 419 EXPECT_EQ(assigned_shelf_id, model_->items()[index].id); | |
| 420 | |
| 421 // Unpinning the app should leave the item unpinnned but running. | |
| 422 model_->UnpinAppWithID(app_id); | |
| 423 EXPECT_FALSE(model_->IsAppPinned(app_id)); | |
| 424 EXPECT_EQ(2, model_->item_count()); | |
| 425 EXPECT_EQ(TYPE_APP, model_->items()[index].type); | |
| 426 EXPECT_EQ(app_id, model_->items()[index].app_launch_id.app_id()); | |
| 427 EXPECT_EQ(assigned_shelf_id, model_->items()[index].id); | |
| 428 | |
| 429 // Unpinning the same app id again should have no change. | |
| 430 model_->UnpinAppWithID(app_id); | |
| 431 EXPECT_FALSE(model_->IsAppPinned(app_id)); | |
| 432 EXPECT_EQ(2, model_->item_count()); | |
| 433 EXPECT_EQ(TYPE_APP, model_->items()[index].type); | |
| 434 EXPECT_EQ(app_id, model_->items()[index].app_launch_id.app_id()); | |
| 435 EXPECT_EQ(assigned_shelf_id, model_->items()[index].id); | |
| 436 } | |
|
James Cook
2017/04/19 21:17:54
Nice tests. I always like the comment/do something
msw
2017/04/19 21:33:18
Acknowledged.
| |
| 437 | |
| 303 } // namespace ash | 438 } // namespace ash |
| OLD | NEW |