| 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" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace ash { | 14 namespace ash { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 constexpr char kAppListId[] = "jlfapfmkapbjlfbpjedlinehodkccjee"; | |
| 19 | |
| 20 // ShelfModelObserver implementation that tracks what message are invoked. | 18 // ShelfModelObserver implementation that tracks what message are invoked. |
| 21 class TestShelfModelObserver : public ShelfModelObserver { | 19 class TestShelfModelObserver : public ShelfModelObserver { |
| 22 public: | 20 public: |
| 23 TestShelfModelObserver() {} | 21 TestShelfModelObserver() {} |
| 24 | 22 |
| 25 // Returns a string description of the changes that have occurred since this | 23 // Returns a string description of the changes that have occurred since this |
| 26 // was last invoked. Resets state to initial state. | 24 // was last invoked. Resets state to initial state. |
| 27 std::string StateStringAndClear() { | 25 std::string StateStringAndClear() { |
| 28 std::string result; | 26 std::string result; |
| 29 AddToResult("added=%d", added_count_, &result); | 27 AddToResult("added=%d", added_count_, &result); |
| 30 AddToResult("removed=%d", removed_count_, &result); | 28 AddToResult("removed=%d", removed_count_, &result); |
| 31 AddToResult("changed=%d", changed_count_, &result); | 29 AddToResult("changed=%d", changed_count_, &result); |
| 32 AddToResult("moved=%d", moved_count_, &result); | 30 AddToResult("moved=%d", moved_count_, &result); |
| 33 added_count_ = removed_count_ = changed_count_ = moved_count_ = 0; | 31 added_count_ = removed_count_ = changed_count_ = moved_count_ = 0; |
| 34 return result; | 32 return result; |
| 35 } | 33 } |
| 36 | 34 |
| 37 // ShelfModelObserver overrides: | 35 // ShelfModelObserver overrides: |
| 38 void ShelfItemAdded(int) override { added_count_++; } | 36 void ShelfItemAdded(int) override { added_count_++; } |
| 39 void ShelfItemRemoved(int, const ShelfItem&) override { removed_count_++; } | 37 void ShelfItemRemoved(int, const ShelfItem&) override { removed_count_++; } |
| 40 void ShelfItemChanged(int, const ShelfItem&) override { changed_count_++; } | 38 void ShelfItemChanged(int, const ShelfItem&) override { changed_count_++; } |
| 41 void ShelfItemMoved(int, int) override { moved_count_++; } | 39 void ShelfItemMoved(int, int) override { moved_count_++; } |
| 40 void ShelfItemDelegateChanged(const ShelfID&, ShelfItemDelegate*) override {} |
| 42 | 41 |
| 43 private: | 42 private: |
| 44 void AddToResult(const std::string& format, int count, std::string* result) { | 43 void AddToResult(const std::string& format, int count, std::string* result) { |
| 45 if (!count) | 44 if (!count) |
| 46 return; | 45 return; |
| 47 if (!result->empty()) | 46 if (!result->empty()) |
| 48 *result += " "; | 47 *result += " "; |
| 49 *result += base::StringPrintf(format.c_str(), count); | 48 *result += base::StringPrintf(format.c_str(), count); |
| 50 } | 49 } |
| 51 | 50 |
| 52 int added_count_ = 0; | 51 int added_count_ = 0; |
| 53 int removed_count_ = 0; | 52 int removed_count_ = 0; |
| 54 int changed_count_ = 0; | 53 int changed_count_ = 0; |
| 55 int moved_count_ = 0; | 54 int moved_count_ = 0; |
| 56 | 55 |
| 57 DISALLOW_COPY_AND_ASSIGN(TestShelfModelObserver); | 56 DISALLOW_COPY_AND_ASSIGN(TestShelfModelObserver); |
| 58 }; | 57 }; |
| 59 | 58 |
| 60 } // namespace | 59 } // namespace |
| 61 | 60 |
| 62 class ShelfModelTest : public testing::Test { | 61 class ShelfModelTest : public testing::Test { |
| 63 public: | 62 public: |
| 64 ShelfModelTest() {} | 63 ShelfModelTest() {} |
| 65 ~ShelfModelTest() override {} | 64 ~ShelfModelTest() override {} |
| 66 | 65 |
| 67 void SetUp() override { | 66 void SetUp() override { |
| 68 model_.reset(new ShelfModel); | 67 model_.reset(new ShelfModel); |
| 69 observer_.reset(new TestShelfModelObserver); | 68 observer_.reset(new TestShelfModelObserver); |
| 70 EXPECT_EQ(0, model_->item_count()); | |
| 71 | |
| 72 ShelfItem item; | |
| 73 item.type = TYPE_APP_LIST; | |
| 74 item.id = ShelfID(kAppListId); | |
| 75 model_->Add(item); | |
| 76 EXPECT_EQ(1, model_->item_count()); | |
| 77 | |
| 78 model_->AddObserver(observer_.get()); | 69 model_->AddObserver(observer_.get()); |
| 79 } | 70 } |
| 80 | 71 |
| 81 void TearDown() override { | 72 void TearDown() override { |
| 82 observer_.reset(); | 73 observer_.reset(); |
| 83 model_.reset(); | 74 model_.reset(); |
| 84 } | 75 } |
| 85 | 76 |
| 86 std::unique_ptr<ShelfModel> model_; | 77 std::unique_ptr<ShelfModel> model_; |
| 87 std::unique_ptr<TestShelfModelObserver> observer_; | 78 std::unique_ptr<TestShelfModelObserver> observer_; |
| 88 | 79 |
| 89 private: | 80 private: |
| 90 DISALLOW_COPY_AND_ASSIGN(ShelfModelTest); | 81 DISALLOW_COPY_AND_ASSIGN(ShelfModelTest); |
| 91 }; | 82 }; |
| 92 | 83 |
| 84 TEST_F(ShelfModelTest, IntializesAppListItem) { |
| 85 EXPECT_EQ(1, model_->item_count()); |
| 86 EXPECT_EQ(kAppListId, model_->items()[0].id.app_id); |
| 87 // The ShelfModel does not initialize the AppList's ShelfItemDelegate. |
| 88 // ShelfController does that to prevent Chrome from creating its own delegate. |
| 89 EXPECT_FALSE(model_->GetShelfItemDelegate(ShelfID(kAppListId))); |
| 90 } |
| 91 |
| 93 TEST_F(ShelfModelTest, BasicAssertions) { | 92 TEST_F(ShelfModelTest, BasicAssertions) { |
| 94 // Add an item. | 93 // Add an item. |
| 95 ShelfItem item1; | 94 ShelfItem item1; |
| 96 item1.id = ShelfID("item1"); | 95 item1.id = ShelfID("item1"); |
| 97 item1.type = TYPE_PINNED_APP; | 96 item1.type = TYPE_PINNED_APP; |
| 98 int index = model_->Add(item1); | 97 int index = model_->Add(item1); |
| 99 EXPECT_EQ(2, model_->item_count()); | 98 EXPECT_EQ(2, model_->item_count()); |
| 100 EXPECT_LE(0, model_->ItemIndexByID(item1.id)); | 99 EXPECT_LE(0, model_->ItemIndexByID(item1.id)); |
| 101 EXPECT_NE(model_->items().end(), model_->ItemByID(item1.id)); | 100 EXPECT_NE(model_->items().end(), model_->ItemByID(item1.id)); |
| 102 EXPECT_EQ("added=1", observer_->StateStringAndClear()); | 101 EXPECT_EQ("added=1", observer_->StateStringAndClear()); |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 | 423 |
| 425 // Unpinning the same app id again should have no change. | 424 // Unpinning the same app id again should have no change. |
| 426 model_->UnpinAppWithID(app_id); | 425 model_->UnpinAppWithID(app_id); |
| 427 EXPECT_FALSE(model_->IsAppPinned(app_id)); | 426 EXPECT_FALSE(model_->IsAppPinned(app_id)); |
| 428 EXPECT_EQ(2, model_->item_count()); | 427 EXPECT_EQ(2, model_->item_count()); |
| 429 EXPECT_EQ(TYPE_APP, model_->items()[index].type); | 428 EXPECT_EQ(TYPE_APP, model_->items()[index].type); |
| 430 EXPECT_EQ(item.id, model_->items()[index].id); | 429 EXPECT_EQ(item.id, model_->items()[index].id); |
| 431 } | 430 } |
| 432 | 431 |
| 433 } // namespace ash | 432 } // namespace ash |
| OLD | NEW |