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

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

Issue 2379433002: [Sync] Refactoring of sync integration test checkers to remove boilerplate await methods. (Closed)
Patch Set: Updates for Max. 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/passwords_helper.h" 5 #include "chrome/browser/sync/test/integration/passwords_helper.h"
6 6
7 #include <sstream> 7 #include <sstream>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "base/synchronization/waitable_event.h" 15 #include "base/synchronization/waitable_event.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "chrome/browser/password_manager/password_store_factory.h" 17 #include "chrome/browser/password_manager/password_store_factory.h"
18 #include "chrome/browser/sync/profile_sync_service_factory.h" 18 #include "chrome/browser/sync/profile_sync_service_factory.h"
19 #include "chrome/browser/sync/test/integration/multi_client_status_change_checke r.h"
20 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" 19 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
21 #include "chrome/browser/sync/test/integration/single_client_status_change_check er.h"
22 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" 20 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
23 #include "components/browser_sync/profile_sync_service.h" 21 #include "components/browser_sync/profile_sync_service.h"
24 #include "components/password_manager/core/browser/password_manager_test_utils.h " 22 #include "components/password_manager/core/browser/password_manager_test_utils.h "
25 #include "components/password_manager/core/browser/password_store.h" 23 #include "components/password_manager/core/browser/password_store.h"
26 #include "components/password_manager/core/browser/password_store_consumer.h" 24 #include "components/password_manager/core/browser/password_store_consumer.h"
27 #include "content/public/test/test_utils.h" 25 #include "content/public/test/test_utils.h"
28 26
29 using autofill::PasswordForm; 27 using autofill::PasswordForm;
30 using password_manager::PasswordStore; 28 using password_manager::PasswordStore;
31 using sync_datatype_helper::test; 29 using sync_datatype_helper::test;
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 for (int i = 1; i < test()->num_clients(); ++i) { 187 for (int i = 1; i < test()->num_clients(); ++i) {
190 if (!ProfilesContainSamePasswordForms(0, i)) { 188 if (!ProfilesContainSamePasswordForms(0, i)) {
191 DVLOG(1) << "Profile " << i << " does not contain the same password" 189 DVLOG(1) << "Profile " << i << " does not contain the same password"
192 " forms as Profile 0."; 190 " forms as Profile 0.";
193 return false; 191 return false;
194 } 192 }
195 } 193 }
196 return true; 194 return true;
197 } 195 }
198 196
199 namespace { 197 int GetPasswordCount(int index) {
198 return GetLogins(GetPasswordStore(index)).size();
199 }
200 200
201 // Helper class used in the implementation of 201 int GetVerifierPasswordCount() {
202 // AwaitAllProfilesContainSamePasswordForms. 202 return GetLogins(GetVerifierPasswordStore()).size();
203 class SamePasswordFormsChecker : public MultiClientStatusChangeChecker { 203 }
204 public:
205 SamePasswordFormsChecker();
206 ~SamePasswordFormsChecker() override;
207 204
208 bool IsExitConditionSatisfied() override; 205 PasswordForm CreateTestPasswordForm(int index) {
209 std::string GetDebugMessage() const override; 206 PasswordForm form;
207 form.signon_realm = kFakeSignonRealm;
208 form.origin = GURL(base::StringPrintf(kIndexedFakeOrigin, index));
209 form.username_value =
210 base::ASCIIToUTF16(base::StringPrintf("username%d", index));
211 form.password_value =
212 base::ASCIIToUTF16(base::StringPrintf("password%d", index));
213 form.date_created = base::Time::Now();
214 return form;
215 }
210 216
211 private: 217 } // namespace passwords_helper
212 bool in_progress_;
213 bool needs_recheck_;
214 };
215 218
216 SamePasswordFormsChecker::SamePasswordFormsChecker() 219 SamePasswordFormsChecker::SamePasswordFormsChecker()
217 : MultiClientStatusChangeChecker( 220 : MultiClientStatusChangeChecker(
218 sync_datatype_helper::test()->GetSyncServices()), 221 sync_datatype_helper::test()->GetSyncServices()),
219 in_progress_(false), 222 in_progress_(false),
220 needs_recheck_(false) {} 223 needs_recheck_(false) {}
221 224
222 SamePasswordFormsChecker::~SamePasswordFormsChecker() {}
223
224 // This method needs protection against re-entrancy. 225 // This method needs protection against re-entrancy.
225 // 226 //
226 // This function indirectly calls GetLogins(), which starts a RunLoop on the UI 227 // This function indirectly calls GetLogins(), which starts a RunLoop on the UI
227 // thread. This can be a problem, since the next task to execute could very 228 // thread. This can be a problem, since the next task to execute could very
228 // well contain a ProfileSyncService::OnStateChanged() event, which would 229 // well contain a ProfileSyncService::OnStateChanged() event, which would
229 // trigger another call to this here function, and start another layer of 230 // trigger another call to this here function, and start another layer of
230 // nested RunLoops. That makes the StatusChangeChecker's Quit() method 231 // nested RunLoops. That makes the StatusChangeChecker's Quit() method
231 // ineffective. 232 // ineffective.
232 // 233 //
233 // The work-around is to not allow re-entrancy. But we can't just drop 234 // The work-around is to not allow re-entrancy. But we can't just drop
234 // IsExitConditionSatisifed() calls if one is already in progress. Instead, we 235 // IsExitConditionSatisifed() calls if one is already in progress. Instead, we
235 // set a flag to ask the current execution of IsExitConditionSatisfied() to be 236 // set a flag to ask the current execution of IsExitConditionSatisfied() to be
236 // re-run. This ensures that the return value is always based on the most 237 // re-run. This ensures that the return value is always based on the most
237 // up-to-date state. 238 // up-to-date state.
238 bool SamePasswordFormsChecker::IsExitConditionSatisfied() { 239 bool SamePasswordFormsChecker::IsExitConditionSatisfied() {
239 if (in_progress_) { 240 if (in_progress_) {
240 LOG(WARNING) << "Setting flag and returning early to prevent nesting."; 241 LOG(WARNING) << "Setting flag and returning early to prevent nesting.";
241 needs_recheck_ = true; 242 needs_recheck_ = true;
242 return false; 243 return false;
243 } 244 }
244 245
245 // Keep retrying until we get a good reading. 246 // Keep retrying until we get a good reading.
246 bool result = false; 247 bool result = false;
247 in_progress_ = true; 248 in_progress_ = true;
248 do { 249 do {
249 needs_recheck_ = false; 250 needs_recheck_ = false;
250 result = AllProfilesContainSamePasswordForms(); 251 result = passwords_helper::AllProfilesContainSamePasswordForms();
251 } while (needs_recheck_); 252 } while (needs_recheck_);
252 in_progress_ = false; 253 in_progress_ = false;
253 return result; 254 return result;
254 } 255 }
255 256
256 std::string SamePasswordFormsChecker::GetDebugMessage() const { 257 std::string SamePasswordFormsChecker::GetDebugMessage() const {
257 return "Waiting for matching passwords"; 258 return "Waiting for matching passwords";
258 } 259 }
259 260
260 } // namespace
261
262 bool AwaitAllProfilesContainSamePasswordForms() {
263 SamePasswordFormsChecker checker;
264 checker.Wait();
265 return !checker.TimedOut();
266 }
267
268 namespace {
269
270 // Helper class used in the implementation of
271 // AwaitProfileContainSamePasswordFormsAsVerifier.
272 class SamePasswordFormsAsVerifierChecker
273 : public SingleClientStatusChangeChecker {
274 public:
275 explicit SamePasswordFormsAsVerifierChecker(int index);
276 ~SamePasswordFormsAsVerifierChecker() override;
277
278 bool IsExitConditionSatisfied() override;
279 std::string GetDebugMessage() const override;
280
281 private:
282 int index_;
283
284 bool in_progress_;
285 bool needs_recheck_;
286 };
287
288 SamePasswordFormsAsVerifierChecker::SamePasswordFormsAsVerifierChecker(int i) 261 SamePasswordFormsAsVerifierChecker::SamePasswordFormsAsVerifierChecker(int i)
289 : SingleClientStatusChangeChecker( 262 : SingleClientStatusChangeChecker(
290 sync_datatype_helper::test()->GetSyncService(i)), 263 sync_datatype_helper::test()->GetSyncService(i)),
291 index_(i), 264 index_(i),
292 in_progress_(false), 265 in_progress_(false),
293 needs_recheck_(false) { 266 needs_recheck_(false) {
294 } 267 }
295 268
296 SamePasswordFormsAsVerifierChecker::~SamePasswordFormsAsVerifierChecker() {
297 }
298
299 // This method uses the same re-entrancy prevention trick as 269 // This method uses the same re-entrancy prevention trick as
300 // the SamePasswordFormsChecker. 270 // the SamePasswordFormsChecker.
301 bool SamePasswordFormsAsVerifierChecker::IsExitConditionSatisfied() { 271 bool SamePasswordFormsAsVerifierChecker::IsExitConditionSatisfied() {
302 if (in_progress_) { 272 if (in_progress_) {
303 LOG(WARNING) << "Setting flag and returning early to prevent nesting."; 273 LOG(WARNING) << "Setting flag and returning early to prevent nesting.";
304 needs_recheck_ = true; 274 needs_recheck_ = true;
305 return false; 275 return false;
306 } 276 }
307 277
308 // Keep retrying until we get a good reading. 278 // Keep retrying until we get a good reading.
309 bool result = false; 279 bool result = false;
310 in_progress_ = true; 280 in_progress_ = true;
311 do { 281 do {
312 needs_recheck_ = false; 282 needs_recheck_ = false;
313 result = ProfileContainsSamePasswordFormsAsVerifier(index_); 283 result =
284 passwords_helper::ProfileContainsSamePasswordFormsAsVerifier(index_);
314 } while (needs_recheck_); 285 } while (needs_recheck_);
315 in_progress_ = false; 286 in_progress_ = false;
316 return result; 287 return result;
317 } 288 }
318 289
319 std::string SamePasswordFormsAsVerifierChecker::GetDebugMessage() const { 290 std::string SamePasswordFormsAsVerifierChecker::GetDebugMessage() const {
320 return "Waiting for passwords to match verifier"; 291 return "Waiting for passwords to match verifier";
321 } 292 }
322
323 } // namespace
324
325 bool AwaitProfileContainsSamePasswordFormsAsVerifier(int index) {
326 SamePasswordFormsAsVerifierChecker checker(index);
327 checker.Wait();
328 return !checker.TimedOut();
329 }
330
331 int GetPasswordCount(int index) {
332 return GetLogins(GetPasswordStore(index)).size();
333 }
334
335 int GetVerifierPasswordCount() {
336 return GetLogins(GetVerifierPasswordStore()).size();
337 }
338
339 PasswordForm CreateTestPasswordForm(int index) {
340 PasswordForm form;
341 form.signon_realm = kFakeSignonRealm;
342 form.origin = GURL(base::StringPrintf(kIndexedFakeOrigin, index));
343 form.username_value =
344 base::ASCIIToUTF16(base::StringPrintf("username%d", index));
345 form.password_value =
346 base::ASCIIToUTF16(base::StringPrintf("password%d", index));
347 form.date_created = base::Time::Now();
348 return form;
349 }
350
351 } // namespace passwords_helper
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698