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
3 // found in the LICENSE file.
5 #include "chrome/browser/sync/test/integration/sync_app_list_helper.h"
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
10 #include "chrome/browser/sync/test/integration/sync_test.h"
11 #include "chrome/browser/ui/app_list/app_list_syncable_service.h"
12 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
13 #include "chrome/common/extensions/sync_helper.h"
14 #include "extensions/browser/app_sorting.h"
15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/browser/extension_system.h"
17 #include "ui/app_list/app_list_folder_item.h"
18 #include "ui/app_list/app_list_item.h"
19 #include "ui/app_list/app_list_model.h"
21 using app_list::AppListFolderItem
;
22 using app_list::AppListItem
;
23 using app_list::AppListItemList
;
24 using app_list::AppListSyncableService
;
25 using app_list::AppListSyncableServiceFactory
;
27 SyncAppListHelper
* SyncAppListHelper::GetInstance() {
28 SyncAppListHelper
* instance
= Singleton
<SyncAppListHelper
>::get();
29 instance
->SetupIfNecessary(sync_datatype_helper::test());
33 SyncAppListHelper::SyncAppListHelper() : test_(NULL
), setup_completed_(false) {}
35 SyncAppListHelper::~SyncAppListHelper() {}
37 void SyncAppListHelper::SetupIfNecessary(SyncTest
* test
) {
38 if (setup_completed_
) {
39 DCHECK_EQ(test
, test_
);
44 for (int i
= 0; i
< test
->num_clients(); ++i
) {
45 extensions::ExtensionSystem::Get(test_
->GetProfile(i
))
46 ->InitForRegularProfile(true);
48 extensions::ExtensionSystem::Get(test_
->verifier())
49 ->InitForRegularProfile(true);
51 setup_completed_
= true;
54 bool SyncAppListHelper::AppListMatchesVerifier(Profile
* profile
) {
55 AppListSyncableService
* service
=
56 AppListSyncableServiceFactory::GetForProfile(profile
);
57 AppListSyncableService
* verifier
=
58 AppListSyncableServiceFactory::GetForProfile(test_
->verifier());
59 // Note: sync item entries may not exist in verifier, but item lists should
61 if (service
->GetModel()->top_level_item_list()->item_count() !=
62 verifier
->GetModel()->top_level_item_list()->item_count()) {
63 LOG(ERROR
) << "Model item count: "
64 << service
->GetModel()->top_level_item_list()->item_count()
66 << verifier
->GetModel()->top_level_item_list()->item_count();
71 i
< service
->GetModel()->top_level_item_list()->item_count(); ++i
) {
72 AppListItem
* item1
= service
->GetModel()->top_level_item_list()->item_at(i
);
74 verifier
->GetModel()->top_level_item_list()->item_at(i
);
75 if (item1
->CompareForTest(item2
))
78 LOG(ERROR
) << "Item(" << i
<< "): " << item1
->ToDebugString()
79 << " != " << item2
->ToDebugString();
81 if (!verifier
->GetModel()->top_level_item_list()->FindItemIndex(item1
->id(),
83 LOG(ERROR
) << " Item(" << i
<< "): " << item1
->ToDebugString()
84 << " Not in verifier.";
86 LOG(ERROR
) << " Item(" << i
<< "): " << item1
->ToDebugString()
87 << " Has different verifier index: " << index2
;
88 item2
= verifier
->GetModel()->top_level_item_list()->item_at(index2
);
89 LOG(ERROR
) << " Verifier Item(" << index2
90 << "): " << item2
->ToDebugString();
97 bool SyncAppListHelper::AllProfilesHaveSameAppListAsVerifier() {
99 for (int i
= 0; i
< test_
->num_clients(); ++i
) {
100 if (!AppListMatchesVerifier(test_
->GetProfile(i
))) {
101 LOG(ERROR
) << "Profile " << i
102 << " doesn't have the same app list as the verifier profile.";
107 Profile
* verifier
= test_
->verifier();
108 DVLOG(1) << "Verifier: "
109 << AppListSyncableServiceFactory::GetForProfile(verifier
);
110 PrintAppList(test_
->verifier());
111 for (int i
= 0; i
< test_
->num_clients(); ++i
) {
112 Profile
* profile
= test_
->GetProfile(i
);
113 DVLOG(1) << "Profile: " << i
<< ": "
114 << AppListSyncableServiceFactory::GetForProfile(profile
);
115 PrintAppList(profile
);
121 void SyncAppListHelper::MoveApp(Profile
* profile
, size_t from
, size_t to
) {
122 AppListSyncableService
* service
=
123 AppListSyncableServiceFactory::GetForProfile(profile
);
124 service
->GetModel()->top_level_item_list()->MoveItem(from
, to
);
127 void SyncAppListHelper::MoveAppToFolder(Profile
* profile
,
129 const std::string
& folder_id
) {
130 AppListSyncableService
* service
=
131 AppListSyncableServiceFactory::GetForProfile(profile
);
132 service
->GetModel()->MoveItemToFolder(
133 service
->GetModel()->top_level_item_list()->item_at(index
), folder_id
);
136 void SyncAppListHelper::MoveAppFromFolder(Profile
* profile
,
137 size_t index_in_folder
,
138 const std::string
& folder_id
) {
139 AppListSyncableService
* service
=
140 AppListSyncableServiceFactory::GetForProfile(profile
);
141 AppListFolderItem
* folder
= service
->GetModel()->FindFolderItem(folder_id
);
143 LOG(ERROR
) << "Folder not found: " << folder_id
;
146 service
->GetModel()->MoveItemToFolder(
147 folder
->item_list()->item_at(index_in_folder
), "");
150 void SyncAppListHelper::CopyOrdinalsToVerifier(Profile
* profile
,
151 const std::string
& id
) {
152 AppListSyncableService
* service
=
153 AppListSyncableServiceFactory::GetForProfile(profile
);
154 AppListSyncableService
* verifier
=
155 AppListSyncableServiceFactory::GetForProfile(test_
->verifier());
156 verifier
->GetModel()->top_level_item_list()->SetItemPosition(
157 verifier
->GetModel()->FindItem(id
),
158 service
->GetModel()->FindItem(id
)->position());
161 void SyncAppListHelper::PrintAppList(Profile
* profile
) {
162 AppListSyncableService
* service
=
163 AppListSyncableServiceFactory::GetForProfile(profile
);
165 i
< service
->GetModel()->top_level_item_list()->item_count(); ++i
) {
166 AppListItem
* item
= service
->GetModel()->top_level_item_list()->item_at(i
);
167 std::string label
= base::StringPrintf("Item(%d): ", static_cast<int>(i
));
168 PrintItem(profile
, item
, label
);
172 void SyncAppListHelper::PrintItem(Profile
* profile
,
174 const std::string
& label
) {
175 extensions::AppSorting
* s
=
176 extensions::ExtensionPrefs::Get(profile
)->app_sorting();
177 std::string id
= item
->id();
178 if (item
->GetItemType() == AppListFolderItem::kItemType
) {
179 DVLOG(1) << label
<< item
->ToDebugString();
180 AppListFolderItem
* folder
= static_cast<AppListFolderItem
*>(item
);
181 for (size_t i
= 0; i
< folder
->item_list()->item_count(); ++i
) {
182 AppListItem
* child
= folder
->item_list()->item_at(i
);
183 std::string child_label
=
184 base::StringPrintf(" Folder Item(%d): ", static_cast<int>(i
));
185 PrintItem(profile
, child
, child_label
);
189 DVLOG(1) << label
<< item
->ToDebugString()
190 << " Page: " << s
->GetPageOrdinal(id
).ToDebugString().substr(0, 8)
192 << s
->GetAppLaunchOrdinal(id
).ToDebugString().substr(0, 8);