OLD | NEW |
(Empty) | |
| 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" |
| 6 #include "components/signin/core/account_id/account_id.h" |
| 7 #include "components/signin/core/browser/account_tracker_service.h" |
| 8 #include "components/signin/core/browser/fake_signin_manager.h" |
| 9 #include "components/signin/core/browser/test_signin_client.h" |
| 10 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 11 #include "mojo/public/cpp/bindings/binding_set.h" |
| 12 #include "services/identity/identity_service.h" |
| 13 #include "services/identity/public/interfaces/constants.mojom.h" |
| 14 #include "services/identity/public/interfaces/identity_manager.mojom.h" |
| 15 #include "services/service_manager/public/cpp/binder_registry.h" |
| 16 #include "services/service_manager/public/cpp/service_context.h" |
| 17 #include "services/service_manager/public/cpp/service_test.h" |
| 18 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |
| 19 |
| 20 namespace identity { |
| 21 namespace { |
| 22 |
| 23 const std::string kTestGaiaId = "dummyId"; |
| 24 const std::string kTestEmail = "me@dummy.com"; |
| 25 |
| 26 class ServiceTestClient : public service_manager::test::ServiceTestClient, |
| 27 public service_manager::mojom::ServiceFactory { |
| 28 public: |
| 29 ServiceTestClient(service_manager::test::ServiceTest* test, |
| 30 SigninManagerBase* signin_manager) |
| 31 : service_manager::test::ServiceTestClient(test), |
| 32 signin_manager_(signin_manager) { |
| 33 registry_.AddInterface<service_manager::mojom::ServiceFactory>( |
| 34 base::Bind(&ServiceTestClient::Create, base::Unretained(this))); |
| 35 } |
| 36 |
| 37 protected: |
| 38 void OnBindInterface(const service_manager::BindSourceInfo& source_info, |
| 39 const std::string& interface_name, |
| 40 mojo::ScopedMessagePipeHandle interface_pipe) override { |
| 41 registry_.BindInterface(source_info, interface_name, |
| 42 std::move(interface_pipe)); |
| 43 } |
| 44 |
| 45 void CreateService(service_manager::mojom::ServiceRequest request, |
| 46 const std::string& name) override { |
| 47 if (name == mojom::kServiceName) { |
| 48 identity_service_context_.reset(new service_manager::ServiceContext( |
| 49 base::MakeUnique<IdentityService>(signin_manager_), |
| 50 std::move(request))); |
| 51 } |
| 52 } |
| 53 |
| 54 void Create(const service_manager::BindSourceInfo& source_info, |
| 55 service_manager::mojom::ServiceFactoryRequest request) { |
| 56 service_factory_bindings_.AddBinding(this, std::move(request)); |
| 57 } |
| 58 |
| 59 private: |
| 60 SigninManagerBase* signin_manager_; |
| 61 service_manager::BinderRegistry registry_; |
| 62 mojo::BindingSet<service_manager::mojom::ServiceFactory> |
| 63 service_factory_bindings_; |
| 64 std::unique_ptr<service_manager::ServiceContext> identity_service_context_; |
| 65 }; |
| 66 |
| 67 class IdentityManagerTest : public service_manager::test::ServiceTest { |
| 68 public: |
| 69 IdentityManagerTest() |
| 70 : ServiceTest("identity_unittests", false), |
| 71 signin_client_(&pref_service_), |
| 72 signin_manager_(&signin_client_, &account_tracker_) { |
| 73 AccountTrackerService::RegisterPrefs(pref_service_.registry()); |
| 74 SigninManagerBase::RegisterProfilePrefs(pref_service_.registry()); |
| 75 SigninManagerBase::RegisterPrefs(pref_service_.registry()); |
| 76 |
| 77 account_tracker_.Initialize(&signin_client_); |
| 78 } |
| 79 |
| 80 void OnReceivedPrimaryAccountId(base::Closure quit_closure, |
| 81 const base::Optional<AccountId>& account_id) { |
| 82 primary_account_id_ = account_id; |
| 83 quit_closure.Run(); |
| 84 } |
| 85 |
| 86 protected: |
| 87 void SetUp() override { |
| 88 ServiceTest::SetUp(); |
| 89 |
| 90 connector()->BindInterface(mojom::kServiceName, &identity_manager_); |
| 91 } |
| 92 |
| 93 // service_manager::test::ServiceTest: |
| 94 std::unique_ptr<service_manager::Service> CreateService() override { |
| 95 return base::MakeUnique<ServiceTestClient>(this, &signin_manager_); |
| 96 } |
| 97 |
| 98 mojom::IdentityManagerPtr identity_manager_; |
| 99 base::Optional<AccountId> primary_account_id_; |
| 100 |
| 101 SigninManagerBase* signin_manager() { return &signin_manager_; } |
| 102 |
| 103 private: |
| 104 sync_preferences::TestingPrefServiceSyncable pref_service_; |
| 105 AccountTrackerService account_tracker_; |
| 106 TestSigninClient signin_client_; |
| 107 FakeSigninManagerBase signin_manager_; |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(IdentityManagerTest); |
| 110 }; |
| 111 |
| 112 // Check that the primary account ID is null if not signed in. |
| 113 TEST_F(IdentityManagerTest, NotSignedIn) { |
| 114 base::RunLoop run_loop; |
| 115 identity_manager_->GetPrimaryAccountId( |
| 116 base::Bind(&IdentityManagerTest::OnReceivedPrimaryAccountId, |
| 117 base::Unretained(this), run_loop.QuitClosure())); |
| 118 run_loop.Run(); |
| 119 EXPECT_FALSE(primary_account_id_); |
| 120 } |
| 121 |
| 122 // Check that the primary account ID has expected values if signed in. |
| 123 TEST_F(IdentityManagerTest, SignedIn) { |
| 124 signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail); |
| 125 base::RunLoop run_loop; |
| 126 identity_manager_->GetPrimaryAccountId( |
| 127 base::Bind(&IdentityManagerTest::OnReceivedPrimaryAccountId, |
| 128 base::Unretained(this), run_loop.QuitClosure())); |
| 129 run_loop.Run(); |
| 130 EXPECT_TRUE(primary_account_id_); |
| 131 EXPECT_EQ(kTestGaiaId, primary_account_id_->GetGaiaId()); |
| 132 EXPECT_EQ(kTestEmail, primary_account_id_->GetUserEmail()); |
| 133 } |
| 134 |
| 135 } // namespace |
| 136 } // namespace identity |
OLD | NEW |