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

Side by Side Diff: chrome/browser/sync/test/integration/extensions_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/extensions_helper.h" 5 #include "chrome/browser/sync/test/integration/extensions_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/memory/ptr_util.h"
9 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sync/test/integration/status_change_checker.h"
12 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" 12 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
13 #include "chrome/browser/sync/test/integration/sync_extension_helper.h" 13 #include "chrome/browser/sync/test/integration/sync_extension_helper.h"
14 #include "chrome/browser/sync/test/integration/sync_extension_installer.h" 14 #include "chrome/browser/sync/test/integration/sync_extension_installer.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "content/public/browser/notification_source.h" 15 #include "content/public/browser/notification_source.h"
18 #include "extensions/browser/extension_registry.h" 16 #include "extensions/browser/extension_registry.h"
19 #include "extensions/browser/extension_registry_observer.h"
20 #include "extensions/common/manifest.h" 17 #include "extensions/common/manifest.h"
21 18
22 using sync_datatype_helper::test; 19 using sync_datatype_helper::test;
23 20
24 namespace extensions_helper { 21 namespace extensions_helper {
25 22
26 // Returns a unique extension name based in the integer |index|. 23 // Returns a unique extension name based in the integer |index|.
27 std::string CreateFakeExtensionName(int index) { 24 std::string CreateFakeExtensionName(int index) {
28 return SyncExtensionHelper::GetInstance()->CreateFakeExtensionName(index); 25 return SyncExtensionHelper::GetInstance()->CreateFakeExtensionName(index);
29 } 26 }
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 120
124 bool IsIncognitoEnabled(Profile* profile, int index) { 121 bool IsIncognitoEnabled(Profile* profile, int index) {
125 return SyncExtensionHelper::GetInstance()->IsIncognitoEnabled( 122 return SyncExtensionHelper::GetInstance()->IsIncognitoEnabled(
126 profile, CreateFakeExtensionName(index)); 123 profile, CreateFakeExtensionName(index));
127 } 124 }
128 125
129 void InstallExtensionsPendingForSync(Profile* profile) { 126 void InstallExtensionsPendingForSync(Profile* profile) {
130 SyncExtensionHelper::GetInstance()->InstallExtensionsPendingForSync(profile); 127 SyncExtensionHelper::GetInstance()->InstallExtensionsPendingForSync(profile);
131 } 128 }
132 129
133 namespace { 130 } // namespace extensions_helper
134 131
135 // A helper class to implement waiting for a set of profiles to have matching 132 ExtensionsMatchChecker::ExtensionsMatchChecker()
136 // extensions lists. It waits for calls on both interfaces: 133 : profiles_(test()->GetAllProfiles()) {
137 // ExtensionRegistryObserver and NotificationObserver. Observing 134 DCHECK_GE(profiles_.size(), 2U);
138 // NOTIFICATION_EXTENSION_UPDATING_STARTED notification is needed for tests 135 for (Profile* profile : profiles_) {
139 // against local server because in such tests extensions are not installed and 136 // Begin mocking the installation of synced extensions from the web store.
140 // ExtensionRegistryObserver methods are not called. 137 synced_extension_installers_.push_back(
141 class ExtensionsMatchChecker : public StatusChangeChecker, 138 base::MakeUnique<SyncedExtensionInstaller>(profile));
142 public extensions::ExtensionRegistryObserver,
143 public content::NotificationObserver {
144 public:
145 explicit ExtensionsMatchChecker(const std::vector<Profile*>& profiles);
146 ~ExtensionsMatchChecker() override;
147 139
148 // StatusChangeChecker implementation. 140 extensions::ExtensionRegistry* registry =
149 std::string GetDebugMessage() const override; 141 extensions::ExtensionRegistry::Get(profile);
150 bool IsExitConditionSatisfied() override; 142 registry->AddObserver(this);
151 143 registrar_.Add(this, extensions::NOTIFICATION_EXTENSION_UPDATING_STARTED,
152 // extensions::ExtensionRegistryObserver implementation. 144 content::Source<Profile>(profile));
153 void OnExtensionLoaded(content::BrowserContext* context, 145 }
154 const extensions::Extension* extension) override;
155 void OnExtensionUnloaded(
156 content::BrowserContext* context,
157 const extensions::Extension* extenion,
158 extensions::UnloadedExtensionInfo::Reason reason) override;
159 void OnExtensionInstalled(content::BrowserContext* browser_context,
160 const extensions::Extension* extension,
161 bool is_update) override;
162 void OnExtensionUninstalled(content::BrowserContext* browser_context,
163 const extensions::Extension* extension,
164 extensions::UninstallReason reason) override;
165
166 // content::NotificationObserver implementation.
167 void Observe(int type,
168 const content::NotificationSource& source,
169 const content::NotificationDetails& details) override;
170
171 void Wait();
172
173 private:
174 std::vector<Profile*> profiles_;
175 ScopedVector<SyncedExtensionInstaller> synced_extension_installers_;
176 content::NotificationRegistrar registrar_;
177 bool observing_;
178
179 DISALLOW_COPY_AND_ASSIGN(ExtensionsMatchChecker);
180 };
181
182 ExtensionsMatchChecker::ExtensionsMatchChecker(
183 const std::vector<Profile*>& profiles)
184 : profiles_(profiles), observing_(false) {
185 DCHECK_GE(profiles_.size(), 2U);
186 } 146 }
187 147
188 ExtensionsMatchChecker::~ExtensionsMatchChecker() { 148 ExtensionsMatchChecker::~ExtensionsMatchChecker() {
189 if (observing_) { 149 for (Profile* profile : profiles_) {
190 for (std::vector<Profile*>::iterator it = profiles_.begin(); 150 extensions::ExtensionRegistry* registry =
191 it != profiles_.end(); 151 extensions::ExtensionRegistry::Get(profile);
192 ++it) { 152 registry->RemoveObserver(this);
193 extensions::ExtensionRegistry* registry =
194 extensions::ExtensionRegistry::Get(*it);
195 registry->RemoveObserver(this);
196 }
197 } 153 }
198 } 154 }
199 155
200 std::string ExtensionsMatchChecker::GetDebugMessage() const { 156 std::string ExtensionsMatchChecker::GetDebugMessage() const {
201 return "Waiting for extensions to match"; 157 return "Waiting for extensions to match";
202 } 158 }
203 159
204 bool ExtensionsMatchChecker::IsExitConditionSatisfied() { 160 bool ExtensionsMatchChecker::IsExitConditionSatisfied() {
205 std::vector<Profile*>::iterator it = profiles_.begin(); 161 std::vector<Profile*>::iterator it = profiles_.begin();
206 Profile* profile0 = *it; 162 Profile* profile0 = *it;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 CheckExitCondition(); 197 CheckExitCondition();
242 } 198 }
243 199
244 void ExtensionsMatchChecker::Observe( 200 void ExtensionsMatchChecker::Observe(
245 int type, 201 int type,
246 const content::NotificationSource& source, 202 const content::NotificationSource& source,
247 const content::NotificationDetails& details) { 203 const content::NotificationDetails& details) {
248 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_UPDATING_STARTED, type); 204 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_UPDATING_STARTED, type);
249 CheckExitCondition(); 205 CheckExitCondition();
250 } 206 }
251
252 void ExtensionsMatchChecker::Wait() {
253 for (std::vector<Profile*>::iterator it = profiles_.begin();
254 it != profiles_.end();
255 ++it) {
256 // Begin mocking the installation of synced extensions from the web store.
257 synced_extension_installers_.push_back(new SyncedExtensionInstaller(*it));
258
259 extensions::ExtensionRegistry* registry =
260 extensions::ExtensionRegistry::Get(*it);
261 registry->AddObserver(this);
262 registrar_.Add(this, extensions::NOTIFICATION_EXTENSION_UPDATING_STARTED,
263 content::Source<Profile>(*it));
264 }
265
266 observing_ = true;
267
268 if (IsExitConditionSatisfied()) {
269 DVLOG(1) << "Extensions matched without waiting";
270 return;
271 }
272
273 DVLOG(1) << "Starting Wait: " << GetDebugMessage();
274 StartBlockingWait();
275 }
276
277 } // namespace
278
279 bool AwaitAllProfilesHaveSameExtensions() {
280 std::vector<Profile*> profiles;
281 for (int i = 0; i < test()->num_clients(); ++i) {
282 profiles.push_back(test()->GetProfile(i));
283 }
284
285 ExtensionsMatchChecker checker(profiles);
286 checker.Wait();
287 return !checker.TimedOut();
288 }
289
290 } // namespace extensions_helper
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698