OLD | NEW |
(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 ServiceTestClient(service_manager::test::ServiceTest* test, |
| 32 scoped_refptr<base::SequencedWorkerPool> worker_pool) |
| 33 : service_manager::test::ServiceTestClient(test), |
| 34 worker_pool_(std::move(worker_pool)) {} |
| 35 |
| 36 protected: |
| 37 bool OnConnect(const service_manager::ServiceInfo& remote_info, |
| 38 service_manager::InterfaceRegistry* registry) override { |
| 39 registry->AddInterface<service_manager::mojom::ServiceFactory>(this); |
| 40 return true; |
| 41 } |
| 42 |
| 43 void CreateService(service_manager::mojom::ServiceRequest request, |
| 44 const std::string& name) override { |
| 45 if (name == prefs::mojom::kPrefStoreServiceName) { |
| 46 pref_service_context_.reset(new service_manager::ServiceContext( |
| 47 base::MakeUnique<prefs::PrefStoreManagerImpl>( |
| 48 prefs::PrefStoreManagerImpl::PrefStoreTypes(), worker_pool_), |
| 49 std::move(request))); |
| 50 } |
| 51 } |
| 52 |
| 53 void Create(const service_manager::Identity& remote_identity, |
| 54 service_manager::mojom::ServiceFactoryRequest request) override { |
| 55 service_factory_bindings_.AddBinding(this, std::move(request)); |
| 56 } |
| 57 |
| 58 private: |
| 59 scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
| 60 mojo::BindingSet<service_manager::mojom::ServiceFactory> |
| 61 service_factory_bindings_; |
| 62 std::unique_ptr<service_manager::ServiceContext> pref_service_context_; |
| 63 }; |
| 64 |
| 65 constexpr int kInitialValue = 1; |
| 66 constexpr int kUpdatedValue = 2; |
| 67 constexpr char kKey[] = "some_key"; |
| 68 |
| 69 class PrefServiceFactoryTest : public base::MessageLoop::DestructionObserver, |
| 70 public service_manager::test::ServiceTest { |
| 71 public: |
| 72 PrefServiceFactoryTest() : ServiceTest("prefs_unittests", false) {} |
| 73 |
| 74 protected: |
| 75 void SetUp() override { |
| 76 ServiceTest::SetUp(); |
| 77 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
| 78 |
| 79 // Init the pref service (in production Chrome startup would do this.) |
| 80 mojom::PrefServiceControlPtr control; |
| 81 connector()->BindInterface(mojom::kPrefStoreServiceName, &control); |
| 82 auto config = mojom::PersistentPrefStoreConfiguration::New(); |
| 83 config->set_simple_configuration( |
| 84 mojom::SimplePersistentPrefStoreConfiguration::New( |
| 85 profile_dir_.GetPath().AppendASCII("Preferences"))); |
| 86 control->Init(std::move(config)); |
| 87 } |
| 88 |
| 89 // service_manager::test::ServiceTest: |
| 90 std::unique_ptr<service_manager::Service> CreateService() override { |
| 91 return base::MakeUnique<ServiceTestClient>(this, |
| 92 worker_pool_owner_->pool()); |
| 93 } |
| 94 |
| 95 std::unique_ptr<base::MessageLoop> CreateMessageLoop() override { |
| 96 auto loop = ServiceTest::CreateMessageLoop(); |
| 97 worker_pool_owner_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( |
| 98 2, "PrefServiceFactoryTest"); |
| 99 loop->AddDestructionObserver(this); |
| 100 return loop; |
| 101 } |
| 102 |
| 103 // base::MessageLoop::DestructionObserver |
| 104 void WillDestroyCurrentMessageLoop() override { worker_pool_owner_.reset(); } |
| 105 |
| 106 // Create a fully initialized PrefService synchronously. |
| 107 std::unique_ptr<PrefService> Create() { |
| 108 std::unique_ptr<PrefService> pref_service; |
| 109 base::RunLoop run_loop; |
| 110 auto pref_registry = make_scoped_refptr(new PrefRegistrySimple()); |
| 111 pref_registry->RegisterIntegerPref(kKey, kInitialValue); |
| 112 ConnectToPrefService(connector(), pref_registry, |
| 113 base::Bind(&PrefServiceFactoryTest::OnCreate, |
| 114 run_loop.QuitClosure(), &pref_service)); |
| 115 run_loop.Run(); |
| 116 return pref_service; |
| 117 } |
| 118 |
| 119 // Wait until first update of the pref |key| in |pref_service| synchronously. |
| 120 void WaitForPrefChange(PrefService* pref_service, const std::string& key) { |
| 121 PrefChangeRegistrar registrar; |
| 122 registrar.Init(pref_service); |
| 123 base::RunLoop run_loop; |
| 124 registrar.Add(key, base::Bind(&OnPrefChanged, run_loop.QuitClosure(), key)); |
| 125 run_loop.Run(); |
| 126 } |
| 127 |
| 128 private: |
| 129 // Called when the PrefService has been initialized. |
| 130 static void OnInit(const base::Closure& quit_closure, bool success) { |
| 131 quit_closure.Run(); |
| 132 } |
| 133 |
| 134 // Called when the PrefService has been created. |
| 135 static void OnCreate(const base::Closure& quit_closure, |
| 136 std::unique_ptr<PrefService>* out, |
| 137 std::unique_ptr<PrefService> pref_service) { |
| 138 DCHECK(pref_service); |
| 139 *out = std::move(pref_service); |
| 140 (*out)->AddPrefInitObserver( |
| 141 base::Bind(PrefServiceFactoryTest::OnInit, quit_closure)); |
| 142 } |
| 143 |
| 144 static void OnPrefChanged(const base::Closure& quit_closure, |
| 145 const std::string& expected_path, |
| 146 const std::string& path) { |
| 147 if (path == expected_path) |
| 148 quit_closure.Run(); |
| 149 } |
| 150 |
| 151 base::ScopedTempDir profile_dir_; |
| 152 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_owner_; |
| 153 |
| 154 DISALLOW_COPY_AND_ASSIGN(PrefServiceFactoryTest); |
| 155 }; |
| 156 |
| 157 // Check that a single client can set and read back values. |
| 158 TEST_F(PrefServiceFactoryTest, Basic) { |
| 159 auto pref_service = Create(); |
| 160 |
| 161 // TODO(tibell): Once we have a default store check the value prior to |
| 162 // setting. |
| 163 pref_service->SetInteger(kKey, kUpdatedValue); |
| 164 EXPECT_EQ(kUpdatedValue, pref_service->GetInteger(kKey)); |
| 165 } |
| 166 |
| 167 // Check that updates in one client eventually propagates to the other. |
| 168 TEST_F(PrefServiceFactoryTest, MultipleClients) { |
| 169 auto pref_service = Create(); |
| 170 auto pref_service2 = Create(); |
| 171 |
| 172 // TODO(tibell): Once we have a default store check the value prior to |
| 173 // setting. |
| 174 pref_service->SetInteger(kKey, kUpdatedValue); |
| 175 WaitForPrefChange(pref_service2.get(), kKey); |
| 176 EXPECT_EQ(kUpdatedValue, pref_service2->GetInteger(kKey)); |
| 177 } |
| 178 |
| 179 } // namespace |
| 180 } // namespace prefs |
OLD | NEW |