| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/service_manager/public/cpp/service_test.h" | 5 #include "services/service_manager/public/cpp/service_test.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 void ServiceTestClient::OnStart() { | 25 void ServiceTestClient::OnStart() { |
| 26 test_->OnStartCalled(context()->connector(), context()->identity().name(), | 26 test_->OnStartCalled(context()->connector(), context()->identity().name(), |
| 27 context()->identity().user_id()); | 27 context()->identity().user_id()); |
| 28 } | 28 } |
| 29 | 29 |
| 30 void ServiceTestClient::OnBindInterface( | 30 void ServiceTestClient::OnBindInterface( |
| 31 const BindSourceInfo& source_info, | 31 const BindSourceInfo& source_info, |
| 32 const std::string& interface_name, | 32 const std::string& interface_name, |
| 33 mojo::ScopedMessagePipeHandle interface_pipe) {} | 33 mojo::ScopedMessagePipeHandle interface_pipe) {} |
| 34 | 34 |
| 35 ServiceTest::ServiceTest() {} | 35 ServiceTest::ServiceTest() : ServiceTest(std::string(), true) {} |
| 36 | 36 |
| 37 ServiceTest::ServiceTest(const std::string& test_name, bool init_edk) | 37 ServiceTest::ServiceTest(const std::string& test_name, bool init_edk) |
| 38 : test_name_(test_name), init_edk_(init_edk) {} | 38 : scoped_task_environment_( |
| 39 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
| 40 test_name_(test_name), |
| 41 init_edk_(init_edk) {} |
| 39 | 42 |
| 40 ServiceTest::~ServiceTest() {} | 43 ServiceTest::~ServiceTest() {} |
| 41 | 44 |
| 42 void ServiceTest::InitTestName(const std::string& test_name) { | 45 void ServiceTest::InitTestName(const std::string& test_name) { |
| 43 DCHECK(test_name_.empty()); | 46 DCHECK(test_name_.empty()); |
| 44 test_name_ = test_name; | 47 test_name_ = test_name; |
| 45 } | 48 } |
| 46 | 49 |
| 47 std::unique_ptr<Service> ServiceTest::CreateService() { | 50 std::unique_ptr<Service> ServiceTest::CreateService() { |
| 48 return base::MakeUnique<ServiceTestClient>(this); | 51 return base::MakeUnique<ServiceTestClient>(this); |
| 49 } | 52 } |
| 50 | 53 |
| 51 std::unique_ptr<base::MessageLoop> ServiceTest::CreateMessageLoop() { | |
| 52 return base::MakeUnique<base::MessageLoop>(); | |
| 53 } | |
| 54 | |
| 55 void ServiceTest::OnStartCalled(Connector* connector, | 54 void ServiceTest::OnStartCalled(Connector* connector, |
| 56 const std::string& name, | 55 const std::string& name, |
| 57 const std::string& user_id) { | 56 const std::string& user_id) { |
| 58 DCHECK_EQ(connector_, connector); | 57 DCHECK_EQ(connector_, connector); |
| 59 initialize_name_ = name; | 58 initialize_name_ = name; |
| 60 initialize_userid_ = user_id; | 59 initialize_userid_ = user_id; |
| 61 initialize_called_.Run(); | 60 initialize_called_.Run(); |
| 62 } | 61 } |
| 63 | 62 |
| 64 void ServiceTest::SetUp() { | 63 void ServiceTest::SetUp() { |
| 65 message_loop_ = CreateMessageLoop(); | |
| 66 | |
| 67 DCHECK(!init_edk_); | 64 DCHECK(!init_edk_); |
| 68 | 65 |
| 69 background_service_manager_ = | 66 background_service_manager_ = |
| 70 base::MakeUnique<service_manager::BackgroundServiceManager>(nullptr, | 67 base::MakeUnique<service_manager::BackgroundServiceManager>(nullptr, |
| 71 nullptr); | 68 nullptr); |
| 72 | 69 |
| 73 // Create the service manager connection. We don't proceed until we get our | 70 // Create the service manager connection. We don't proceed until we get our |
| 74 // Service's OnStart() method is called. | 71 // Service's OnStart() method is called. |
| 75 base::RunLoop run_loop; | 72 base::RunLoop run_loop; |
| 76 base::MessageLoop::ScopedNestableTaskAllower allow( | 73 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 77 base::MessageLoop::current()); | 74 base::MessageLoop::current()); |
| 78 initialize_called_ = run_loop.QuitClosure(); | 75 initialize_called_ = run_loop.QuitClosure(); |
| 79 | 76 |
| 80 mojom::ServicePtr service; | 77 mojom::ServicePtr service; |
| 81 context_ = base::MakeUnique<ServiceContext>(CreateService(), | 78 context_ = base::MakeUnique<ServiceContext>(CreateService(), |
| 82 mojom::ServiceRequest(&service)); | 79 mojom::ServiceRequest(&service)); |
| 83 background_service_manager_->RegisterService( | 80 background_service_manager_->RegisterService( |
| 84 Identity(test_name_, mojom::kRootUserID), std::move(service), nullptr); | 81 Identity(test_name_, mojom::kRootUserID), std::move(service), nullptr); |
| 85 connector_ = context_->connector(); | 82 connector_ = context_->connector(); |
| 86 run_loop.Run(); | 83 run_loop.Run(); |
| 87 } | 84 } |
| 88 | 85 |
| 89 void ServiceTest::TearDown() { | 86 void ServiceTest::TearDown() { |
| 90 background_service_manager_.reset(); | 87 background_service_manager_.reset(); |
| 91 context_.reset(); | 88 context_.reset(); |
| 92 message_loop_.reset(); | |
| 93 } | 89 } |
| 94 | 90 |
| 95 } // namespace test | 91 } // namespace test |
| 96 } // namespace service_manager | 92 } // namespace service_manager |
| OLD | NEW |