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); | |
| 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 FakeAppInstance* fake_app_instance = new FakeAppInstance(arc_app_list_prefs); | |
|
Luis Héctor Chávez
2016/07/25 20:57:24
Oops, I didn't notice this before. Can you store t
lgcheng
2016/07/25 21:37:28
Ah sorry for this. I thought these leaks were not
| |
| 163 DCHECK(fake_app_instance); | |
| 164 arc_app_list_prefs->app_instance_holder()->SetInstance(fake_app_instance); | |
| 165 } | |
| 166 | |
| 167 void SyncArcPackageHelper::InstallPackage( | |
| 168 Profile* profile, | |
| 169 const mojom::ArcPackageInfo& package) { | |
| 170 ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile); | |
| 171 DCHECK(arc_app_list_prefs); | |
| 172 FakeAppInstance* fake_app_instance = static_cast<FakeAppInstance*>( | |
| 173 arc_app_list_prefs->app_instance_holder()->instance()); | |
| 174 | |
| 175 DCHECK(fake_app_instance); | |
| 176 // After this function, new package should be added to local sync service | |
| 177 // and install event should be sent to sync server. | |
| 178 fake_app_instance->InstallPackage(package.Clone()); | |
| 179 } | |
| 180 | |
| 181 void SyncArcPackageHelper::UninstallPackage(Profile* profile, | |
| 182 const std::string& package_name) { | |
| 183 ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile); | |
| 184 DCHECK(arc_app_list_prefs); | |
| 185 FakeAppInstance* fake_app_instance = static_cast<FakeAppInstance*>( | |
| 186 arc_app_list_prefs->app_instance_holder()->instance()); | |
| 187 DCHECK(fake_app_instance); | |
| 188 // After this function, package should be removed from local sync service | |
| 189 // and uninstall event should be sent to sync server. | |
| 190 fake_app_instance->UninstallPackage(package_name); | |
| 191 } | |
| 192 | |
| 193 // Packages from local pref are used for these test functions. Packages in local | |
| 194 // pref should be indentical to syncservice after syncservice is launched. | |
| 195 // Packagd update behavior is not synced by design. | |
| 196 bool SyncArcPackageHelper::ArcPackagesMatch(Profile* profile1, | |
| 197 Profile* profile2) { | |
| 198 ArcAppListPrefs* prefs1 = ArcAppListPrefs::Get(profile1); | |
| 199 ArcAppListPrefs* prefs2 = ArcAppListPrefs::Get(profile2); | |
| 200 DCHECK(prefs1); | |
| 201 DCHECK(prefs2); | |
| 202 const std::vector<std::string> pref1_packages = | |
| 203 prefs1->GetPackagesFromPrefs(); | |
| 204 const std::vector<std::string> pref2_packages = | |
| 205 prefs2->GetPackagesFromPrefs(); | |
| 206 if (pref1_packages.size() != pref2_packages.size()) | |
| 207 return false; | |
| 208 for (const auto& package : pref1_packages) { | |
| 209 std::unique_ptr<ArcAppListPrefs::PackageInfo> package_info = | |
| 210 prefs2->GetPackage(package); | |
| 211 if (!package_info.get()) | |
| 212 return false; | |
| 213 } | |
| 214 return true; | |
| 215 } | |
| 216 | |
| 217 bool SyncArcPackageHelper::ArcPackageDetailsMatch(Profile* profile1, | |
| 218 Profile* profile2) { | |
| 219 ArcAppListPrefs* prefs1 = ArcAppListPrefs::Get(profile1); | |
| 220 ArcAppListPrefs* prefs2 = ArcAppListPrefs::Get(profile2); | |
| 221 DCHECK(prefs1); | |
| 222 DCHECK(prefs2); | |
| 223 const std::vector<std::string> pref1_packages = | |
| 224 prefs1->GetPackagesFromPrefs(); | |
| 225 for (const auto& package : pref1_packages) { | |
| 226 std::unique_ptr<ArcAppListPrefs::PackageInfo> package1_info = | |
| 227 prefs1->GetPackage(package); | |
| 228 std::unique_ptr<ArcAppListPrefs::PackageInfo> package2_info = | |
| 229 prefs2->GetPackage(package); | |
| 230 if (!package2_info.get()) | |
| 231 return false; | |
| 232 if (package1_info->package_version != package2_info->package_version) | |
| 233 return false; | |
| 234 if (package1_info->last_backup_android_id != | |
| 235 package2_info->last_backup_android_id) | |
| 236 return false; | |
| 237 if (package1_info->last_backup_time != package2_info->last_backup_time) | |
| 238 return false; | |
| 239 } | |
| 240 return true; | |
| 241 } | |
| 242 | |
| 243 } // namespace arc | |
| OLD | NEW |