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" |
Ken Rockot | 543f5e3 | 2018-02-04 02:13:50 | [diff] [blame^] | 23 | #include "services/service_manager/public/mojom/service_factory.mojom.h" |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 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 | |
Ken Rockot | cf03eee | 2018-01-16 18:48:01 | [diff] [blame] | 64 | void CreateService( |
| 65 | service_manager::mojom::ServiceRequest request, |
| 66 | const std::string& name, |
| 67 | service_manager::mojom::PIDReceiverPtr pid_receiver) override { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 68 | if (name == mojom::kServiceName) { |
| 69 | identity_service_context_.reset(new service_manager::ServiceContext( |
Jeremy Roman | 83533d7a | 2017-11-15 23:39:06 | [diff] [blame] | 70 | std::make_unique<IdentityService>(account_tracker_, signin_manager_, |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 71 | token_service_), |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 72 | std::move(request))); |
| 73 | } |
| 74 | } |
| 75 | |
Ben Goodger | 21ada1e | 2017-07-19 14:53:01 | [diff] [blame] | 76 | void Create(service_manager::mojom::ServiceFactoryRequest request) { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 77 | service_factory_bindings_.AddBinding(this, std::move(request)); |
| 78 | } |
| 79 | |
| 80 | private: |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 81 | AccountTrackerService* account_tracker_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 82 | SigninManagerBase* signin_manager_; |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 83 | ProfileOAuth2TokenService* token_service_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 84 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 90 | class IdentityManagerImplTest : public service_manager::test::ServiceTest { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 91 | public: |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 92 | IdentityManagerImplTest() |
Jay Civelli | b9b27a9c | 2017-10-27 22:08:53 | [diff] [blame] | 93 | : ServiceTest("identity_unittests"), |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 94 | signin_client_(&pref_service_), |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 95 | #if defined(OS_CHROMEOS) |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 96 | signin_manager_(&signin_client_, &account_tracker_) { |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 97 | #else |
| 98 | signin_manager_(&signin_client_, |
| 99 | &token_service_, |
| 100 | &account_tracker_, |
| 101 | nullptr) { |
| 102 | #endif |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 103 | 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 Blundell | d829d85 | 2017-06-30 11:06:06 | [diff] [blame] | 110 | void TearDown() override { |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 111 | // Shut down the SigninManager so that the IdentityManagerImpl doesn't end |
| 112 | // up outliving it. |
Colin Blundell | d829d85 | 2017-06-30 11:06:06 | [diff] [blame] | 113 | signin_manager_.Shutdown(); |
| 114 | ServiceTest::TearDown(); |
| 115 | } |
| 116 | |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 117 | void OnReceivedPrimaryAccountInfo( |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 118 | base::RepeatingClosure quit_closure, |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 119 | const base::Optional<AccountInfo>& account_info, |
| 120 | const AccountState& account_state) { |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 121 | primary_account_info_ = account_info; |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 122 | primary_account_state_ = account_state; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 123 | quit_closure.Run(); |
| 124 | } |
| 125 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 126 | void OnPrimaryAccountAvailable(base::RepeatingClosure quit_closure, |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 127 | 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 Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 136 | void OnReceivedAccountInfoFromGaiaId( |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 137 | base::RepeatingClosure quit_closure, |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 138 | const base::Optional<AccountInfo>& account_info, |
| 139 | const AccountState& account_state) { |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 140 | account_info_from_gaia_id_ = account_info; |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 141 | account_state_from_gaia_id_ = account_state; |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 142 | quit_closure.Run(); |
| 143 | } |
| 144 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 145 | void OnGotAccounts(base::RepeatingClosure quit_closure, |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 146 | std::vector<mojom::AccountPtr>* output, |
| 147 | std::vector<mojom::AccountPtr> accounts) { |
| 148 | *output = std::move(accounts); |
| 149 | quit_closure.Run(); |
| 150 | } |
| 151 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 152 | void OnReceivedAccessToken(base::RepeatingClosure quit_closure, |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 153 | 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 | |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 161 | protected: |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 162 | void SetUp() override { ServiceTest::SetUp(); } |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 163 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 164 | mojom::IdentityManager* GetIdentityManagerImpl() { |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 165 | if (!identity_manager_) |
| 166 | connector()->BindInterface(mojom::kServiceName, &identity_manager_); |
| 167 | return identity_manager_.get(); |
| 168 | } |
| 169 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 170 | void ResetIdentityManagerImpl() { identity_manager_.reset(); } |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 171 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 172 | void FlushIdentityManagerImplForTesting() { |
| 173 | GetIdentityManagerImpl(); |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 174 | identity_manager_.FlushForTesting(); |
| 175 | } |
| 176 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 177 | void SetIdentityManagerImplConnectionErrorHandler( |
| 178 | base::RepeatingClosure handler) { |
| 179 | GetIdentityManagerImpl(); |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 180 | identity_manager_.set_connection_error_handler(handler); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // service_manager::test::ServiceTest: |
| 184 | std::unique_ptr<service_manager::Service> CreateService() override { |
Jeremy Roman | 83533d7a | 2017-11-15 23:39:06 | [diff] [blame] | 185 | return std::make_unique<ServiceTestClient>( |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 186 | this, &account_tracker_, &signin_manager_, &token_service_); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | mojom::IdentityManagerPtr identity_manager_; |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 190 | base::Optional<AccountInfo> primary_account_info_; |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 191 | AccountState primary_account_state_; |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 192 | base::Optional<AccountInfo> account_info_from_gaia_id_; |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 193 | AccountState account_state_from_gaia_id_; |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 194 | base::Optional<std::string> access_token_; |
| 195 | GoogleServiceAuthError access_token_error_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 196 | |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 197 | AccountTrackerService* account_tracker() { return &account_tracker_; } |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 198 | SigninManagerBase* signin_manager() { return &signin_manager_; } |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 199 | FakeProfileOAuth2TokenService* token_service() { return &token_service_; } |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 200 | |
| 201 | private: |
| 202 | sync_preferences::TestingPrefServiceSyncable pref_service_; |
| 203 | AccountTrackerService account_tracker_; |
| 204 | TestSigninClient signin_client_; |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 205 | SigninManagerForTest signin_manager_; |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 206 | FakeProfileOAuth2TokenService token_service_; |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 207 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 208 | DISALLOW_COPY_AND_ASSIGN(IdentityManagerImplTest); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 209 | }; |
| 210 | |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 211 | // Tests that it is not possible to connect to the Identity Manager if |
| 212 | // initiated after SigninManager shutdown. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 213 | TEST_F(IdentityManagerImplTest, SigninManagerShutdownBeforeConnection) { |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 214 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 222 | FlushIdentityManagerImplForTesting(); |
| 223 | ResetIdentityManagerImpl(); |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 224 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 225 | // Make a call to connect to the IdentityManagerImpl *after* SigninManager |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 226 | // shutdown; it should get notified of an error when the Identity Service |
| 227 | // drops the connection. |
| 228 | signin_manager()->Shutdown(); |
Colin Blundell | d829d85 | 2017-06-30 11:06:06 | [diff] [blame] | 229 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 230 | SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure()); |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 231 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 232 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 233 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 234 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 235 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 243 | TEST_F(IdentityManagerImplTest, SigninManagerShutdownAfterConnection) { |
Colin Blundell | 25a8082 | 2017-07-13 06:17:52 | [diff] [blame] | 244 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 245 | SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure()); |
Colin Blundell | d829d85 | 2017-06-30 11:06:06 | [diff] [blame] | 246 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 247 | // 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 Blundell | d829d85 | 2017-06-30 11:06:06 | [diff] [blame] | 251 | signin_manager()->Shutdown(); |
| 252 | run_loop.Run(); |
| 253 | } |
| 254 | |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 255 | // 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 259 | TEST_F(IdentityManagerImplTest, IdentityManagerImplShutdownWithActiveRequest) { |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 260 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 261 | SetIdentityManagerImplConnectionErrorHandler(run_loop.QuitClosure()); |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 262 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 263 | // 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 Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 269 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 270 | // Ensure that the IdentityManagerImpl has received the above call before |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 271 | // invoking SigninManagerBase::Shutdown(), as otherwise this test is |
| 272 | // pointless. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 273 | FlushIdentityManagerImplForTesting(); |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 274 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 275 | // This flow is what would cause a DCHECK to fire if IdentityManagerImpl is |
| 276 | // not properly closing its binding on shutdown. |
Colin Blundell | f9792c0 | 2017-09-19 18:02:53 | [diff] [blame] | 277 | signin_manager()->Shutdown(); |
| 278 | run_loop.Run(); |
| 279 | } |
| 280 | |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 281 | // Check that the primary account info is null if not signed in. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 282 | TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoNotSignedIn) { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 283 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 284 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 285 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 286 | base::Unretained(this), run_loop.QuitClosure())); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 287 | run_loop.Run(); |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 288 | EXPECT_FALSE(primary_account_info_); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 289 | } |
| 290 | |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 291 | // Check that the primary account info has expected values if signed in without |
| 292 | // a refresh token available. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 293 | TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoSignedInNoRefreshToken) { |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 294 | signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail); |
| 295 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 296 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 297 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 298 | base::Unretained(this), run_loop.QuitClosure())); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 299 | run_loop.Run(); |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 300 | EXPECT_TRUE(primary_account_info_); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 301 | EXPECT_EQ(signin_manager()->GetAuthenticatedAccountId(), |
| 302 | primary_account_info_->account_id); |
Colin Blundell | 704d198e | 2017-06-05 11:27:13 | [diff] [blame] | 303 | EXPECT_EQ(kTestGaiaId, primary_account_info_->gaia); |
| 304 | EXPECT_EQ(kTestEmail, primary_account_info_->email); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 305 | EXPECT_FALSE(primary_account_state_.has_refresh_token); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 306 | EXPECT_TRUE(primary_account_state_.is_primary_account); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // Check that the primary account info has expected values if signed in with a |
| 310 | // refresh token available. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 311 | TEST_F(IdentityManagerImplTest, GetPrimaryAccountInfoSignedInRefreshToken) { |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 312 | signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail); |
| 313 | token_service()->UpdateCredentials( |
| 314 | signin_manager()->GetAuthenticatedAccountId(), kTestRefreshToken); |
| 315 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 316 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 317 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 318 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 319 | 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 Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 326 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 331 | TEST_F(IdentityManagerImplTest, GetPrimaryAccountWhenAvailableSignedIn) { |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 332 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 339 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 340 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 341 | base::Unretained(this), run_loop.QuitClosure(), |
| 342 | base::Unretained(&account_info), base::Unretained(&account_state))); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 343 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 356 | TEST_F(IdentityManagerImplTest, GetPrimaryAccountWhenAvailableSignInLater) { |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 357 | AccountInfo account_info; |
| 358 | AccountState account_state; |
| 359 | |
| 360 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 361 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 362 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 363 | base::Unretained(this), run_loop.QuitClosure(), |
| 364 | base::Unretained(&account_info), base::Unretained(&account_state))); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 365 | |
| 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 370 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 371 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 372 | base::Unretained(this), run_loop2.QuitClosure())); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 373 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 394 | TEST_F(IdentityManagerImplTest, |
| 395 | GetPrimaryAccountWhenAvailableTokenAvailableLater) { |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 396 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 402 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 403 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 404 | base::Unretained(this), run_loop.QuitClosure(), |
| 405 | base::Unretained(&account_info), base::Unretained(&account_state))); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 406 | |
| 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 412 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 413 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 414 | base::Unretained(this), run_loop2.QuitClosure())); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 415 | 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 Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 436 | // 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 442 | TEST_F(IdentityManagerImplTest, |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 443 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 452 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 453 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 454 | base::Unretained(this), run_loop.QuitClosure(), |
| 455 | base::Unretained(&account_info), base::Unretained(&account_state))); |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 456 | |
| 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 462 | GetIdentityManagerImpl()->GetAccountInfoFromGaiaId( |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 463 | kTestGaiaId, |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 464 | base::BindRepeating( |
| 465 | &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId, |
| 466 | base::Unretained(this), run_loop2.QuitClosure())); |
Colin Blundell | 2c8951de | 2017-07-07 13:23:45 | [diff] [blame] | 467 | 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 Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 496 | // 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 499 | TEST_F(IdentityManagerImplTest, |
| 500 | GetPrimaryAccountWhenAvailableOverlappingCalls) { |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 501 | AccountInfo account_info1; |
| 502 | AccountState account_state1; |
| 503 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 504 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 505 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 506 | base::Unretained(this), run_loop.QuitClosure(), |
| 507 | base::Unretained(&account_info1), base::Unretained(&account_state1))); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 508 | |
| 509 | AccountInfo account_info2; |
| 510 | AccountState account_state2; |
| 511 | base::RunLoop run_loop2; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 512 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 513 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 514 | base::Unretained(this), run_loop2.QuitClosure(), |
| 515 | base::Unretained(&account_info2), base::Unretained(&account_state2))); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 516 | |
| 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 521 | GetIdentityManagerImpl()->GetPrimaryAccountInfo(base::BindRepeating( |
| 522 | &IdentityManagerImplTest::OnReceivedPrimaryAccountInfo, |
| 523 | base::Unretained(this), run_loop3.QuitClosure())); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 524 | 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); |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 548 | } |
| 549 | |
Colin Blundell | 86ee410 | 2017-10-09 16:55:03 | [diff] [blame] | 550 | // Check that GetPrimaryAccountWhenAvailable() doesn't return the account as |
| 551 | // available if the refresh token has an auth error. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 552 | TEST_F(IdentityManagerImplTest, |
Colin Blundell | 86ee410 | 2017-10-09 16:55:03 | [diff] [blame] | 553 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 568 | GetIdentityManagerImpl()->GetPrimaryAccountWhenAvailable(base::BindRepeating( |
| 569 | &IdentityManagerImplTest::OnPrimaryAccountAvailable, |
| 570 | base::Unretained(this), run_loop.QuitClosure(), |
| 571 | base::Unretained(&account_info), base::Unretained(&account_state))); |
Colin Blundell | 86ee410 | 2017-10-09 16:55:03 | [diff] [blame] | 572 | |
| 573 | // Flush the Identity Manager and check that the callback didn't fire. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 574 | FlushIdentityManagerImplForTesting(); |
Colin Blundell | 86ee410 | 2017-10-09 16:55:03 | [diff] [blame] | 575 | 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 Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 593 | // Check that the account info for a given GAIA ID is null if that GAIA ID is |
| 594 | // unknown. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 595 | TEST_F(IdentityManagerImplTest, GetAccountInfoForUnknownGaiaID) { |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 596 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 597 | GetIdentityManagerImpl()->GetAccountInfoFromGaiaId( |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 598 | kTestGaiaId, |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 599 | base::BindRepeating( |
| 600 | &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId, |
| 601 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 602 | 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 Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 607 | // GAIA ID is known and there is no refresh token available for it. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 608 | TEST_F(IdentityManagerImplTest, GetAccountInfoForKnownGaiaIdNoRefreshToken) { |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 609 | std::string account_id = |
| 610 | account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail); |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 611 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 612 | GetIdentityManagerImpl()->GetAccountInfoFromGaiaId( |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 613 | kTestGaiaId, |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 614 | base::BindRepeating( |
| 615 | &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId, |
| 616 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 617 | run_loop.Run(); |
| 618 | EXPECT_TRUE(account_info_from_gaia_id_); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 619 | EXPECT_EQ(account_id, account_info_from_gaia_id_->account_id); |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 620 | EXPECT_EQ(kTestGaiaId, account_info_from_gaia_id_->gaia); |
| 621 | EXPECT_EQ(kTestEmail, account_info_from_gaia_id_->email); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 622 | EXPECT_FALSE(account_state_from_gaia_id_.has_refresh_token); |
Colin Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 623 | EXPECT_FALSE(account_state_from_gaia_id_.is_primary_account); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 624 | } |
| 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 628 | TEST_F(IdentityManagerImplTest, GetAccountInfoForKnownGaiaIdRefreshToken) { |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 629 | std::string account_id = |
| 630 | account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail); |
| 631 | token_service()->UpdateCredentials(account_id, kTestRefreshToken); |
| 632 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 633 | GetIdentityManagerImpl()->GetAccountInfoFromGaiaId( |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 634 | kTestGaiaId, |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 635 | base::BindRepeating( |
| 636 | &IdentityManagerImplTest::OnReceivedAccountInfoFromGaiaId, |
| 637 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 638 | 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 Blundell | bdf4642d | 2017-07-06 07:00:03 | [diff] [blame] | 644 | EXPECT_FALSE(account_state_from_gaia_id_.is_primary_account); |
Colin Blundell | 2f987fd | 2017-06-23 10:49:06 | [diff] [blame] | 645 | } |
| 646 | |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 647 | // Check the implementation of GetAccounts() when there are no accounts. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 648 | TEST_F(IdentityManagerImplTest, GetAccountsNoAccount) { |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 649 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 655 | GetIdentityManagerImpl()->GetAccounts(base::BindRepeating( |
| 656 | &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this), |
| 657 | run_loop.QuitClosure(), &accounts)); |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 658 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 664 | TEST_F(IdentityManagerImplTest, GetAccountsPrimaryAccount) { |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 665 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 674 | GetIdentityManagerImpl()->GetAccounts(base::BindRepeating( |
| 675 | &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this), |
| 676 | run_loop.QuitClosure(), &accounts)); |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 677 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 693 | TEST_F(IdentityManagerImplTest, GetAccountsMultipleAccounts) { |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 694 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 707 | GetIdentityManagerImpl()->GetAccounts(base::BindRepeating( |
| 708 | &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this), |
| 709 | run_loop.QuitClosure(), &accounts)); |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 710 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 725 | GetIdentityManagerImpl()->GetAccounts(base::BindRepeating( |
| 726 | &IdentityManagerImplTest::OnGotAccounts, base::Unretained(this), |
| 727 | run_loop2.QuitClosure(), &accounts)); |
Colin Blundell | bf44268 | 2017-07-28 13:08:05 | [diff] [blame] | 728 | 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 Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 749 | // Check that the expected error is received if requesting an access token when |
| 750 | // not signed in. |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 751 | TEST_F(IdentityManagerImplTest, GetAccessTokenNotSignedIn) { |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 752 | base::RunLoop run_loop; |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 753 | GetIdentityManagerImpl()->GetAccessToken( |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 754 | kTestGaiaId, ScopeSet(), "dummy_consumer", |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 755 | base::BindRepeating(&IdentityManagerImplTest::OnReceivedAccessToken, |
| 756 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 757 | 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 Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 765 | TEST_F(IdentityManagerImplTest, GetAccessTokenSignedIn) { |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 766 | signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail); |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 767 | std::string account_id = signin_manager()->GetAuthenticatedAccountId(); |
| 768 | token_service()->UpdateCredentials(account_id, kTestRefreshToken); |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 769 | token_service()->set_auto_post_fetch_response_on_message_loop(true); |
| 770 | base::RunLoop run_loop; |
| 771 | |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 772 | GetIdentityManagerImpl()->GetAccessToken( |
Colin Blundell | 57fb650 | 2017-06-27 12:24:34 | [diff] [blame] | 773 | account_id, ScopeSet(), "dummy_consumer", |
Colin Blundell | 7b2a9f0 | 2017-12-19 12:13:03 | [diff] [blame] | 774 | base::BindRepeating(&IdentityManagerImplTest::OnReceivedAccessToken, |
| 775 | base::Unretained(this), run_loop.QuitClosure())); |
Colin Blundell | e89886a | 2017-06-12 11:26:56 | [diff] [blame] | 776 | 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 | |
blundell | 1e21bd3 | 2017-05-03 15:25:57 | [diff] [blame] | 782 | } // namespace |
| 783 | } // namespace identity |