Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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_arc_package_helper.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <unordered_map> | |
| 9 | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "chrome/browser/chromeos/arc/arc_auth_service.h" | |
| 13 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | |
| 14 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
| 15 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | |
| 18 #include "chrome/browser/sync/test/integration/sync_test.h" | |
| 19 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" | |
| 20 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs_factory.h" | |
| 21 #include "chrome/browser/ui/app_list/arc/arc_package_syncable_service.h" | |
| 22 #include "chromeos/chromeos_switches.h" | |
| 23 #include "components/arc/arc_bridge_service.h" | |
| 24 #include "components/arc/test/fake_app_instance.h" | |
| 25 #include "components/arc/test/fake_arc_bridge_service.h" | |
| 26 | |
| 27 namespace arc { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 std::string GetTestPackageName(size_t id) { | |
| 32 return "testarcpackage" + base::IntToString(id); | |
| 33 } | |
| 34 | |
| 35 chromeos::FakeChromeUserManager* GetUserManager() { | |
| 36 return static_cast<chromeos::FakeChromeUserManager*>( | |
| 37 user_manager::UserManager::Get()); | |
| 38 } | |
| 39 | |
| 40 const user_manager::User* CreateUserAndLogin(Profile* profile, size_t id) { | |
| 41 std::string gaia_id = "1234567890" + base::IntToString(id); | |
|
Josh Horwich
2016/07/25 22:14:50
base::SizeTToString
lgcheng
2016/07/25 22:26:38
Done.
| |
| 42 const AccountId account_id( | |
| 43 AccountId::FromUserEmailGaiaId(profile->GetProfileUserName(), gaia_id)); | |
| 44 const user_manager::User* user = GetUserManager()->AddUser(account_id); | |
| 45 GetUserManager()->LoginUser(account_id); | |
| 46 return user; | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 SyncArcPackageHelper* SyncArcPackageHelper::GetInstance() { | |
| 52 SyncArcPackageHelper* instance = base::Singleton<SyncArcPackageHelper>::get(); | |
| 53 DCHECK(sync_datatype_helper::test()); | |
| 54 instance->SetupTest(sync_datatype_helper::test()); | |
| 55 return instance; | |
| 56 } | |
| 57 | |
| 58 SyncArcPackageHelper::SyncArcPackageHelper() | |
| 59 : test_(NULL), setup_completed_(false), user_manager_enabler_(nullptr) {} | |
| 60 | |
| 61 SyncArcPackageHelper::~SyncArcPackageHelper() {} | |
| 62 | |
| 63 void SyncArcPackageHelper::SetupTest(SyncTest* test) { | |
| 64 if (setup_completed_) { | |
| 65 DCHECK_EQ(test, test_); | |
| 66 return; | |
| 67 } | |
| 68 test_ = test; | |
| 69 | |
| 70 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler( | |
| 71 new chromeos::FakeChromeUserManager())); | |
| 72 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 73 chromeos::switches::kEnableArc); | |
| 74 ArcAppListPrefsFactory::SetFactoryForSyncTest(); | |
| 75 size_t id = 0; | |
| 76 for (auto* profile : test_->GetAllProfiles()) | |
| 77 SetupArcService(profile, id++); | |
| 78 setup_completed_ = true; | |
| 79 } | |
| 80 | |
| 81 void SyncArcPackageHelper::CleanUp() { | |
| 82 ArcAuthService::Get()->Shutdown(); | |
| 83 user_manager_enabler_.reset(); | |
| 84 } | |
| 85 | |
| 86 void SyncArcPackageHelper::InstallPackageWithIndex(Profile* profile, | |
| 87 size_t id) { | |
| 88 std::string package_name = GetTestPackageName(id); | |
| 89 | |
| 90 mojom::ArcPackageInfo package; | |
| 91 package.package_name = package_name; | |
| 92 package.package_version = id; | |
| 93 package.last_backup_android_id = id; | |
| 94 package.last_backup_time = id; | |
| 95 package.sync = true; | |
| 96 | |
| 97 InstallPackage(profile, package); | |
| 98 } | |
| 99 | |
| 100 void SyncArcPackageHelper::UninstallPackageWithIndex(Profile* profile, | |
| 101 size_t id) { | |
| 102 std::string package_name = GetTestPackageName(id); | |
| 103 UninstallPackage(profile, package_name); | |
| 104 } | |
| 105 | |
| 106 void SyncArcPackageHelper::ClearPackages(Profile* profile) { | |
| 107 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile); | |
| 108 DCHECK(prefs); | |
| 109 const std::vector<std::string> pref_packages = prefs->GetPackagesFromPrefs(); | |
| 110 for (const auto& package : pref_packages) { | |
| 111 UninstallPackage(profile, package); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 bool SyncArcPackageHelper::AllProfilesHaveSamePackages() { | |
| 116 const auto& profiles = test_->GetAllProfiles(); | |
| 117 for (auto* profile : profiles) { | |
| 118 if (profile != profiles.front() && | |
| 119 !ArcPackagesMatch(profiles.front(), profile)) { | |
| 120 DVLOG(1) << "Packages match failed!"; | |
| 121 return false; | |
| 122 } | |
| 123 } | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 bool SyncArcPackageHelper::AllProfilesHaveSamePackageDetails() { | |
| 128 if (!AllProfilesHaveSamePackages()) { | |
| 129 DVLOG(1) << "Packages match failed, skip packages detail match."; | |
| 130 return false; | |
| 131 } | |
| 132 | |
| 133 const auto& profiles = test_->GetAllProfiles(); | |
| 134 for (auto* profile : profiles) { | |
| 135 if (profile != profiles.front() && | |
| 136 !ArcPackageDetailsMatch(profiles.front(), profile)) { | |
| 137 DVLOG(1) << "Profile1: " << ArcPackageSyncableService::Get(profile); | |
| 138 DVLOG(1) << "Profile2: " | |
| 139 << ArcPackageSyncableService::Get(profiles.front()); | |
| 140 return false; | |
| 141 } | |
| 142 } | |
| 143 return true; | |
| 144 } | |
| 145 | |
| 146 void SyncArcPackageHelper::SetupArcService(Profile* profile, size_t id) { | |
| 147 DCHECK(profile); | |
| 148 const user_manager::User* user = CreateUserAndLogin(profile, id); | |
| 149 // Having the user-to-profile mapping ready to avoid using the real profile | |
| 150 // manager (which is null). | |
| 151 chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting(user, | |
| 152 profile); | |
| 153 | |
| 154 ArcAuthService* auth_service = ArcAuthService::Get(); | |
| 155 DCHECK(auth_service); | |
| 156 ArcAuthService::DisableUIForTesting(); | |
| 157 auth_service->OnPrimaryUserProfilePrepared(profile); | |
| 158 auth_service->EnableArc(); | |
| 159 | |
| 160 ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile); | |
| 161 DCHECK(arc_app_list_prefs); | |
| 162 instance_map_[profile].reset(new FakeAppInstance(arc_app_list_prefs)); | |
| 163 DCHECK(instance_map_[profile].get()); | |
| 164 arc_app_list_prefs->app_instance_holder()->SetInstance( | |
| 165 instance_map_[profile].get()); | |
| 166 } | |
| 167 | |
| 168 void SyncArcPackageHelper::InstallPackage( | |
| 169 Profile* profile, | |
| 170 const mojom::ArcPackageInfo& package) { | |
| 171 ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile); | |
| 172 DCHECK(arc_app_list_prefs); | |
| 173 FakeAppInstance* fake_app_instance = static_cast<FakeAppInstance*>( | |
| 174 arc_app_list_prefs->app_instance_holder()->instance()); | |
| 175 | |
| 176 DCHECK(fake_app_instance); | |
| 177 // After this function, new package should be added to local sync service | |
| 178 // and install event should be sent to sync server. | |
| 179 fake_app_instance->InstallPackage(package.Clone()); | |
| 180 } | |
| 181 | |
| 182 void SyncArcPackageHelper::UninstallPackage(Profile* profile, | |
| 183 const std::string& package_name) { | |
| 184 ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile); | |
| 185 DCHECK(arc_app_list_prefs); | |
| 186 FakeAppInstance* fake_app_instance = static_cast<FakeAppInstance*>( | |
| 187 arc_app_list_prefs->app_instance_holder()->instance()); | |
| 188 DCHECK(fake_app_instance); | |
| 189 // After this function, package should be removed from local sync service | |
| 190 // and uninstall event should be sent to sync server. | |
| 191 fake_app_instance->UninstallPackage(package_name); | |
| 192 } | |
| 193 | |
| 194 // Packages from local pref are used for these test functions. Packages in local | |
| 195 // pref should be indentical to syncservice after syncservice is launched. | |
| 196 // Packagd update behavior is not synced by design. | |
| 197 bool SyncArcPackageHelper::ArcPackagesMatch(Profile* profile1, | |
| 198 Profile* profile2) { | |
| 199 ArcAppListPrefs* prefs1 = ArcAppListPrefs::Get(profile1); | |
| 200 ArcAppListPrefs* prefs2 = ArcAppListPrefs::Get(profile2); | |
| 201 DCHECK(prefs1); | |
| 202 DCHECK(prefs2); | |
| 203 const std::vector<std::string> pref1_packages = | |
| 204 prefs1->GetPackagesFromPrefs(); | |
| 205 const std::vector<std::string> pref2_packages = | |
| 206 prefs2->GetPackagesFromPrefs(); | |
| 207 if (pref1_packages.size() != pref2_packages.size()) | |
| 208 return false; | |
| 209 for (const auto& package : pref1_packages) { | |
| 210 std::unique_ptr<ArcAppListPrefs::PackageInfo> package_info = | |
| 211 prefs2->GetPackage(package); | |
| 212 if (!package_info.get()) | |
| 213 return false; | |
| 214 } | |
| 215 return true; | |
| 216 } | |
| 217 | |
| 218 bool SyncArcPackageHelper::ArcPackageDetailsMatch(Profile* profile1, | |
| 219 Profile* profile2) { | |
| 220 ArcAppListPrefs* prefs1 = ArcAppListPrefs::Get(profile1); | |
| 221 ArcAppListPrefs* prefs2 = ArcAppListPrefs::Get(profile2); | |
| 222 DCHECK(prefs1); | |
| 223 DCHECK(prefs2); | |
| 224 const std::vector<std::string> pref1_packages = | |
| 225 prefs1->GetPackagesFromPrefs(); | |
| 226 for (const auto& package : pref1_packages) { | |
| 227 std::unique_ptr<ArcAppListPrefs::PackageInfo> package1_info = | |
| 228 prefs1->GetPackage(package); | |
| 229 std::unique_ptr<ArcAppListPrefs::PackageInfo> package2_info = | |
| 230 prefs2->GetPackage(package); | |
| 231 if (!package2_info.get()) | |
| 232 return false; | |
| 233 if (package1_info->package_version != package2_info->package_version) | |
| 234 return false; | |
| 235 if (package1_info->last_backup_android_id != | |
| 236 package2_info->last_backup_android_id) | |
| 237 return false; | |
| 238 if (package1_info->last_backup_time != package2_info->last_backup_time) | |
| 239 return false; | |
| 240 } | |
| 241 return true; | |
| 242 } | |
| 243 | |
| 244 } // namespace arc | |
| OLD | NEW |