sync_app_list_helper.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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. 4 5#include "chrome/browser/sync/test/integration/sync_app_list_helper.h" 6 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_system.h" 16#include "ui/app_list/app_list_folder_item.h" 17#include "ui/app_list/app_list_item.h" 18#include "ui/app_list/app_list_model.h" 19 20using app_list::AppListFolderItem; 21using app_list::AppListItem; 22using app_list::AppListItemList; 23using app_list::AppListSyncableService; 24using app_list::AppListSyncableServiceFactory; 25 26SyncAppListHelper* SyncAppListHelper::GetInstance() { 27 SyncAppListHelper* instance = Singleton<SyncAppListHelper>::get(); 28 instance->SetupIfNecessary(sync_datatype_helper::test()); 29 return instance; 30} 31 32SyncAppListHelper::SyncAppListHelper() : test_(NULL), setup_completed_(false) {} 33 34SyncAppListHelper::~SyncAppListHelper() {} 35 36void SyncAppListHelper::SetupIfNecessary(SyncTest* test) { 37 if (setup_completed_) { 38 DCHECK_EQ(test, test_); 39 return; 40 } 41 test_ = test; 42 43 for (int i = 0; i < test->num_clients(); ++i) { 44 extensions::ExtensionSystem::Get(test_->GetProfile(i)) 45 ->InitForRegularProfile(true); 46 } 47 extensions::ExtensionSystem::Get(test_->verifier()) 48 ->InitForRegularProfile(true); 49 50 setup_completed_ = true; 51} 52 53bool SyncAppListHelper::AppListMatchesVerifier(Profile* profile) { 54 AppListSyncableService* service = 55 AppListSyncableServiceFactory::GetForProfile(profile); 56 AppListSyncableService* verifier = 57 AppListSyncableServiceFactory::GetForProfile(test_->verifier()); 58 // Note: sync item entries may not exist in verifier, but item lists should 59 // match. 60 if (service->model()->top_level_item_list()->item_count() != 61 verifier->model()->top_level_item_list()->item_count()) { 62 LOG(ERROR) << "Model item count: " 63 << service->model()->top_level_item_list()->item_count() 64 << " != " 65 << verifier->model()->top_level_item_list()->item_count(); 66 return false; 67 } 68 bool res = true; 69 for (size_t i = 0; i < service->model()->top_level_item_list()->item_count(); 70 ++i) { 71 AppListItem* item1 = service->model()->top_level_item_list()->item_at(i); 72 AppListItem* item2 = verifier->model()->top_level_item_list()->item_at(i); 73 if (item1->CompareForTest(item2)) 74 continue; 75 76 LOG(ERROR) << "Item(" << i << "): " << item1->ToDebugString() 77 << " != " << item2->ToDebugString(); 78 size_t index2; 79 if (!verifier->model()->top_level_item_list()->FindItemIndex(item1->id(), 80 &index2)) { 81 LOG(ERROR) << " Item(" << i << "): " << item1->ToDebugString() 82 << " Not in verifier."; 83 } else { 84 LOG(ERROR) << " Item(" << i << "): " << item1->ToDebugString() 85 << " Has different verifier index: " << index2; 86 item2 = verifier->model()->top_level_item_list()->item_at(index2); 87 LOG(ERROR) << " Verifier Item(" << index2 88 << "): " << item2->ToDebugString(); 89 } 90 res = false; 91 } 92 return res; 93} 94 95bool SyncAppListHelper::AllProfilesHaveSameAppListAsVerifier() { 96 bool res = true; 97 for (int i = 0; i < test_->num_clients(); ++i) { 98 if (!AppListMatchesVerifier(test_->GetProfile(i))) { 99 LOG(ERROR) << "Profile " << i 100 << " doesn't have the same app list as the verifier profile."; 101 res = false; 102 } 103 } 104 if (!res) { 105 Profile* verifier = test_->verifier(); 106 VLOG(1) << "Verifier: " 107 << AppListSyncableServiceFactory::GetForProfile(verifier); 108 PrintAppList(test_->verifier()); 109 for (int i = 0; i < test_->num_clients(); ++i) { 110 Profile* profile = test_->GetProfile(i); 111 VLOG(1) << "Profile: " << i << ": " 112 << AppListSyncableServiceFactory::GetForProfile(profile); 113 PrintAppList(profile); 114 } 115 } 116 return res; 117} 118 119void SyncAppListHelper::MoveApp(Profile* profile, size_t from, size_t to) { 120 AppListSyncableService* service = 121 AppListSyncableServiceFactory::GetForProfile(profile); 122 service->model()->top_level_item_list()->MoveItem(from, to); 123} 124 125void SyncAppListHelper::MoveAppToFolder(Profile* profile, 126 size_t index, 127 const std::string& folder_id) { 128 AppListSyncableService* service = 129 AppListSyncableServiceFactory::GetForProfile(profile); 130 service->model()->MoveItemToFolder( 131 service->model()->top_level_item_list()->item_at(index), folder_id); 132} 133 134void SyncAppListHelper::MoveAppFromFolder(Profile* profile, 135 size_t index_in_folder, 136 const std::string& folder_id) { 137 AppListSyncableService* service = 138 AppListSyncableServiceFactory::GetForProfile(profile); 139 AppListFolderItem* folder = service->model()->FindFolderItem(folder_id); 140 if (!folder) { 141 LOG(ERROR) << "Folder not found: " << folder_id; 142 return; 143 } 144 service->model()->MoveItemToFolder( 145 folder->item_list()->item_at(index_in_folder), ""); 146} 147 148void SyncAppListHelper::CopyOrdinalsToVerifier(Profile* profile, 149 const std::string& id) { 150 AppListSyncableService* service = 151 AppListSyncableServiceFactory::GetForProfile(profile); 152 AppListSyncableService* verifier = 153 AppListSyncableServiceFactory::GetForProfile(test_->verifier()); 154 verifier->model()->top_level_item_list()->SetItemPosition( 155 verifier->model()->FindItem(id), 156 service->model()->FindItem(id)->position()); 157} 158 159void SyncAppListHelper::PrintAppList(Profile* profile) { 160 AppListSyncableService* service = 161 AppListSyncableServiceFactory::GetForProfile(profile); 162 for (size_t i = 0; i < service->model()->top_level_item_list()->item_count(); 163 ++i) { 164 AppListItem* item = service->model()->top_level_item_list()->item_at(i); 165 std::string label = base::StringPrintf("Item(%d): ", static_cast<int>(i)); 166 PrintItem(profile, item, label); 167 } 168} 169 170void SyncAppListHelper::PrintItem(Profile* profile, 171 AppListItem* item, 172 const std::string& label) { 173 extensions::AppSorting* s = 174 extensions::ExtensionPrefs::Get(profile)->app_sorting(); 175 std::string id = item->id(); 176 if (item->GetItemType() == AppListFolderItem::kItemType) { 177 VLOG(1) << label << item->ToDebugString(); 178 AppListFolderItem* folder = static_cast<AppListFolderItem*>(item); 179 for (size_t i = 0; i < folder->item_list()->item_count(); ++i) { 180 AppListItem* child = folder->item_list()->item_at(i); 181 std::string child_label = 182 base::StringPrintf(" Folder Item(%d): ", static_cast<int>(i)); 183 PrintItem(profile, child, child_label); 184 } 185 return; 186 } 187 VLOG(1) << label << item->ToDebugString() 188 << " Page: " << s->GetPageOrdinal(id).ToDebugString().substr(0, 8) 189 << " Item: " 190 << s->GetAppLaunchOrdinal(id).ToDebugString().substr(0, 8); 191} 192