OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "chrome/browser/ui/app_list/app_list_prefs.h" |
| 7 #include "chrome/browser/ui/app_list/extension_app_item.h" |
| 8 #include "chrome/browser/ui/app_list/model_pref_updater.h" |
| 9 #include "components/pref_registry/testing_pref_service_syncable.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/app_list/app_list_folder_item.h" |
| 12 #include "ui/app_list/app_list_item.h" |
| 13 #include "ui/app_list/test/app_list_test_model.h" |
| 14 |
| 15 namespace app_list { |
| 16 namespace test { |
| 17 |
| 18 class TestExtensionAppItem : public AppListItem { |
| 19 public: |
| 20 explicit TestExtensionAppItem(const std::string& id) : AppListItem(id) {} |
| 21 virtual ~TestExtensionAppItem() {} |
| 22 |
| 23 virtual const char* GetItemType() const OVERRIDE { |
| 24 return ExtensionAppItem::kItemType; |
| 25 } |
| 26 |
| 27 private: |
| 28 DISALLOW_COPY_AND_ASSIGN(TestExtensionAppItem); |
| 29 }; |
| 30 |
| 31 class ModelPrefUpdaterTest : public testing::Test { |
| 32 public: |
| 33 ModelPrefUpdaterTest() {} |
| 34 virtual ~ModelPrefUpdaterTest() {} |
| 35 |
| 36 virtual void SetUp() OVERRIDE { |
| 37 AppListPrefs::RegisterProfilePrefs(pref_service_.registry()); |
| 38 prefs_.reset(AppListPrefs::Create(&pref_service_)); |
| 39 model_.reset(new AppListTestModel()); |
| 40 pref_updater_.reset(new ModelPrefUpdater(prefs_.get(), model_.get())); |
| 41 } |
| 42 |
| 43 AppListTestModel* model() { return model_.get(); } |
| 44 |
| 45 AppListPrefs* prefs() { return prefs_.get(); } |
| 46 |
| 47 bool AppListItemMatchesPrefs(AppListItem* item) { |
| 48 scoped_ptr<AppListPrefs::AppListInfo> info = |
| 49 prefs_->GetAppListInfo(item->id()); |
| 50 AppListPrefs::AppListInfo::ItemType expected_type = |
| 51 AppListPrefs::AppListInfo::ITEM_TYPE_INVALID; |
| 52 if (item->GetItemType() == ExtensionAppItem::kItemType) |
| 53 expected_type = AppListPrefs::AppListInfo::APP_ITEM; |
| 54 else if (item->GetItemType() == AppListFolderItem::kItemType) |
| 55 expected_type = AppListPrefs::AppListInfo::FOLDER_ITEM; |
| 56 |
| 57 return info && info->position.Equals(item->position()) && |
| 58 info->name == item->name() && info->parent_id == item->folder_id() && |
| 59 info->item_type == expected_type; |
| 60 } |
| 61 |
| 62 private: |
| 63 user_prefs::TestingPrefServiceSyncable pref_service_; |
| 64 scoped_ptr<AppListTestModel> model_; |
| 65 scoped_ptr<AppListPrefs> prefs_; |
| 66 scoped_ptr<ModelPrefUpdater> pref_updater_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(ModelPrefUpdaterTest); |
| 69 }; |
| 70 |
| 71 TEST_F(ModelPrefUpdaterTest, ModelChange) { |
| 72 AppListPrefs::AppListInfoMap infos; |
| 73 prefs()->GetAllAppListInfos(&infos); |
| 74 EXPECT_EQ(0u, infos.size()); |
| 75 |
| 76 AppListFolderItem* folder = |
| 77 new AppListFolderItem("folder1", AppListFolderItem::FOLDER_TYPE_NORMAL); |
| 78 model()->AddItem(folder); |
| 79 |
| 80 prefs()->GetAllAppListInfos(&infos); |
| 81 EXPECT_EQ(1u, infos.size()); |
| 82 EXPECT_TRUE(AppListItemMatchesPrefs(folder)); |
| 83 |
| 84 AppListItem* item = new TestExtensionAppItem("Item 0"); |
| 85 model()->AddItem(item); |
| 86 |
| 87 prefs()->GetAllAppListInfos(&infos); |
| 88 EXPECT_EQ(2u, infos.size()); |
| 89 EXPECT_TRUE(AppListItemMatchesPrefs(folder)); |
| 90 EXPECT_TRUE(AppListItemMatchesPrefs(item)); |
| 91 |
| 92 model()->MoveItemToFolder(item, folder->id()); |
| 93 EXPECT_EQ(folder->id(), item->folder_id()); |
| 94 |
| 95 prefs()->GetAllAppListInfos(&infos); |
| 96 EXPECT_EQ(2u, infos.size()); |
| 97 EXPECT_TRUE(AppListItemMatchesPrefs(folder)); |
| 98 EXPECT_TRUE(AppListItemMatchesPrefs(item)); |
| 99 } |
| 100 |
| 101 } // namespace test |
| 102 } // namespace app_list |
OLD | NEW |