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

Side by Side Diff: chrome/browser/sync/test/integration/sync_auth_test.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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "base/macros.h" 5 #include "base/macros.h"
6 #include "base/strings/stringprintf.h" 6 #include "base/strings/stringprintf.h"
7 #include "base/threading/platform_thread.h" 7 #include "base/threading/platform_thread.h"
8 #include "base/time/time.h" 8 #include "base/time/time.h"
9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
10 #include "chrome/browser/sync/test/integration/bookmarks_helper.h" 10 #include "chrome/browser/sync/test/integration/bookmarks_helper.h"
11 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" 11 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
12 #include "chrome/browser/sync/test/integration/single_client_status_change_check er.h" 12 #include "chrome/browser/sync/test/integration/single_client_status_change_check er.h"
13 #include "chrome/browser/sync/test/integration/sync_integration_test_util.h"
14 #include "chrome/browser/sync/test/integration/sync_test.h" 13 #include "chrome/browser/sync/test/integration/sync_test.h"
14 #include "chrome/browser/sync/test/integration/updated_progress_marker_checker.h "
15 #include "components/browser_sync/profile_sync_service.h" 15 #include "components/browser_sync/profile_sync_service.h"
16 #include "components/signin/core/browser/profile_oauth2_token_service.h" 16 #include "components/signin/core/browser/profile_oauth2_token_service.h"
17 #include "google_apis/gaia/google_service_auth_error.h" 17 #include "google_apis/gaia/google_service_auth_error.h"
18 #include "net/http/http_status_code.h" 18 #include "net/http/http_status_code.h"
19 #include "net/url_request/url_request_status.h" 19 #include "net/url_request/url_request_status.h"
20 20
21 using bookmarks_helper::AddURL; 21 using bookmarks_helper::AddURL;
22 using sync_integration_test_util::AwaitCommitActivityCompletion;
23 22
24 const char kShortLivedOAuth2Token[] = 23 const char kShortLivedOAuth2Token[] =
25 "{" 24 "{"
26 " \"refresh_token\": \"short_lived_refresh_token\"," 25 " \"refresh_token\": \"short_lived_refresh_token\","
27 " \"access_token\": \"short_lived_access_token\"," 26 " \"access_token\": \"short_lived_access_token\","
28 " \"expires_in\": 5," // 5 seconds. 27 " \"expires_in\": 5," // 5 seconds.
29 " \"token_type\": \"Bearer\"" 28 " \"token_type\": \"Bearer\""
30 "}"; 29 "}";
31 30
32 const char kValidOAuth2Token[] = "{" 31 const char kValidOAuth2Token[] = "{"
(...skipping 11 matching lines...) Expand all
44 " \"error\": \"invalid_client\"" 43 " \"error\": \"invalid_client\""
45 "}"; 44 "}";
46 45
47 const char kEmptyOAuth2Token[] = ""; 46 const char kEmptyOAuth2Token[] = "";
48 47
49 const char kMalformedOAuth2Token[] = "{ \"foo\": "; 48 const char kMalformedOAuth2Token[] = "{ \"foo\": ";
50 49
51 class TestForAuthError : public SingleClientStatusChangeChecker { 50 class TestForAuthError : public SingleClientStatusChangeChecker {
52 public: 51 public:
53 explicit TestForAuthError(browser_sync::ProfileSyncService* service); 52 explicit TestForAuthError(browser_sync::ProfileSyncService* service);
54 ~TestForAuthError() override; 53
54 // StatusChangeChecker implementation.
55 bool IsExitConditionSatisfied() override; 55 bool IsExitConditionSatisfied() override;
56 std::string GetDebugMessage() const override; 56 std::string GetDebugMessage() const override;
57 }; 57 };
58 58
59 TestForAuthError::TestForAuthError(browser_sync::ProfileSyncService* service) 59 TestForAuthError::TestForAuthError(browser_sync::ProfileSyncService* service)
60 : SingleClientStatusChangeChecker(service) {} 60 : SingleClientStatusChangeChecker(service) {}
61 61
62 TestForAuthError::~TestForAuthError() {}
63
64 bool TestForAuthError::IsExitConditionSatisfied() { 62 bool TestForAuthError::IsExitConditionSatisfied() {
65 return !service()->HasUnsyncedItems() || 63 return !service()->HasUnsyncedItems() ||
66 (service()->GetSyncTokenStatus().last_get_token_error.state() != 64 (service()->GetSyncTokenStatus().last_get_token_error.state() !=
67 GoogleServiceAuthError::NONE); 65 GoogleServiceAuthError::NONE);
68 } 66 }
69 67
70 std::string TestForAuthError::GetDebugMessage() const { 68 std::string TestForAuthError::GetDebugMessage() const {
71 return "Waiting for auth error"; 69 return "Waiting for auth error";
72 } 70 }
73 71
74 class SyncAuthTest : public SyncTest { 72 class SyncAuthTest : public SyncTest {
75 public: 73 public:
76 SyncAuthTest() : SyncTest(SINGLE_CLIENT), bookmark_index_(0) {} 74 SyncAuthTest() : SyncTest(SINGLE_CLIENT), bookmark_index_(0) {}
77 ~SyncAuthTest() override {} 75 ~SyncAuthTest() override {}
78 76
79 // Helper function that adds a bookmark and waits for either an auth error, or 77 // Helper function that adds a bookmark and waits for either an auth error, or
80 // for the bookmark to be committed. Returns true if it detects an auth 78 // for the bookmark to be committed. Returns true if it detects an auth
81 // error, false if the bookmark is committed successfully. 79 // error, false if the bookmark is committed successfully.
82 bool AttemptToTriggerAuthError() { 80 bool AttemptToTriggerAuthError() {
83 int bookmark_index = GetNextBookmarkIndex(); 81 int bookmark_index = GetNextBookmarkIndex();
84 std::string title = base::StringPrintf("Bookmark %d", bookmark_index); 82 std::string title = base::StringPrintf("Bookmark %d", bookmark_index);
85 GURL url = GURL(base::StringPrintf("http://www.foo%d.com", bookmark_index)); 83 GURL url = GURL(base::StringPrintf("http://www.foo%d.com", bookmark_index));
86 EXPECT_TRUE(AddURL(0, title, url) != NULL); 84 EXPECT_TRUE(AddURL(0, title, url) != NULL);
87 85
88 // Run until the bookmark is committed or an auth error is encountered. 86 // Run until the bookmark is committed or an auth error is encountered.
89 TestForAuthError checker_(GetSyncService(0)); 87 TestForAuthError(GetSyncService(0)).Wait();
90 checker_.Wait();
91 88
92 GoogleServiceAuthError oauth_error = 89 GoogleServiceAuthError oauth_error =
93 GetSyncService(0)->GetSyncTokenStatus().last_get_token_error; 90 GetSyncService(0)->GetSyncTokenStatus().last_get_token_error;
94 91
95 return oauth_error.state() != GoogleServiceAuthError::NONE; 92 return oauth_error.state() != GoogleServiceAuthError::NONE;
96 } 93 }
97 94
98 void DisableTokenFetchRetries() { 95 void DisableTokenFetchRetries() {
99 // If ProfileSyncService observes a transient error like SERVICE_UNAVAILABLE 96 // If ProfileSyncService observes a transient error like SERVICE_UNAVAILABLE
100 // or CONNECTION_FAILED, this means the OAuth2TokenService has given up 97 // or CONNECTION_FAILED, this means the OAuth2TokenService has given up
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 ASSERT_TRUE(AttemptToTriggerAuthError()); 286 ASSERT_TRUE(AttemptToTriggerAuthError());
290 ASSERT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest()); 287 ASSERT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest());
291 288
292 // Trigger an auth success state and set up a new valid OAuth2 token. 289 // Trigger an auth success state and set up a new valid OAuth2 token.
293 GetFakeServer()->SetAuthenticated(); 290 GetFakeServer()->SetAuthenticated();
294 SetOAuth2TokenResponse(kValidOAuth2Token, 291 SetOAuth2TokenResponse(kValidOAuth2Token,
295 net::HTTP_OK, 292 net::HTTP_OK,
296 net::URLRequestStatus::SUCCESS); 293 net::URLRequestStatus::SUCCESS);
297 294
298 // Verify that the next sync cycle is successful, and uses the new auth token. 295 // Verify that the next sync cycle is successful, and uses the new auth token.
299 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0))); 296 ASSERT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait());
300 std::string new_token = GetSyncService(0)->GetAccessTokenForTest(); 297 std::string new_token = GetSyncService(0)->GetAccessTokenForTest();
301 ASSERT_NE(old_token, new_token); 298 ASSERT_NE(old_token, new_token);
302 } 299 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698