Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(495)

Side by Side Diff: services/preferences/pref_service_factory_unittest.cc

Issue 2762333003: Pref service: add a factory to create the client lib (Closed)
Patch Set: Make tests compile on Windows Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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 "services/preferences/public/cpp/pref_service_factory.h"
6
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/test/sequenced_worker_pool_owner.h"
11 #include "components/prefs/pref_change_registrar.h"
12 #include "components/prefs/pref_registry_simple.h"
13 #include "components/prefs/pref_service.h"
14 #include "mojo/public/cpp/bindings/binding_set.h"
15 #include "services/preferences/public/cpp/pref_store_manager_impl.h"
16 #include "services/preferences/public/interfaces/preferences.mojom.h"
17 #include "services/service_manager/public/cpp/interface_factory.h"
18 #include "services/service_manager/public/cpp/interface_registry.h"
19 #include "services/service_manager/public/cpp/service_context.h"
20 #include "services/service_manager/public/cpp/service_test.h"
21 #include "services/service_manager/public/interfaces/service_factory.mojom.h"
22
23 namespace prefs {
24 namespace {
25
26 class ServiceTestClient : public service_manager::test::ServiceTestClient,
27 public service_manager::mojom::ServiceFactory,
28 public service_manager::InterfaceFactory<
29 service_manager::mojom::ServiceFactory> {
30 public:
31 explicit ServiceTestClient(
Sam McNally 2017/03/22 04:34:03 Remove explicit.
tibell 2017/03/22 05:32:04 Done.
32 service_manager::test::ServiceTest* test,
33 scoped_refptr<base::SequencedWorkerPool> worker_pool)
34 : service_manager::test::ServiceTestClient(test),
35 worker_pool_(std::move(worker_pool)) {}
36
37 protected:
38 bool OnConnect(const service_manager::ServiceInfo& remote_info,
39 service_manager::InterfaceRegistry* registry) override {
40 registry->AddInterface<service_manager::mojom::ServiceFactory>(this);
41 return true;
42 }
43
44 void CreateService(service_manager::mojom::ServiceRequest request,
45 const std::string& name) override {
46 if (name == prefs::mojom::kPrefStoreServiceName) {
47 pref_service_context_.reset(new service_manager::ServiceContext(
48 base::MakeUnique<prefs::PrefStoreManagerImpl>(
49 prefs::PrefStoreManagerImpl::PrefStoreTypes(), worker_pool_),
50 std::move(request)));
51 }
52 }
53
54 void Create(const service_manager::Identity& remote_identity,
55 service_manager::mojom::ServiceFactoryRequest request) override {
56 service_factory_bindings_.AddBinding(this, std::move(request));
57 }
58
59 private:
60 scoped_refptr<base::SequencedWorkerPool> worker_pool_;
61 mojo::BindingSet<service_manager::mojom::ServiceFactory>
62 service_factory_bindings_;
63 std::unique_ptr<service_manager::ServiceContext> pref_service_context_;
64 };
65
66 constexpr int kInitialValue = 1;
67 constexpr int kUpdatedValue = 2;
68 constexpr char kKey[] = "some_key";
69
70 class PrefServiceFactoryTest : public base::MessageLoop::DestructionObserver,
71 public service_manager::test::ServiceTest {
72 public:
73 PrefServiceFactoryTest() : ServiceTest("prefs_unittests", false) {}
74
75 protected:
76 void SetUp() override {
77 ServiceTest::SetUp();
78 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
79
80 // Init the pref service (in production Chrome startup would do this.)
81 mojom::PrefServiceControlPtr control;
82 connector()->BindInterface(mojom::kPrefStoreServiceName, &control);
83 auto config = mojom::PersistentPrefStoreConfiguration::New();
84 config->set_simple_configuration(
85 mojom::SimplePersistentPrefStoreConfiguration::New(
86 profile_dir_.GetPath().AppendASCII("Preferences")));
87 control->Init(std::move(config));
88 }
89
90 // service_manager::test::ServiceTest:
91 std::unique_ptr<service_manager::Service> CreateService() override {
92 return base::MakeUnique<ServiceTestClient>(this,
93 worker_pool_owner_->pool());
94 }
95
96 std::unique_ptr<base::MessageLoop> CreateMessageLoop() override {
97 auto loop = ServiceTest::CreateMessageLoop();
98 worker_pool_owner_ = base::MakeUnique<base::SequencedWorkerPoolOwner>(
99 2, "PrefServiceFactoryTest");
100 loop->AddDestructionObserver(this);
101 return loop;
102 }
103
104 // base::MessageLoop::DestructionObserver
105 void WillDestroyCurrentMessageLoop() override { worker_pool_owner_.reset(); }
Sam McNally 2017/03/22 04:34:03 :(
tibell 2017/03/22 05:32:04 Acknowledged.
106
107 // Called when the PrefService has been initialized.
108 static void OnInit(const base::Closure& quit_closure, bool success) {
Sam McNally 2017/03/22 04:34:03 private?
tibell 2017/03/22 05:32:04 Done.
109 quit_closure.Run();
110 }
111
112 // Called when the PrefService has been created.
113 static void OnCreate(const base::Closure& quit_closure,
Sam McNally 2017/03/22 04:34:03 private?
tibell 2017/03/22 05:32:04 Done.
114 std::unique_ptr<PrefService>* out,
115 std::unique_ptr<PrefService> pref_service) {
116 DCHECK(pref_service);
117 *out = std::move(pref_service);
118 (*out)->AddPrefInitObserver(
119 base::Bind(PrefServiceFactoryTest::OnInit, quit_closure));
120 }
121
122 // Create a fully initialized PrefService synchronously.
123 std::unique_ptr<PrefService> Create() {
124 std::unique_ptr<PrefService> pref_service;
125 base::RunLoop run_loop;
126 auto pref_registry = make_scoped_refptr(new PrefRegistrySimple());
127 pref_registry->RegisterIntegerPref(kKey, kInitialValue);
128 CreatePrefService(
129 connector(), pref_registry,
130 base::Bind(&PrefServiceFactoryTest::OnCreate, run_loop.QuitClosure(),
131 base::Unretained(&pref_service)));
Sam McNally 2017/03/22 04:34:03 Is the Unretained() necessary?
tibell 2017/03/22 05:32:04 Done.
132 run_loop.Run();
133 return pref_service;
134 }
135
136 static void OnPrefChanged(const base::Closure& quit_closure,
Sam McNally 2017/03/22 04:34:03 private?
tibell 2017/03/22 05:32:04 Done.
137 const std::string& expected_path,
138 const std::string& path) {
139 if (path == expected_path)
140 quit_closure.Run();
141 }
142
143 // Wait until first update of the pref |key| in |pref_service| synchronously.
144 void WaitForPrefChange(PrefService* pref_service, const std::string& key) {
145 PrefChangeRegistrar registrar;
146 registrar.Init(pref_service);
147 base::RunLoop run_loop;
148 registrar.Add(key, base::Bind(&OnPrefChanged, run_loop.QuitClosure(), key));
149 run_loop.Run();
150 }
151
152 private:
153 base::ScopedTempDir profile_dir_;
154 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_owner_;
155
156 DISALLOW_COPY_AND_ASSIGN(PrefServiceFactoryTest);
157 };
158
159 // Check that a single client can set and read back values.
160 TEST_F(PrefServiceFactoryTest, Basic) {
161 auto pref_service = Create();
162
163 // TODO(tibell): Once we have a default store check the value prior to
164 // setting.
165 pref_service->SetInteger(kKey, kUpdatedValue);
166 EXPECT_EQ(kUpdatedValue, pref_service->GetInteger(kKey));
167 }
168
169 // Check that updates in one client eventually propagates to the other.
170 TEST_F(PrefServiceFactoryTest, MultipleClients) {
171 auto pref_service = Create();
172 auto pref_service2 = Create();
173
174 // TODO(tibell): Once we have a default store check the value prior to
175 // setting.
176 pref_service->SetInteger(kKey, kUpdatedValue);
177 WaitForPrefChange(pref_service2.get(), kKey);
178 EXPECT_EQ(kUpdatedValue, pref_service2->GetInteger(kKey));
179 }
180
181 } // namespace
182 } // namespace prefs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698