| 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_item_list.h" | 5 #include "ui/app_list/app_list_item_list.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> |
| 9 #include <utility> | 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "ui/app_list/app_list_folder_item.h" | 15 #include "ui/app_list/app_list_folder_item.h" |
| 16 #include "ui/app_list/app_list_item.h" | 16 #include "ui/app_list/app_list_item.h" |
| 17 #include "ui/app_list/app_list_item_list_observer.h" | 17 #include "ui/app_list/app_list_item_list_observer.h" |
| 18 | 18 |
| 19 namespace app_list { | 19 namespace app_list { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 | 77 |
| 78 protected: | 78 protected: |
| 79 AppListItem* FindItem(const std::string& id) { | 79 AppListItem* FindItem(const std::string& id) { |
| 80 return item_list_.FindItem(id); | 80 return item_list_.FindItem(id); |
| 81 } | 81 } |
| 82 | 82 |
| 83 bool FindItemIndex(const std::string& id, size_t* index) { | 83 bool FindItemIndex(const std::string& id, size_t* index) { |
| 84 return item_list_.FindItemIndex(id, index); | 84 return item_list_.FindItemIndex(id, index); |
| 85 } | 85 } |
| 86 | 86 |
| 87 scoped_ptr<AppListItem> CreateItem(const std::string& name) { | 87 std::unique_ptr<AppListItem> CreateItem(const std::string& name) { |
| 88 scoped_ptr<AppListItem> item(new AppListItem(name)); | 88 std::unique_ptr<AppListItem> item(new AppListItem(name)); |
| 89 size_t nitems = item_list_.item_count(); | 89 size_t nitems = item_list_.item_count(); |
| 90 syncer::StringOrdinal position; | 90 syncer::StringOrdinal position; |
| 91 if (nitems == 0) | 91 if (nitems == 0) |
| 92 position = syncer::StringOrdinal::CreateInitialOrdinal(); | 92 position = syncer::StringOrdinal::CreateInitialOrdinal(); |
| 93 else | 93 else |
| 94 position = item_list_.item_at(nitems - 1)->position().CreateAfter(); | 94 position = item_list_.item_at(nitems - 1)->position().CreateAfter(); |
| 95 item->set_position(position); | 95 item->set_position(position); |
| 96 return item; | 96 return item; |
| 97 } | 97 } |
| 98 | 98 |
| 99 AppListItem* CreateAndAddItem(const std::string& name) { | 99 AppListItem* CreateAndAddItem(const std::string& name) { |
| 100 scoped_ptr<AppListItem> item(CreateItem(name)); | 100 std::unique_ptr<AppListItem> item(CreateItem(name)); |
| 101 return item_list_.AddItem(std::move(item)); | 101 return item_list_.AddItem(std::move(item)); |
| 102 } | 102 } |
| 103 | 103 |
| 104 scoped_ptr<AppListItem> RemoveItem(const std::string& id) { | 104 std::unique_ptr<AppListItem> RemoveItem(const std::string& id) { |
| 105 return item_list_.RemoveItem(id); | 105 return item_list_.RemoveItem(id); |
| 106 } | 106 } |
| 107 | 107 |
| 108 scoped_ptr<AppListItem> RemoveItemAt(size_t index) { | 108 std::unique_ptr<AppListItem> RemoveItemAt(size_t index) { |
| 109 return item_list_.RemoveItemAt(index); | 109 return item_list_.RemoveItemAt(index); |
| 110 } | 110 } |
| 111 | 111 |
| 112 syncer::StringOrdinal CreatePositionBefore( | 112 syncer::StringOrdinal CreatePositionBefore( |
| 113 const syncer::StringOrdinal& position) { | 113 const syncer::StringOrdinal& position) { |
| 114 return item_list_.CreatePositionBefore(position); | 114 return item_list_.CreatePositionBefore(position); |
| 115 } | 115 } |
| 116 | 116 |
| 117 bool VerifyItemListOrdinals() { | 117 bool VerifyItemListOrdinals() { |
| 118 bool res = true; | 118 bool res = true; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 EXPECT_TRUE(VerifyItemListOrdinals()); | 160 EXPECT_TRUE(VerifyItemListOrdinals()); |
| 161 | 161 |
| 162 size_t index; | 162 size_t index; |
| 163 EXPECT_TRUE(FindItemIndex(item_0->id(), &index)); | 163 EXPECT_TRUE(FindItemIndex(item_0->id(), &index)); |
| 164 EXPECT_EQ(index, 0u); | 164 EXPECT_EQ(index, 0u); |
| 165 EXPECT_TRUE(FindItemIndex(item_1->id(), &index)); | 165 EXPECT_TRUE(FindItemIndex(item_1->id(), &index)); |
| 166 EXPECT_EQ(index, 1u); | 166 EXPECT_EQ(index, 1u); |
| 167 EXPECT_TRUE(FindItemIndex(item_2->id(), &index)); | 167 EXPECT_TRUE(FindItemIndex(item_2->id(), &index)); |
| 168 EXPECT_EQ(index, 2u); | 168 EXPECT_EQ(index, 2u); |
| 169 | 169 |
| 170 scoped_ptr<AppListItem> item_3(CreateItem(GetItemId(3))); | 170 std::unique_ptr<AppListItem> item_3(CreateItem(GetItemId(3))); |
| 171 EXPECT_FALSE(FindItemIndex(item_3->id(), &index)); | 171 EXPECT_FALSE(FindItemIndex(item_3->id(), &index)); |
| 172 } | 172 } |
| 173 | 173 |
| 174 TEST_F(AppListItemListTest, RemoveItemAt) { | 174 TEST_F(AppListItemListTest, RemoveItemAt) { |
| 175 AppListItem* item_0 = CreateAndAddItem(GetItemId(0)); | 175 AppListItem* item_0 = CreateAndAddItem(GetItemId(0)); |
| 176 AppListItem* item_1 = CreateAndAddItem(GetItemId(1)); | 176 AppListItem* item_1 = CreateAndAddItem(GetItemId(1)); |
| 177 AppListItem* item_2 = CreateAndAddItem(GetItemId(2)); | 177 AppListItem* item_2 = CreateAndAddItem(GetItemId(2)); |
| 178 EXPECT_EQ(item_list_.item_count(), 3u); | 178 EXPECT_EQ(item_list_.item_count(), 3u); |
| 179 EXPECT_EQ(observer_.items_added(), 3u); | 179 EXPECT_EQ(observer_.items_added(), 3u); |
| 180 size_t index; | 180 size_t index; |
| 181 EXPECT_TRUE(FindItemIndex(item_1->id(), &index)); | 181 EXPECT_TRUE(FindItemIndex(item_1->id(), &index)); |
| 182 EXPECT_EQ(index, 1u); | 182 EXPECT_EQ(index, 1u); |
| 183 EXPECT_TRUE(VerifyItemListOrdinals()); | 183 EXPECT_TRUE(VerifyItemListOrdinals()); |
| 184 | 184 |
| 185 scoped_ptr<AppListItem> item_removed = RemoveItemAt(1); | 185 std::unique_ptr<AppListItem> item_removed = RemoveItemAt(1); |
| 186 EXPECT_EQ(item_removed.get(), item_1); | 186 EXPECT_EQ(item_removed.get(), item_1); |
| 187 EXPECT_FALSE(FindItem(item_1->id())); | 187 EXPECT_FALSE(FindItem(item_1->id())); |
| 188 EXPECT_EQ(item_list_.item_count(), 2u); | 188 EXPECT_EQ(item_list_.item_count(), 2u); |
| 189 EXPECT_EQ(observer_.items_removed(), 1u); | 189 EXPECT_EQ(observer_.items_removed(), 1u); |
| 190 EXPECT_EQ(item_list_.item_at(0), item_0); | 190 EXPECT_EQ(item_list_.item_at(0), item_0); |
| 191 EXPECT_EQ(item_list_.item_at(1), item_2); | 191 EXPECT_EQ(item_list_.item_at(1), item_2); |
| 192 EXPECT_TRUE(VerifyItemListOrdinals()); | 192 EXPECT_TRUE(VerifyItemListOrdinals()); |
| 193 } | 193 } |
| 194 | 194 |
| 195 TEST_F(AppListItemListTest, RemoveItem) { | 195 TEST_F(AppListItemListTest, RemoveItem) { |
| 196 AppListItem* item_0 = CreateAndAddItem(GetItemId(0)); | 196 AppListItem* item_0 = CreateAndAddItem(GetItemId(0)); |
| 197 AppListItem* item_1 = CreateAndAddItem(GetItemId(1)); | 197 AppListItem* item_1 = CreateAndAddItem(GetItemId(1)); |
| 198 AppListItem* item_2 = CreateAndAddItem(GetItemId(2)); | 198 AppListItem* item_2 = CreateAndAddItem(GetItemId(2)); |
| 199 EXPECT_EQ(item_list_.item_count(), 3u); | 199 EXPECT_EQ(item_list_.item_count(), 3u); |
| 200 EXPECT_EQ(observer_.items_added(), 3u); | 200 EXPECT_EQ(observer_.items_added(), 3u); |
| 201 EXPECT_EQ(item_0, item_list_.item_at(0)); | 201 EXPECT_EQ(item_0, item_list_.item_at(0)); |
| 202 EXPECT_EQ(item_1, item_list_.item_at(1)); | 202 EXPECT_EQ(item_1, item_list_.item_at(1)); |
| 203 EXPECT_EQ(item_2, item_list_.item_at(2)); | 203 EXPECT_EQ(item_2, item_list_.item_at(2)); |
| 204 EXPECT_TRUE(VerifyItemListOrdinals()); | 204 EXPECT_TRUE(VerifyItemListOrdinals()); |
| 205 | 205 |
| 206 size_t index; | 206 size_t index; |
| 207 EXPECT_TRUE(FindItemIndex(item_1->id(), &index)); | 207 EXPECT_TRUE(FindItemIndex(item_1->id(), &index)); |
| 208 EXPECT_EQ(index, 1u); | 208 EXPECT_EQ(index, 1u); |
| 209 | 209 |
| 210 scoped_ptr<AppListItem> item_removed = RemoveItem(item_1->id()); | 210 std::unique_ptr<AppListItem> item_removed = RemoveItem(item_1->id()); |
| 211 EXPECT_EQ(item_removed.get(), item_1); | 211 EXPECT_EQ(item_removed.get(), item_1); |
| 212 EXPECT_FALSE(FindItem(item_1->id())); | 212 EXPECT_FALSE(FindItem(item_1->id())); |
| 213 EXPECT_EQ(item_list_.item_count(), 2u); | 213 EXPECT_EQ(item_list_.item_count(), 2u); |
| 214 EXPECT_EQ(observer_.items_removed(), 1u); | 214 EXPECT_EQ(observer_.items_removed(), 1u); |
| 215 EXPECT_TRUE(VerifyItemListOrdinals()); | 215 EXPECT_TRUE(VerifyItemListOrdinals()); |
| 216 } | 216 } |
| 217 | 217 |
| 218 TEST_F(AppListItemListTest, MoveItem) { | 218 TEST_F(AppListItemListTest, MoveItem) { |
| 219 CreateAndAddItem(GetItemId(0)); | 219 CreateAndAddItem(GetItemId(0)); |
| 220 CreateAndAddItem(GetItemId(1)); | 220 CreateAndAddItem(GetItemId(1)); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 EXPECT_TRUE(VerifyItemListOrdinals()); | 360 EXPECT_TRUE(VerifyItemListOrdinals()); |
| 361 EXPECT_TRUE(VerifyItemOrder4(2, 0, 3, 1)); | 361 EXPECT_TRUE(VerifyItemOrder4(2, 0, 3, 1)); |
| 362 // last -> last | 362 // last -> last |
| 363 item_list_.SetItemPosition(item_list_.item_at(3), | 363 item_list_.SetItemPosition(item_list_.item_at(3), |
| 364 item_list_.item_at(3)->position().CreateAfter()); | 364 item_list_.item_at(3)->position().CreateAfter()); |
| 365 EXPECT_TRUE(VerifyItemListOrdinals()); | 365 EXPECT_TRUE(VerifyItemListOrdinals()); |
| 366 EXPECT_TRUE(VerifyItemOrder4(2, 0, 3, 1)); | 366 EXPECT_TRUE(VerifyItemOrder4(2, 0, 3, 1)); |
| 367 } | 367 } |
| 368 | 368 |
| 369 } // namespace app_list | 369 } // namespace app_list |
| OLD | NEW |