blob: acc80815a074831feaaa9675a65d8a89cced4dc0 [file] [log] [blame]
blundell1e21bd32017-05-03 15:25:571// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/run_loop.h"
Colin Blundell2c8951de2017-07-07 13:23:456#include "build/build_config.h"
Colin Blundell704d198e2017-06-05 11:27:137#include "components/signin/core/browser/account_info.h"
blundell1e21bd32017-05-03 15:25:578#include "components/signin/core/browser/account_tracker_service.h"
Colin Blundelle89886a2017-06-12 11:26:569#include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
blundell1e21bd32017-05-03 15:25:5710#include "components/signin/core/browser/fake_signin_manager.h"
11#include "components/signin/core/browser/test_signin_client.h"
12#include "components/sync_preferences/testing_pref_service_syncable.h"
Colin Blundell86ee4102017-10-09 16:55:0313#include "google_apis/gaia/fake_oauth2_token_service_delegate.h"
blundell1e21bd32017-05-03 15:25:5714#include "mojo/public/cpp/bindings/binding_set.h"
15#include "services/identity/identity_service.h"
Colin Blundell57fb6502017-06-27 12:24:3416#include "services/identity/public/cpp/account_state.h"
Colin Blundelle89886a2017-06-12 11:26:5617#include "services/identity/public/cpp/scope_set.h"
blundell1e21bd32017-05-03 15:25:5718#include "services/identity/public/interfaces/constants.mojom.h"
19#include "services/identity/public/interfaces/identity_manager.mojom.h"
20#include "services/service_manager/public/cpp/binder_registry.h"
21#include "services/service_manager/public/cpp/service_context.h"
22#include "services/service_manager/public/cpp/service_test.h"
23#include "services/service_manager/public/interfaces/service_factory.mojom.h"
24
25namespace identity {
26namespace {
27
Colin Blundell2c8951de2017-07-07 13:23:4528#if defined(OS_CHROMEOS)
29using SigninManagerForTest = FakeSigninManagerBase;
30#else
31using SigninManagerForTest = FakeSigninManager;
32#endif // OS_CHROMEOS
33
Colin Blundellbf442682017-07-28 13:08:0534const char kTestGaiaId[] = "dummyId";
35const char kTestEmail[] = "[email protected]";
36const char kSecondaryTestGaiaId[] = "secondaryDummyId";
37const char kSecondaryTestEmail[] = "[email protected]";
38const char kTestRefreshToken[] = "dummy-refresh-token";
39const char kTestAccessToken[] = "access_token";
blundell1e21bd32017-05-03 15:25:5740
41class ServiceTestClient : public service_manager::test::ServiceTestClient,
42 public service_manager::mojom::ServiceFactory {
43 public:
44 ServiceTestClient(service_manager::test::ServiceTest* test,
Colin Blundell2f987fd2017-06-23 10:49:0645 AccountTrackerService* account_tracker,
Colin Blundelle89886a2017-06-12 11:26:5646 SigninManagerBase* signin_manager,
47 ProfileOAuth2TokenService* token_service)
blundell1e21bd32017-05-03 15:25:5748 : service_manager::test::ServiceTestClient(test),
Colin Blundell2f987fd2017-06-23 10:49:0649 account_tracker_(account_tracker),
Colin Blundelle89886a2017-06-12 11:26:5650 signin_manager_(signin_manager),
51 token_service_(token_service) {
blundell1e21bd32017-05-03 15:25:5752 registry_.AddInterface<service_manager::mojom::ServiceFactory>(
Colin Blundell7b2a9f02017-12-19 12:13:0353 base::BindRepeating(&ServiceTestClient::Create,
54 base::Unretained(this)));
blundell1e21bd32017-05-03 15:25:5755 }
56
57 protected:
58 void OnBindInterface(const service_manager::BindSourceInfo& source_info,
59 const std::string& interface_name,
60 mojo::ScopedMessagePipeHandle interface_pipe) override {
Ben Goodger21ada1e2017-07-19 14:53:0161 registry_.BindInterface(interface_name, std::move(interface_pipe));
blundell1e21bd32017-05-03 15:25:5762 }
63
64 void CreateService(service_manager::mojom::ServiceRequest request,
65 const std::string& name) override {
66 if (name == mojom::kServiceName) {
67 identity_service_context_.reset(new service_manager::ServiceContext(
Jeremy Roman83533d7a2017-11-15 23:39:0668 std::make_unique<IdentityService>(account_tracker_, signin_manager_,
Colin Blundell2f987fd2017-06-23 10:49:0669 token_service_),
blundell1e21bd32017-05-03 15:25:5770 std::move(request)));
71 }
72 }
73
Ben Goodger21ada1e2017-07-19 14:53:0174 void Create(service_manager::mojom::ServiceFactoryRequest request) {
blundell1e21bd32017-05-03 15:25:5775 service_factory_bindings_.AddBinding(this, std::move(request));
76 }
77
78 private:
Colin Blundell2f987fd2017-06-23 10:49:0679 AccountTrackerService* account_tracker_;
blundell1e21bd32017-05-03 15:25:5780 SigninManagerBase* signin_manager_;
Colin Blundelle89886a2017-06-12 11:26:5681 ProfileOAuth2TokenService* token_service_;
blundell1e21bd32017-05-03 15:25:5782 service_manager::BinderRegistry registry_;
83 mojo::BindingSet<service_manager::mojom::ServiceFactory>
84 service_factory_bindings_;
85 std::unique_ptr<service_manager::ServiceContext> identity_service_context_;
86};
87
Colin Blundell7b2a9f02017-12-19 12:13:0388class IdentityManagerImplTest : public service_manager::test::ServiceTest {
blundell1e21bd32017-05-03 15:25:5789 public:
Colin Blundell7b2a9f02017-12-19 12:13:0390 IdentityManagerImplTest()
Jay Civellib9b27a9c2017-10-27 22:08:5391 : ServiceTest("identity_unittests"),
blundell1e21bd32017-05-03 15:25:5792 signin_client_(&pref_service_),
Colin Blundell2c8951de2017-07-07 13:23:4593#if defined(OS_CHROMEOS)
blundell1e21bd32017-05-03 15:25:5794 signin_manager_(&signin_client_, &account_tracker_) {
Colin Blundell2c8951de2017-07-07 13:23:4595#else
96 signin_manager_(&signin_client_,
97 &token_service_,
98 &account_tracker_,
99 nullptr) {
100#endif
blundell1e21bd32017-05-03 15:25:57101 AccountTrackerService::RegisterPrefs(pref_service_.registry());
102 SigninManagerBase::RegisterProfilePrefs(pref_service_.registry());
103 SigninManagerBase::RegisterPrefs(pref_service_.registry());
104
105 account_tracker_.Initialize(&signin_client_);
106 }
107
Colin Blundelld829d852017-06-30 11:06:06108 void TearDown() override {
Colin Blundell7b2a9f02017-12-19 12:13:03109 // Shut down the SigninManager so that the IdentityManagerImpl doesn't end
110 // up outliving it.
Colin Blundelld829d852017-06-30 11:06:06111 signin_manager_.Shutdown();
112 ServiceTest::TearDown();
113 }
114
Colin Blundell704d198e2017-06-05 11:27:13115 void OnReceivedPrimaryAccountInfo(
Colin Blundell7b2a9f02017-12-19 12:13:03116 base::RepeatingClosure quit_closure,
Colin Blundell57fb6502017-06-27 12:24:34117 const base::Optional<AccountInfo>& account_info,
118 const AccountState& account_state) {
Colin Blundell704d198e2017-06-05 11:27:13119 primary_account_info_ = account_info;
Colin Blundell57fb6502017-06-27 12:24:34120 primary_account_state_ = account_state;
blundell1e21bd32017-05-03 15:25:57121 quit_closure.Run();
122 }
123
Colin Blundell7b2a9f02017-12-19 12:13:03124 void OnPrimaryAccountAvailable(base::RepeatingClosure quit_closure,
Colin Blundellbdf4642d2017-07-06 07:00:03125 AccountInfo* caller_account_info,
126 AccountState* caller_account_state,
127 const AccountInfo& account_info,
128 const AccountState& account_state) {
129 *caller_account_info = account_info;
130 *caller_account_state = account_state;
131 quit_closure.Run();
132 }
133
Colin Blundell2f987fd2017-06-23 10:49:06134 void OnReceivedAccountInfoFromGaiaId(
Colin Blundell7b2a9f02017-12-19 12:13:03135 base::RepeatingClosure quit_closure,
Colin Blundell57fb6502017-06-27 12:24:34136 const base::Optional<AccountInfo>& account_info,
137 const AccountState& account_state) {
Colin Blundell2f987fd2017-06-23 10:49:06138 account_info_from_gaia_id_ = account_info;
Colin Blundell57fb6502017-06-27 12:24:34139 account_state_from_gaia_id_ = account_state;
Colin Blundell2f987fd2017-06-23 10:49:06140 quit_closure.Run();
141 }
142
Colin Blundell7b2a9f02017-12-19 12:13:03143 void OnGotAccounts(base::RepeatingClosure quit_closure,
Colin Blundellbf442682017-07-28 13:08:05144 std::vector<mojom::AccountPtr>* output,
145 std::vector<mojom::AccountPtr> accounts) {
146 *output = std::move(accounts);
147 quit_closure.Run();
148 }
149
Colin Blundell7b2a9f02017-12-19 12:13:03150 void OnReceivedAccessToken(base::RepeatingClosure quit_closure,
Colin Blundelle89886a2017-06-12 11:26:56151 const base::Optional<std::string>& access_token,
152 base::Time expiration_time,
153 const GoogleServiceAuthError& error) {
154 access_token_ = access_token;
155 access_token_error_ = error;
156 quit_closure.Run();
157 }
158
blundell1e21bd32017-05-03 15:25:57159 protected:
Colin Blundell25a80822017-07-13 06:17:52160 void SetUp() override { ServiceTest::SetUp(); }
blundell1e21bd32017-05-03 15:25:57161
Colin Blundell7b2a9f02017-12-19 12:13:03162 mojom::IdentityManager* GetIdentityManagerImpl() {
Colin Blundell25a80822017-07-13 06:17:52163 if (!identity_manager_)
164 connector()->BindInterface(mojom::kServiceName, &identity_manager_);
165 return identity_manager_.get();
166 }
167
Colin Blundell7b2a9f02017-12-19 12:13:03168 void ResetIdentityManagerImpl() { identity_manager_.reset(); }
Colin Blundell25a80822017-07-13 06:17:52169
Colin Blundell7b2a9f02017-12-19 12:13:03170 void FlushIdentityManagerImplForTesting() {
171 GetIdentityManagerImpl();
Colin Blundell25a80822017-07-13 06:17:52172 identity_manager_.FlushForTesting();
173 }
174
Colin Blundell7b2a9f02017-12-19 12:13:03175 void SetIdentityManagerImplConnectionErrorHandler(
176 base::RepeatingClosure handler) {
177 GetIdentityManagerImpl();
Colin Blundell25a80822017-07-13 06:17:52178 identity_manager_.set_connection_error_handler(handler);
blundell1e21bd32017-05-03 15:25:57179 }
180
181 // service_manager::test::ServiceTest:
182 std::unique_ptr<service_manager::Service> CreateService() override {
Jeremy Roman83533d7a2017-11-15 23:39:06183 return std::make_unique<ServiceTestClient>(
Colin Blundell2f987fd2017-06-23 10:49:06184 this, &account_tracker_, &signin_manager_, &token_service_);
blundell1e21bd32017-05-03 15:25:57185 }
186
187 mojom::IdentityManagerPtr identity_manager_;
Colin Blundell704d198e2017-06-05 11:27:13188 base::Optional<AccountInfo> primary_account_info_;
Colin Blundell57fb6502017-06-27 12:24:34189 AccountState primary_account_state_;
Colin Blundell2f987fd2017-06-23 10:49:06190 base::Optional<AccountInfo> account_info_from_gaia_id_;
Colin Blundell57fb6502017-06-27 12:24:34191 AccountState account_state_from_gaia_id_;
Colin Blundelle89886a2017-06-12 11:26:56192 base::Optional<std::string> access_token_;
193 GoogleServiceAuthError access_token_error_;
blundell1e21bd32017-05-03 15:25:57194
Colin Blundell2f987fd2017-06-23 10:49:06195 AccountTrackerService* account_tracker() { return &account_tracker_; }
blundell1e21bd32017-05-03 15:25:57196 SigninManagerBase* signin_manager() { return &signin_manager_; }
Colin Blundelle89886a2017-06-12 11:26:56197 FakeProfileOAuth2TokenService* token_service() { return &token_service_; }
blundell1e21bd32017-05-03 15:25:57198
199 private:
200 sync_preferences::TestingPrefServiceSyncable pref_service_;
201 AccountTrackerService account_tracker_;
202 TestSigninClient signin_client_;
Colin Blundell2c8951de2017-07-07 13:23:45203 SigninManagerForTest signin_manager_;
Colin Blundelle89886a2017-06-12 11:26:56204 FakeProfileOAuth2TokenService token_service_;
blundell1e21bd32017-05-03 15:25:57205
Colin Blundell7b2a9f02017-12-19 12:13:03206 DISALLOW_COPY_AND_ASSIGN(IdentityManagerImplTest);
blundell1e21bd32017-05-03 15:25:57207};
208
Colin Blundell25a80822017-07-13 06:17:52209// Tests that it is not possible to connect to the Identity Manager if
210// initiated after SigninManager shutdown.
Colin Blundell7b2a9f02017-12-19 12:13:03211TEST_F(IdentityManagerImplTest, SigninManagerShutdownBeforeConnection) {
Colin Blundell25a80822017-07-13 06:17:52212 AccountInfo sentinel;
213 sentinel.account_id = "sentinel";
214 primary_account_info_ = sentinel;
215
216 // Ensure that the Identity Service has actually been created before
217 // invoking SigninManagerBase::Shutdown(), since otherwise this test will
218 // spin forever. Then reset the Identity Manager so that the next request
219 // makes a fresh connection.
Colin Blundell7b2a9f02017-12-19 12:13:03220 FlushIdentityManagerImplForTesting();
221 ResetIdentityManagerImpl();
Colin Blundell25a80822017-07-13 06:17:52222
Colin Blundell7b2a9f02017-12-19 12:13:03223 // Make a call to connect to the IdentityManagerImpl *after* SigninManager
Colin Blundell25a80822017-07-13 06:17:52224 // shutdown; it should get notified of an error when the Identity Service
225 // drops the connection.
226 signin_manager()->Shutdown();
Colin Blundelld829d852017-06-30 11:06:06227 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03228 SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure());
Colin Blundell25a80822017-07-13 06:17:52229
Colin Blundell7b2a9f02017-12-19 12:13:03230 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
231 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
232 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundell25a80822017-07-13 06:17:52233 run_loop.Run();
234
235 // Verify that the callback to GetPrimaryAccountInfo() was not invoked.
236 EXPECT_TRUE(primary_account_info_);
237 EXPECT_EQ("sentinel", primary_account_info_->account_id);
238}
239
240// Tests that the Identity Manager destroys itself on SigninManager shutdown.
Colin Blundell7b2a9f02017-12-19 12:13:03241TEST_F(IdentityManagerImplTest, SigninManagerShutdownAfterConnection) {
Colin Blundell25a80822017-07-13 06:17:52242 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03243 SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure());
Colin Blundelld829d852017-06-30 11:06:06244
Colin Blundell7b2a9f02017-12-19 12:13:03245 // Ensure that the IdentityManagerImpl instance has actually been created
246 // before invoking SigninManagerBase::Shutdown(), since otherwise this test
247 // will spin forever.
248 FlushIdentityManagerImplForTesting();
Colin Blundelld829d852017-06-30 11:06:06249 signin_manager()->Shutdown();
250 run_loop.Run();
251}
252
Colin Blundellf9792c02017-09-19 18:02:53253// Tests that the Identity Manager properly handles its own destruction in the
254// case where there is an active consumer request (i.e., a pending callback from
255// a Mojo call). In particular, this flow should not cause a DCHECK to fire in
256// debug mode.
Colin Blundell7b2a9f02017-12-19 12:13:03257TEST_F(IdentityManagerImplTest, IdentityManagerImplShutdownWithActiveRequest) {
Colin Blundellf9792c02017-09-19 18:02:53258 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03259 SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure());
Colin Blundellf9792c02017-09-19 18:02:53260
Colin Blundell7b2a9f02017-12-19 12:13:03261 // Call a method on the IdentityManagerImpl that will cause it to store a
262 // pending callback. This callback will never be invoked, so just pass dummy
263 // arguments to it.
264 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
265 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
266 base::Unretained(this), base::RepeatingClosure(), nullptr, nullptr));
Colin Blundellf9792c02017-09-19 18:02:53267
Colin Blundell7b2a9f02017-12-19 12:13:03268 // Ensure that the IdentityManagerImpl has received the above call before
Colin Blundellf9792c02017-09-19 18:02:53269 // invoking SigninManagerBase::Shutdown(), as otherwise this test is
270 // pointless.
Colin Blundell7b2a9f02017-12-19 12:13:03271 FlushIdentityManagerImplForTesting();
Colin Blundellf9792c02017-09-19 18:02:53272
Colin Blundell7b2a9f02017-12-19 12:13:03273 // This flow is what would cause a DCHECK to fire if IdentityManagerImpl is
274 // not properly closing its binding on shutdown.
Colin Blundellf9792c02017-09-19 18:02:53275 signin_manager()->Shutdown();
276 run_loop.Run();
277}
278
Colin Blundell2f987fd2017-06-23 10:49:06279// Check that the primary account info is null if not signed in.
Colin Blundell7b2a9f02017-12-19 12:13:03280TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoNotSignedIn) {
blundell1e21bd32017-05-03 15:25:57281 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03282 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
283 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
284 base::Unretained(this), run_loop.QuitClosure()));
blundell1e21bd32017-05-03 15:25:57285 run_loop.Run();
Colin Blundell704d198e2017-06-05 11:27:13286 EXPECT_FALSE(primary_account_info_);
blundell1e21bd32017-05-03 15:25:57287}
288
Colin Blundell57fb6502017-06-27 12:24:34289// Check that the primary account info has expected values if signed in without
290// a refresh token available.
Colin Blundell7b2a9f02017-12-19 12:13:03291TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoSignedInNoRefreshToken) {
blundell1e21bd32017-05-03 15:25:57292 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
293 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03294 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
295 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
296 base::Unretained(this), run_loop.QuitClosure()));
blundell1e21bd32017-05-03 15:25:57297 run_loop.Run();
Colin Blundell704d198e2017-06-05 11:27:13298 EXPECT_TRUE(primary_account_info_);
Colin Blundell57fb6502017-06-27 12:24:34299 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
300 primary_account_info_->account_id);
Colin Blundell704d198e2017-06-05 11:27:13301 EXPECT_EQ(kTestGaiaId, primary_account_info_->gaia);
302 EXPECT_EQ(kTestEmail, primary_account_info_->email);
Colin Blundell57fb6502017-06-27 12:24:34303 EXPECT_FALSE(primary_account_state_.has_refresh_token);
Colin Blundellbdf4642d2017-07-06 07:00:03304 EXPECT_TRUE(primary_account_state_.is_primary_account);
Colin Blundell57fb6502017-06-27 12:24:34305}
306
307// Check that the primary account info has expected values if signed in with a
308// refresh token available.
Colin Blundell7b2a9f02017-12-19 12:13:03309TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoSignedInRefreshToken) {
Colin Blundell57fb6502017-06-27 12:24:34310 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
311 token_service()->UpdateCredentials(
312 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
313 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03314 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
315 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
316 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundell57fb6502017-06-27 12:24:34317 run_loop.Run();
318 EXPECT_TRUE(primary_account_info_);
319 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
320 primary_account_info_->account_id);
321 EXPECT_EQ(kTestGaiaId, primary_account_info_->gaia);
322 EXPECT_EQ(kTestEmail, primary_account_info_->email);
323 EXPECT_TRUE(primary_account_state_.has_refresh_token);
Colin Blundellbdf4642d2017-07-06 07:00:03324 EXPECT_TRUE(primary_account_state_.is_primary_account);
325}
326
327// Check that GetPrimaryAccountWhenAvailable() returns immediately in the
328// case where the primary account is available when the call is received.
Colin Blundell7b2a9f02017-12-19 12:13:03329TEST_F(IdentityManagerImplTest, GetPrimaryAccountWhenAvailableSignedIn) {
Colin Blundellbdf4642d2017-07-06 07:00:03330 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
331 token_service()->UpdateCredentials(
332 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
333
334 AccountInfo account_info;
335 AccountState account_state;
336 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03337 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
338 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
339 base::Unretained(this), run_loop.QuitClosure(),
340 base::Unretained(&account_info), base::Unretained(&account_state)));
Colin Blundellbdf4642d2017-07-06 07:00:03341 run_loop.Run();
342
343 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
344 account_info.account_id);
345 EXPECT_EQ(kTestGaiaId, account_info.gaia);
346 EXPECT_EQ(kTestEmail, account_info.email);
347 EXPECT_TRUE(account_state.has_refresh_token);
348 EXPECT_TRUE(account_state.is_primary_account);
349}
350
351// Check that GetPrimaryAccountWhenAvailable() returns the expected account
352// info in the case where the primary account is made available *after* the
353// call is received.
Colin Blundell7b2a9f02017-12-19 12:13:03354TEST_F(IdentityManagerImplTest, GetPrimaryAccountWhenAvailableSignInLater) {
Colin Blundellbdf4642d2017-07-06 07:00:03355 AccountInfo account_info;
356 AccountState account_state;
357
358 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03359 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
360 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
361 base::Unretained(this), run_loop.QuitClosure(),
362 base::Unretained(&account_info), base::Unretained(&account_state)));
Colin Blundellbdf4642d2017-07-06 07:00:03363
364 // Verify that the primary account info is not currently available (this also
365 // serves to ensure that the preceding call has been received by the Identity
366 // Manager before proceeding).
367 base::RunLoop run_loop2;
Colin Blundell7b2a9f02017-12-19 12:13:03368 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
369 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
370 base::Unretained(this), run_loop2.QuitClosure()));
Colin Blundellbdf4642d2017-07-06 07:00:03371 run_loop2.Run();
372 EXPECT_FALSE(primary_account_info_);
373
374 // Make the primary account available and check that the callback is invoked
375 // as expected.
376 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
377 token_service()->UpdateCredentials(
378 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
379 run_loop.Run();
380
381 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
382 account_info.account_id);
383 EXPECT_EQ(kTestGaiaId, account_info.gaia);
384 EXPECT_EQ(kTestEmail, account_info.email);
385 EXPECT_TRUE(account_state.has_refresh_token);
386 EXPECT_TRUE(account_state.is_primary_account);
387}
388
389// Check that GetPrimaryAccountWhenAvailable() returns the expected account
390// info in the case where signin is done before the call is received but the
391// refresh token is made available only *after* the call is received.
Colin Blundell7b2a9f02017-12-19 12:13:03392TEST_F(IdentityManagerImplTest,
393 GetPrimaryAccountWhenAvailableTokenAvailableLater) {
Colin Blundellbdf4642d2017-07-06 07:00:03394 AccountInfo account_info;
395 AccountState account_state;
396
397 // Sign in, but don't set the refresh token yet.
398 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
399 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03400 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
401 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
402 base::Unretained(this), run_loop.QuitClosure(),
403 base::Unretained(&account_info), base::Unretained(&account_state)));
Colin Blundellbdf4642d2017-07-06 07:00:03404
405 // Verify that the primary account info is present, but that the primary
406 // account is not yet considered available (this also
407 // serves to ensure that the preceding call has been received by the Identity
408 // Manager before proceeding).
409 base::RunLoop run_loop2;
Colin Blundell7b2a9f02017-12-19 12:13:03410 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
411 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
412 base::Unretained(this), run_loop2.QuitClosure()));
Colin Blundellbdf4642d2017-07-06 07:00:03413 run_loop2.Run();
414
415 EXPECT_TRUE(primary_account_info_);
416 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
417 primary_account_info_->account_id);
418 EXPECT_TRUE(account_info.account_id.empty());
419
420 // Set the refresh token and check that the callback is invoked as expected
421 // (i.e., the primary account is now considered available).
422 token_service()->UpdateCredentials(
423 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
424 run_loop.Run();
425
426 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
427 account_info.account_id);
428 EXPECT_EQ(kTestGaiaId, account_info.gaia);
429 EXPECT_EQ(kTestEmail, account_info.email);
430 EXPECT_TRUE(account_state.has_refresh_token);
431 EXPECT_TRUE(account_state.is_primary_account);
432}
433
Colin Blundell2c8951de2017-07-07 13:23:45434// Check that GetPrimaryAccountWhenAvailable() returns the expected account info
435// in the case where the token is available before the call is received but the
436// account is made authenticated only *after* the call is received. This test is
437// relevant only on non-ChromeOS platforms, as the flow being tested here is not
438// possible on ChromeOS.
439#if !defined(OS_CHROMEOS)
Colin Blundell7b2a9f02017-12-19 12:13:03440TEST_F(IdentityManagerImplTest,
Colin Blundell2c8951de2017-07-07 13:23:45441 GetPrimaryAccountWhenAvailableAuthenticationAvailableLater) {
442 AccountInfo account_info;
443 AccountState account_state;
444
445 // Set the refresh token, but don't sign in yet.
446 std::string account_id_to_use =
447 account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail);
448 token_service()->UpdateCredentials(account_id_to_use, kTestRefreshToken);
449 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03450 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
451 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
452 base::Unretained(this), run_loop.QuitClosure(),
453 base::Unretained(&account_info), base::Unretained(&account_state)));
Colin Blundell2c8951de2017-07-07 13:23:45454
455 // Verify that the account is present and has a refresh token, but that the
456 // primary account is not yet considered available (this also serves to ensure
457 // that the preceding call has been received by the Identity Manager before
458 // proceeding).
459 base::RunLoop run_loop2;
Colin Blundell7b2a9f02017-12-19 12:13:03460 GetIdentityManagerImpl()->GetAccountInfoFromGaiaId(
Colin Blundell2c8951de2017-07-07 13:23:45461 kTestGaiaId,
Colin Blundell7b2a9f02017-12-19 12:13:03462 base::BindRepeating(
463 &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId,
464 base::Unretained(this), run_loop2.QuitClosure()));
Colin Blundell2c8951de2017-07-07 13:23:45465 run_loop2.Run();
466
467 EXPECT_TRUE(account_info_from_gaia_id_);
468 EXPECT_EQ(account_id_to_use, account_info_from_gaia_id_->account_id);
469 EXPECT_EQ(kTestGaiaId, account_info_from_gaia_id_->gaia);
470 EXPECT_EQ(kTestEmail, account_info_from_gaia_id_->email);
471 EXPECT_TRUE(account_state_from_gaia_id_.has_refresh_token);
472 EXPECT_FALSE(account_state_from_gaia_id_.is_primary_account);
473
474 EXPECT_TRUE(account_info.account_id.empty());
475
476 // Sign the user in and check that the callback is invoked as expected (i.e.,
477 // the primary account is now considered available). Note that it is necessary
478 // to call SignIn() here to ensure that GoogleSigninSucceeded() is fired by
479 // the fake signin manager.
480 static_cast<FakeSigninManager*>(signin_manager())
481 ->SignIn(kTestGaiaId, kTestEmail, "password");
482
483 run_loop.Run();
484
485 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
486 account_info.account_id);
487 EXPECT_EQ(kTestGaiaId, account_info.gaia);
488 EXPECT_EQ(kTestEmail, account_info.email);
489 EXPECT_TRUE(account_state.has_refresh_token);
490 EXPECT_TRUE(account_state.is_primary_account);
491}
492#endif
493
Colin Blundellbdf4642d2017-07-06 07:00:03494// Check that GetPrimaryAccountWhenAvailable() returns the expected account
495// info to all callers in the case where the primary account is made available
496// after multiple overlapping calls have been received.
Colin Blundell7b2a9f02017-12-19 12:13:03497TEST_F(IdentityManagerImplTest,
498 GetPrimaryAccountWhenAvailableOverlappingCalls) {
Colin Blundellbdf4642d2017-07-06 07:00:03499 AccountInfo account_info1;
500 AccountState account_state1;
501 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03502 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
503 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
504 base::Unretained(this), run_loop.QuitClosure(),
505 base::Unretained(&account_info1), base::Unretained(&account_state1)));
Colin Blundellbdf4642d2017-07-06 07:00:03506
507 AccountInfo account_info2;
508 AccountState account_state2;
509 base::RunLoop run_loop2;
Colin Blundell7b2a9f02017-12-19 12:13:03510 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
511 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
512 base::Unretained(this), run_loop2.QuitClosure(),
513 base::Unretained(&account_info2), base::Unretained(&account_state2)));
Colin Blundellbdf4642d2017-07-06 07:00:03514
515 // Verify that the primary account info is not currently available (this also
516 // serves to ensure that the preceding call has been received by the Identity
517 // Manager before proceeding).
518 base::RunLoop run_loop3;
Colin Blundell7b2a9f02017-12-19 12:13:03519 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
520 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
521 base::Unretained(this), run_loop3.QuitClosure()));
Colin Blundellbdf4642d2017-07-06 07:00:03522 run_loop3.Run();
523 EXPECT_FALSE(primary_account_info_);
524
525 // Make the primary account available and check that the callbacks are invoked
526 // as expected.
527 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
528 token_service()->UpdateCredentials(
529 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
530 run_loop.Run();
531 run_loop2.Run();
532
533 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
534 account_info1.account_id);
535 EXPECT_EQ(kTestGaiaId, account_info1.gaia);
536 EXPECT_EQ(kTestEmail, account_info1.email);
537 EXPECT_TRUE(account_state1.has_refresh_token);
538 EXPECT_TRUE(account_state1.is_primary_account);
539
540 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
541 account_info2.account_id);
542 EXPECT_EQ(kTestGaiaId, account_info2.gaia);
543 EXPECT_EQ(kTestEmail, account_info2.email);
544 EXPECT_TRUE(account_state2.has_refresh_token);
545 EXPECT_TRUE(account_state2.is_primary_account);
blundell1e21bd32017-05-03 15:25:57546}
547
Colin Blundell86ee4102017-10-09 16:55:03548// Check that GetPrimaryAccountWhenAvailable() doesn't return the account as
549// available if the refresh token has an auth error.
Colin Blundell7b2a9f02017-12-19 12:13:03550TEST_F(IdentityManagerImplTest,
Colin Blundell86ee4102017-10-09 16:55:03551 GetPrimaryAccountWhenAvailableRefreshTokenHasAuthError) {
552 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
553 token_service()->UpdateCredentials(
554 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
555 FakeOAuth2TokenServiceDelegate* delegate =
556 static_cast<FakeOAuth2TokenServiceDelegate*>(
557 token_service()->GetDelegate());
558 delegate->SetLastErrorForAccount(
559 signin_manager()->GetAuthenticatedAccountId(),
560 GoogleServiceAuthError(
561 GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS));
562
563 AccountInfo account_info;
564 AccountState account_state;
565 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03566 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
567 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
568 base::Unretained(this), run_loop.QuitClosure(),
569 base::Unretained(&account_info), base::Unretained(&account_state)));
Colin Blundell86ee4102017-10-09 16:55:03570
571 // Flush the Identity Manager and check that the callback didn't fire.
Colin Blundell7b2a9f02017-12-19 12:13:03572 FlushIdentityManagerImplForTesting();
Colin Blundell86ee4102017-10-09 16:55:03573 EXPECT_TRUE(account_info.account_id.empty());
574
575 // Clear the auth error, update credentials, and check that the callback
576 // fires.
577 delegate->SetLastErrorForAccount(
578 signin_manager()->GetAuthenticatedAccountId(), GoogleServiceAuthError());
579 token_service()->UpdateCredentials(
580 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
581 run_loop.Run();
582
583 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
584 account_info.account_id);
585 EXPECT_EQ(kTestGaiaId, account_info.gaia);
586 EXPECT_EQ(kTestEmail, account_info.email);
587 EXPECT_TRUE(account_state.has_refresh_token);
588 EXPECT_TRUE(account_state.is_primary_account);
589}
590
Colin Blundell2f987fd2017-06-23 10:49:06591// Check that the account info for a given GAIA ID is null if that GAIA ID is
592// unknown.
Colin Blundell7b2a9f02017-12-19 12:13:03593TEST_F(IdentityManagerImplTest, GetAccountInfoForUnknownGaiaID) {
Colin Blundell2f987fd2017-06-23 10:49:06594 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03595 GetIdentityManagerImpl()->GetAccountInfoFromGaiaId(
Colin Blundell2f987fd2017-06-23 10:49:06596 kTestGaiaId,
Colin Blundell7b2a9f02017-12-19 12:13:03597 base::BindRepeating(
598 &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId,
599 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundell2f987fd2017-06-23 10:49:06600 run_loop.Run();
601 EXPECT_FALSE(account_info_from_gaia_id_);
602}
603
604// Check that the account info for a given GAIA ID has expected values if that
Colin Blundell57fb6502017-06-27 12:24:34605// GAIA ID is known and there is no refresh token available for it.
Colin Blundell7b2a9f02017-12-19 12:13:03606TEST_F(IdentityManagerImplTest, GetAccountInfoForKnownGaiaIdNoRefreshToken) {
Colin Blundell57fb6502017-06-27 12:24:34607 std::string account_id =
608 account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail);
Colin Blundell2f987fd2017-06-23 10:49:06609 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03610 GetIdentityManagerImpl()->GetAccountInfoFromGaiaId(
Colin Blundell2f987fd2017-06-23 10:49:06611 kTestGaiaId,
Colin Blundell7b2a9f02017-12-19 12:13:03612 base::BindRepeating(
613 &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId,
614 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundell2f987fd2017-06-23 10:49:06615 run_loop.Run();
616 EXPECT_TRUE(account_info_from_gaia_id_);
Colin Blundell57fb6502017-06-27 12:24:34617 EXPECT_EQ(account_id, account_info_from_gaia_id_->account_id);
Colin Blundell2f987fd2017-06-23 10:49:06618 EXPECT_EQ(kTestGaiaId, account_info_from_gaia_id_->gaia);
619 EXPECT_EQ(kTestEmail, account_info_from_gaia_id_->email);
Colin Blundell57fb6502017-06-27 12:24:34620 EXPECT_FALSE(account_state_from_gaia_id_.has_refresh_token);
Colin Blundellbdf4642d2017-07-06 07:00:03621 EXPECT_FALSE(account_state_from_gaia_id_.is_primary_account);
Colin Blundell57fb6502017-06-27 12:24:34622}
623
624// Check that the account info for a given GAIA ID has expected values if that
625// GAIA ID is known and has a refresh token available.
Colin Blundell7b2a9f02017-12-19 12:13:03626TEST_F(IdentityManagerImplTest, GetAccountInfoForKnownGaiaIdRefreshToken) {
Colin Blundell57fb6502017-06-27 12:24:34627 std::string account_id =
628 account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail);
629 token_service()->UpdateCredentials(account_id, kTestRefreshToken);
630 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03631 GetIdentityManagerImpl()->GetAccountInfoFromGaiaId(
Colin Blundell57fb6502017-06-27 12:24:34632 kTestGaiaId,
Colin Blundell7b2a9f02017-12-19 12:13:03633 base::BindRepeating(
634 &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId,
635 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundell57fb6502017-06-27 12:24:34636 run_loop.Run();
637 EXPECT_TRUE(account_info_from_gaia_id_);
638 EXPECT_EQ(account_id, account_info_from_gaia_id_->account_id);
639 EXPECT_EQ(kTestGaiaId, account_info_from_gaia_id_->gaia);
640 EXPECT_EQ(kTestEmail, account_info_from_gaia_id_->email);
641 EXPECT_TRUE(account_state_from_gaia_id_.has_refresh_token);
Colin Blundellbdf4642d2017-07-06 07:00:03642 EXPECT_FALSE(account_state_from_gaia_id_.is_primary_account);
Colin Blundell2f987fd2017-06-23 10:49:06643}
644
Colin Blundellbf442682017-07-28 13:08:05645// Check the implementation of GetAccounts() when there are no accounts.
Colin Blundell7b2a9f02017-12-19 12:13:03646TEST_F(IdentityManagerImplTest, GetAccountsNoAccount) {
Colin Blundellbf442682017-07-28 13:08:05647 token_service()->LoadCredentials("dummy");
648
649 std::vector<mojom::AccountPtr> accounts;
650
651 // Check that an empty list is returned when there are no accounts.
652 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03653 GetIdentityManagerImpl()->GetAccounts(base::BindRepeating(
654 &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this),
655 run_loop.QuitClosure(), &accounts));
Colin Blundellbf442682017-07-28 13:08:05656 run_loop.Run();
657 EXPECT_EQ(0u, accounts.size());
658}
659
660// Check the implementation of GetAccounts() when there is a single account,
661// which is the primary account.
Colin Blundell7b2a9f02017-12-19 12:13:03662TEST_F(IdentityManagerImplTest, GetAccountsPrimaryAccount) {
Colin Blundellbf442682017-07-28 13:08:05663 token_service()->LoadCredentials("dummy");
664 std::vector<mojom::AccountPtr> accounts;
665
666 // Add a primary account.
667 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
668 token_service()->UpdateCredentials(
669 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
670
671 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03672 GetIdentityManagerImpl()->GetAccounts(base::BindRepeating(
673 &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this),
674 run_loop.QuitClosure(), &accounts));
Colin Blundellbf442682017-07-28 13:08:05675 run_loop.Run();
676
677 // Verify that |accounts| contains the primary account.
678 EXPECT_EQ(1u, accounts.size());
679 const mojom::AccountPtr& primary_account = accounts[0];
680 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
681 primary_account->info.account_id);
682 EXPECT_EQ(kTestGaiaId, primary_account->info.gaia);
683 EXPECT_EQ(kTestEmail, primary_account->info.email);
684 EXPECT_TRUE(primary_account->state.has_refresh_token);
685 EXPECT_TRUE(primary_account->state.is_primary_account);
686}
687
688// Check the implementation of GetAccounts() when there are multiple accounts,
689// in particular that ProfileOAuth2TokenService is the source of truth for
690// whether an account is present.
Colin Blundell7b2a9f02017-12-19 12:13:03691TEST_F(IdentityManagerImplTest, GetAccountsMultipleAccounts) {
Colin Blundellbf442682017-07-28 13:08:05692 token_service()->LoadCredentials("dummy");
693 std::vector<mojom::AccountPtr> accounts;
694
695 // Add a primary account.
696 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
697 token_service()->UpdateCredentials(
698 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
699
700 // Add a secondary account with AccountTrackerService, but don't yet make
701 // ProfileOAuth2TokenService aware of it.
702 std::string secondary_account_id = account_tracker()->SeedAccountInfo(
703 kSecondaryTestGaiaId, kSecondaryTestEmail);
704 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03705 GetIdentityManagerImpl()->GetAccounts(base::BindRepeating(
706 &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this),
707 run_loop.QuitClosure(), &accounts));
Colin Blundellbf442682017-07-28 13:08:05708 run_loop.Run();
709
710 // Verify that |accounts| contains only the primary account at this time.
711 EXPECT_EQ(1u, accounts.size());
712 const mojom::AccountPtr& primary_account = accounts[0];
713 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
714 primary_account->info.account_id);
715 EXPECT_EQ(kTestGaiaId, primary_account->info.gaia);
716 EXPECT_EQ(kTestEmail, primary_account->info.email);
717 EXPECT_TRUE(primary_account->state.has_refresh_token);
718 EXPECT_TRUE(primary_account->state.is_primary_account);
719
720 // Make PO2TS aware of the secondary account.
721 token_service()->UpdateCredentials(secondary_account_id, kTestRefreshToken);
722 base::RunLoop run_loop2;
Colin Blundell7b2a9f02017-12-19 12:13:03723 GetIdentityManagerImpl()->GetAccounts(base::BindRepeating(
724 &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this),
725 run_loop2.QuitClosure(), &accounts));
Colin Blundellbf442682017-07-28 13:08:05726 run_loop2.Run();
727
728 // Verify that |accounts| contains both accounts, with the primary account
729 // being first and having the same information as previously.
730 EXPECT_EQ(2u, accounts.size());
731 const mojom::AccountPtr& primary_account_redux = accounts[0];
732 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
733 primary_account_redux->info.account_id);
734 EXPECT_EQ(kTestGaiaId, primary_account_redux->info.gaia);
735 EXPECT_EQ(kTestEmail, primary_account_redux->info.email);
736 EXPECT_TRUE(primary_account_redux->state.has_refresh_token);
737 EXPECT_TRUE(primary_account_redux->state.is_primary_account);
738
739 const mojom::AccountPtr& secondary_account = accounts[1];
740 EXPECT_EQ(secondary_account_id, secondary_account->info.account_id);
741 EXPECT_EQ(kSecondaryTestGaiaId, secondary_account->info.gaia);
742 EXPECT_EQ(kSecondaryTestEmail, secondary_account->info.email);
743 EXPECT_TRUE(secondary_account->state.has_refresh_token);
744 EXPECT_FALSE(secondary_account->state.is_primary_account);
745}
746
Colin Blundelle89886a2017-06-12 11:26:56747// Check that the expected error is received if requesting an access token when
748// not signed in.
Colin Blundell7b2a9f02017-12-19 12:13:03749TEST_F(IdentityManagerImplTest, GetAccessTokenNotSignedIn) {
Colin Blundelle89886a2017-06-12 11:26:56750 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03751 GetIdentityManagerImpl()->GetAccessToken(
Colin Blundelle89886a2017-06-12 11:26:56752 kTestGaiaId, ScopeSet(), "dummy_consumer",
Colin Blundell7b2a9f02017-12-19 12:13:03753 base::BindRepeating(&IdentityManagerImplTest::OnReceivedAccessToken,
754 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundelle89886a2017-06-12 11:26:56755 run_loop.Run();
756 EXPECT_FALSE(access_token_);
757 EXPECT_EQ(GoogleServiceAuthError::State::USER_NOT_SIGNED_UP,
758 access_token_error_.state());
759}
760
761// Check that the expected access token is received if requesting an access
762// token when signed in.
Colin Blundell7b2a9f02017-12-19 12:13:03763TEST_F(IdentityManagerImplTest, GetAccessTokenSignedIn) {
Colin Blundelle89886a2017-06-12 11:26:56764 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
Colin Blundell57fb6502017-06-27 12:24:34765 std::string account_id = signin_manager()->GetAuthenticatedAccountId();
766 token_service()->UpdateCredentials(account_id, kTestRefreshToken);
Colin Blundelle89886a2017-06-12 11:26:56767 token_service()->set_auto_post_fetch_response_on_message_loop(true);
768 base::RunLoop run_loop;
769
Colin Blundell7b2a9f02017-12-19 12:13:03770 GetIdentityManagerImpl()->GetAccessToken(
Colin Blundell57fb6502017-06-27 12:24:34771 account_id, ScopeSet(), "dummy_consumer",
Colin Blundell7b2a9f02017-12-19 12:13:03772 base::BindRepeating(&IdentityManagerImplTest::OnReceivedAccessToken,
773 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundelle89886a2017-06-12 11:26:56774 run_loop.Run();
775 EXPECT_TRUE(access_token_);
776 EXPECT_EQ(kTestAccessToken, access_token_.value());
777 EXPECT_EQ(GoogleServiceAuthError::State::NONE, access_token_error_.state());
778}
779
blundell1e21bd32017-05-03 15:25:57780} // namespace
781} // namespace identity