blob: 39f9208c5b317e966265bef71e3b91c827b4c245 [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"
Ken Rockot543f5e32018-02-04 02:13:5023#include "services/service_manager/public/mojom/service_factory.mojom.h"
blundell1e21bd32017-05-03 15:25:5724
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
Ken Rockotcf03eee2018-01-16 18:48:0164 void CreateService(
65 service_manager::mojom::ServiceRequest request,
66 const std::string& name,
67 service_manager::mojom::PIDReceiverPtr pid_receiver) override {
blundell1e21bd32017-05-03 15:25:5768 if (name == mojom::kServiceName) {
69 identity_service_context_.reset(new service_manager::ServiceContext(
Jeremy Roman83533d7a2017-11-15 23:39:0670 std::make_unique<IdentityService>(account_tracker_, signin_manager_,
Colin Blundell2f987fd2017-06-23 10:49:0671 token_service_),
blundell1e21bd32017-05-03 15:25:5772 std::move(request)));
73 }
74 }
75
Ben Goodger21ada1e2017-07-19 14:53:0176 void Create(service_manager::mojom::ServiceFactoryRequest request) {
blundell1e21bd32017-05-03 15:25:5777 service_factory_bindings_.AddBinding(this, std::move(request));
78 }
79
80 private:
Colin Blundell2f987fd2017-06-23 10:49:0681 AccountTrackerService* account_tracker_;
blundell1e21bd32017-05-03 15:25:5782 SigninManagerBase* signin_manager_;
Colin Blundelle89886a2017-06-12 11:26:5683 ProfileOAuth2TokenService* token_service_;
blundell1e21bd32017-05-03 15:25:5784 service_manager::BinderRegistry registry_;
85 mojo::BindingSet<service_manager::mojom::ServiceFactory>
86 service_factory_bindings_;
87 std::unique_ptr<service_manager::ServiceContext> identity_service_context_;
88};
89
Colin Blundell7b2a9f02017-12-19 12:13:0390class IdentityManagerImplTest : public service_manager::test::ServiceTest {
blundell1e21bd32017-05-03 15:25:5791 public:
Colin Blundell7b2a9f02017-12-19 12:13:0392 IdentityManagerImplTest()
Jay Civellib9b27a9c2017-10-27 22:08:5393 : ServiceTest("identity_unittests"),
blundell1e21bd32017-05-03 15:25:5794 signin_client_(&pref_service_),
Colin Blundell2c8951de2017-07-07 13:23:4595#if defined(OS_CHROMEOS)
blundell1e21bd32017-05-03 15:25:5796 signin_manager_(&signin_client_, &account_tracker_) {
Colin Blundell2c8951de2017-07-07 13:23:4597#else
98 signin_manager_(&signin_client_,
99 &token_service_,
100 &account_tracker_,
101 nullptr) {
102#endif
blundell1e21bd32017-05-03 15:25:57103 AccountTrackerService::RegisterPrefs(pref_service_.registry());
104 SigninManagerBase::RegisterProfilePrefs(pref_service_.registry());
105 SigninManagerBase::RegisterPrefs(pref_service_.registry());
106
107 account_tracker_.Initialize(&signin_client_);
108 }
109
Colin Blundelld829d852017-06-30 11:06:06110 void TearDown() override {
Colin Blundell7b2a9f02017-12-19 12:13:03111 // Shut down the SigninManager so that the IdentityManagerImpl doesn't end
112 // up outliving it.
Colin Blundelld829d852017-06-30 11:06:06113 signin_manager_.Shutdown();
114 ServiceTest::TearDown();
115 }
116
Colin Blundell704d198e2017-06-05 11:27:13117 void OnReceivedPrimaryAccountInfo(
Colin Blundell7b2a9f02017-12-19 12:13:03118 base::RepeatingClosure quit_closure,
Colin Blundell57fb6502017-06-27 12:24:34119 const base::Optional<AccountInfo>& account_info,
120 const AccountState& account_state) {
Colin Blundell704d198e2017-06-05 11:27:13121 primary_account_info_ = account_info;
Colin Blundell57fb6502017-06-27 12:24:34122 primary_account_state_ = account_state;
blundell1e21bd32017-05-03 15:25:57123 quit_closure.Run();
124 }
125
Colin Blundell7b2a9f02017-12-19 12:13:03126 void OnPrimaryAccountAvailable(base::RepeatingClosure quit_closure,
Colin Blundellbdf4642d2017-07-06 07:00:03127 AccountInfo* caller_account_info,
128 AccountState* caller_account_state,
129 const AccountInfo& account_info,
130 const AccountState& account_state) {
131 *caller_account_info = account_info;
132 *caller_account_state = account_state;
133 quit_closure.Run();
134 }
135
Colin Blundell2f987fd2017-06-23 10:49:06136 void OnReceivedAccountInfoFromGaiaId(
Colin Blundell7b2a9f02017-12-19 12:13:03137 base::RepeatingClosure quit_closure,
Colin Blundell57fb6502017-06-27 12:24:34138 const base::Optional<AccountInfo>& account_info,
139 const AccountState& account_state) {
Colin Blundell2f987fd2017-06-23 10:49:06140 account_info_from_gaia_id_ = account_info;
Colin Blundell57fb6502017-06-27 12:24:34141 account_state_from_gaia_id_ = account_state;
Colin Blundell2f987fd2017-06-23 10:49:06142 quit_closure.Run();
143 }
144
Colin Blundell7b2a9f02017-12-19 12:13:03145 void OnGotAccounts(base::RepeatingClosure quit_closure,
Colin Blundellbf442682017-07-28 13:08:05146 std::vector<mojom::AccountPtr>* output,
147 std::vector<mojom::AccountPtr> accounts) {
148 *output = std::move(accounts);
149 quit_closure.Run();
150 }
151
Colin Blundell7b2a9f02017-12-19 12:13:03152 void OnReceivedAccessToken(base::RepeatingClosure quit_closure,
Colin Blundelle89886a2017-06-12 11:26:56153 const base::Optional<std::string>& access_token,
154 base::Time expiration_time,
155 const GoogleServiceAuthError& error) {
156 access_token_ = access_token;
157 access_token_error_ = error;
158 quit_closure.Run();
159 }
160
blundell1e21bd32017-05-03 15:25:57161 protected:
Colin Blundell25a80822017-07-13 06:17:52162 void SetUp() override { ServiceTest::SetUp(); }
blundell1e21bd32017-05-03 15:25:57163
Colin Blundell7b2a9f02017-12-19 12:13:03164 mojom::IdentityManager* GetIdentityManagerImpl() {
Colin Blundell25a80822017-07-13 06:17:52165 if (!identity_manager_)
166 connector()->BindInterface(mojom::kServiceName, &identity_manager_);
167 return identity_manager_.get();
168 }
169
Colin Blundell7b2a9f02017-12-19 12:13:03170 void ResetIdentityManagerImpl() { identity_manager_.reset(); }
Colin Blundell25a80822017-07-13 06:17:52171
Colin Blundell7b2a9f02017-12-19 12:13:03172 void FlushIdentityManagerImplForTesting() {
173 GetIdentityManagerImpl();
Colin Blundell25a80822017-07-13 06:17:52174 identity_manager_.FlushForTesting();
175 }
176
Colin Blundell7b2a9f02017-12-19 12:13:03177 void SetIdentityManagerImplConnectionErrorHandler(
178 base::RepeatingClosure handler) {
179 GetIdentityManagerImpl();
Colin Blundell25a80822017-07-13 06:17:52180 identity_manager_.set_connection_error_handler(handler);
blundell1e21bd32017-05-03 15:25:57181 }
182
183 // service_manager::test::ServiceTest:
184 std::unique_ptr<service_manager::Service> CreateService() override {
Jeremy Roman83533d7a2017-11-15 23:39:06185 return std::make_unique<ServiceTestClient>(
Colin Blundell2f987fd2017-06-23 10:49:06186 this, &account_tracker_, &signin_manager_, &token_service_);
blundell1e21bd32017-05-03 15:25:57187 }
188
189 mojom::IdentityManagerPtr identity_manager_;
Colin Blundell704d198e2017-06-05 11:27:13190 base::Optional<AccountInfo> primary_account_info_;
Colin Blundell57fb6502017-06-27 12:24:34191 AccountState primary_account_state_;
Colin Blundell2f987fd2017-06-23 10:49:06192 base::Optional<AccountInfo> account_info_from_gaia_id_;
Colin Blundell57fb6502017-06-27 12:24:34193 AccountState account_state_from_gaia_id_;
Colin Blundelle89886a2017-06-12 11:26:56194 base::Optional<std::string> access_token_;
195 GoogleServiceAuthError access_token_error_;
blundell1e21bd32017-05-03 15:25:57196
Colin Blundell2f987fd2017-06-23 10:49:06197 AccountTrackerService* account_tracker() { return &account_tracker_; }
blundell1e21bd32017-05-03 15:25:57198 SigninManagerBase* signin_manager() { return &signin_manager_; }
Colin Blundelle89886a2017-06-12 11:26:56199 FakeProfileOAuth2TokenService* token_service() { return &token_service_; }
blundell1e21bd32017-05-03 15:25:57200
201 private:
202 sync_preferences::TestingPrefServiceSyncable pref_service_;
203 AccountTrackerService account_tracker_;
204 TestSigninClient signin_client_;
Colin Blundell2c8951de2017-07-07 13:23:45205 SigninManagerForTest signin_manager_;
Colin Blundelle89886a2017-06-12 11:26:56206 FakeProfileOAuth2TokenService token_service_;
blundell1e21bd32017-05-03 15:25:57207
Colin Blundell7b2a9f02017-12-19 12:13:03208 DISALLOW_COPY_AND_ASSIGN(IdentityManagerImplTest);
blundell1e21bd32017-05-03 15:25:57209};
210
Colin Blundell25a80822017-07-13 06:17:52211// Tests that it is not possible to connect to the Identity Manager if
212// initiated after SigninManager shutdown.
Colin Blundell7b2a9f02017-12-19 12:13:03213TEST_F(IdentityManagerImplTest, SigninManagerShutdownBeforeConnection) {
Colin Blundell25a80822017-07-13 06:17:52214 AccountInfo sentinel;
215 sentinel.account_id = "sentinel";
216 primary_account_info_ = sentinel;
217
218 // Ensure that the Identity Service has actually been created before
219 // invoking SigninManagerBase::Shutdown(), since otherwise this test will
220 // spin forever. Then reset the Identity Manager so that the next request
221 // makes a fresh connection.
Colin Blundell7b2a9f02017-12-19 12:13:03222 FlushIdentityManagerImplForTesting();
223 ResetIdentityManagerImpl();
Colin Blundell25a80822017-07-13 06:17:52224
Colin Blundell7b2a9f02017-12-19 12:13:03225 // Make a call to connect to the IdentityManagerImpl *after* SigninManager
Colin Blundell25a80822017-07-13 06:17:52226 // shutdown; it should get notified of an error when the Identity Service
227 // drops the connection.
228 signin_manager()->Shutdown();
Colin Blundelld829d852017-06-30 11:06:06229 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03230 SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure());
Colin Blundell25a80822017-07-13 06:17:52231
Colin Blundell7b2a9f02017-12-19 12:13:03232 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
233 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
234 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundell25a80822017-07-13 06:17:52235 run_loop.Run();
236
237 // Verify that the callback to GetPrimaryAccountInfo() was not invoked.
238 EXPECT_TRUE(primary_account_info_);
239 EXPECT_EQ("sentinel", primary_account_info_->account_id);
240}
241
242// Tests that the Identity Manager destroys itself on SigninManager shutdown.
Colin Blundell7b2a9f02017-12-19 12:13:03243TEST_F(IdentityManagerImplTest, SigninManagerShutdownAfterConnection) {
Colin Blundell25a80822017-07-13 06:17:52244 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03245 SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure());
Colin Blundelld829d852017-06-30 11:06:06246
Colin Blundell7b2a9f02017-12-19 12:13:03247 // Ensure that the IdentityManagerImpl instance has actually been created
248 // before invoking SigninManagerBase::Shutdown(), since otherwise this test
249 // will spin forever.
250 FlushIdentityManagerImplForTesting();
Colin Blundelld829d852017-06-30 11:06:06251 signin_manager()->Shutdown();
252 run_loop.Run();
253}
254
Colin Blundellf9792c02017-09-19 18:02:53255// Tests that the Identity Manager properly handles its own destruction in the
256// case where there is an active consumer request (i.e., a pending callback from
257// a Mojo call). In particular, this flow should not cause a DCHECK to fire in
258// debug mode.
Colin Blundell7b2a9f02017-12-19 12:13:03259TEST_F(IdentityManagerImplTest, IdentityManagerImplShutdownWithActiveRequest) {
Colin Blundellf9792c02017-09-19 18:02:53260 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03261 SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure());
Colin Blundellf9792c02017-09-19 18:02:53262
Colin Blundell7b2a9f02017-12-19 12:13:03263 // Call a method on the IdentityManagerImpl that will cause it to store a
264 // pending callback. This callback will never be invoked, so just pass dummy
265 // arguments to it.
266 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
267 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
268 base::Unretained(this), base::RepeatingClosure(), nullptr, nullptr));
Colin Blundellf9792c02017-09-19 18:02:53269
Colin Blundell7b2a9f02017-12-19 12:13:03270 // Ensure that the IdentityManagerImpl has received the above call before
Colin Blundellf9792c02017-09-19 18:02:53271 // invoking SigninManagerBase::Shutdown(), as otherwise this test is
272 // pointless.
Colin Blundell7b2a9f02017-12-19 12:13:03273 FlushIdentityManagerImplForTesting();
Colin Blundellf9792c02017-09-19 18:02:53274
Colin Blundell7b2a9f02017-12-19 12:13:03275 // This flow is what would cause a DCHECK to fire if IdentityManagerImpl is
276 // not properly closing its binding on shutdown.
Colin Blundellf9792c02017-09-19 18:02:53277 signin_manager()->Shutdown();
278 run_loop.Run();
279}
280
Colin Blundell2f987fd2017-06-23 10:49:06281// Check that the primary account info is null if not signed in.
Colin Blundell7b2a9f02017-12-19 12:13:03282TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoNotSignedIn) {
blundell1e21bd32017-05-03 15:25:57283 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03284 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
285 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
286 base::Unretained(this), run_loop.QuitClosure()));
blundell1e21bd32017-05-03 15:25:57287 run_loop.Run();
Colin Blundell704d198e2017-06-05 11:27:13288 EXPECT_FALSE(primary_account_info_);
blundell1e21bd32017-05-03 15:25:57289}
290
Colin Blundell57fb6502017-06-27 12:24:34291// Check that the primary account info has expected values if signed in without
292// a refresh token available.
Colin Blundell7b2a9f02017-12-19 12:13:03293TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoSignedInNoRefreshToken) {
blundell1e21bd32017-05-03 15:25:57294 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
295 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03296 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
297 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
298 base::Unretained(this), run_loop.QuitClosure()));
blundell1e21bd32017-05-03 15:25:57299 run_loop.Run();
Colin Blundell704d198e2017-06-05 11:27:13300 EXPECT_TRUE(primary_account_info_);
Colin Blundell57fb6502017-06-27 12:24:34301 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
302 primary_account_info_->account_id);
Colin Blundell704d198e2017-06-05 11:27:13303 EXPECT_EQ(kTestGaiaId, primary_account_info_->gaia);
304 EXPECT_EQ(kTestEmail, primary_account_info_->email);
Colin Blundell57fb6502017-06-27 12:24:34305 EXPECT_FALSE(primary_account_state_.has_refresh_token);
Colin Blundellbdf4642d2017-07-06 07:00:03306 EXPECT_TRUE(primary_account_state_.is_primary_account);
Colin Blundell57fb6502017-06-27 12:24:34307}
308
309// Check that the primary account info has expected values if signed in with a
310// refresh token available.
Colin Blundell7b2a9f02017-12-19 12:13:03311TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoSignedInRefreshToken) {
Colin Blundell57fb6502017-06-27 12:24:34312 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
313 token_service()->UpdateCredentials(
314 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
315 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03316 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
317 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
318 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundell57fb6502017-06-27 12:24:34319 run_loop.Run();
320 EXPECT_TRUE(primary_account_info_);
321 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
322 primary_account_info_->account_id);
323 EXPECT_EQ(kTestGaiaId, primary_account_info_->gaia);
324 EXPECT_EQ(kTestEmail, primary_account_info_->email);
325 EXPECT_TRUE(primary_account_state_.has_refresh_token);
Colin Blundellbdf4642d2017-07-06 07:00:03326 EXPECT_TRUE(primary_account_state_.is_primary_account);
327}
328
329// Check that GetPrimaryAccountWhenAvailable() returns immediately in the
330// case where the primary account is available when the call is received.
Colin Blundell7b2a9f02017-12-19 12:13:03331TEST_F(IdentityManagerImplTest, GetPrimaryAccountWhenAvailableSignedIn) {
Colin Blundellbdf4642d2017-07-06 07:00:03332 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
333 token_service()->UpdateCredentials(
334 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
335
336 AccountInfo account_info;
337 AccountState account_state;
338 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03339 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
340 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
341 base::Unretained(this), run_loop.QuitClosure(),
342 base::Unretained(&account_info), base::Unretained(&account_state)));
Colin Blundellbdf4642d2017-07-06 07:00:03343 run_loop.Run();
344
345 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
346 account_info.account_id);
347 EXPECT_EQ(kTestGaiaId, account_info.gaia);
348 EXPECT_EQ(kTestEmail, account_info.email);
349 EXPECT_TRUE(account_state.has_refresh_token);
350 EXPECT_TRUE(account_state.is_primary_account);
351}
352
353// Check that GetPrimaryAccountWhenAvailable() returns the expected account
354// info in the case where the primary account is made available *after* the
355// call is received.
Colin Blundell7b2a9f02017-12-19 12:13:03356TEST_F(IdentityManagerImplTest, GetPrimaryAccountWhenAvailableSignInLater) {
Colin Blundellbdf4642d2017-07-06 07:00:03357 AccountInfo account_info;
358 AccountState account_state;
359
360 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03361 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
362 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
363 base::Unretained(this), run_loop.QuitClosure(),
364 base::Unretained(&account_info), base::Unretained(&account_state)));
Colin Blundellbdf4642d2017-07-06 07:00:03365
366 // Verify that the primary account info is not currently available (this also
367 // serves to ensure that the preceding call has been received by the Identity
368 // Manager before proceeding).
369 base::RunLoop run_loop2;
Colin Blundell7b2a9f02017-12-19 12:13:03370 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
371 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
372 base::Unretained(this), run_loop2.QuitClosure()));
Colin Blundellbdf4642d2017-07-06 07:00:03373 run_loop2.Run();
374 EXPECT_FALSE(primary_account_info_);
375
376 // Make the primary account available and check that the callback is invoked
377 // as expected.
378 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
379 token_service()->UpdateCredentials(
380 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
381 run_loop.Run();
382
383 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
384 account_info.account_id);
385 EXPECT_EQ(kTestGaiaId, account_info.gaia);
386 EXPECT_EQ(kTestEmail, account_info.email);
387 EXPECT_TRUE(account_state.has_refresh_token);
388 EXPECT_TRUE(account_state.is_primary_account);
389}
390
391// Check that GetPrimaryAccountWhenAvailable() returns the expected account
392// info in the case where signin is done before the call is received but the
393// refresh token is made available only *after* the call is received.
Colin Blundell7b2a9f02017-12-19 12:13:03394TEST_F(IdentityManagerImplTest,
395 GetPrimaryAccountWhenAvailableTokenAvailableLater) {
Colin Blundellbdf4642d2017-07-06 07:00:03396 AccountInfo account_info;
397 AccountState account_state;
398
399 // Sign in, but don't set the refresh token yet.
400 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
401 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03402 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
403 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
404 base::Unretained(this), run_loop.QuitClosure(),
405 base::Unretained(&account_info), base::Unretained(&account_state)));
Colin Blundellbdf4642d2017-07-06 07:00:03406
407 // Verify that the primary account info is present, but that the primary
408 // account is not yet considered available (this also
409 // serves to ensure that the preceding call has been received by the Identity
410 // Manager before proceeding).
411 base::RunLoop run_loop2;
Colin Blundell7b2a9f02017-12-19 12:13:03412 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
413 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
414 base::Unretained(this), run_loop2.QuitClosure()));
Colin Blundellbdf4642d2017-07-06 07:00:03415 run_loop2.Run();
416
417 EXPECT_TRUE(primary_account_info_);
418 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
419 primary_account_info_->account_id);
420 EXPECT_TRUE(account_info.account_id.empty());
421
422 // Set the refresh token and check that the callback is invoked as expected
423 // (i.e., the primary account is now considered available).
424 token_service()->UpdateCredentials(
425 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
426 run_loop.Run();
427
428 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
429 account_info.account_id);
430 EXPECT_EQ(kTestGaiaId, account_info.gaia);
431 EXPECT_EQ(kTestEmail, account_info.email);
432 EXPECT_TRUE(account_state.has_refresh_token);
433 EXPECT_TRUE(account_state.is_primary_account);
434}
435
Colin Blundell2c8951de2017-07-07 13:23:45436// Check that GetPrimaryAccountWhenAvailable() returns the expected account info
437// in the case where the token is available before the call is received but the
438// account is made authenticated only *after* the call is received. This test is
439// relevant only on non-ChromeOS platforms, as the flow being tested here is not
440// possible on ChromeOS.
441#if !defined(OS_CHROMEOS)
Colin Blundell7b2a9f02017-12-19 12:13:03442TEST_F(IdentityManagerImplTest,
Colin Blundell2c8951de2017-07-07 13:23:45443 GetPrimaryAccountWhenAvailableAuthenticationAvailableLater) {
444 AccountInfo account_info;
445 AccountState account_state;
446
447 // Set the refresh token, but don't sign in yet.
448 std::string account_id_to_use =
449 account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail);
450 token_service()->UpdateCredentials(account_id_to_use, kTestRefreshToken);
451 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03452 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
453 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
454 base::Unretained(this), run_loop.QuitClosure(),
455 base::Unretained(&account_info), base::Unretained(&account_state)));
Colin Blundell2c8951de2017-07-07 13:23:45456
457 // Verify that the account is present and has a refresh token, but that the
458 // primary account is not yet considered available (this also serves to ensure
459 // that the preceding call has been received by the Identity Manager before
460 // proceeding).
461 base::RunLoop run_loop2;
Colin Blundell7b2a9f02017-12-19 12:13:03462 GetIdentityManagerImpl()->GetAccountInfoFromGaiaId(
Colin Blundell2c8951de2017-07-07 13:23:45463 kTestGaiaId,
Colin Blundell7b2a9f02017-12-19 12:13:03464 base::BindRepeating(
465 &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId,
466 base::Unretained(this), run_loop2.QuitClosure()));
Colin Blundell2c8951de2017-07-07 13:23:45467 run_loop2.Run();
468
469 EXPECT_TRUE(account_info_from_gaia_id_);
470 EXPECT_EQ(account_id_to_use, account_info_from_gaia_id_->account_id);
471 EXPECT_EQ(kTestGaiaId, account_info_from_gaia_id_->gaia);
472 EXPECT_EQ(kTestEmail, account_info_from_gaia_id_->email);
473 EXPECT_TRUE(account_state_from_gaia_id_.has_refresh_token);
474 EXPECT_FALSE(account_state_from_gaia_id_.is_primary_account);
475
476 EXPECT_TRUE(account_info.account_id.empty());
477
478 // Sign the user in and check that the callback is invoked as expected (i.e.,
479 // the primary account is now considered available). Note that it is necessary
480 // to call SignIn() here to ensure that GoogleSigninSucceeded() is fired by
481 // the fake signin manager.
482 static_cast<FakeSigninManager*>(signin_manager())
483 ->SignIn(kTestGaiaId, kTestEmail, "password");
484
485 run_loop.Run();
486
487 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
488 account_info.account_id);
489 EXPECT_EQ(kTestGaiaId, account_info.gaia);
490 EXPECT_EQ(kTestEmail, account_info.email);
491 EXPECT_TRUE(account_state.has_refresh_token);
492 EXPECT_TRUE(account_state.is_primary_account);
493}
494#endif
495
Colin Blundellbdf4642d2017-07-06 07:00:03496// Check that GetPrimaryAccountWhenAvailable() returns the expected account
497// info to all callers in the case where the primary account is made available
498// after multiple overlapping calls have been received.
Colin Blundell7b2a9f02017-12-19 12:13:03499TEST_F(IdentityManagerImplTest,
500 GetPrimaryAccountWhenAvailableOverlappingCalls) {
Colin Blundellbdf4642d2017-07-06 07:00:03501 AccountInfo account_info1;
502 AccountState account_state1;
503 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03504 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
505 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
506 base::Unretained(this), run_loop.QuitClosure(),
507 base::Unretained(&account_info1), base::Unretained(&account_state1)));
Colin Blundellbdf4642d2017-07-06 07:00:03508
509 AccountInfo account_info2;
510 AccountState account_state2;
511 base::RunLoop run_loop2;
Colin Blundell7b2a9f02017-12-19 12:13:03512 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
513 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
514 base::Unretained(this), run_loop2.QuitClosure(),
515 base::Unretained(&account_info2), base::Unretained(&account_state2)));
Colin Blundellbdf4642d2017-07-06 07:00:03516
517 // Verify that the primary account info is not currently available (this also
518 // serves to ensure that the preceding call has been received by the Identity
519 // Manager before proceeding).
520 base::RunLoop run_loop3;
Colin Blundell7b2a9f02017-12-19 12:13:03521 GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating(
522 &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo,
523 base::Unretained(this), run_loop3.QuitClosure()));
Colin Blundellbdf4642d2017-07-06 07:00:03524 run_loop3.Run();
525 EXPECT_FALSE(primary_account_info_);
526
527 // Make the primary account available and check that the callbacks are invoked
528 // as expected.
529 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
530 token_service()->UpdateCredentials(
531 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
532 run_loop.Run();
533 run_loop2.Run();
534
535 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
536 account_info1.account_id);
537 EXPECT_EQ(kTestGaiaId, account_info1.gaia);
538 EXPECT_EQ(kTestEmail, account_info1.email);
539 EXPECT_TRUE(account_state1.has_refresh_token);
540 EXPECT_TRUE(account_state1.is_primary_account);
541
542 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
543 account_info2.account_id);
544 EXPECT_EQ(kTestGaiaId, account_info2.gaia);
545 EXPECT_EQ(kTestEmail, account_info2.email);
546 EXPECT_TRUE(account_state2.has_refresh_token);
547 EXPECT_TRUE(account_state2.is_primary_account);
blundell1e21bd32017-05-03 15:25:57548}
549
Colin Blundell86ee4102017-10-09 16:55:03550// Check that GetPrimaryAccountWhenAvailable() doesn't return the account as
551// available if the refresh token has an auth error.
Colin Blundell7b2a9f02017-12-19 12:13:03552TEST_F(IdentityManagerImplTest,
Colin Blundell86ee4102017-10-09 16:55:03553 GetPrimaryAccountWhenAvailableRefreshTokenHasAuthError) {
554 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
555 token_service()->UpdateCredentials(
556 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
557 FakeOAuth2TokenServiceDelegate* delegate =
558 static_cast<FakeOAuth2TokenServiceDelegate*>(
559 token_service()->GetDelegate());
560 delegate->SetLastErrorForAccount(
561 signin_manager()->GetAuthenticatedAccountId(),
562 GoogleServiceAuthError(
563 GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS));
564
565 AccountInfo account_info;
566 AccountState account_state;
567 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03568 GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating(
569 &IdentityManagerImplTest::OnPrimaryAccountAvailable,
570 base::Unretained(this), run_loop.QuitClosure(),
571 base::Unretained(&account_info), base::Unretained(&account_state)));
Colin Blundell86ee4102017-10-09 16:55:03572
573 // Flush the Identity Manager and check that the callback didn't fire.
Colin Blundell7b2a9f02017-12-19 12:13:03574 FlushIdentityManagerImplForTesting();
Colin Blundell86ee4102017-10-09 16:55:03575 EXPECT_TRUE(account_info.account_id.empty());
576
577 // Clear the auth error, update credentials, and check that the callback
578 // fires.
579 delegate->SetLastErrorForAccount(
580 signin_manager()->GetAuthenticatedAccountId(), GoogleServiceAuthError());
581 token_service()->UpdateCredentials(
582 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
583 run_loop.Run();
584
585 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
586 account_info.account_id);
587 EXPECT_EQ(kTestGaiaId, account_info.gaia);
588 EXPECT_EQ(kTestEmail, account_info.email);
589 EXPECT_TRUE(account_state.has_refresh_token);
590 EXPECT_TRUE(account_state.is_primary_account);
591}
592
Colin Blundell2f987fd2017-06-23 10:49:06593// Check that the account info for a given GAIA ID is null if that GAIA ID is
594// unknown.
Colin Blundell7b2a9f02017-12-19 12:13:03595TEST_F(IdentityManagerImplTest, GetAccountInfoForUnknownGaiaID) {
Colin Blundell2f987fd2017-06-23 10:49:06596 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03597 GetIdentityManagerImpl()->GetAccountInfoFromGaiaId(
Colin Blundell2f987fd2017-06-23 10:49:06598 kTestGaiaId,
Colin Blundell7b2a9f02017-12-19 12:13:03599 base::BindRepeating(
600 &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId,
601 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundell2f987fd2017-06-23 10:49:06602 run_loop.Run();
603 EXPECT_FALSE(account_info_from_gaia_id_);
604}
605
606// Check that the account info for a given GAIA ID has expected values if that
Colin Blundell57fb6502017-06-27 12:24:34607// GAIA ID is known and there is no refresh token available for it.
Colin Blundell7b2a9f02017-12-19 12:13:03608TEST_F(IdentityManagerImplTest, GetAccountInfoForKnownGaiaIdNoRefreshToken) {
Colin Blundell57fb6502017-06-27 12:24:34609 std::string account_id =
610 account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail);
Colin Blundell2f987fd2017-06-23 10:49:06611 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03612 GetIdentityManagerImpl()->GetAccountInfoFromGaiaId(
Colin Blundell2f987fd2017-06-23 10:49:06613 kTestGaiaId,
Colin Blundell7b2a9f02017-12-19 12:13:03614 base::BindRepeating(
615 &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId,
616 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundell2f987fd2017-06-23 10:49:06617 run_loop.Run();
618 EXPECT_TRUE(account_info_from_gaia_id_);
Colin Blundell57fb6502017-06-27 12:24:34619 EXPECT_EQ(account_id, account_info_from_gaia_id_->account_id);
Colin Blundell2f987fd2017-06-23 10:49:06620 EXPECT_EQ(kTestGaiaId, account_info_from_gaia_id_->gaia);
621 EXPECT_EQ(kTestEmail, account_info_from_gaia_id_->email);
Colin Blundell57fb6502017-06-27 12:24:34622 EXPECT_FALSE(account_state_from_gaia_id_.has_refresh_token);
Colin Blundellbdf4642d2017-07-06 07:00:03623 EXPECT_FALSE(account_state_from_gaia_id_.is_primary_account);
Colin Blundell57fb6502017-06-27 12:24:34624}
625
626// Check that the account info for a given GAIA ID has expected values if that
627// GAIA ID is known and has a refresh token available.
Colin Blundell7b2a9f02017-12-19 12:13:03628TEST_F(IdentityManagerImplTest, GetAccountInfoForKnownGaiaIdRefreshToken) {
Colin Blundell57fb6502017-06-27 12:24:34629 std::string account_id =
630 account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail);
631 token_service()->UpdateCredentials(account_id, kTestRefreshToken);
632 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03633 GetIdentityManagerImpl()->GetAccountInfoFromGaiaId(
Colin Blundell57fb6502017-06-27 12:24:34634 kTestGaiaId,
Colin Blundell7b2a9f02017-12-19 12:13:03635 base::BindRepeating(
636 &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId,
637 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundell57fb6502017-06-27 12:24:34638 run_loop.Run();
639 EXPECT_TRUE(account_info_from_gaia_id_);
640 EXPECT_EQ(account_id, account_info_from_gaia_id_->account_id);
641 EXPECT_EQ(kTestGaiaId, account_info_from_gaia_id_->gaia);
642 EXPECT_EQ(kTestEmail, account_info_from_gaia_id_->email);
643 EXPECT_TRUE(account_state_from_gaia_id_.has_refresh_token);
Colin Blundellbdf4642d2017-07-06 07:00:03644 EXPECT_FALSE(account_state_from_gaia_id_.is_primary_account);
Colin Blundell2f987fd2017-06-23 10:49:06645}
646
Colin Blundellbf442682017-07-28 13:08:05647// Check the implementation of GetAccounts() when there are no accounts.
Colin Blundell7b2a9f02017-12-19 12:13:03648TEST_F(IdentityManagerImplTest, GetAccountsNoAccount) {
Colin Blundellbf442682017-07-28 13:08:05649 token_service()->LoadCredentials("dummy");
650
651 std::vector<mojom::AccountPtr> accounts;
652
653 // Check that an empty list is returned when there are no accounts.
654 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03655 GetIdentityManagerImpl()->GetAccounts(base::BindRepeating(
656 &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this),
657 run_loop.QuitClosure(), &accounts));
Colin Blundellbf442682017-07-28 13:08:05658 run_loop.Run();
659 EXPECT_EQ(0u, accounts.size());
660}
661
662// Check the implementation of GetAccounts() when there is a single account,
663// which is the primary account.
Colin Blundell7b2a9f02017-12-19 12:13:03664TEST_F(IdentityManagerImplTest, GetAccountsPrimaryAccount) {
Colin Blundellbf442682017-07-28 13:08:05665 token_service()->LoadCredentials("dummy");
666 std::vector<mojom::AccountPtr> accounts;
667
668 // Add a primary account.
669 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
670 token_service()->UpdateCredentials(
671 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
672
673 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03674 GetIdentityManagerImpl()->GetAccounts(base::BindRepeating(
675 &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this),
676 run_loop.QuitClosure(), &accounts));
Colin Blundellbf442682017-07-28 13:08:05677 run_loop.Run();
678
679 // Verify that |accounts| contains the primary account.
680 EXPECT_EQ(1u, accounts.size());
681 const mojom::AccountPtr& primary_account = accounts[0];
682 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
683 primary_account->info.account_id);
684 EXPECT_EQ(kTestGaiaId, primary_account->info.gaia);
685 EXPECT_EQ(kTestEmail, primary_account->info.email);
686 EXPECT_TRUE(primary_account->state.has_refresh_token);
687 EXPECT_TRUE(primary_account->state.is_primary_account);
688}
689
690// Check the implementation of GetAccounts() when there are multiple accounts,
691// in particular that ProfileOAuth2TokenService is the source of truth for
692// whether an account is present.
Colin Blundell7b2a9f02017-12-19 12:13:03693TEST_F(IdentityManagerImplTest, GetAccountsMultipleAccounts) {
Colin Blundellbf442682017-07-28 13:08:05694 token_service()->LoadCredentials("dummy");
695 std::vector<mojom::AccountPtr> accounts;
696
697 // Add a primary account.
698 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
699 token_service()->UpdateCredentials(
700 signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken);
701
702 // Add a secondary account with AccountTrackerService, but don't yet make
703 // ProfileOAuth2TokenService aware of it.
704 std::string secondary_account_id = account_tracker()->SeedAccountInfo(
705 kSecondaryTestGaiaId, kSecondaryTestEmail);
706 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03707 GetIdentityManagerImpl()->GetAccounts(base::BindRepeating(
708 &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this),
709 run_loop.QuitClosure(), &accounts));
Colin Blundellbf442682017-07-28 13:08:05710 run_loop.Run();
711
712 // Verify that |accounts| contains only the primary account at this time.
713 EXPECT_EQ(1u, accounts.size());
714 const mojom::AccountPtr& primary_account = accounts[0];
715 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
716 primary_account->info.account_id);
717 EXPECT_EQ(kTestGaiaId, primary_account->info.gaia);
718 EXPECT_EQ(kTestEmail, primary_account->info.email);
719 EXPECT_TRUE(primary_account->state.has_refresh_token);
720 EXPECT_TRUE(primary_account->state.is_primary_account);
721
722 // Make PO2TS aware of the secondary account.
723 token_service()->UpdateCredentials(secondary_account_id, kTestRefreshToken);
724 base::RunLoop run_loop2;
Colin Blundell7b2a9f02017-12-19 12:13:03725 GetIdentityManagerImpl()->GetAccounts(base::BindRepeating(
726 &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this),
727 run_loop2.QuitClosure(), &accounts));
Colin Blundellbf442682017-07-28 13:08:05728 run_loop2.Run();
729
730 // Verify that |accounts| contains both accounts, with the primary account
731 // being first and having the same information as previously.
732 EXPECT_EQ(2u, accounts.size());
733 const mojom::AccountPtr& primary_account_redux = accounts[0];
734 EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(),
735 primary_account_redux->info.account_id);
736 EXPECT_EQ(kTestGaiaId, primary_account_redux->info.gaia);
737 EXPECT_EQ(kTestEmail, primary_account_redux->info.email);
738 EXPECT_TRUE(primary_account_redux->state.has_refresh_token);
739 EXPECT_TRUE(primary_account_redux->state.is_primary_account);
740
741 const mojom::AccountPtr& secondary_account = accounts[1];
742 EXPECT_EQ(secondary_account_id, secondary_account->info.account_id);
743 EXPECT_EQ(kSecondaryTestGaiaId, secondary_account->info.gaia);
744 EXPECT_EQ(kSecondaryTestEmail, secondary_account->info.email);
745 EXPECT_TRUE(secondary_account->state.has_refresh_token);
746 EXPECT_FALSE(secondary_account->state.is_primary_account);
747}
748
Colin Blundelle89886a2017-06-12 11:26:56749// Check that the expected error is received if requesting an access token when
750// not signed in.
Colin Blundell7b2a9f02017-12-19 12:13:03751TEST_F(IdentityManagerImplTest, GetAccessTokenNotSignedIn) {
Colin Blundelle89886a2017-06-12 11:26:56752 base::RunLoop run_loop;
Colin Blundell7b2a9f02017-12-19 12:13:03753 GetIdentityManagerImpl()->GetAccessToken(
Colin Blundelle89886a2017-06-12 11:26:56754 kTestGaiaId, ScopeSet(), "dummy_consumer",
Colin Blundell7b2a9f02017-12-19 12:13:03755 base::BindRepeating(&IdentityManagerImplTest::OnReceivedAccessToken,
756 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundelle89886a2017-06-12 11:26:56757 run_loop.Run();
758 EXPECT_FALSE(access_token_);
759 EXPECT_EQ(GoogleServiceAuthError::State::USER_NOT_SIGNED_UP,
760 access_token_error_.state());
761}
762
763// Check that the expected access token is received if requesting an access
764// token when signed in.
Colin Blundell7b2a9f02017-12-19 12:13:03765TEST_F(IdentityManagerImplTest, GetAccessTokenSignedIn) {
Colin Blundelle89886a2017-06-12 11:26:56766 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
Colin Blundell57fb6502017-06-27 12:24:34767 std::string account_id = signin_manager()->GetAuthenticatedAccountId();
768 token_service()->UpdateCredentials(account_id, kTestRefreshToken);
Colin Blundelle89886a2017-06-12 11:26:56769 token_service()->set_auto_post_fetch_response_on_message_loop(true);
770 base::RunLoop run_loop;
771
Colin Blundell7b2a9f02017-12-19 12:13:03772 GetIdentityManagerImpl()->GetAccessToken(
Colin Blundell57fb6502017-06-27 12:24:34773 account_id, ScopeSet(), "dummy_consumer",
Colin Blundell7b2a9f02017-12-19 12:13:03774 base::BindRepeating(&IdentityManagerImplTest::OnReceivedAccessToken,
775 base::Unretained(this), run_loop.QuitClosure()));
Colin Blundelle89886a2017-06-12 11:26:56776 run_loop.Run();
777 EXPECT_TRUE(access_token_);
778 EXPECT_EQ(kTestAccessToken, access_token_.value());
779 EXPECT_EQ(GoogleServiceAuthError::State::NONE, access_token_error_.state());
780}
781
blundell1e21bd32017-05-03 15:25:57782} // namespace
783} // namespace identity