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

Side by Side Diff: services/device/device_service_test_base.cc

Issue 2812223006: Replace device_sensor browsertest by service unittest. (Closed)
Patch Set: eliminate "unreachable code" warning. Created 3 years, 8 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
« no previous file with comments | « services/device/device_service_test_base.h ('k') | services/device/sensors/DEPS » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "services/device/device_service_test_base.h" 5 #include "services/device/device_service_test_base.h"
6 6
7 #include <memory> 7 #include <memory>
8
9 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
10 #include "base/threading/thread.h" 9 #include "base/memory/ref_counted.h"
11 #include "mojo/public/cpp/bindings/binding_set.h" 10 #include "mojo/public/cpp/bindings/binding_set.h"
12 #include "services/device/device_service.h" 11 #include "services/device/device_service.h"
13 #include "services/device/public/interfaces/constants.mojom.h" 12 #include "services/device/public/interfaces/constants.mojom.h"
14 #include "services/service_manager/public/cpp/binder_registry.h" 13 #include "services/service_manager/public/cpp/binder_registry.h"
15 #include "services/service_manager/public/cpp/interface_factory.h" 14 #include "services/service_manager/public/cpp/interface_factory.h"
16 #include "services/service_manager/public/cpp/service_context.h" 15 #include "services/service_manager/public/cpp/service_context.h"
17 #include "services/service_manager/public/interfaces/service_factory.mojom.h" 16 #include "services/service_manager/public/interfaces/service_factory.mojom.h"
18 17
19 namespace device { 18 namespace device {
20 19
21 namespace { 20 namespace {
22 21
23 const char kTestServiceName[] = "device_unittests"; 22 const char kTestServiceName[] = "device_unittests";
24 23
25 // The test service responsible to package Device Service. 24 // The test service responsible to package Device Service.
26 class ServiceTestClient : public service_manager::test::ServiceTestClient, 25 class ServiceTestClient : public service_manager::test::ServiceTestClient,
27 public service_manager::mojom::ServiceFactory, 26 public service_manager::mojom::ServiceFactory,
28 public service_manager::InterfaceFactory< 27 public service_manager::InterfaceFactory<
29 service_manager::mojom::ServiceFactory> { 28 service_manager::mojom::ServiceFactory> {
30 public: 29 public:
31 explicit ServiceTestClient(service_manager::test::ServiceTest* test) 30 explicit ServiceTestClient(
31 service_manager::test::ServiceTest* test,
32 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
32 : service_manager::test::ServiceTestClient(test), 33 : service_manager::test::ServiceTestClient(test),
33 io_thread_("DeviceServiceTestIOThread"), 34 file_thread_("DeviceServiceTestFileThread"),
34 file_thread_("DeviceServiceTestFileThread") { 35 io_task_runner_(std::move(io_task_runner)) {
35 registry_.AddInterface<service_manager::mojom::ServiceFactory>(this); 36 registry_.AddInterface<service_manager::mojom::ServiceFactory>(this);
36 } 37 }
37 ~ServiceTestClient() override {} 38 ~ServiceTestClient() override {}
38 39
39 protected: 40 protected:
40 void OnBindInterface(const service_manager::ServiceInfo& source_info, 41 void OnBindInterface(const service_manager::ServiceInfo& source_info,
41 const std::string& interface_name, 42 const std::string& interface_name,
42 mojo::ScopedMessagePipeHandle interface_pipe) override { 43 mojo::ScopedMessagePipeHandle interface_pipe) override {
43 registry_.BindInterface(source_info.identity, interface_name, 44 registry_.BindInterface(source_info.identity, interface_name,
44 std::move(interface_pipe)); 45 std::move(interface_pipe));
45 } 46 }
46 47
47 void CreateService(service_manager::mojom::ServiceRequest request, 48 void CreateService(service_manager::mojom::ServiceRequest request,
48 const std::string& name) override { 49 const std::string& name) override {
49 if (name == device::mojom::kServiceName) { 50 if (name == device::mojom::kServiceName) {
50 io_thread_.Start();
51 file_thread_.Start(); 51 file_thread_.Start();
52 #if defined(OS_ANDROID) 52 #if defined(OS_ANDROID)
53 device_service_context_.reset(new service_manager::ServiceContext( 53 device_service_context_.reset(new service_manager::ServiceContext(
54 CreateDeviceService(file_thread_.task_runner(), 54 CreateDeviceService(file_thread_.task_runner(), io_task_runner_,
55 io_thread_.task_runner(),
56 wake_lock_context_callback_), 55 wake_lock_context_callback_),
57 std::move(request))); 56 std::move(request)));
58 #else 57 #else
59 device_service_context_.reset(new service_manager::ServiceContext( 58 device_service_context_.reset(new service_manager::ServiceContext(
60 CreateDeviceService(file_thread_.task_runner(), 59 CreateDeviceService(file_thread_.task_runner(), io_task_runner_),
61 io_thread_.task_runner()),
62 std::move(request))); 60 std::move(request)));
63 #endif 61 #endif
64 } 62 }
65 } 63 }
66 64
67 void Create(const service_manager::Identity& remote_identity, 65 void Create(const service_manager::Identity& remote_identity,
68 service_manager::mojom::ServiceFactoryRequest request) override { 66 service_manager::mojom::ServiceFactoryRequest request) override {
69 service_factory_bindings_.AddBinding(this, std::move(request)); 67 service_factory_bindings_.AddBinding(this, std::move(request));
70 } 68 }
71 69
72 private: 70 private:
73 base::Thread io_thread_;
74 base::Thread file_thread_; 71 base::Thread file_thread_;
75 service_manager::BinderRegistry registry_; 72 service_manager::BinderRegistry registry_;
76 mojo::BindingSet<service_manager::mojom::ServiceFactory> 73 mojo::BindingSet<service_manager::mojom::ServiceFactory>
77 service_factory_bindings_; 74 service_factory_bindings_;
78 std::unique_ptr<service_manager::ServiceContext> device_service_context_; 75 std::unique_ptr<service_manager::ServiceContext> device_service_context_;
79 76
77 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
78
80 WakeLockContextCallback wake_lock_context_callback_; 79 WakeLockContextCallback wake_lock_context_callback_;
81 }; 80 };
82 81
83 } // namespace 82 } // namespace
84 83
85 DeviceServiceTestBase::DeviceServiceTestBase() 84 DeviceServiceTestBase::DeviceServiceTestBase()
86 : ServiceTest(kTestServiceName) {} 85 : ServiceTest(kTestServiceName), io_thread_("DeviceServiceTestIOThread") {}
87 86
88 DeviceServiceTestBase::~DeviceServiceTestBase() {} 87 DeviceServiceTestBase::~DeviceServiceTestBase() {}
89 88
90 std::unique_ptr<service_manager::Service> 89 std::unique_ptr<service_manager::Service>
91 DeviceServiceTestBase::CreateService() { 90 DeviceServiceTestBase::CreateService() {
92 return base::MakeUnique<ServiceTestClient>(this); 91 // Instead of starting a new IO thread, we use the main thread to simulate
92 // the IO thread. This is because there are some sub-services in device
93 // (DeviceSensorService, for example) which are type of
94 // base::Singleton<base::LeakySingletonTraits>, this means the singleton
95 // instance is never deleted. When all the service unit test cases run in
96 // same process the singleton service instance stays alive across multiple
97 // unit test cases. There are some thread_checkers in the singleton
98 // instance so we have to make sure passing the same IO thread to
99 // each test case. We use the main thread to provide this consistency.
100
101 return base::MakeUnique<ServiceTestClient>(
102 this, base::ThreadTaskRunnerHandle::Get());
93 } 103 }
94 104
95 } // namespace device 105 } // namespace device
OLDNEW
« no previous file with comments | « services/device/device_service_test_base.h ('k') | services/device/sensors/DEPS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698