| 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" |
| 11 #include "components/gcm_driver/fake_gcm_driver.h" | 13 #include "components/gcm_driver/fake_gcm_driver.h" |
| 12 #include "google_apis/gaia/fake_identity_provider.h" | 14 #include "google_apis/gaia/fake_identity_provider.h" |
| 13 #include "google_apis/gaia/fake_oauth2_token_service.h" | 15 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 14 #include "google_apis/gaia/google_service_auth_error.h" | 16 #include "google_apis/gaia/google_service_auth_error.h" |
| 15 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 16 #include "net/url_request/test_url_fetcher_factory.h" | 18 #include "net/url_request/test_url_fetcher_factory.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 namespace gcm { | 22 namespace gcm { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 public: | 69 public: |
| 68 CustomFakeGCMDriver(); | 70 CustomFakeGCMDriver(); |
| 69 ~CustomFakeGCMDriver() override; | 71 ~CustomFakeGCMDriver() override; |
| 70 | 72 |
| 71 // GCMDriver overrides: | 73 // GCMDriver overrides: |
| 72 void SetAccountTokens( | 74 void SetAccountTokens( |
| 73 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) override; | 75 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) override; |
| 74 void AddConnectionObserver(GCMConnectionObserver* observer) override; | 76 void AddConnectionObserver(GCMConnectionObserver* observer) override; |
| 75 void RemoveConnectionObserver(GCMConnectionObserver* observer) override; | 77 void RemoveConnectionObserver(GCMConnectionObserver* observer) override; |
| 76 bool IsConnected() const override { return connected_; } | 78 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_; | |
| 102 | 101 |
| 103 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); | 102 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); |
| 104 }; | 103 }; |
| 105 | 104 |
| 106 CustomFakeGCMDriver::CustomFakeGCMDriver() | 105 CustomFakeGCMDriver::CustomFakeGCMDriver() |
| 107 : connected_(true), | 106 : connected_(true), |
| 108 update_accounts_called_(false), | 107 update_accounts_called_(false), |
| 109 last_connection_observer_(NULL), | 108 last_connection_observer_(NULL), |
| 110 removed_connection_observer_(NULL) { | 109 removed_connection_observer_(NULL) { |
| 111 } | 110 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 135 last_connection_observer_->OnConnected(ip_endpoint_); | 134 last_connection_observer_->OnConnected(ip_endpoint_); |
| 136 } | 135 } |
| 137 | 136 |
| 138 void CustomFakeGCMDriver::ResetResults() { | 137 void CustomFakeGCMDriver::ResetResults() { |
| 139 accounts_.clear(); | 138 accounts_.clear(); |
| 140 update_accounts_called_ = false; | 139 update_accounts_called_ = false; |
| 141 last_connection_observer_ = NULL; | 140 last_connection_observer_ = NULL; |
| 142 removed_connection_observer_ = NULL; | 141 removed_connection_observer_ = NULL; |
| 143 } | 142 } |
| 144 | 143 |
| 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 | |
| 154 } // namespace | 144 } // namespace |
| 155 | 145 |
| 156 class GCMAccountTrackerTest : public testing::Test { | 146 class GCMAccountTrackerTest : public testing::Test { |
| 157 public: | 147 public: |
| 158 GCMAccountTrackerTest(); | 148 GCMAccountTrackerTest(); |
| 159 ~GCMAccountTrackerTest() override; | 149 ~GCMAccountTrackerTest() override; |
| 160 | 150 |
| 161 // Helpers to pass fake events to the tracker. Tests should have either a pair | 151 // Helpers to pass fake events to the tracker. Tests should have either a pair |
| 162 // of Start/FinishAccountSignIn or SignInAccount per account. Don't mix. | 152 // of Start/FinishAccountSignIn or SignInAccount per account. Don't mix. |
| 163 // Call to SignOutAccount is not mandatory. | 153 // Call to SignOutAccount is not mandatory. |
| 164 void StartAccountSignIn(const std::string& account_key); | 154 void StartAccountSignIn(const std::string& account_key); |
| 165 void FinishAccountSignIn(const std::string& account_key); | 155 void FinishAccountSignIn(const std::string& account_key); |
| 166 void SignInAccount(const std::string& account_key); | 156 void SignInAccount(const std::string& account_key); |
| 167 void SignOutAccount(const std::string& account_key); | 157 void SignOutAccount(const std::string& account_key); |
| 168 | 158 |
| 169 // Helpers for dealing with OAuth2 access token requests. | 159 // Helpers for dealing with OAuth2 access token requests. |
| 170 void IssueAccessToken(const std::string& account_key); | 160 void IssueAccessToken(const std::string& account_key); |
| 171 void IssueExpiredAccessToken(const std::string& account_key); | 161 void IssueExpiredAccessToken(const std::string& account_key); |
| 172 void IssueError(const std::string& account_key); | 162 void IssueError(const std::string& account_key); |
| 173 | 163 |
| 174 // Accessors to account tracker and gcm driver. | 164 // Accessors to account tracker and gcm driver. |
| 175 GCMAccountTracker* tracker() { return tracker_.get(); } | 165 GCMAccountTracker* tracker() { return tracker_.get(); } |
| 176 CustomFakeGCMDriver* driver() { return &driver_; } | 166 CustomFakeGCMDriver* driver() { return &driver_; } |
| 177 | 167 |
| 178 // Accessors to private methods of account tracker. | |
| 179 bool IsFetchingRequired() const; | |
| 180 bool IsTokenReportingRequired() const; | |
| 181 base::TimeDelta GetTimeToNextTokenReporting() const; | |
| 182 | |
| 183 private: | 168 private: |
| 184 CustomFakeGCMDriver driver_; | 169 CustomFakeGCMDriver driver_; |
| 185 | 170 |
| 186 base::MessageLoop message_loop_; | 171 base::MessageLoop message_loop_; |
| 187 net::TestURLFetcherFactory test_fetcher_factory_; | 172 net::TestURLFetcherFactory test_fetcher_factory_; |
| 188 scoped_ptr<FakeOAuth2TokenService> fake_token_service_; | 173 scoped_ptr<FakeOAuth2TokenService> fake_token_service_; |
| 189 scoped_ptr<FakeIdentityProvider> fake_identity_provider_; | 174 scoped_ptr<FakeIdentityProvider> fake_identity_provider_; |
| 190 scoped_ptr<GCMAccountTracker> tracker_; | 175 scoped_ptr<GCMAccountTracker> tracker_; |
| 191 }; | 176 }; |
| 192 | 177 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 fake_token_service_->IssueAllTokensForAccount( | 230 fake_token_service_->IssueAllTokensForAccount( |
| 246 account_key, MakeAccessToken(account_key), base::Time::Now()); | 231 account_key, MakeAccessToken(account_key), base::Time::Now()); |
| 247 } | 232 } |
| 248 | 233 |
| 249 void GCMAccountTrackerTest::IssueError(const std::string& account_key) { | 234 void GCMAccountTrackerTest::IssueError(const std::string& account_key) { |
| 250 fake_token_service_->IssueErrorForAllPendingRequestsForAccount( | 235 fake_token_service_->IssueErrorForAllPendingRequestsForAccount( |
| 251 account_key, | 236 account_key, |
| 252 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); | 237 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); |
| 253 } | 238 } |
| 254 | 239 |
| 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::GetTimeToNextTokenReporting() const { | |
| 264 return tracker_->GetTimeToNextTokenReporting(); | |
| 265 } | |
| 266 | |
| 267 TEST_F(GCMAccountTrackerTest, NoAccounts) { | 240 TEST_F(GCMAccountTrackerTest, NoAccounts) { |
| 268 EXPECT_FALSE(driver()->update_accounts_called()); | 241 EXPECT_FALSE(driver()->update_accounts_called()); |
| 269 tracker()->Start(); | 242 tracker()->Start(); |
| 270 // Callback should not be called if there where no accounts provided. | 243 // Callback should not be called if there where no accounts provided. |
| 271 EXPECT_FALSE(driver()->update_accounts_called()); | 244 EXPECT_FALSE(driver()->update_accounts_called()); |
| 272 EXPECT_TRUE(driver()->accounts().empty()); | 245 EXPECT_TRUE(driver()->accounts().empty()); |
| 273 } | 246 } |
| 274 | 247 |
| 275 // Verifies that callback is called after a token is issued for a single account | 248 // Verifies that callback is called after a token is issued for a single account |
| 276 // with a specific scope. In this scenario, the underlying account tracker is | 249 // 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... |
| 423 StartAccountSignIn(kAccountId1); | 396 StartAccountSignIn(kAccountId1); |
| 424 tracker()->Start(); | 397 tracker()->Start(); |
| 425 FinishAccountSignIn(kAccountId1); | 398 FinishAccountSignIn(kAccountId1); |
| 426 | 399 |
| 427 EXPECT_EQ(0UL, tracker()->get_pending_token_request_count()); | 400 EXPECT_EQ(0UL, tracker()->get_pending_token_request_count()); |
| 428 driver()->SetConnected(true); | 401 driver()->SetConnected(true); |
| 429 | 402 |
| 430 EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); | 403 EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); |
| 431 } | 404 } |
| 432 | 405 |
| 433 TEST_F(GCMAccountTrackerTest, InvalidateExpiredTokens) { | 406 TEST_F(GCMAccountTrackerTest, IvalidateExpiredTokens) { |
| 434 StartAccountSignIn(kAccountId1); | 407 StartAccountSignIn(kAccountId1); |
| 435 StartAccountSignIn(kAccountId2); | 408 StartAccountSignIn(kAccountId2); |
| 436 tracker()->Start(); | 409 tracker()->Start(); |
| 437 FinishAccountSignIn(kAccountId1); | 410 FinishAccountSignIn(kAccountId1); |
| 438 FinishAccountSignIn(kAccountId2); | 411 FinishAccountSignIn(kAccountId2); |
| 439 | 412 |
| 440 EXPECT_EQ(2UL, tracker()->get_pending_token_request_count()); | 413 EXPECT_EQ(2UL, tracker()->get_pending_token_request_count()); |
| 441 | 414 |
| 442 IssueExpiredAccessToken(kAccountId1); | 415 IssueExpiredAccessToken(kAccountId1); |
| 443 IssueAccessToken(kAccountId2); | 416 IssueAccessToken(kAccountId2); |
| 444 // Because the first token is expired, we expect the sanitize to kick in and | 417 // Because the first token is expired, we expect the sanitize to kick in and |
| 445 // clean it up before the SetAccessToken is called. This also means a new | 418 // clean it up before the SetAccessToken is called. This also means a new |
| 446 // token request will be issued | 419 // token request will be issued |
| 447 EXPECT_FALSE(driver()->update_accounts_called()); | 420 EXPECT_FALSE(driver()->update_accounts_called()); |
| 448 EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); | 421 EXPECT_EQ(1UL, tracker()->get_pending_token_request_count()); |
| 449 } | 422 } |
| 450 | 423 |
| 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, GetTimeToNextTokenReporting) { | |
| 479 tracker()->Start(); | |
| 480 // At this point the last token fetch time is never. | |
| 481 EXPECT_EQ(base::TimeDelta(), GetTimeToNextTokenReporting()); | |
| 482 | |
| 483 driver()->SetLastTokenFetchTime(base::Time::Now()); | |
| 484 EXPECT_TRUE(GetTimeToNextTokenReporting() <= | |
| 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 | |
| 508 // TODO(fgorski): Add test for adding account after removal >> make sure it does | 424 // TODO(fgorski): Add test for adding account after removal >> make sure it does |
| 509 // not mark removal. | 425 // not mark removal. |
| 510 | 426 |
| 511 } // namespace gcm | 427 } // namespace gcm |
| OLD | NEW |