Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync / test / integration / sync_app_list_helper.cc
blob5751c0aa6af97da9b6233a45edb06d0e4f421295
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 "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/extension_system.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
11 #include "chrome/browser/sync/test/integration/sync_test.h"
12 #include "chrome/browser/ui/app_list/app_list_syncable_service.h"
13 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
14 #include "chrome/common/extensions/sync_helper.h"
15 #include "ui/app_list/app_list_item.h"
16 #include "ui/app_list/app_list_model.h"
18 using app_list::AppListItemList;
19 using app_list::AppListItem;
20 using app_list::AppListSyncableService;
21 using app_list::AppListSyncableServiceFactory;
23 SyncAppListHelper* SyncAppListHelper::GetInstance() {
24 SyncAppListHelper* instance = Singleton<SyncAppListHelper>::get();
25 instance->SetupIfNecessary(sync_datatype_helper::test());
26 return instance;
29 SyncAppListHelper::SyncAppListHelper() : test_(NULL), setup_completed_(false) {
32 SyncAppListHelper::~SyncAppListHelper() {
35 void SyncAppListHelper::SetupIfNecessary(SyncTest* test) {
36 if (setup_completed_) {
37 DCHECK_EQ(test, test_);
38 return;
40 test_ = test;
42 for (int i = 0; i < test->num_clients(); ++i) {
43 extensions::ExtensionSystem::Get(
44 test_->GetProfile(i))->InitForRegularProfile(true);
46 extensions::ExtensionSystem::Get(
47 test_->verifier())->InitForRegularProfile(true);
49 setup_completed_ = true;
52 bool SyncAppListHelper::AppListMatchesVerifier(Profile* profile) {
53 AppListSyncableService* service =
54 AppListSyncableServiceFactory::GetForProfile(profile);
55 AppListSyncableService* verifier =
56 AppListSyncableServiceFactory::GetForProfile(test_->verifier());
57 // Note: sync item entries may not exist in verifier, but item lists should
58 // match.
59 if (service->model()->item_list()->item_count() !=
60 verifier->model()->item_list()->item_count()) {
61 LOG(ERROR) << "Model item count: "
62 << service->model()->item_list()->item_count()
63 << " != " << verifier->model()->item_list()->item_count();
64 return false;
66 bool res = true;
67 for (size_t i = 0; i < service->model()->item_list()->item_count(); ++i) {
68 AppListItem* item1 = service->model()->item_list()->item_at(i);
69 AppListItem* item2 = verifier->model()->item_list()->item_at(i);
70 if (item1->CompareForTest(item2))
71 continue;
73 LOG(ERROR) << "Item(" << i << "): " << item1->ToDebugString()
74 << " != " << item2->ToDebugString();
75 size_t index2;
76 if (!verifier->model()->item_list()->FindItemIndex(item1->id(), &index2)) {
77 LOG(ERROR) << " Item(" << i << "): " << item1->ToDebugString()
78 << " Not in verifier.";
79 } else {
80 LOG(ERROR) << " Item(" << i << "): " << item1->ToDebugString()
81 << " Has different verifier index: " << index2;
82 item2 = verifier->model()->item_list()->item_at(index2);
83 LOG(ERROR) << " Verifier Item(" << index2 << "): "
84 << item2->ToDebugString();
86 res = false;
88 return res;
91 bool SyncAppListHelper::AllProfilesHaveSameAppListAsVerifier() {
92 bool res = true;
93 for (int i = 0; i < test_->num_clients(); ++i) {
94 if (!AppListMatchesVerifier(test_->GetProfile(i))) {
95 LOG(ERROR) << "Profile " << i
96 << " doesn't have the same app list as the verifier profile.";
97 res = false;
100 if (!res) {
101 VLOG(1) << "Verifier:";
102 PrintAppList(test_->verifier());
103 for (int i = 0; i < test_->num_clients(); ++i) {
104 VLOG(1) << "Profile: " << i;
105 PrintAppList(test_->GetProfile(i));
108 return res;
111 void SyncAppListHelper::MoveApp(Profile* profile, size_t from, size_t to) {
112 AppListSyncableService* service =
113 AppListSyncableServiceFactory::GetForProfile(profile);
114 service->model()->item_list()->MoveItem(from, to);
117 void SyncAppListHelper::CopyOrdinalsToVerifier(Profile* profile,
118 const std::string& id) {
119 AppListSyncableService* service =
120 AppListSyncableServiceFactory::GetForProfile(profile);
121 AppListSyncableService* verifier =
122 AppListSyncableServiceFactory::GetForProfile(test_->verifier());
123 verifier->model()->item_list()->SetItemPosition(
124 verifier->model()->item_list()->FindItem(id),
125 service->model()->item_list()->FindItem(id)->position());
128 void SyncAppListHelper::PrintAppList(Profile* profile) {
129 AppListSyncableService* service =
130 AppListSyncableServiceFactory::GetForProfile(profile);
131 for (size_t i = 0; i < service->model()->item_list()->item_count(); ++i) {
132 AppListItem* item = service->model()->item_list()->item_at(i);
133 extensions::AppSorting* s =
134 extensions::ExtensionSystem::Get(profile)->extension_service()->
135 extension_prefs()->app_sorting();
136 std::string id = item->id();
137 VLOG(1)
138 << "Item(" << i << "): " << item->ToDebugString()
139 << " Page: " << s->GetPageOrdinal(id).ToDebugString().substr(0, 8)
140 << " Item: " << s->GetAppLaunchOrdinal(id).ToDebugString().substr(0, 8);