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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: services/device/device_service_test_base.cc
diff --git a/services/device/device_service_test_base.cc b/services/device/device_service_test_base.cc
index 595f669d9ce93f35533c3786dc811853ea1bacf0..8be69ff88197f05fc2f2df9eab695420077be490 100644
--- a/services/device/device_service_test_base.cc
+++ b/services/device/device_service_test_base.cc
@@ -5,9 +5,7 @@
#include "services/device/device_service_test_base.h"
#include <memory>
-
#include "base/memory/ptr_util.h"
-#include "base/threading/thread.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "services/device/device_service.h"
#include "services/device/public/interfaces/constants.mojom.h"
@@ -28,10 +26,12 @@ class ServiceTestClient : public service_manager::test::ServiceTestClient,
public service_manager::InterfaceFactory<
service_manager::mojom::ServiceFactory> {
public:
- explicit ServiceTestClient(service_manager::test::ServiceTest* test)
+ explicit ServiceTestClient(
+ service_manager::test::ServiceTest* test,
+ scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
: service_manager::test::ServiceTestClient(test),
- io_thread_("DeviceServiceTestIOThread"),
- file_thread_("DeviceServiceTestFileThread") {
+ file_thread_("DeviceServiceTestFileThread"),
+ io_task_runner_(std::move(io_task_runner)) {
registry_.AddInterface<service_manager::mojom::ServiceFactory>(this);
}
~ServiceTestClient() override {}
@@ -47,18 +47,15 @@ class ServiceTestClient : public service_manager::test::ServiceTestClient,
void CreateService(service_manager::mojom::ServiceRequest request,
const std::string& name) override {
if (name == device::mojom::kServiceName) {
- io_thread_.Start();
file_thread_.Start();
#if defined(OS_ANDROID)
device_service_context_.reset(new service_manager::ServiceContext(
- CreateDeviceService(file_thread_.task_runner(),
- io_thread_.task_runner(),
+ CreateDeviceService(file_thread_.task_runner(), io_task_runner_,
wake_lock_context_callback_),
std::move(request)));
#else
device_service_context_.reset(new service_manager::ServiceContext(
- CreateDeviceService(file_thread_.task_runner(),
- io_thread_.task_runner()),
+ CreateDeviceService(file_thread_.task_runner(), io_task_runner_),
std::move(request)));
#endif
}
@@ -70,26 +67,38 @@ class ServiceTestClient : public service_manager::test::ServiceTestClient,
}
private:
- base::Thread io_thread_;
base::Thread file_thread_;
service_manager::BinderRegistry registry_;
mojo::BindingSet<service_manager::mojom::ServiceFactory>
service_factory_bindings_;
std::unique_ptr<service_manager::ServiceContext> device_service_context_;
+ scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
+
WakeLockContextCallback wake_lock_context_callback_;
};
} // namespace
DeviceServiceTestBase::DeviceServiceTestBase()
- : ServiceTest(kTestServiceName) {}
+ : ServiceTest(kTestServiceName), io_thread_("DeviceServiceTestIOThread") {}
DeviceServiceTestBase::~DeviceServiceTestBase() {}
std::unique_ptr<service_manager::Service>
DeviceServiceTestBase::CreateService() {
- return base::MakeUnique<ServiceTestClient>(this);
+ // Instead of start a new IO thread, we use main thread to simulate IO thread.
+ // The reason is: there are some sub services in device (DeviceSensorService
+ // for example) which are type of
+ // base::Singleton< ,base::LeakySingletonTraits<>>, this means the singleton
+ // instance can only be deleted when process exit. While all the service unit
+ // test cases run in same process, so the singleton service instance keeps
+ // alive across multiple unit test cases. There are some thread_checkers in
+ // the singleton instance, we have to make sure passing the same IO thread to
+ // 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.
+
+ return base::MakeUnique<ServiceTestClient>(
+ this, base::ThreadTaskRunnerHandle::Get());
}
} // namespace device

Powered by Google App Engine
This is Rietveld 408576698