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

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

Issue 2812223006: Replace device_sensor browsertest by service unittest. (Closed)
Patch Set: Replace device_sensor browsertest by service unittest. 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
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"
11 #include "mojo/public/cpp/bindings/binding_set.h" 9 #include "mojo/public/cpp/bindings/binding_set.h"
12 #include "services/device/device_service.h" 10 #include "services/device/device_service.h"
13 #include "services/device/public/interfaces/constants.mojom.h" 11 #include "services/device/public/interfaces/constants.mojom.h"
14 #include "services/service_manager/public/cpp/binder_registry.h" 12 #include "services/service_manager/public/cpp/binder_registry.h"
15 #include "services/service_manager/public/cpp/interface_factory.h" 13 #include "services/service_manager/public/cpp/interface_factory.h"
16 #include "services/service_manager/public/cpp/service_context.h" 14 #include "services/service_manager/public/cpp/service_context.h"
17 #include "services/service_manager/public/interfaces/service_factory.mojom.h" 15 #include "services/service_manager/public/interfaces/service_factory.mojom.h"
18 16
19 namespace device { 17 namespace device {
20 18
21 namespace { 19 namespace {
22 20
23 const char kTestServiceName[] = "device_unittests"; 21 const char kTestServiceName[] = "device_unittests";
24 22
25 // The test service responsible to package Device Service. 23 // The test service responsible to package Device Service.
26 class ServiceTestClient : public service_manager::test::ServiceTestClient, 24 class ServiceTestClient : public service_manager::test::ServiceTestClient,
27 public service_manager::mojom::ServiceFactory, 25 public service_manager::mojom::ServiceFactory,
28 public service_manager::InterfaceFactory< 26 public service_manager::InterfaceFactory<
29 service_manager::mojom::ServiceFactory> { 27 service_manager::mojom::ServiceFactory> {
30 public: 28 public:
31 explicit ServiceTestClient(service_manager::test::ServiceTest* test) 29 explicit ServiceTestClient(
30 service_manager::test::ServiceTest* test,
31 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
32 : service_manager::test::ServiceTestClient(test), 32 : service_manager::test::ServiceTestClient(test),
33 io_thread_("DeviceServiceTestIOThread"), 33 file_thread_("DeviceServiceTestFileThread"),
34 file_thread_("DeviceServiceTestFileThread") { 34 io_task_runner_(std::move(io_task_runner)) {
35 registry_.AddInterface<service_manager::mojom::ServiceFactory>(this); 35 registry_.AddInterface<service_manager::mojom::ServiceFactory>(this);
36 } 36 }
37 ~ServiceTestClient() override {} 37 ~ServiceTestClient() override {}
38 38
39 protected: 39 protected:
40 void OnBindInterface(const service_manager::ServiceInfo& source_info, 40 void OnBindInterface(const service_manager::ServiceInfo& source_info,
41 const std::string& interface_name, 41 const std::string& interface_name,
42 mojo::ScopedMessagePipeHandle interface_pipe) override { 42 mojo::ScopedMessagePipeHandle interface_pipe) override {
43 registry_.BindInterface(source_info.identity, interface_name, 43 registry_.BindInterface(source_info.identity, interface_name,
44 std::move(interface_pipe)); 44 std::move(interface_pipe));
45 } 45 }
46 46
47 void CreateService(service_manager::mojom::ServiceRequest request, 47 void CreateService(service_manager::mojom::ServiceRequest request,
48 const std::string& name) override { 48 const std::string& name) override {
49 if (name == device::mojom::kServiceName) { 49 if (name == device::mojom::kServiceName) {
50 io_thread_.Start();
51 file_thread_.Start(); 50 file_thread_.Start();
52 #if defined(OS_ANDROID) 51 #if defined(OS_ANDROID)
53 device_service_context_.reset(new service_manager::ServiceContext( 52 device_service_context_.reset(new service_manager::ServiceContext(
54 CreateDeviceService(file_thread_.task_runner(), 53 CreateDeviceService(file_thread_.task_runner(), io_task_runner_,
55 io_thread_.task_runner(),
56 wake_lock_context_callback_), 54 wake_lock_context_callback_),
57 std::move(request))); 55 std::move(request)));
58 #else 56 #else
59 device_service_context_.reset(new service_manager::ServiceContext( 57 device_service_context_.reset(new service_manager::ServiceContext(
60 CreateDeviceService(file_thread_.task_runner(), 58 CreateDeviceService(file_thread_.task_runner(), io_task_runner_),
61 io_thread_.task_runner()),
62 std::move(request))); 59 std::move(request)));
63 #endif 60 #endif
64 } 61 }
65 } 62 }
66 63
67 void Create(const service_manager::Identity& remote_identity, 64 void Create(const service_manager::Identity& remote_identity,
68 service_manager::mojom::ServiceFactoryRequest request) override { 65 service_manager::mojom::ServiceFactoryRequest request) override {
69 service_factory_bindings_.AddBinding(this, std::move(request)); 66 service_factory_bindings_.AddBinding(this, std::move(request));
70 } 67 }
71 68
72 private: 69 private:
73 base::Thread io_thread_;
74 base::Thread file_thread_; 70 base::Thread file_thread_;
75 service_manager::BinderRegistry registry_; 71 service_manager::BinderRegistry registry_;
76 mojo::BindingSet<service_manager::mojom::ServiceFactory> 72 mojo::BindingSet<service_manager::mojom::ServiceFactory>
77 service_factory_bindings_; 73 service_factory_bindings_;
78 std::unique_ptr<service_manager::ServiceContext> device_service_context_; 74 std::unique_ptr<service_manager::ServiceContext> device_service_context_;
79 75
76 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
77
80 WakeLockContextCallback wake_lock_context_callback_; 78 WakeLockContextCallback wake_lock_context_callback_;
81 }; 79 };
82 80
83 } // namespace 81 } // namespace
84 82
85 DeviceServiceTestBase::DeviceServiceTestBase() 83 DeviceServiceTestBase::DeviceServiceTestBase()
86 : ServiceTest(kTestServiceName) {} 84 : ServiceTest(kTestServiceName), io_thread_("DeviceServiceTestIOThread") {}
87 85
88 DeviceServiceTestBase::~DeviceServiceTestBase() {} 86 DeviceServiceTestBase::~DeviceServiceTestBase() {}
89 87
90 std::unique_ptr<service_manager::Service> 88 std::unique_ptr<service_manager::Service>
91 DeviceServiceTestBase::CreateService() { 89 DeviceServiceTestBase::CreateService() {
92 return base::MakeUnique<ServiceTestClient>(this); 90 // Instead of start a new IO thread, we use main thread to simulate IO thread.
91 // The reason is: there are some sub services in device (DeviceSensorService
92 // for example) which are type of
93 // base::Singleton< ,base::LeakySingletonTraits<>>, this means the singleton
94 // instance can only be deleted when process exit. While all the service unit
95 // test cases run in same process, so the singleton service instance keeps
96 // alive across multiple unit test cases. There are some thread_checkers in
97 // the singleton instance, we have to make sure passing the same IO thread to
98 // each test case. So we decide to use main thread to simulate IO thread.
Reilly Grant (use Gerrit) 2017/04/18 19:36:17 // Instead of starting a new IO thread, we use the
ke.he 2017/04/19 06:49:01 Done.
99
100 return base::MakeUnique<ServiceTestClient>(
101 this, base::ThreadTaskRunnerHandle::Get());
93 } 102 }
94 103
95 } // namespace device 104 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698