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

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

Issue 2379433002: [Sync] Refactoring of sync integration test checkers to remove boilerplate await methods. (Closed)
Patch Set: Rebase Created 4 years, 2 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/sync/test/integration/apps_helper.h" 5 #include "chrome/browser/sync/test/integration/apps_helper.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/sync/test/integration/status_change_checker.h"
13 #include "chrome/browser/sync/test/integration/sync_app_helper.h" 12 #include "chrome/browser/sync/test/integration/sync_app_helper.h"
14 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" 13 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
15 #include "chrome/browser/sync/test/integration/sync_extension_helper.h" 14 #include "chrome/browser/sync/test/integration/sync_extension_helper.h"
16 #include "chrome/browser/sync/test/integration/sync_extension_installer.h" 15 #include "chrome/browser/sync/test/integration/sync_extension_installer.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/notification_service.h" 16 #include "content/public/browser/notification_service.h"
20 #include "extensions/browser/extension_prefs.h" 17 #include "extensions/browser/extension_prefs.h"
21 #include "extensions/browser/extension_prefs_observer.h"
22 #include "extensions/browser/extension_registry.h" 18 #include "extensions/browser/extension_registry.h"
23 #include "extensions/browser/extension_registry_observer.h"
24 #include "extensions/common/manifest.h" 19 #include "extensions/common/manifest.h"
25 20
26 using sync_datatype_helper::test; 21 using sync_datatype_helper::test;
27 22
28 namespace { 23 namespace {
29 24
30 std::string CreateFakeAppName(int index) { 25 std::string CreateFakeAppName(int index) {
31 return "fakeapp" + base::IntToString(index); 26 return "fakeapp" + base::IntToString(index);
32 } 27 }
33 28
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 void CopyNTPOrdinals(Profile* source, Profile* destination, int index) { 137 void CopyNTPOrdinals(Profile* source, Profile* destination, int index) {
143 SetPageOrdinalForApp(destination, index, GetPageOrdinalForApp(source, index)); 138 SetPageOrdinalForApp(destination, index, GetPageOrdinalForApp(source, index));
144 SetAppLaunchOrdinalForApp( 139 SetAppLaunchOrdinalForApp(
145 destination, index, GetAppLaunchOrdinalForApp(source, index)); 140 destination, index, GetAppLaunchOrdinalForApp(source, index));
146 } 141 }
147 142
148 void FixNTPOrdinalCollisions(Profile* profile) { 143 void FixNTPOrdinalCollisions(Profile* profile) {
149 SyncAppHelper::GetInstance()->FixNTPOrdinalCollisions(profile); 144 SyncAppHelper::GetInstance()->FixNTPOrdinalCollisions(profile);
150 } 145 }
151 146
152 namespace { 147 } // namespace apps_helper
153 148
154 // A helper class to implement waiting for a set of profiles to have matching 149 AppsMatchChecker::AppsMatchChecker() : profiles_(test()->GetAllProfiles()) {
155 // extensions lists. 150 DCHECK_GE(profiles_.size(), 2U);
156 class AppsMatchChecker : public StatusChangeChecker,
157 public extensions::ExtensionRegistryObserver,
158 public extensions::ExtensionPrefsObserver,
159 public content::NotificationObserver {
160 public:
161 explicit AppsMatchChecker(const std::vector<Profile*>& profiles);
162 ~AppsMatchChecker() override;
163 151
164 // StatusChangeChecker implementation. 152 for (Profile* profile : profiles_) {
165 std::string GetDebugMessage() const override; 153 // Begin mocking the installation of synced extensions from the web store.
166 bool IsExitConditionSatisfied() override; 154 synced_extension_installers_.push_back(
155 new SyncedExtensionInstaller(profile));
167 156
168 // extensions::ExtensionRegistryObserver implementation. 157 // Register as an observer of ExtensionsRegistry to receive notifications of
169 void OnExtensionLoaded(content::BrowserContext* context, 158 // big events, like installs and uninstalls.
170 const extensions::Extension* extension) override; 159 extensions::ExtensionRegistry* registry =
171 void OnExtensionUnloaded( 160 extensions::ExtensionRegistry::Get(profile);
172 content::BrowserContext* context, 161 registry->AddObserver(this);
173 const extensions::Extension* extenion,
174 extensions::UnloadedExtensionInfo::Reason reason) override;
175 void OnExtensionInstalled(content::BrowserContext* browser_context,
176 const extensions::Extension* extension,
177 bool is_update) override;
178 void OnExtensionUninstalled(content::BrowserContext* browser_context,
179 const extensions::Extension* extension,
180 extensions::UninstallReason reason) override;
181 162
182 // extensions::ExtensionPrefsObserver implementation. 163 // Register for ExtensionPrefs events, too, so we can get notifications
183 void OnExtensionDisableReasonsChanged(const std::string& extension_id, 164 // about
184 int disabled_reasons) override; 165 // smaller but still syncable events, like launch type changes.
185 void OnExtensionRegistered(const std::string& extension_id, 166 extensions::ExtensionPrefs* prefs =
186 const base::Time& install_time, 167 extensions::ExtensionPrefs::Get(profile);
187 bool is_enabled) override; 168 prefs->AddObserver(this);
188 void OnExtensionPrefsLoaded(const std::string& extension_id, 169 }
189 const extensions::ExtensionPrefs* prefs) override;
190 void OnExtensionPrefsDeleted(const std::string& extension_id) override;
191 void OnExtensionStateChanged(const std::string& extension_id,
192 bool state) override;
193 170
194 // Implementation of content::NotificationObserver. 171 registrar_.Add(this, chrome::NOTIFICATION_APP_LAUNCHER_REORDERED,
195 void Observe(int type, 172 content::NotificationService::AllSources());
196 const content::NotificationSource& source,
197 const content::NotificationDetails& details) override;
198
199 void Wait();
200
201 private:
202 std::vector<Profile*> profiles_;
203 bool observing_;
204
205 content::NotificationRegistrar registrar_;
206
207 // This installs apps, too.
208 ScopedVector<SyncedExtensionInstaller> synced_extension_installers_;
209
210 DISALLOW_COPY_AND_ASSIGN(AppsMatchChecker);
211 };
212
213 AppsMatchChecker::AppsMatchChecker(const std::vector<Profile*>& profiles)
214 : profiles_(profiles), observing_(false) {
215 DCHECK_GE(profiles_.size(), 2U);
216 } 173 }
217 174
218 AppsMatchChecker::~AppsMatchChecker() { 175 AppsMatchChecker::~AppsMatchChecker() {
219 if (observing_) { 176 for (Profile* profile : profiles_) {
220 for (std::vector<Profile*>::iterator it = profiles_.begin(); 177 extensions::ExtensionRegistry* registry =
221 it != profiles_.end(); 178 extensions::ExtensionRegistry::Get(profile);
222 ++it) { 179 registry->RemoveObserver(this);
223 extensions::ExtensionRegistry* registry = 180 extensions::ExtensionPrefs* prefs =
224 extensions::ExtensionRegistry::Get(*it); 181 extensions::ExtensionPrefs::Get(profile);
225 registry->RemoveObserver(this); 182 prefs->RemoveObserver(this);
226 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(*it);
227 prefs->RemoveObserver(this);
228 }
229 } 183 }
184
185 registrar_.Remove(this, chrome::NOTIFICATION_APP_LAUNCHER_REORDERED,
186 content::NotificationService::AllSources());
230 } 187 }
231 188
232 std::string AppsMatchChecker::GetDebugMessage() const { 189 std::string AppsMatchChecker::GetDebugMessage() const {
233 return "Waiting for apps to match"; 190 return "Waiting for apps to match";
234 } 191 }
235 192
236 bool AppsMatchChecker::IsExitConditionSatisfied() { 193 bool AppsMatchChecker::IsExitConditionSatisfied() {
237 std::vector<Profile*>::iterator it = profiles_.begin(); 194 std::vector<Profile*>::iterator it = profiles_.begin();
238 Profile* profile0 = *it; 195 Profile* profile0 = *it;
239 ++it; 196 ++it;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 CheckExitCondition(); 257 CheckExitCondition();
301 } 258 }
302 259
303 void AppsMatchChecker::Observe(int type, 260 void AppsMatchChecker::Observe(int type,
304 const content::NotificationSource& source, 261 const content::NotificationSource& source,
305 const content::NotificationDetails& details) { 262 const content::NotificationDetails& details) {
306 DCHECK_EQ(chrome::NOTIFICATION_APP_LAUNCHER_REORDERED, type); 263 DCHECK_EQ(chrome::NOTIFICATION_APP_LAUNCHER_REORDERED, type);
307 CheckExitCondition(); 264 CheckExitCondition();
308 } 265 }
309 266
310 void AppsMatchChecker::Wait() {
311 for (std::vector<Profile*>::iterator it = profiles_.begin();
312 it != profiles_.end();
313 ++it) {
314 // Begin mocking the installation of synced extensions from the web store.
315 synced_extension_installers_.push_back(new SyncedExtensionInstaller(*it));
316
317 // Register as an observer of ExtensionsRegistry to receive notifications of
318 // big events, like installs and uninstalls.
319 extensions::ExtensionRegistry* registry =
320 extensions::ExtensionRegistry::Get(*it);
321 registry->AddObserver(this);
322
323 // Register for ExtensionPrefs events, too, so we can get notifications
324 // about
325 // smaller but still syncable events, like launch type changes.
326 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(*it);
327 prefs->AddObserver(this);
328 }
329
330 registrar_.Add(this,
331 chrome::NOTIFICATION_APP_LAUNCHER_REORDERED,
332 content::NotificationService::AllSources());
333
334 observing_ = true;
335
336 if (IsExitConditionSatisfied()) {
337 DVLOG(1) << "Apps matched without waiting";
338 return;
339 }
340
341 DVLOG(1) << "Starting Wait: " << GetDebugMessage();
342 StartBlockingWait();
343 }
344
345 } // namespace
346
347 bool AwaitAllProfilesHaveSameApps() {
348 std::vector<Profile*> profiles;
349 if (test()->use_verifier()) {
350 profiles.push_back(test()->verifier());
351 }
352 for (int i = 0; i < test()->num_clients(); ++i) {
353 profiles.push_back(test()->GetProfile(i));
354 }
355
356 AppsMatchChecker checker(profiles);
357 checker.Wait();
358 return !checker.TimedOut();
359 }
360
361 } // namespace apps_helper
OLDNEW
« no previous file with comments | « chrome/browser/sync/test/integration/apps_helper.h ('k') | chrome/browser/sync/test/integration/autofill_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698