1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/services/gcm/gcm_account_tracker.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <map>
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <string>
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/scoped_ptr.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/message_loop/message_loop.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "google_apis/gaia/fake_identity_provider.h"
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "google_apis/gaia/fake_oauth2_token_service.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "google_apis/gaia/google_service_auth_error.h"
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/http_status_code.h"
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/url_request/test_url_fetcher_factory.h"
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/url_request/url_request_test_util.h"
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "testing/gtest/include/gtest/gtest.h"
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace gcm {
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace {
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdochconst char kAccountId1[] = "account_1";
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdochconst char kAccountId2[] = "account_2";
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstd::string AccountKeyToObfuscatedId(const std::string email) {
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return "obfid-" + email;
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstd::string GetValidTokenInfoResponse(const std::string account_key) {
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return std::string("{ \"id\": \"") + AccountKeyToObfuscatedId(account_key) +
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch         "\" }";
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstd::string MakeAccessToken(const std::string& account_key) {
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return "access_token-" + account_key;
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass GCMAccountTrackerTest : public testing::Test {
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public:
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  GCMAccountTrackerTest();
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ~GCMAccountTrackerTest();
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Callback for the account tracker.
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void UpdateAccounts(const std::map<std::string, std::string>& accounts);
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Helpers to pass fake events to the tracker. Tests should have either a pair
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // of Start/FinishAccountSignIn or SignInAccount per account. Don't mix.
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Call to SignOutAccount is not mandatory.
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void StartAccountSignIn(const std::string& account_key);
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void FinishAccountSignIn(const std::string& account_key);
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void SignInAccount(const std::string& account_key);
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void SignOutAccount(const std::string& account_key);
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Helpers for dealing with OAuth2 access token requests.
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void IssueAccessToken(const std::string& account_key);
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void IssueError(const std::string& account_key);
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Test results and helpers.
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void ResetResults();
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool update_accounts_called() const { return update_accounts_called_; }
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const std::map<std::string, std::string>& accounts() const {
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return accounts_;
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Accessor to account tracker.
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  GCMAccountTracker* tracker() { return tracker_.get(); }
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private:
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::map<std::string, std::string> accounts_;
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool update_accounts_called_;
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::MessageLoop message_loop_;
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  net::TestURLFetcherFactory test_fetcher_factory_;
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<FakeOAuth2TokenService> fake_token_service_;
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<FakeIdentityProvider> fake_identity_provider_;
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<GCMAccountTracker> tracker_;
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
83116680a4aac90f2aa7413d9095a592090648e557Ben MurdochGCMAccountTrackerTest::GCMAccountTrackerTest()
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : update_accounts_called_(false) {
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  fake_token_service_.reset(new FakeOAuth2TokenService());
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  fake_identity_provider_.reset(
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new FakeIdentityProvider(fake_token_service_.get()));
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<gaia::AccountTracker> gaia_account_tracker(
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new gaia::AccountTracker(fake_identity_provider_.get(),
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                               new net::TestURLRequestContextGetter(
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                   message_loop_.message_loop_proxy())));
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker_.reset(new GCMAccountTracker(
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      gaia_account_tracker.Pass(),
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      base::Bind(&GCMAccountTrackerTest::UpdateAccounts,
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 base::Unretained(this))));
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
101116680a4aac90f2aa7413d9095a592090648e557Ben MurdochGCMAccountTrackerTest::~GCMAccountTrackerTest() {
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (tracker_)
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    tracker_->Shutdown();
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid GCMAccountTrackerTest::UpdateAccounts(
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::map<std::string, std::string>& accounts) {
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  update_accounts_called_ = true;
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  accounts_ = accounts;
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
112116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid GCMAccountTrackerTest::ResetResults() {
113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  accounts_.clear();
114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  update_accounts_called_ = false;
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
117116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid GCMAccountTrackerTest::StartAccountSignIn(const std::string& account_key) {
118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  fake_identity_provider_->LogIn(account_key);
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  fake_token_service_->AddAccount(account_key);
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
122116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid GCMAccountTrackerTest::FinishAccountSignIn(
123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& account_key) {
124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueAccessToken(account_key);
125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  net::TestURLFetcher* fetcher = test_fetcher_factory_.GetFetcherByID(
127116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      gaia::GaiaOAuthClient::kUrlFetcherId);
128116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_TRUE(fetcher);
129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  fetcher->set_response_code(net::HTTP_OK);
130116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  fetcher->SetResponseString(GetValidTokenInfoResponse(account_key));
131116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  fetcher->delegate()->OnURLFetchComplete(fetcher);
132116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
133116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
134116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid GCMAccountTrackerTest::SignInAccount(const std::string& account_key) {
135116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  StartAccountSignIn(account_key);
136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FinishAccountSignIn(account_key);
137116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
139116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid GCMAccountTrackerTest::SignOutAccount(const std::string& account_key) {
140116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  fake_token_service_->RemoveAccount(account_key);
141116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
143116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid GCMAccountTrackerTest::IssueAccessToken(const std::string& account_key) {
144116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  fake_token_service_->IssueAllTokensForAccount(
145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      account_key, MakeAccessToken(account_key), base::Time::Max());
146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
147116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
148116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid GCMAccountTrackerTest::IssueError(const std::string& account_key) {
149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  fake_token_service_->IssueErrorForAllPendingRequestsForAccount(
150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      account_key,
151116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
153116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
154116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(GCMAccountTrackerTest, NoAccounts) {
155116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_FALSE(update_accounts_called());
156116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Start();
157116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Callback should not be called if there where no accounts provided.
158116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_FALSE(update_accounts_called());
159116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(accounts().empty());
160116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Stop();
161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
163116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Verifies that callback is called after a token is issued for a single account
164116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// with a specific scope. In this scenario, the underlying account tracker is
165116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// still working when the CompleteCollectingTokens is called for the first time.
166116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(GCMAccountTrackerTest, SingleAccount) {
167116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  StartAccountSignIn(kAccountId1);
168116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
169116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Start();
170116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // We don't have any accounts to report, but given the inner account tracker
171116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // is still working we don't make a call with empty accounts list.
172116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_FALSE(update_accounts_called());
173116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
174116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // This concludes the work of inner account tracker.
175116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FinishAccountSignIn(kAccountId1);
176116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueAccessToken(kAccountId1);
177116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
178116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(update_accounts_called());
179116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::map<std::string, std::string> expected_accounts;
181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1);
182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(expected_accounts, accounts());
183116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Stop();
184116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
185116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
186116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(GCMAccountTrackerTest, MultipleAccounts) {
187116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  StartAccountSignIn(kAccountId1);
188116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  StartAccountSignIn(kAccountId2);
189116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Start();
191116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_FALSE(update_accounts_called());
192116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
193116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FinishAccountSignIn(kAccountId1);
194116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueAccessToken(kAccountId1);
195116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_FALSE(update_accounts_called());
196116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
197116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FinishAccountSignIn(kAccountId2);
198116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueAccessToken(kAccountId2);
199116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(update_accounts_called());
200116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
201116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::map<std::string, std::string> expected_accounts;
202116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1);
203116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  expected_accounts[kAccountId2] = MakeAccessToken(kAccountId2);
204116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(expected_accounts, accounts());
205116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
206116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Stop();
207116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
208116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
209116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(GCMAccountTrackerTest, AccountAdded) {
210116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Start();
211116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ResetResults();
212116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
213116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignInAccount(kAccountId1);
214116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_FALSE(update_accounts_called());
215116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
216116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueAccessToken(kAccountId1);
217116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(update_accounts_called());
218116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
219116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::map<std::string, std::string> expected_accounts;
220116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1);
221116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(expected_accounts, accounts());
222116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
223116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Stop();
224116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
225116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
226116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(GCMAccountTrackerTest, AccountRemoved) {
227116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignInAccount(kAccountId1);
228116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignInAccount(kAccountId2);
229116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
230116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Start();
231116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueAccessToken(kAccountId1);
232116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueAccessToken(kAccountId2);
233116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(update_accounts_called());
234116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
235116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ResetResults();
236116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_FALSE(update_accounts_called());
237116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
238116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignOutAccount(kAccountId2);
239116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(update_accounts_called());
240116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
241116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::map<std::string, std::string> expected_accounts;
242116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1);
243116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(expected_accounts, accounts());
244116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
245116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Stop();
246116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
247116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
248116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(GCMAccountTrackerTest, GetTokenFailed) {
249116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignInAccount(kAccountId1);
250116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignInAccount(kAccountId2);
251116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
252116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Start();
253116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueAccessToken(kAccountId1);
254116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_FALSE(update_accounts_called());
255116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
256116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueError(kAccountId2);
257116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(update_accounts_called());
258116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
259116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::map<std::string, std::string> expected_accounts;
260116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1);
261116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(expected_accounts, accounts());
262116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
263116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Stop();
264116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
265116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
266116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(GCMAccountTrackerTest, GetTokenFailedAccountRemoved) {
267116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignInAccount(kAccountId1);
268116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignInAccount(kAccountId2);
269116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
270116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Start();
271116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueAccessToken(kAccountId1);
272116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueError(kAccountId2);
273116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
274116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ResetResults();
275116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignOutAccount(kAccountId2);
276116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(update_accounts_called());
277116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
278116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::map<std::string, std::string> expected_accounts;
279116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1);
280116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(expected_accounts, accounts());
281116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
282116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Stop();
283116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
284116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
285116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(GCMAccountTrackerTest, AccountRemovedWhileRequestsPending) {
286116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignInAccount(kAccountId1);
287116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignInAccount(kAccountId2);
288116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
289116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Start();
290116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueAccessToken(kAccountId1);
291116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_FALSE(update_accounts_called());
292116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
293116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SignOutAccount(kAccountId2);
294116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IssueAccessToken(kAccountId2);
295116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(update_accounts_called());
296116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
297116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::map<std::string, std::string> expected_accounts;
298116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  expected_accounts[kAccountId1] = MakeAccessToken(kAccountId1);
299116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(expected_accounts, accounts());
300116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
301116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  tracker()->Stop();
302116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
303116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
304116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// TODO(fgorski): Add test for adding account after removal >> make sure it does
305116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// not mark removal.
306116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
307116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace gcm
308