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