blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 1 | // 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 Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 6 | #include "build/build_config.h" |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 7 | #include "components/signin/core/browser/account_info.h" |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 8 | #include "components/signin/core/browser/account_tracker_service.h" |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 9 | #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 10 | #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 Blundell | 86ee410 | 2017-10-09 16:55:03 | [diff] [blame] | 13 | #include "google_apis/gaia/fake_oauth2_token_service_delegate.h" |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 14 | #include "mojo/public/cpp/bindings/binding_set.h" |
| 15 | #include "services/identity/identity_service.h" |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 16 | #include "services/identity/public/cpp/account_state.h" |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 17 | #include "services/identity/public/cpp/scope_set.h" |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 18 | #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 | |
| 25 | namespace identity { |
| 26 | namespace { |
| 27 | |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 28 | #if defined(OS_CHROMEOS) |
| 29 | using SigninManagerForTest = FakeSigninManagerBase; |
| 30 | #else |
| 31 | using SigninManagerForTest = FakeSigninManager; |
| 32 | #endif // OS_CHROMEOS |
| 33 | |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 34 | const char kTestGaiaId[] = "dummyId"; |
| 35 | const char kTestEmail[] = "[email protected]"; |
| 36 | const char kSecondaryTestGaiaId[] = "secondaryDummyId"; |
| 37 | const char kSecondaryTestEmail[] = "[email protected]"; |
| 38 | const char kTestRefreshToken[] = "dummy-refresh-token"; |
| 39 | const char kTestAccessToken[] = "access_token"; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 40 | |
| 41 | class ServiceTestClient : public service_manager::test::ServiceTestClient, |
| 42 | public service_manager::mojom::ServiceFactory { |
| 43 | public: |
| 44 | ServiceTestClient(service_manager::test::ServiceTest* test, |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 45 | AccountTrackerService* account_tracker, |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 46 | SigninManagerBase* signin_manager, |
| 47 | ProfileOAuth2TokenService* token_service) |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 48 | : service_manager::test::ServiceTestClient(test), |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 49 | account_tracker_(account_tracker), |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 50 | signin_manager_(signin_manager), |
| 51 | token_service_(token_service) { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 52 | registry_.AddInterface<service_manager::mojom::ServiceFactory>( |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 53 | base::BindRepeating(&ServiceTestClient::Create, |
| 54 | base::Unretained(this))); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 55 | } |
| 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 Goodger | 21ada1e | 2017-07-19 14:53:01 | [diff] [blame] | 61 | registry_.BindInterface(interface_name, std::move(interface_pipe)); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 62 | } |
| 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 Roman | 83533d7a | 2017-11-15 23:39:06 | [diff] [blame] | 68 | std::make_unique<IdentityService>(account_tracker_, signin_manager_, |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 69 | token_service_), |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 70 | std::move(request))); |
| 71 | } |
| 72 | } |
| 73 | |
Ben Goodger | 21ada1e | 2017-07-19 14:53:01 | [diff] [blame] | 74 | void Create(service_manager::mojom::ServiceFactoryRequest request) { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 75 | service_factory_bindings_.AddBinding(this, std::move(request)); |
| 76 | } |
| 77 | |
| 78 | private: |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 79 | AccountTrackerService* account_tracker_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 80 | SigninManagerBase* signin_manager_; |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 81 | ProfileOAuth2TokenService* token_service_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 82 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 88 | class IdentityManagerImplTest : public service_manager::test::ServiceTest { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 89 | public: |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 90 | IdentityManagerImplTest() |
Jay Civelli | b9b27a9c | 2017-10-27 22:08:53 | [diff] [blame] | 91 | : ServiceTest("identity_unittests"), |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 92 | signin_client_(&pref_service_), |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 93 | #if defined(OS_CHROMEOS) |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 94 | signin_manager_(&signin_client_, &account_tracker_) { |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 95 | #else |
| 96 | signin_manager_(&signin_client_, |
| 97 | &token_service_, |
| 98 | &account_tracker_, |
| 99 | nullptr) { |
| 100 | #endif |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 101 | 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 Blundell | d829d85 | 2017-06-30 11:06:06 | [diff] [blame] | 108 | void TearDown() override { |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 109 | // Shut down the SigninManager so that the IdentityManagerImpl doesn't end |
| 110 | // up outliving it. |
Colin Blundell | d829d85 | 2017-06-30 11:06:06 | [diff] [blame] | 111 | signin_manager_.Shutdown(); |
| 112 | ServiceTest::TearDown(); |
| 113 | } |
| 114 | |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 115 | void OnReceivedPrimaryAccountInfo( |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 116 | base::RepeatingClosure quit_closure, |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 117 | const base::Optional<AccountInfo>& account_info, |
| 118 | const AccountState& account_state) { |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 119 | primary_account_info_ = account_info; |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 120 | primary_account_state_ = account_state; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 121 | quit_closure.Run(); |
| 122 | } |
| 123 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 124 | void OnPrimaryAccountAvailable(base::RepeatingClosure quit_closure, |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 125 | 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 Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 134 | void OnReceivedAccountInfoFromGaiaId( |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 135 | base::RepeatingClosure quit_closure, |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 136 | const base::Optional<AccountInfo>& account_info, |
| 137 | const AccountState& account_state) { |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 138 | account_info_from_gaia_id_ = account_info; |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 139 | account_state_from_gaia_id_ = account_state; |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 140 | quit_closure.Run(); |
| 141 | } |
| 142 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 143 | void OnGotAccounts(base::RepeatingClosure quit_closure, |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 144 | std::vector<mojom::AccountPtr>* output, |
| 145 | std::vector<mojom::AccountPtr> accounts) { |
| 146 | *output = std::move(accounts); |
| 147 | quit_closure.Run(); |
| 148 | } |
| 149 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 150 | void OnReceivedAccessToken(base::RepeatingClosure quit_closure, |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 151 | 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 | |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 159 | protected: |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 160 | void SetUp() override { ServiceTest::SetUp(); } |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 161 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 162 | mojom::IdentityManager* GetIdentityManagerImpl() { |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 163 | if (!identity_manager_) |
| 164 | connector()->BindInterface(mojom::kServiceName, &identity_manager_); |
| 165 | return identity_manager_.get(); |
| 166 | } |
| 167 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 168 | void ResetIdentityManagerImpl() { identity_manager_.reset(); } |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 169 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 170 | void FlushIdentityManagerImplForTesting() { |
| 171 | GetIdentityManagerImpl(); |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 172 | identity_manager_.FlushForTesting(); |
| 173 | } |
| 174 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 175 | void SetIdentityManagerImplConnectionErrorHandler( |
| 176 | base::RepeatingClosure handler) { |
| 177 | GetIdentityManagerImpl(); |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 178 | identity_manager_.set_connection_error_handler(handler); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // service_manager::test::ServiceTest: |
| 182 | std::unique_ptr<service_manager::Service> CreateService() override { |
Jeremy Roman | 83533d7a | 2017-11-15 23:39:06 | [diff] [blame] | 183 | return std::make_unique<ServiceTestClient>( |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 184 | this, &account_tracker_, &signin_manager_, &token_service_); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | mojom::IdentityManagerPtr identity_manager_; |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 188 | base::Optional<AccountInfo> primary_account_info_; |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 189 | AccountState primary_account_state_; |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 190 | base::Optional<AccountInfo> account_info_from_gaia_id_; |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 191 | AccountState account_state_from_gaia_id_; |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 192 | base::Optional<std::string> access_token_; |
| 193 | GoogleServiceAuthError access_token_error_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 194 | |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 195 | AccountTrackerService* account_tracker() { return &account_tracker_; } |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 196 | SigninManagerBase* signin_manager() { return &signin_manager_; } |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 197 | FakeProfileOAuth2TokenService* token_service() { return &token_service_; } |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 198 | |
| 199 | private: |
| 200 | sync_preferences::TestingPrefServiceSyncable pref_service_; |
| 201 | AccountTrackerService account_tracker_; |
| 202 | TestSigninClient signin_client_; |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 203 | SigninManagerForTest signin_manager_; |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 204 | FakeProfileOAuth2TokenService token_service_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 205 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 206 | DISALLOW_COPY_AND_ASSIGN(IdentityManagerImplTest); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 207 | }; |
| 208 | |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 209 | // Tests that it is not possible to connect to the Identity Manager if |
| 210 | // initiated after SigninManager shutdown. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 211 | TEST_F(IdentityManagerImplTest, SigninManagerShutdownBeforeConnection) { |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 212 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 220 | FlushIdentityManagerImplForTesting(); |
| 221 | ResetIdentityManagerImpl(); |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 222 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 223 | // Make a call to connect to the IdentityManagerImpl *after* SigninManager |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 224 | // shutdown; it should get notified of an error when the Identity Service |
| 225 | // drops the connection. |
| 226 | signin_manager()->Shutdown(); |
Colin Blundell | d829d85 | 2017-06-30 11:06:06 | [diff] [blame] | 227 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 228 | SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure()); |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 229 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 230 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 231 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 232 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 233 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 241 | TEST_F(IdentityManagerImplTest, SigninManagerShutdownAfterConnection) { |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 242 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 243 | SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure()); |
Colin Blundell | d829d85 | 2017-06-30 11:06:06 | [diff] [blame] | 244 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 245 | // 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 Blundell | d829d85 | 2017-06-30 11:06:06 | [diff] [blame] | 249 | signin_manager()->Shutdown(); |
| 250 | run_loop.Run(); |
| 251 | } |
| 252 | |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 253 | // 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 257 | TEST_F(IdentityManagerImplTest, IdentityManagerImplShutdownWithActiveRequest) { |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 258 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 259 | SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure()); |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 260 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 261 | // 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 Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 267 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 268 | // Ensure that the IdentityManagerImpl has received the above call before |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 269 | // invoking SigninManagerBase::Shutdown(), as otherwise this test is |
| 270 | // pointless. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 271 | FlushIdentityManagerImplForTesting(); |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 272 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 273 | // This flow is what would cause a DCHECK to fire if IdentityManagerImpl is |
| 274 | // not properly closing its binding on shutdown. |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 275 | signin_manager()->Shutdown(); |
| 276 | run_loop.Run(); |
| 277 | } |
| 278 | |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 279 | // Check that the primary account info is null if not signed in. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 280 | TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoNotSignedIn) { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 281 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 282 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 283 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 284 | base::Unretained(this), run_loop.QuitClosure())); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 285 | run_loop.Run(); |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 286 | EXPECT_FALSE(primary_account_info_); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 287 | } |
| 288 | |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 289 | // Check that the primary account info has expected values if signed in without |
| 290 | // a refresh token available. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 291 | TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoSignedInNoRefreshToken) { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 292 | signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail); |
| 293 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 294 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 295 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 296 | base::Unretained(this), run_loop.QuitClosure())); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 297 | run_loop.Run(); |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 298 | EXPECT_TRUE(primary_account_info_); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 299 | EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(), |
| 300 | primary_account_info_->account_id); |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 301 | EXPECT_EQ(kTestGaiaId, primary_account_info_->gaia); |
| 302 | EXPECT_EQ(kTestEmail, primary_account_info_->email); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 303 | EXPECT_FALSE(primary_account_state_.has_refresh_token); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 304 | EXPECT_TRUE(primary_account_state_.is_primary_account); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // Check that the primary account info has expected values if signed in with a |
| 308 | // refresh token available. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 309 | TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoSignedInRefreshToken) { |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 310 | signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail); |
| 311 | token_service()->UpdateCredentials( |
| 312 | signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken); |
| 313 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 314 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 315 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 316 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 317 | 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 Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 324 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 329 | TEST_F(IdentityManagerImplTest, GetPrimaryAccountWhenAvailableSignedIn) { |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 330 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 337 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 338 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 339 | base::Unretained(this), run_loop.QuitClosure(), |
| 340 | base::Unretained(&account_info), base::Unretained(&account_state))); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 341 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 354 | TEST_F(IdentityManagerImplTest, GetPrimaryAccountWhenAvailableSignInLater) { |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 355 | AccountInfo account_info; |
| 356 | AccountState account_state; |
| 357 | |
| 358 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 359 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 360 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 361 | base::Unretained(this), run_loop.QuitClosure(), |
| 362 | base::Unretained(&account_info), base::Unretained(&account_state))); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 363 | |
| 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 368 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 369 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 370 | base::Unretained(this), run_loop2.QuitClosure())); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 371 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 392 | TEST_F(IdentityManagerImplTest, |
| 393 | GetPrimaryAccountWhenAvailableTokenAvailableLater) { |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 394 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 400 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 401 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 402 | base::Unretained(this), run_loop.QuitClosure(), |
| 403 | base::Unretained(&account_info), base::Unretained(&account_state))); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 404 | |
| 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 410 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 411 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 412 | base::Unretained(this), run_loop2.QuitClosure())); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 413 | 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 Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 434 | // 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 440 | TEST_F(IdentityManagerImplTest, |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 441 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 450 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 451 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 452 | base::Unretained(this), run_loop.QuitClosure(), |
| 453 | base::Unretained(&account_info), base::Unretained(&account_state))); |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 454 | |
| 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 460 | GetIdentityManagerImpl()->GetAccountInfoFromGaiaId( |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 461 | kTestGaiaId, |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 462 | base::BindRepeating( |
| 463 | &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId, |
| 464 | base::Unretained(this), run_loop2.QuitClosure())); |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 465 | 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 Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 494 | // 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 497 | TEST_F(IdentityManagerImplTest, |
| 498 | GetPrimaryAccountWhenAvailableOverlappingCalls) { |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 499 | AccountInfo account_info1; |
| 500 | AccountState account_state1; |
| 501 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 502 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 503 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 504 | base::Unretained(this), run_loop.QuitClosure(), |
| 505 | base::Unretained(&account_info1), base::Unretained(&account_state1))); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 506 | |
| 507 | AccountInfo account_info2; |
| 508 | AccountState account_state2; |
| 509 | base::RunLoop run_loop2; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 510 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 511 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 512 | base::Unretained(this), run_loop2.QuitClosure(), |
| 513 | base::Unretained(&account_info2), base::Unretained(&account_state2))); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 514 | |
| 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 519 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 520 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 521 | base::Unretained(this), run_loop3.QuitClosure())); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 522 | 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); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 546 | } |
| 547 | |
Colin Blundell | 86ee410 | 2017-10-09 16:55:03 | [diff] [blame] | 548 | // Check that GetPrimaryAccountWhenAvailable() doesn't return the account as |
| 549 | // available if the refresh token has an auth error. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 550 | TEST_F(IdentityManagerImplTest, |
Colin Blundell | 86ee410 | 2017-10-09 16:55:03 | [diff] [blame] | 551 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 566 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 567 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 568 | base::Unretained(this), run_loop.QuitClosure(), |
| 569 | base::Unretained(&account_info), base::Unretained(&account_state))); |
Colin Blundell | 86ee410 | 2017-10-09 16:55:03 | [diff] [blame] | 570 | |
| 571 | // Flush the Identity Manager and check that the callback didn't fire. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 572 | FlushIdentityManagerImplForTesting(); |
Colin Blundell | 86ee410 | 2017-10-09 16:55:03 | [diff] [blame] | 573 | 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 Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 591 | // Check that the account info for a given GAIA ID is null if that GAIA ID is |
| 592 | // unknown. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 593 | TEST_F(IdentityManagerImplTest, GetAccountInfoForUnknownGaiaID) { |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 594 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 595 | GetIdentityManagerImpl()->GetAccountInfoFromGaiaId( |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 596 | kTestGaiaId, |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 597 | base::BindRepeating( |
| 598 | &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId, |
| 599 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 600 | 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 Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 605 | // GAIA ID is known and there is no refresh token available for it. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 606 | TEST_F(IdentityManagerImplTest, GetAccountInfoForKnownGaiaIdNoRefreshToken) { |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 607 | std::string account_id = |
| 608 | account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail); |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 609 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 610 | GetIdentityManagerImpl()->GetAccountInfoFromGaiaId( |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 611 | kTestGaiaId, |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 612 | base::BindRepeating( |
| 613 | &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId, |
| 614 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 615 | run_loop.Run(); |
| 616 | EXPECT_TRUE(account_info_from_gaia_id_); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 617 | EXPECT_EQ(account_id, account_info_from_gaia_id_->account_id); |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 618 | EXPECT_EQ(kTestGaiaId, account_info_from_gaia_id_->gaia); |
| 619 | EXPECT_EQ(kTestEmail, account_info_from_gaia_id_->email); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 620 | EXPECT_FALSE(account_state_from_gaia_id_.has_refresh_token); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 621 | EXPECT_FALSE(account_state_from_gaia_id_.is_primary_account); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 622 | } |
| 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 626 | TEST_F(IdentityManagerImplTest, GetAccountInfoForKnownGaiaIdRefreshToken) { |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 627 | std::string account_id = |
| 628 | account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail); |
| 629 | token_service()->UpdateCredentials(account_id, kTestRefreshToken); |
| 630 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 631 | GetIdentityManagerImpl()->GetAccountInfoFromGaiaId( |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 632 | kTestGaiaId, |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 633 | base::BindRepeating( |
| 634 | &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId, |
| 635 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 636 | 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 Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 642 | EXPECT_FALSE(account_state_from_gaia_id_.is_primary_account); |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 643 | } |
| 644 | |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 645 | // Check the implementation of GetAccounts() when there are no accounts. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 646 | TEST_F(IdentityManagerImplTest, GetAccountsNoAccount) { |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 647 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 653 | GetIdentityManagerImpl()->GetAccounts(base::BindRepeating( |
| 654 | &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this), |
| 655 | run_loop.QuitClosure(), &accounts)); |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 656 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 662 | TEST_F(IdentityManagerImplTest, GetAccountsPrimaryAccount) { |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 663 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 672 | GetIdentityManagerImpl()->GetAccounts(base::BindRepeating( |
| 673 | &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this), |
| 674 | run_loop.QuitClosure(), &accounts)); |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 675 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 691 | TEST_F(IdentityManagerImplTest, GetAccountsMultipleAccounts) { |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 692 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 705 | GetIdentityManagerImpl()->GetAccounts(base::BindRepeating( |
| 706 | &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this), |
| 707 | run_loop.QuitClosure(), &accounts)); |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 708 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 723 | GetIdentityManagerImpl()->GetAccounts(base::BindRepeating( |
| 724 | &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this), |
| 725 | run_loop2.QuitClosure(), &accounts)); |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 726 | 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 Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 747 | // Check that the expected error is received if requesting an access token when |
| 748 | // not signed in. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 749 | TEST_F(IdentityManagerImplTest, GetAccessTokenNotSignedIn) { |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 750 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 751 | GetIdentityManagerImpl()->GetAccessToken( |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 752 | kTestGaiaId, ScopeSet(), "dummy_consumer", |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 753 | base::BindRepeating(&IdentityManagerImplTest::OnReceivedAccessToken, |
| 754 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 755 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 763 | TEST_F(IdentityManagerImplTest, GetAccessTokenSignedIn) { |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 764 | signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 765 | std::string account_id = signin_manager()->GetAuthenticatedAccountId(); |
| 766 | token_service()->UpdateCredentials(account_id, kTestRefreshToken); |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 767 | token_service()->set_auto_post_fetch_response_on_message_loop(true); |
| 768 | base::RunLoop run_loop; |
| 769 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 770 | GetIdentityManagerImpl()->GetAccessToken( |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 771 | account_id, ScopeSet(), "dummy_consumer", |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame^] | 772 | base::BindRepeating(&IdentityManagerImplTest::OnReceivedAccessToken, |
| 773 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 774 | 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 | |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 780 | } // namespace |
| 781 | } // namespace identity |