1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/test/integration/sync_arc_package_helper.h"
#include <vector>
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/app_list/arc/arc_package_syncable_service.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/session/arc_session_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chromeos/ash/experiences/arc/session/connection_holder.h"
#include "chromeos/ash/experiences/arc/test/connection_holder_util.h"
#include "chromeos/ash/experiences/arc/test/fake_app_instance.h"
#include "components/sync/protocol/arc_package_specifics.pb.h"
#include "components/sync/protocol/entity_specifics.pb.h"
namespace arc {
namespace {
std::string GetTestPackageName(size_t id) {
return "testarcpackage" + base::NumberToString(id);
}
} // namespace
// static
SyncArcPackageHelper* SyncArcPackageHelper::GetInstance() {
SyncArcPackageHelper* instance = base::Singleton<SyncArcPackageHelper>::get();
DCHECK(sync_datatype_helper::test());
instance->SetupTest(sync_datatype_helper::test());
return instance;
}
// static
sync_pb::EntitySpecifics SyncArcPackageHelper::GetTestSpecifics(size_t id) {
sync_pb::EntitySpecifics specifics;
sync_pb::ArcPackageSpecifics* arc_specifics = specifics.mutable_arc_package();
arc_specifics->set_package_name(GetTestPackageName(id));
arc_specifics->set_package_version(id);
arc_specifics->set_last_backup_android_id(id);
arc_specifics->set_last_backup_time(id);
return specifics;
}
SyncArcPackageHelper::SyncArcPackageHelper() = default;
SyncArcPackageHelper::~SyncArcPackageHelper() = default;
void SyncArcPackageHelper::SetupTest(SyncTest* test) {
if (setup_completed_) {
DCHECK_EQ(test, test_);
return;
}
test_ = test;
for (Profile* profile : test_->GetAllProfiles()) {
EnableArcService(profile);
SendRefreshPackageList(profile);
}
setup_completed_ = true;
}
void SyncArcPackageHelper::InstallPackageWithIndex(Profile* profile,
size_t id) {
std::string package_name = GetTestPackageName(id);
mojom::ArcPackageInfo package;
package.package_name = package_name;
package.package_version = id;
package.last_backup_android_id = id;
package.last_backup_time = id;
package.sync = true;
InstallPackage(profile, package);
}
void SyncArcPackageHelper::UninstallPackageWithIndex(Profile* profile,
size_t id) {
std::string package_name = GetTestPackageName(id);
UninstallPackage(profile, package_name);
}
void SyncArcPackageHelper::ClearPackages(Profile* profile) {
const ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile);
DCHECK(prefs);
const std::vector<std::string> pref_packages = prefs->GetPackagesFromPrefs();
for (const std::string& package : pref_packages) {
UninstallPackage(profile, package);
}
}
bool SyncArcPackageHelper::HasOnlyTestPackages(Profile* profile,
const std::vector<size_t>& ids) {
const ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile);
DCHECK(prefs);
if (prefs->GetPackagesFromPrefs().size() != ids.size()) {
return false;
}
for (const size_t id : ids) {
std::unique_ptr<ArcAppListPrefs::PackageInfo> package_info =
prefs->GetPackage(GetTestPackageName(id));
if (!package_info) {
return false;
}
// See InstallPackageWithIndex().
if (package_info->package_version != static_cast<int32_t>(id) ||
package_info->last_backup_android_id != static_cast<int64_t>(id) ||
package_info->last_backup_time != static_cast<int64_t>(id) ||
!package_info->should_sync) {
return false;
}
}
return true;
}
bool SyncArcPackageHelper::AllProfilesHaveSamePackages() {
const std::vector<raw_ptr<Profile, VectorExperimental>>& profiles =
test_->GetAllProfiles();
for (Profile* profile : profiles) {
if (profile != profiles.front() &&
!ArcPackagesMatch(profiles.front(), profile)) {
DVLOG(1) << "Packages match failed!";
return false;
}
}
return true;
}
bool SyncArcPackageHelper::AllProfilesHaveSamePackageDetails() {
if (!AllProfilesHaveSamePackages()) {
DVLOG(1) << "Packages match failed, skip packages detail match.";
return false;
}
const std::vector<raw_ptr<Profile, VectorExperimental>>& profiles =
test_->GetAllProfiles();
for (Profile* profile : profiles) {
if (profile != profiles.front() &&
!ArcPackageDetailsMatch(profiles.front(), profile)) {
DVLOG(1) << "Profile1: " << ArcPackageSyncableService::Get(profile);
DVLOG(1) << "Profile2: "
<< ArcPackageSyncableService::Get(profiles.front());
return false;
}
}
return true;
}
void SyncArcPackageHelper::EnableArcService(Profile* profile) {
DCHECK(profile);
DCHECK(!instance_map_[profile]);
arc::SetArcPlayStoreEnabledForProfile(profile, true);
// Usually ArcPlayStoreEnabledPreferenceHandler would take care of propagating
// prefs changes to observers, but that's not the case in integration tests.
arc::ArcSessionManager::Get()->NotifyArcPlayStoreEnabledChanged(true);
ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile);
DCHECK(arc_app_list_prefs);
base::RunLoop run_loop;
arc_app_list_prefs->SetDefaultAppsReadyCallback(run_loop.QuitClosure());
run_loop.Run();
instance_map_[profile] =
std::make_unique<FakeAppInstance>(arc_app_list_prefs);
DCHECK(instance_map_[profile].get());
arc_app_list_prefs->app_connection_holder()->SetInstance(
instance_map_[profile].get());
WaitForInstanceReady(arc_app_list_prefs->app_connection_holder());
}
void SyncArcPackageHelper::SendRefreshPackageList(Profile* profile) {
// OnPackageListRefreshed will be called when AppInstance is ready.
// For fakeAppInstance we use SendRefreshPackageList to make sure that
// OnPackageListRefreshed will be called.
instance_map_[profile]->SendRefreshPackageList({});
}
void SyncArcPackageHelper::DisableArcService(Profile* profile) {
DCHECK(profile);
DCHECK(instance_map_[profile]);
arc::SetArcPlayStoreEnabledForProfile(profile, false);
// Usually ArcPlayStoreEnabledPreferenceHandler would take care of propagating
// prefs changes to observers, but that's not the case in integration tests.
arc::ArcSessionManager::Get()->NotifyArcPlayStoreEnabledChanged(false);
ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile);
DCHECK(arc_app_list_prefs);
arc_app_list_prefs->app_connection_holder()->CloseInstance(
instance_map_[profile].get());
instance_map_.erase(profile);
}
void SyncArcPackageHelper::InstallPackage(
Profile* profile,
const mojom::ArcPackageInfo& package) {
ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile);
DCHECK(arc_app_list_prefs);
mojom::AppInstance* app_instance = ARC_GET_INSTANCE_FOR_METHOD(
arc_app_list_prefs->app_connection_holder(), InstallPackage);
DCHECK(app_instance);
// After this function, new package should be added to local sync service
// and install event should be sent to sync server.
app_instance->InstallPackage(package.Clone());
}
void SyncArcPackageHelper::UninstallPackage(Profile* profile,
const std::string& package_name) {
ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile);
DCHECK(arc_app_list_prefs);
mojom::AppInstance* app_instance = ARC_GET_INSTANCE_FOR_METHOD(
arc_app_list_prefs->app_connection_holder(), UninstallPackage);
DCHECK(app_instance);
// After this function, package should be removed from local sync service
// and uninstall event should be sent to sync server.
app_instance->UninstallPackage(package_name);
}
// Packages from local pref are used for these test functions. Packages in local
// pref should be indentical to syncservice after syncservice is launched.
// Packagd update behavior is not synced by design.
bool SyncArcPackageHelper::ArcPackagesMatch(Profile* profile1,
Profile* profile2) {
const ArcAppListPrefs* prefs1 = ArcAppListPrefs::Get(profile1);
const ArcAppListPrefs* prefs2 = ArcAppListPrefs::Get(profile2);
DCHECK(prefs1);
DCHECK(prefs2);
const std::vector<std::string> pref1_packages =
prefs1->GetPackagesFromPrefs();
const std::vector<std::string> pref2_packages =
prefs2->GetPackagesFromPrefs();
if (pref1_packages.size() != pref2_packages.size()) {
return false;
}
for (const std::string& package : pref1_packages) {
std::unique_ptr<ArcAppListPrefs::PackageInfo> package_info =
prefs2->GetPackage(package);
if (!package_info.get()) {
return false;
}
}
return true;
}
bool SyncArcPackageHelper::ArcPackageDetailsMatch(Profile* profile1,
Profile* profile2) {
const ArcAppListPrefs* prefs1 = ArcAppListPrefs::Get(profile1);
const ArcAppListPrefs* prefs2 = ArcAppListPrefs::Get(profile2);
DCHECK(prefs1);
DCHECK(prefs2);
const std::vector<std::string> pref1_packages =
prefs1->GetPackagesFromPrefs();
for (const std::string& package : pref1_packages) {
std::unique_ptr<ArcAppListPrefs::PackageInfo> package1_info =
prefs1->GetPackage(package);
std::unique_ptr<ArcAppListPrefs::PackageInfo> package2_info =
prefs2->GetPackage(package);
if (!package2_info.get()) {
return false;
}
if (package1_info->package_version != package2_info->package_version) {
return false;
}
if (package1_info->last_backup_android_id !=
package2_info->last_backup_android_id) {
return false;
}
if (package1_info->last_backup_time != package2_info->last_backup_time) {
return false;
}
}
return true;
}
} // namespace arc
|