| 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 "ui/app_list/app_list_model.h" | 5 #include "ui/app_list/app_list_model.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/command_line.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/app_list/app_list_folder_item.h" | 13 #include "ui/app_list/app_list_folder_item.h" |
| 13 #include "ui/app_list/app_list_item.h" | 14 #include "ui/app_list/app_list_item.h" |
| 14 #include "ui/app_list/app_list_model_observer.h" | 15 #include "ui/app_list/app_list_model_observer.h" |
| 16 #include "ui/app_list/app_list_switches.h" |
| 15 #include "ui/app_list/test/app_list_test_model.h" | 17 #include "ui/app_list/test/app_list_test_model.h" |
| 16 #include "ui/base/models/list_model_observer.h" | 18 #include "ui/base/models/list_model_observer.h" |
| 17 | 19 |
| 18 namespace app_list { | 20 namespace app_list { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 class TestObserver : public AppListModelObserver, | 24 class TestObserver : public AppListModelObserver { |
| 23 public AppListItemListObserver { | |
| 24 public: | 25 public: |
| 25 TestObserver() | 26 TestObserver() |
| 26 : status_changed_count_(0), | 27 : status_changed_count_(0), |
| 27 items_added_(0), | 28 items_added_(0), |
| 28 items_removed_(0), | 29 items_removed_(0), |
| 29 items_moved_(0) { | 30 items_moved_(0) { |
| 30 } | 31 } |
| 31 virtual ~TestObserver() { | 32 virtual ~TestObserver() { |
| 32 } | 33 } |
| 33 | 34 |
| 34 // AppListModelObserver | 35 // AppListModelObserver |
| 35 virtual void OnAppListModelStatusChanged() OVERRIDE { | 36 virtual void OnAppListModelStatusChanged() OVERRIDE { |
| 36 ++status_changed_count_; | 37 ++status_changed_count_; |
| 37 } | 38 } |
| 38 | 39 |
| 39 // AppListItemListObserver | 40 virtual void OnAppListItemAdded(AppListItem* item) OVERRIDE { |
| 40 virtual void OnListItemAdded(size_t index, AppListItem* item) OVERRIDE { | |
| 41 items_added_++; | 41 items_added_++; |
| 42 } | 42 } |
| 43 | 43 |
| 44 virtual void OnListItemRemoved(size_t index, AppListItem* item) OVERRIDE { | 44 virtual void OnAppListItemWillBeDeleted(AppListItem* item) OVERRIDE { |
| 45 items_removed_++; | 45 items_removed_++; |
| 46 } | 46 } |
| 47 | 47 |
| 48 virtual void OnListItemMoved(size_t from_index, | 48 virtual void OnAppListItemUpdated(AppListItem* item) OVERRIDE { |
| 49 size_t to_index, | |
| 50 AppListItem* item) OVERRIDE { | |
| 51 items_moved_++; | 49 items_moved_++; |
| 52 } | 50 } |
| 53 | 51 |
| 54 int status_changed_count() const { return status_changed_count_; } | 52 int status_changed_count() const { return status_changed_count_; } |
| 55 int signin_changed_count() const { return signin_changed_count_; } | |
| 56 size_t items_added() { return items_added_; } | 53 size_t items_added() { return items_added_; } |
| 57 size_t items_removed() { return items_removed_; } | 54 size_t items_removed() { return items_removed_; } |
| 58 size_t items_moved() { return items_moved_; } | 55 size_t items_moved() { return items_moved_; } |
| 59 | 56 |
| 60 void ResetCounts() { | 57 void ResetCounts() { |
| 61 status_changed_count_ = 0; | 58 status_changed_count_ = 0; |
| 62 signin_changed_count_ = 0; | |
| 63 items_added_ = 0; | 59 items_added_ = 0; |
| 64 items_removed_ = 0; | 60 items_removed_ = 0; |
| 65 items_moved_ = 0; | 61 items_moved_ = 0; |
| 66 } | 62 } |
| 67 | 63 |
| 68 private: | 64 private: |
| 69 int status_changed_count_; | 65 int status_changed_count_; |
| 70 int signin_changed_count_; | |
| 71 size_t items_added_; | 66 size_t items_added_; |
| 72 size_t items_removed_; | 67 size_t items_removed_; |
| 73 size_t items_moved_; | 68 size_t items_moved_; |
| 74 | 69 |
| 75 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 70 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 76 }; | 71 }; |
| 77 | 72 |
| 78 } // namespace | 73 } // namespace |
| 79 | 74 |
| 80 class AppListModelTest : public testing::Test { | 75 class AppListModelTest : public testing::Test { |
| 81 public: | 76 public: |
| 82 AppListModelTest() {} | 77 AppListModelTest() {} |
| 83 virtual ~AppListModelTest() {} | 78 virtual ~AppListModelTest() {} |
| 84 | 79 |
| 85 // testing::Test overrides: | 80 // testing::Test overrides: |
| 86 virtual void SetUp() OVERRIDE { | 81 virtual void SetUp() OVERRIDE { |
| 87 model_.AddObserver(&observer_); | 82 model_.AddObserver(&observer_); |
| 88 model_.item_list()->AddObserver(&observer_); | |
| 89 } | 83 } |
| 90 virtual void TearDown() OVERRIDE { | 84 virtual void TearDown() OVERRIDE { |
| 91 model_.RemoveObserver(&observer_); | 85 model_.RemoveObserver(&observer_); |
| 92 model_.item_list()->RemoveObserver(&observer_); | |
| 93 } | 86 } |
| 94 | 87 |
| 95 protected: | 88 protected: |
| 96 bool ItemObservedByFolder(AppListFolderItem* folder, | 89 bool ItemObservedByFolder(AppListFolderItem* folder, |
| 97 AppListItem* item) { | 90 AppListItem* item) { |
| 98 return item->observers_.HasObserver(folder); | 91 return item->observers_.HasObserver(folder); |
| 99 } | 92 } |
| 100 | 93 |
| 94 void AddFolderItem(AppListFolderItem* folder, |
| 95 const std::string& name) { |
| 96 folder->item_list()->AddItem(model_.CreateItem(name, name)); |
| 97 } |
| 98 |
| 101 test::AppListTestModel model_; | 99 test::AppListTestModel model_; |
| 102 TestObserver observer_; | 100 TestObserver observer_; |
| 103 | 101 |
| 104 private: | 102 private: |
| 105 DISALLOW_COPY_AND_ASSIGN(AppListModelTest); | 103 DISALLOW_COPY_AND_ASSIGN(AppListModelTest); |
| 106 }; | 104 }; |
| 107 | 105 |
| 108 TEST_F(AppListModelTest, SetStatus) { | 106 TEST_F(AppListModelTest, SetStatus) { |
| 109 EXPECT_EQ(AppListModel::STATUS_NORMAL, model_.status()); | 107 EXPECT_EQ(AppListModel::STATUS_NORMAL, model_.status()); |
| 110 model_.SetStatus(AppListModel::STATUS_SYNCING); | 108 model_.SetStatus(AppListModel::STATUS_SYNCING); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 131 EXPECT_EQ(model_.GetItemName(0), item0->id()); | 129 EXPECT_EQ(model_.GetItemName(0), item0->id()); |
| 132 AppListItem* item1 = model_.item_list()->item_at(1); | 130 AppListItem* item1 = model_.item_list()->item_at(1); |
| 133 ASSERT_TRUE(item1); | 131 ASSERT_TRUE(item1); |
| 134 EXPECT_EQ(model_.GetItemName(1), item1->id()); | 132 EXPECT_EQ(model_.GetItemName(1), item1->id()); |
| 135 } | 133 } |
| 136 | 134 |
| 137 TEST_F(AppListModelTest, ModelFindItem) { | 135 TEST_F(AppListModelTest, ModelFindItem) { |
| 138 const size_t num_apps = 2; | 136 const size_t num_apps = 2; |
| 139 model_.PopulateApps(num_apps); | 137 model_.PopulateApps(num_apps); |
| 140 std::string item_name0 = model_.GetItemName(0); | 138 std::string item_name0 = model_.GetItemName(0); |
| 141 AppListItem* item0 = model_.item_list()->FindItem(item_name0); | 139 AppListItem* item0 = model_.FindItem(item_name0); |
| 142 ASSERT_TRUE(item0); | 140 ASSERT_TRUE(item0); |
| 143 EXPECT_EQ(item_name0, item0->id()); | 141 EXPECT_EQ(item_name0, item0->id()); |
| 144 std::string item_name1 = model_.GetItemName(1); | 142 std::string item_name1 = model_.GetItemName(1); |
| 145 AppListItem* item1 = model_.item_list()->FindItem(item_name1); | 143 AppListItem* item1 = model_.FindItem(item_name1); |
| 146 ASSERT_TRUE(item1); | 144 ASSERT_TRUE(item1); |
| 147 EXPECT_EQ(item_name1, item1->id()); | 145 EXPECT_EQ(item_name1, item1->id()); |
| 148 } | 146 } |
| 149 | 147 |
| 150 TEST_F(AppListModelTest, ModelAddItem) { | 148 TEST_F(AppListModelTest, ModelAddItem) { |
| 151 const size_t num_apps = 2; | 149 const size_t num_apps = 2; |
| 152 model_.PopulateApps(num_apps); | 150 model_.PopulateApps(num_apps); |
| 153 // Adding another item will add it to the end. | 151 // Adding another item will add it to the end. |
| 154 model_.CreateAndAddItem("Added Item 1"); | 152 model_.CreateAndAddItem("Added Item 1"); |
| 155 ASSERT_EQ(num_apps + 1, model_.item_list()->item_count()); | 153 ASSERT_EQ(num_apps + 1, model_.item_list()->item_count()); |
| 156 EXPECT_EQ("Added Item 1", model_.item_list()->item_at(num_apps)->id()); | 154 EXPECT_EQ("Added Item 1", model_.item_list()->item_at(num_apps)->id()); |
| 157 // Add an item between items 0 and 1. | 155 // Add an item between items 0 and 1. |
| 158 AppListItem* item0 = model_.item_list()->item_at(0); | 156 AppListItem* item0 = model_.item_list()->item_at(0); |
| 159 ASSERT_TRUE(item0); | 157 ASSERT_TRUE(item0); |
| 160 AppListItem* item1 = model_.item_list()->item_at(1); | 158 AppListItem* item1 = model_.item_list()->item_at(1); |
| 161 ASSERT_TRUE(item1); | 159 ASSERT_TRUE(item1); |
| 162 AppListItem* item2 = model_.CreateItem("Added Item 2", "Added Item 2"); | 160 AppListItem* item2 = model_.CreateItem("Added Item 2", "Added Item 2"); |
| 163 model_.item_list()->AddItem(item2); | 161 model_.AddItem(item2); |
| 164 model_.item_list()->SetItemPosition( | 162 model_.SetItemPosition( |
| 165 item2, item0->position().CreateBetween(item1->position())); | 163 item2, item0->position().CreateBetween(item1->position())); |
| 166 EXPECT_EQ(num_apps + 2, model_.item_list()->item_count()); | 164 EXPECT_EQ(num_apps + 2, model_.item_list()->item_count()); |
| 165 EXPECT_EQ(num_apps + 2, observer_.items_added()); |
| 167 EXPECT_EQ("Added Item 2", model_.item_list()->item_at(1)->id()); | 166 EXPECT_EQ("Added Item 2", model_.item_list()->item_at(1)->id()); |
| 168 } | 167 } |
| 169 | 168 |
| 170 TEST_F(AppListModelTest, ModelMoveItem) { | 169 TEST_F(AppListModelTest, ModelMoveItem) { |
| 171 const size_t num_apps = 3; | 170 const size_t num_apps = 3; |
| 172 model_.PopulateApps(num_apps); | 171 model_.PopulateApps(num_apps); |
| 173 // Adding another item will add it to the end. | 172 // Adding another item will add it to the end. |
| 174 model_.CreateAndAddItem("Inserted Item"); | 173 model_.CreateAndAddItem("Inserted Item"); |
| 175 ASSERT_EQ(num_apps + 1, model_.item_list()->item_count()); | 174 ASSERT_EQ(num_apps + 1, model_.item_list()->item_count()); |
| 176 // Move it to the position 1. | 175 // Move it to the position 1. |
| 177 model_.item_list()->MoveItem(num_apps, 1); | 176 model_.item_list()->MoveItem(num_apps, 1); |
| 177 EXPECT_EQ(1u, observer_.items_moved()); |
| 178 AppListItem* item = model_.item_list()->item_at(1); | 178 AppListItem* item = model_.item_list()->item_at(1); |
| 179 ASSERT_TRUE(item); | 179 ASSERT_TRUE(item); |
| 180 EXPECT_EQ("Inserted Item", item->id()); | 180 EXPECT_EQ("Inserted Item", item->id()); |
| 181 } | 181 } |
| 182 | 182 |
| 183 TEST_F(AppListModelTest, ModelRemoveItem) { | 183 TEST_F(AppListModelTest, ModelRemoveItem) { |
| 184 const size_t num_apps = 4; | 184 const size_t num_apps = 4; |
| 185 model_.PopulateApps(num_apps); | 185 model_.PopulateApps(num_apps); |
| 186 // Remove an item in the middle. | 186 // Remove an item in the middle. |
| 187 model_.item_list()->DeleteItem(model_.GetItemName(1)); | 187 model_.DeleteItem(model_.GetItemName(1)); |
| 188 EXPECT_EQ(num_apps - 1, model_.item_list()->item_count()); | 188 EXPECT_EQ(num_apps - 1, model_.item_list()->item_count()); |
| 189 EXPECT_EQ(1u, observer_.items_removed()); | 189 EXPECT_EQ(1u, observer_.items_removed()); |
| 190 // Remove the first item in the list. | 190 // Remove the first item in the list. |
| 191 model_.item_list()->DeleteItem(model_.GetItemName(0)); | 191 model_.DeleteItem(model_.GetItemName(0)); |
| 192 EXPECT_EQ(num_apps - 2, model_.item_list()->item_count()); | 192 EXPECT_EQ(num_apps - 2, model_.item_list()->item_count()); |
| 193 EXPECT_EQ(2u, observer_.items_removed()); | 193 EXPECT_EQ(2u, observer_.items_removed()); |
| 194 // Remove the last item in the list. | 194 // Remove the last item in the list. |
| 195 model_.item_list()->DeleteItem(model_.GetItemName(num_apps - 1)); | 195 model_.DeleteItem(model_.GetItemName(num_apps - 1)); |
| 196 EXPECT_EQ(num_apps - 3, model_.item_list()->item_count()); | 196 EXPECT_EQ(num_apps - 3, model_.item_list()->item_count()); |
| 197 EXPECT_EQ(3u, observer_.items_removed()); | 197 EXPECT_EQ(3u, observer_.items_removed()); |
| 198 // Ensure that the first item is the expected one | 198 // Ensure that the first item is the expected one |
| 199 AppListItem* item0 = model_.item_list()->item_at(0); | 199 AppListItem* item0 = model_.item_list()->item_at(0); |
| 200 ASSERT_TRUE(item0); | 200 ASSERT_TRUE(item0); |
| 201 EXPECT_EQ(model_.GetItemName(2), item0->id()); | 201 EXPECT_EQ(model_.GetItemName(2), item0->id()); |
| 202 } | 202 } |
| 203 | 203 |
| 204 TEST_F(AppListModelTest, ModelRemoveItemByType) { | |
| 205 const size_t num_apps = 4; | |
| 206 model_.PopulateApps(num_apps); | |
| 207 model_.item_list()->AddItem(new AppListFolderItem("folder1")); | |
| 208 model_.item_list()->AddItem(new AppListFolderItem("folder2")); | |
| 209 model_.item_list()->DeleteItemsByType(test::AppListTestModel::kItemType); | |
| 210 EXPECT_EQ(num_apps, observer_.items_removed()); | |
| 211 EXPECT_EQ(2u, model_.item_list()->item_count()); | |
| 212 model_.item_list()->DeleteItemsByType(AppListFolderItem::kItemType); | |
| 213 EXPECT_EQ(num_apps + 2, observer_.items_removed()); | |
| 214 EXPECT_EQ(0u, model_.item_list()->item_count()); | |
| 215 // Delete all items | |
| 216 observer_.ResetCounts(); | |
| 217 model_.PopulateApps(num_apps); | |
| 218 model_.item_list()->AddItem(new AppListFolderItem("folder1")); | |
| 219 model_.item_list()->AddItem(new AppListFolderItem("folder2")); | |
| 220 model_.item_list()->DeleteItemsByType(NULL /* all items */); | |
| 221 EXPECT_EQ(num_apps + 2, observer_.items_removed()); | |
| 222 EXPECT_EQ(0u, model_.item_list()->item_count()); | |
| 223 } | |
| 224 | |
| 225 TEST_F(AppListModelTest, AppOrder) { | 204 TEST_F(AppListModelTest, AppOrder) { |
| 226 const size_t num_apps = 5; | 205 const size_t num_apps = 5; |
| 227 model_.PopulateApps(num_apps); | 206 model_.PopulateApps(num_apps); |
| 228 // Ensure order is preserved. | 207 // Ensure order is preserved. |
| 229 for (size_t i = 1; i < num_apps; ++i) { | 208 for (size_t i = 1; i < num_apps; ++i) { |
| 230 EXPECT_TRUE(model_.item_list()->item_at(i)->position().GreaterThan( | 209 EXPECT_TRUE(model_.item_list()->item_at(i)->position().GreaterThan( |
| 231 model_.item_list()->item_at(i - 1)->position())); | 210 model_.item_list()->item_at(i - 1)->position())); |
| 232 } | 211 } |
| 233 // Move an app | 212 // Move an app |
| 234 model_.item_list()->MoveItem(num_apps - 1, 1); | 213 model_.item_list()->MoveItem(num_apps - 1, 1); |
| 235 // Ensure order is preserved. | 214 // Ensure order is preserved. |
| 236 for (size_t i = 1; i < num_apps; ++i) { | 215 for (size_t i = 1; i < num_apps; ++i) { |
| 237 EXPECT_TRUE(model_.item_list()->item_at(i)->position().GreaterThan( | 216 EXPECT_TRUE(model_.item_list()->item_at(i)->position().GreaterThan( |
| 238 model_.item_list()->item_at(i - 1)->position())); | 217 model_.item_list()->item_at(i - 1)->position())); |
| 239 } | 218 } |
| 240 } | 219 } |
| 241 | 220 |
| 242 TEST_F(AppListModelTest, FolderItem) { | 221 TEST_F(AppListModelTest, FolderItem) { |
| 243 scoped_ptr<AppListFolderItem> folder(new AppListFolderItem("folder1")); | 222 scoped_ptr<AppListFolderItem> folder(new AppListFolderItem("folder1")); |
| 244 const size_t num_folder_apps = 8; | 223 const size_t num_folder_apps = 8; |
| 245 const size_t num_observed_apps = 4; | 224 const size_t num_observed_apps = 4; |
| 246 for (int i = 0; static_cast<size_t>(i) < num_folder_apps; ++i) { | 225 for (int i = 0; static_cast<size_t>(i) < num_folder_apps; ++i) { |
| 247 std::string name = model_.GetItemName(i); | 226 std::string name = model_.GetItemName(i); |
| 248 folder->item_list()->AddItem(model_.CreateItem(name, name)); | 227 AddFolderItem(folder.get(), name); |
| 249 } | 228 } |
| 250 // Check that items 0 and 3 are observed. | 229 // Check that items 0 and 3 are observed. |
| 251 EXPECT_TRUE(ItemObservedByFolder( | 230 EXPECT_TRUE(ItemObservedByFolder( |
| 252 folder.get(), folder->item_list()->item_at(0))); | 231 folder.get(), folder->item_list()->item_at(0))); |
| 253 EXPECT_TRUE(ItemObservedByFolder( | 232 EXPECT_TRUE(ItemObservedByFolder( |
| 254 folder.get(), folder->item_list()->item_at(num_observed_apps - 1))); | 233 folder.get(), folder->item_list()->item_at(num_observed_apps - 1))); |
| 255 // Check that item 4 is not observed. | 234 // Check that item 4 is not observed. |
| 256 EXPECT_FALSE(ItemObservedByFolder( | 235 EXPECT_FALSE(ItemObservedByFolder( |
| 257 folder.get(), folder->item_list()->item_at(num_observed_apps))); | 236 folder.get(), folder->item_list()->item_at(num_observed_apps))); |
| 258 folder->item_list()->MoveItem(num_observed_apps, 0); | 237 folder->item_list()->MoveItem(num_observed_apps, 0); |
| 259 // Confirm that everything was moved where expected. | 238 // Confirm that everything was moved where expected. |
| 260 EXPECT_EQ(model_.GetItemName(num_observed_apps), | 239 EXPECT_EQ(model_.GetItemName(num_observed_apps), |
| 261 folder->item_list()->item_at(0)->id()); | 240 folder->item_list()->item_at(0)->id()); |
| 262 EXPECT_EQ(model_.GetItemName(0), | 241 EXPECT_EQ(model_.GetItemName(0), |
| 263 folder->item_list()->item_at(1)->id()); | 242 folder->item_list()->item_at(1)->id()); |
| 264 EXPECT_EQ(model_.GetItemName(num_observed_apps - 1), | 243 EXPECT_EQ(model_.GetItemName(num_observed_apps - 1), |
| 265 folder->item_list()->item_at(num_observed_apps)->id()); | 244 folder->item_list()->item_at(num_observed_apps)->id()); |
| 266 // Check that items 0 and 3 are observed. | 245 // Check that items 0 and 3 are observed. |
| 267 EXPECT_TRUE(ItemObservedByFolder( | 246 EXPECT_TRUE(ItemObservedByFolder( |
| 268 folder.get(), folder->item_list()->item_at(0))); | 247 folder.get(), folder->item_list()->item_at(0))); |
| 269 EXPECT_TRUE(ItemObservedByFolder( | 248 EXPECT_TRUE(ItemObservedByFolder( |
| 270 folder.get(), folder->item_list()->item_at(num_observed_apps - 1))); | 249 folder.get(), folder->item_list()->item_at(num_observed_apps - 1))); |
| 271 // Check that item 4 is not observed. | 250 // Check that item 4 is not observed. |
| 272 EXPECT_FALSE(ItemObservedByFolder( | 251 EXPECT_FALSE(ItemObservedByFolder( |
| 273 folder.get(), folder->item_list()->item_at(num_observed_apps))); | 252 folder.get(), folder->item_list()->item_at(num_observed_apps))); |
| 274 } | 253 } |
| 275 | 254 |
| 276 } // namespace app_list | 255 } // namespace app_list |
| OLD | NEW |