| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/services/gcm/gcm_account_tracker.h" | 5 #include "chrome/browser/services/gcm/gcm_account_tracker.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/run_loop.h" | |
| 13 #include "components/gcm_driver/fake_gcm_driver.h" | 11 #include "components/gcm_driver/fake_gcm_driver.h" |
| 14 #include "google_apis/gaia/fake_identity_provider.h" | 12 #include "google_apis/gaia/fake_identity_provider.h" |
| 15 #include "google_apis/gaia/fake_oauth2_token_service.h" | 13 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 16 #include "google_apis/gaia/google_service_auth_error.h" | 14 #include "google_apis/gaia/google_service_auth_error.h" |
| 17 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
| 18 #include "net/url_request/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.h" |
| 19 #include "net/url_request/url_request_test_util.h" | 17 #include "net/url_request/url_request_test_util.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 19 |
| 22 namespace gcm { | 20 namespace gcm { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 public: | 67 public: |
| 70 CustomFakeGCMDriver(); | 68 CustomFakeGCMDriver(); |
| 71 ~CustomFakeGCMDriver() override; | 69 ~CustomFakeGCMDriver() override; |
| 72 | 70 |
| 73 // GCMDriver overrides: | 71 // GCMDriver overrides: |
| 74 void SetAccountTokens( | 72 void SetAccountTokens( |
| 75 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) override; | 73 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) override; |
| 76 void AddConnectionObserver(GCMConnectionObserver* observer) override; | 74 void AddConnectionObserver(GCMConnectionObserver* observer) override; |
| 77 void RemoveConnectionObserver(GCMConnectionObserver* observer) override; | 75 void RemoveConnectionObserver(GCMConnectionObserver* observer) override; |
| 78 bool IsConnected() const override { return connected_; } | 76 bool IsConnected() const override { return connected_; } |
| 77 base::Time GetLastTokenFetchTime() override; |
| 78 void SetLastTokenFetchTime(const base::Time& time) override; |
| 79 | 79 |
| 80 // Test results and helpers. | 80 // Test results and helpers. |
| 81 void SetConnected(bool connected); | 81 void SetConnected(bool connected); |
| 82 void ResetResults(); | 82 void ResetResults(); |
| 83 bool update_accounts_called() const { return update_accounts_called_; } | 83 bool update_accounts_called() const { return update_accounts_called_; } |
| 84 const std::vector<GCMClient::AccountTokenInfo>& accounts() const { | 84 const std::vector<GCMClient::AccountTokenInfo>& accounts() const { |
| 85 return accounts_; | 85 return accounts_; |
| 86 } | 86 } |
| 87 const GCMConnectionObserver* last_connection_observer() const { | 87 const GCMConnectionObserver* last_connection_observer() const { |
| 88 return last_connection_observer_; | 88 return last_connection_observer_; |
| 89 } | 89 } |
| 90 const GCMConnectionObserver* last_removed_connection_observer() const { | 90 const GCMConnectionObserver* last_removed_connection_observer() const { |
| 91 return removed_connection_observer_; | 91 return removed_connection_observer_; |
| 92 } | 92 } |
| 93 | 93 |
| 94 private: | 94 private: |
| 95 bool connected_; | 95 bool connected_; |
| 96 std::vector<GCMClient::AccountTokenInfo> accounts_; | 96 std::vector<GCMClient::AccountTokenInfo> accounts_; |
| 97 bool update_accounts_called_; | 97 bool update_accounts_called_; |
| 98 GCMConnectionObserver* last_connection_observer_; | 98 GCMConnectionObserver* last_connection_observer_; |
| 99 GCMConnectionObserver* removed_connection_observer_; | 99 GCMConnectionObserver* removed_connection_observer_; |
| 100 net::IPEndPoint ip_endpoint_; | 100 net::IPEndPoint ip_endpoint_; |
| 101 base::Time last_token_fetch_time_; |
| 101 | 102 |
| 102 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); | 103 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); |
| 103 }; | 104 }; |
| 104 | 105 |
| 105 CustomFakeGCMDriver::CustomFakeGCMDriver() | 106 CustomFakeGCMDriver::CustomFakeGCMDriver() |
| 106 : connected_(true), | 107 : connected_(true), |
| 107 update_accounts_called_(false), | 108 update_accounts_called_(false), |
| 108 last_connection_observer_(NULL), | 109 last_connection_observer_(NULL), |
| 109 removed_connection_observer_(NULL) { | 110 removed_connection_observer_(NULL) { |
| 110 } | 111 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 134 last_connection_observer_->OnConnected(ip_endpoint_); | 135 last_connection_observer_->OnConnected(ip_endpoint_); |
| 135 } | 136 } |
| 136 | 137 |
| 137 void CustomFakeGCMDriver::ResetResults() { | 138 void CustomFakeGCMDriver::ResetResults() { |
| 138 accounts_.clear(); | 139 accounts_.clear(); |
| 139 update_accounts_called_ = false; | 140 update_accounts_called_ = false; |
| 140 last_connection_observer_ = NULL; | 141 last_connection_observer_ = NULL; |
| 141 removed_connection_observer_ = NULL; | 142 removed_connection_observer_ = NULL; |
| 142 } | 143 } |
| 143 | 144 |
| 145 |
| 146 base::Time CustomFakeGCMDriver::GetLastTokenFetchTime() { |
| 147 return last_token_fetch_time_; |
| 148 } |
| 149 |
| 150 void CustomFakeGCMDriver::SetLastTokenFetchTime(const base::Time& time) { |
| 151 last_token_fetch_time_ = time; |
| 152 } |
| 153 |
| 144 } // namespace | 154 } // namespace |
| 145 | 155 |
| 146 class GCMAccountTrackerTest : public testing::Test { | 156 class GCMAccountTrackerTest : public testing::Test { |
| 147 public: | 157 public: |
| 148 GCMAccountTrackerTest(); | 158 GCMAccountTrackerTest(); |
| 149 ~GCMAccountTrackerTest() override; | 159 ~GCMAccountTrackerTest() override; |
| 150 | 160 |
| 151 // Helpers to pass fake events to the tracker. Tests should have either a pair | 161 // Helpers to pass fake events to the tracker. Tests should have either a pair |
| 152 // of Start/FinishAccountSignIn or SignInAccount per account. Don't mix. | 162 // of Start/FinishAccountSignIn or SignInAccount per account. Don't mix. |
| 153 // Call to SignOutAccount is not mandatory. | 163 // Call to SignOutAccount is not mandatory. |
| 154 void StartAccountSignIn(const std::string& account_key); | 164 void StartAccountSignIn(const std::string& account_key); |
| 155 void FinishAccountSignIn(const std::string& account_key); | 165 void FinishAccountSignIn(const std::string& account_key); |
| 156 void SignInAccount(const std::string& account_key); | 166 void SignInAccount(const std::string& account_key); |
| 157 void SignOutAccount(const std::string& account_key); | 167 void SignOutAccount(const std::string& account_key); |
| 158 | 168 |
| 159 // Helpers for dealing with OAuth2 access token requests. | 169 // Helpers for dealing with OAuth2 access token requests. |
| 160 void IssueAccessToken(const std::string& account_key); | 170 void IssueAccessToken(const std::string& account_key); |
| 161 void IssueExpiredAccessToken(const std::string& account_key); | 171 void IssueExpiredAccessToken(const std::string& account_key); |
| 162 void IssueError(const std::string& account_key); | 172 void IssueError(const std::string& account_key); |
| 163 | 173 |
| 164 // Accessors to account tracker and gcm driver. | 174 // Accessors to account tracker and gcm driver. |
| 165 GCMAccountTracker* tracker() { return tracker_.get(); } | 175 GCMAccountTracker* tracker() { return tracker_.get(); } |
| 166 CustomFakeGCMDriver* driver() { return &driver_; } | 176 CustomFakeGCMDriver* driver() { return &driver_; } |
| 167 | 177 |
| 178 // Accessors to private methods of account tracker. |
| 179 bool IsFetchingRequired() const; |
| 180 bool IsTokenReportingRequired() const; |
| 181 base::TimeDelta GetTimeToNextTokenFetching() const; |
| 182 |
| 168 private: | 183 private: |
| 169 CustomFakeGCMDriver driver_; | 184 CustomFakeGCMDriver driver_; |
| 170 | 185 |
| 171 base::MessageLoop message_loop_; | 186 base::MessageLoop message_loop_; |
| 172 net::TestURLFetcherFactory test_fetcher_factory_; | 187 net::TestURLFetcherFactory test_fetcher_factory_; |
| 173 scoped_ptr<FakeOAuth2TokenService> fake_token_service_; | 188 scoped_ptr<FakeOAuth2TokenService> fake_token_service_; |
| 174 scoped_ptr<FakeIdentityProvider> fake_identity_provider_; | 189 scoped_ptr<FakeIdentityProvider> fake_identity_provider_; |
| 175 scoped_ptr<GCMAccountTracker> tracker_; | 190 scoped_ptr<GCMAccountTracker> tracker_; |
| 176 }; | 191 }; |
| 177 | 192 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 fake_token_service_->IssueAllTokensForAccount( | 245 fake_token_service_->IssueAllTokensForAccount( |
| 231 account_key, MakeAccessToken(account_key), base::Time::Now()); | 246 account_key, MakeAccessToken(account_key), base::Time::Now()); |
| 232 } | 247 } |
| 233 | 248 |
| 234 void GCMAccountTrackerTest::IssueError(const std::string& account_key) { | 249 void GCMAccountTrackerTest::IssueError(const std::string& account_key) { |
| 235 fake_token_service_->IssueErrorForAllPendingRequestsForAccount( | 250 fake_token_service_->IssueErrorForAllPendingRequestsForAccount( |
| 236 account_key, | 251 account_key, |
| 237 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); | 252 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); |
| 238 } | 253 } |
| 239 | 254 |
| 255 bool GCMAccountTrackerTest::IsFetchingRequired() const { |
| 256 return tracker_->IsTokenFetchingRequired(); |
| 257 } |
| 258 |
| 259 bool GCMAccountTrackerTest::IsTokenReportingRequired() const { |
| 260 return tracker_->IsTokenReportingRequired(); |
| 261 } |
| 262 |
| 263 base::TimeDelta GCMAccountTrackerTest::GetTimeToNextTokenFetching() const { |
| 264 return tracker_->GetTimeToNextTokenFetching(); |
| 265 } |
| 266 |
| 240 TEST_F(GCMAccountTrackerTest, NoAccounts) { | 267 TEST_F(GCMAccountTrackerTest, NoAccounts) { |
| 241 EXPECT_FALSE(driver()->update_accounts_called()); | 268 EXPECT_FALSE(driver()->update_accounts_called()); |
| 242 tracker()->Start(); | 269 tracker()->Start(); |
| 243 // Callback should not be called if there where no accounts provided. | 270 // Callback should not be called if there where no accounts provided. |
| 244 EXPECT_FALSE(driver()->update_accounts_called()); | 271 EXPECT_FALSE(driver()->update_accounts_called()); |
| 245 EXPECT_TRUE(driver()->accounts().empty()); | 272 EXPECT_TRUE(driver()->accounts().empty()); |
| 246 } | 273 } |
| 247 | 274 |
| 248 // Verifies that callback is called after a token is issued for a single account | 275 // Verifies that callback is called after a token is issued for a single account |
| 249 // with a specific scope. In this scenario, the underlying account tracker is | 276 // with a specific scope. In this scenario, the underlying account tracker is |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 StartAccountSignIn(kAccountId1); | 423 StartAccountSignIn(kAccountId1); |
| 397 tracker()->Start(); | 424 tracker()->Start(); |
| 398 FinishAccountSignIn(kAccountId1); | 425 FinishAccountSignIn(kAccountId1); |
| 399 | 426 |
| 400 EXPECT_EQ(0UL, tracker()->get_pending_token_request_count()); | 427 EXPECT_EQ(0UL, tracker()->get_pending_token_request_count()); |
| 401 driver()->SetConnected(true); | 428 driver()->SetConnected(true); |
| 402 | 429 |
| 403 EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); | 430 EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); |
| 404 } | 431 } |
| 405 | 432 |
| 406 TEST_F(GCMAccountTrackerTest, IvalidateExpiredTokens) { | 433 TEST_F(GCMAccountTrackerTest, InvalidateExpiredTokens) { |
| 407 StartAccountSignIn(kAccountId1); | 434 StartAccountSignIn(kAccountId1); |
| 408 StartAccountSignIn(kAccountId2); | 435 StartAccountSignIn(kAccountId2); |
| 409 tracker()->Start(); | 436 tracker()->Start(); |
| 410 FinishAccountSignIn(kAccountId1); | 437 FinishAccountSignIn(kAccountId1); |
| 411 FinishAccountSignIn(kAccountId2); | 438 FinishAccountSignIn(kAccountId2); |
| 412 | 439 |
| 413 EXPECT_EQ(2UL, tracker()->get_pending_token_request_count()); | 440 EXPECT_EQ(2UL, tracker()->get_pending_token_request_count()); |
| 414 | 441 |
| 415 IssueExpiredAccessToken(kAccountId1); | 442 IssueExpiredAccessToken(kAccountId1); |
| 416 IssueAccessToken(kAccountId2); | 443 IssueAccessToken(kAccountId2); |
| 417 // Because the first token is expired, we expect the sanitize to kick in and | 444 // Because the first token is expired, we expect the sanitize to kick in and |
| 418 // clean it up before the SetAccessToken is called. This also means a new | 445 // clean it up before the SetAccessToken is called. This also means a new |
| 419 // token request will be issued | 446 // token request will be issued |
| 420 EXPECT_FALSE(driver()->update_accounts_called()); | 447 EXPECT_FALSE(driver()->update_accounts_called()); |
| 421 EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); | 448 EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); |
| 422 } | 449 } |
| 423 | 450 |
| 451 // Testing for whether there are still more tokens to be fetched. Typically the |
| 452 // need for token fetching triggers immediate request, unless there is no |
| 453 // connection, that is why connection is set on and off in this test. |
| 454 TEST_F(GCMAccountTrackerTest, IsTokenFetchingRequired) { |
| 455 tracker()->Start(); |
| 456 driver()->SetConnected(false); |
| 457 EXPECT_FALSE(IsFetchingRequired()); |
| 458 StartAccountSignIn(kAccountId1); |
| 459 FinishAccountSignIn(kAccountId1); |
| 460 EXPECT_TRUE(IsFetchingRequired()); |
| 461 |
| 462 driver()->SetConnected(true); |
| 463 EXPECT_FALSE(IsFetchingRequired()); // Indicates that fetching has started. |
| 464 IssueAccessToken(kAccountId1); |
| 465 EXPECT_FALSE(IsFetchingRequired()); |
| 466 |
| 467 driver()->SetConnected(false); |
| 468 StartAccountSignIn(kAccountId2); |
| 469 FinishAccountSignIn(kAccountId2); |
| 470 EXPECT_TRUE(IsFetchingRequired()); |
| 471 |
| 472 IssueExpiredAccessToken(kAccountId2); |
| 473 // Make sure that if the token was expired it is still needed. |
| 474 EXPECT_TRUE(IsFetchingRequired()); |
| 475 } |
| 476 |
| 477 // Tests what is the expected time to the next token fetching. |
| 478 TEST_F(GCMAccountTrackerTest, GetTimeToNextTokenFetching) { |
| 479 tracker()->Start(); |
| 480 // At this point the last token fetch time is never. |
| 481 EXPECT_EQ(base::TimeDelta(), GetTimeToNextTokenFetching()); |
| 482 |
| 483 driver()->SetLastTokenFetchTime(base::Time::Now()); |
| 484 EXPECT_TRUE(GetTimeToNextTokenFetching() <= |
| 485 base::TimeDelta::FromSeconds(12 * 60 * 60)); |
| 486 } |
| 487 |
| 488 // Tests conditions when token reporting is required. |
| 489 TEST_F(GCMAccountTrackerTest, IsTokenReportingRequired) { |
| 490 tracker()->Start(); |
| 491 // Required because it is overdue. |
| 492 EXPECT_TRUE(IsTokenReportingRequired()); |
| 493 |
| 494 // Not required because it just happened. |
| 495 driver()->SetLastTokenFetchTime(base::Time::Now()); |
| 496 EXPECT_FALSE(IsTokenReportingRequired()); |
| 497 |
| 498 SignInAccount(kAccountId1); |
| 499 IssueAccessToken(kAccountId1); |
| 500 driver()->ResetResults(); |
| 501 // Reporting was triggered, which means testing for required will give false, |
| 502 // but we have the update call. |
| 503 SignOutAccount(kAccountId1); |
| 504 EXPECT_TRUE(driver()->update_accounts_called()); |
| 505 EXPECT_FALSE(IsTokenReportingRequired()); |
| 506 } |
| 507 |
| 424 // TODO(fgorski): Add test for adding account after removal >> make sure it does | 508 // TODO(fgorski): Add test for adding account after removal >> make sure it does |
| 425 // not mark removal. | 509 // not mark removal. |
| 426 | 510 |
| 427 } // namespace gcm | 511 } // namespace gcm |
| OLD | NEW |