Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: chrome/browser/sync/test/integration/sync_arc_package_helper.cc

Issue 2174753004: arc:Integration test for Arc package Chrome sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@current
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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(int 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, const char* id) {
41 std::string gaia_id = "1234567890";
42 gaia_id.append(id);
43 const AccountId account_id(AccountId::FromUserEmailGaiaId(
44 profile->GetProfileUserName(), gaia_id));
45 const user_manager::User* user = GetUserManager()->AddUser(account_id);
46 GetUserManager()->LoginUser(account_id);
47 return user;
48 }
49
50 } // namespace
51
52 SyncArcPackageHelper* SyncArcPackageHelper::GetInstance() {
53 SyncArcPackageHelper* instance = base::Singleton<SyncArcPackageHelper>::get();
54 DCHECK(sync_datatype_helper::test());
55 instance->SetupTest(sync_datatype_helper::test());
56 return instance;
57 }
58
59 SyncArcPackageHelper::SyncArcPackageHelper()
60 : test_(NULL),
61 setup_completed_(false),
62 user_manager_enabler_(nullptr) {}
63
64 SyncArcPackageHelper::~SyncArcPackageHelper() {}
65
66 void SyncArcPackageHelper::SetupTest(SyncTest* test) {
67 if (setup_completed_) {
68 DCHECK_EQ(test, test_);
69 return;
70 }
71 test_ = test;
72
73 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler(
74 new chromeos::FakeChromeUserManager()));
75 base::CommandLine::ForCurrentProcess()->AppendSwitch(
76 chromeos::switches::kEnableArc);
77 ArcAppListPrefsFactory::SetFactoryForSynctTest();
78 char id = '0';
Josh Horwich 2016/07/25 16:48:08 Are you counting on pointer-to-this being a null-t
lgcheng 2016/07/25 20:45:32 It was a poor and confusion implementation to use
79 for (auto* profile : test_->GetAllProfiles()) {
80 SetupArcService(profile, &id);
81 id++;
82 }
83 setup_completed_ = true;
84 }
85
86 void SyncArcPackageHelper::ClearUp() {
87 ArcAuthService::Get()->Shutdown();
88 user_manager_enabler_.reset();
89 }
90
91 void SyncArcPackageHelper::InstallPackageWithIndex(Profile* profile, int id) {
92 std::string package_name = GetTestPackageName(id);
93
94 mojom::ArcPackageInfo package;
95 package.package_name = package_name;
96 package.package_version = id;
97 package.last_backup_android_id = id;
98 package.last_backup_time = id;
99 package.sync = true;
100
101 InstallPackage(profile, package);
102 }
103
104 void SyncArcPackageHelper::UninstallPackageWithIndex(Profile* profile, int id) {
105 std::string package_name = GetTestPackageName(id);
106 UninstallPackage(profile, package_name);
107 }
108
109 void SyncArcPackageHelper::ClearPackages(Profile* profile) {
110 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile);
111 DCHECK(prefs);
112 const std::vector<std::string> pref_packages =
113 prefs->GetPackagesFromPrefs();
114 for (const auto& package : pref_packages) {
115 UninstallPackage(profile, package);
116 }
117 }
118
119 bool SyncArcPackageHelper::AllProfilesHaveSamePackages() {
120 const auto& profiles = test_->GetAllProfiles();
121 for (auto* profile : profiles) {
122 if (profile != profiles.front() &&
123 !ArcPackageMatch(profiles.front(), profile)) {
124 DVLOG(1) << "Packages match failed!";
125 return false;
126 }
127 }
128 return true;
129 }
130
131 bool SyncArcPackageHelper::AllProfilesHaveSamePackageDetail() {
132 if (!AllProfilesHaveSamePackages()) {
133 DVLOG(1) << "Packages match failed, skip packages detail match.";
134 return false;
135 }
136
137 const auto& profiles = test_->GetAllProfiles();
138 for (auto* profile : profiles) {
139 if (profile != profiles.front() &&
140 !ArcPackageMatch(profiles.front(), profile)) {
141 DVLOG(1) << "Profile1: "
142 << ArcPackageSyncableService::Get(profile);
143 DVLOG(1) << "Profile2: "
144 << ArcPackageSyncableService::Get(profiles.front());
145 return false;
146 }
147 }
148 return true;
149 }
150
151 void SyncArcPackageHelper::SetupArcService(Profile* profile, const char* id) {
152 DCHECK(profile);
153 const user_manager::User* user = CreateUserAndLogin(profile, id);
154 // Having the user-to-profile mapping ready to avoid using the real profile
155 // manager (which is null).
156 chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting(user,
157 profile);
158
159 ArcAuthService* auth_service = ArcAuthService::Get();
160 DCHECK(auth_service);
161 ArcAuthService::DisableUIForTesting();
162 auth_service->OnPrimaryUserProfilePrepared(profile);
163 auth_service->EnableArc();
164
165 ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile);
166 DCHECK(arc_app_list_prefs);
167 FakeAppInstance* fake_app_instance =
168 new FakeAppInstance(arc_app_list_prefs);
169 DCHECK(fake_app_instance);
170 arc_app_list_prefs->app_instance_holder()->SetInstance(fake_app_instance);
171 }
172
173 void SyncArcPackageHelper::InstallPackage(Profile* profile,
174 const mojom::ArcPackageInfo& package) {
175 ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile);
176 DCHECK(arc_app_list_prefs);
177 FakeAppInstance* fake_app_instance = static_cast<FakeAppInstance*> (
178 arc_app_list_prefs->app_instance_holder()->instance());
179
180 DCHECK(fake_app_instance);
181 // After this function, new package should be added to local sync service
182 // and install event should be sent to sync server.
183 fake_app_instance->InstallPackage(package.Clone());
184 }
185
186 void SyncArcPackageHelper::UninstallPackage(Profile* profile,
187 const std::string& package_name) {
188 ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile);
189 DCHECK(arc_app_list_prefs);
190 FakeAppInstance* fake_app_instance = static_cast<FakeAppInstance*> (
191 arc_app_list_prefs->app_instance_holder()->instance());
192 DCHECK(fake_app_instance);
193 // After this function, package should be removed from local sync service
194 // and uninstall event should be sent to sync server.
195 fake_app_instance->UninstallPackage(package_name);
196 }
197
198 // Packages from local pref are used for these test functions. Packages in local
199 // pref should be indentical to syncservice after syncservice is launched.
200 // Packagd update behavior is not synced by design.
201 bool SyncArcPackageHelper::ArcPackageMatch(Profile* profile1,
202 Profile* profile2) {
203 ArcAppListPrefs* prefs1 = ArcAppListPrefs::Get(profile1);
204 ArcAppListPrefs* prefs2 = ArcAppListPrefs::Get(profile2);
205 DCHECK(prefs1);
206 DCHECK(prefs2);
207 const std::vector<std::string> pref1_packages =
208 prefs1->GetPackagesFromPrefs();
209 const std::vector<std::string> pref2_packages =
210 prefs2->GetPackagesFromPrefs();
211 if (pref1_packages.size()!=pref2_packages.size())
Luis Héctor Chávez 2016/07/25 17:56:24 can you run git cl format?
lgcheng 2016/07/25 20:45:31 Done.
212 return false;
213 for (const auto& package : pref1_packages) {
214 std::unique_ptr<ArcAppListPrefs::PackageInfo> package_info =
215 prefs2->GetPackage(package);
216 if (!package_info.get())
217 return false;
218 }
219 return true;
220 }
221
222 bool SyncArcPackageHelper::ArcPackageDetailMatch(Profile* profile1,
223 Profile* profile2) {
224 ArcAppListPrefs* prefs1 = ArcAppListPrefs::Get(profile1);
225 ArcAppListPrefs* prefs2 = ArcAppListPrefs::Get(profile2);
226 DCHECK(prefs1);
227 DCHECK(prefs2);
228 const std::vector<std::string> pref1_packages =
229 prefs1->GetPackagesFromPrefs();
230 for (const auto& package : pref1_packages) {
231 std::unique_ptr<ArcAppListPrefs::PackageInfo> package1_info =
232 prefs1->GetPackage(package);
233 std::unique_ptr<ArcAppListPrefs::PackageInfo> package2_info =
234 prefs2->GetPackage(package);
235 if (!package2_info.get())
236 return false;
237 if (package1_info->package_version != package2_info->package_version)
238 return false;
239 if (package1_info->last_backup_android_id !=
240 package2_info->last_backup_android_id)
241 return false;
242 if (package1_info->last_backup_time != package2_info->last_backup_time)
243 return false;
244 }
245 return true;
246
247 }
248
249 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698