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.
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"
18 class TestExtensionAppItem
: public AppListItem
{
20 explicit TestExtensionAppItem(const std::string
& id
) : AppListItem(id
) {}
21 ~TestExtensionAppItem() override
{}
23 const char* GetItemType() const override
{
24 return ExtensionAppItem::kItemType
;
28 DISALLOW_COPY_AND_ASSIGN(TestExtensionAppItem
);
31 class ModelPrefUpdaterTest
: public testing::Test
{
33 ModelPrefUpdaterTest() {}
34 ~ModelPrefUpdaterTest() override
{}
36 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()));
43 AppListTestModel
* model() { return model_
.get(); }
45 AppListPrefs
* prefs() { return prefs_
.get(); }
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
;
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
;
63 user_prefs::TestingPrefServiceSyncable pref_service_
;
64 scoped_ptr
<AppListTestModel
> model_
;
65 scoped_ptr
<AppListPrefs
> prefs_
;
66 scoped_ptr
<ModelPrefUpdater
> pref_updater_
;
68 DISALLOW_COPY_AND_ASSIGN(ModelPrefUpdaterTest
);
71 TEST_F(ModelPrefUpdaterTest
, ModelChange
) {
72 AppListPrefs::AppListInfoMap infos
;
73 prefs()->GetAllAppListInfos(&infos
);
74 EXPECT_EQ(0u, infos
.size());
76 AppListFolderItem
* folder
=
77 new AppListFolderItem("folder1", AppListFolderItem::FOLDER_TYPE_NORMAL
);
78 model()->AddItem(folder
);
80 prefs()->GetAllAppListInfos(&infos
);
81 EXPECT_EQ(1u, infos
.size());
82 EXPECT_TRUE(AppListItemMatchesPrefs(folder
));
84 AppListItem
* item
= new TestExtensionAppItem("Item 0");
85 model()->AddItem(item
);
87 prefs()->GetAllAppListInfos(&infos
);
88 EXPECT_EQ(2u, infos
.size());
89 EXPECT_TRUE(AppListItemMatchesPrefs(folder
));
90 EXPECT_TRUE(AppListItemMatchesPrefs(item
));
92 model()->MoveItemToFolder(item
, folder
->id());
93 EXPECT_EQ(folder
->id(), item
->folder_id());
95 prefs()->GetAllAppListInfos(&infos
);
96 EXPECT_EQ(2u, infos
.size());
97 EXPECT_TRUE(AppListItemMatchesPrefs(folder
));
98 EXPECT_TRUE(AppListItemMatchesPrefs(item
));
102 } // namespace app_list