| 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 "services/service_manager/background/background_service_manager.h" | 10 #include "services/service_manager/background/background_service_manager.h" |
| 11 #include "services/service_manager/public/cpp/service.h" | 11 #include "services/service_manager/public/cpp/service.h" |
| 12 #include "services/service_manager/public/cpp/service_context.h" |
| 12 | 13 |
| 13 namespace service_manager { | 14 namespace service_manager { |
| 14 namespace test { | 15 namespace test { |
| 15 | 16 |
| 16 ServiceTestClient::ServiceTestClient(ServiceTest* test) : test_(test) {} | 17 ServiceTestClient::ServiceTestClient(ServiceTest* test) : test_(test) {} |
| 18 |
| 17 ServiceTestClient::~ServiceTestClient() {} | 19 ServiceTestClient::~ServiceTestClient() {} |
| 18 | 20 |
| 19 void ServiceTestClient::OnStart(const ServiceInfo& info) { | 21 void ServiceTestClient::OnStart(ServiceContext* context) { |
| 20 test_->OnStartCalled(connector(), info.identity.name(), | 22 test_->OnStartCalled(context->connector(), context->identity().name(), |
| 21 info.identity.user_id()); | 23 context->identity().user_id()); |
| 22 } | 24 } |
| 23 | 25 |
| 26 bool ServiceTestClient::OnConnect(const ServiceInfo& remote_info, |
| 27 InterfaceRegistry* registry) { |
| 28 return false; |
| 29 } |
| 30 |
| 31 |
| 24 ServiceTest::ServiceTest() {} | 32 ServiceTest::ServiceTest() {} |
| 33 |
| 25 ServiceTest::ServiceTest(const std::string& test_name) | 34 ServiceTest::ServiceTest(const std::string& test_name) |
| 26 : test_name_(test_name) {} | 35 : test_name_(test_name) {} |
| 36 |
| 27 ServiceTest::~ServiceTest() {} | 37 ServiceTest::~ServiceTest() {} |
| 28 | 38 |
| 29 void ServiceTest::InitTestName(const std::string& test_name) { | 39 void ServiceTest::InitTestName(const std::string& test_name) { |
| 30 DCHECK(test_name_.empty()); | 40 DCHECK(test_name_.empty()); |
| 31 test_name_ = test_name; | 41 test_name_ = test_name; |
| 32 } | 42 } |
| 33 | 43 |
| 34 std::unique_ptr<Service> ServiceTest::CreateService() { | 44 std::unique_ptr<Service> ServiceTest::CreateService() { |
| 35 return base::WrapUnique(new ServiceTestClient(this)); | 45 return base::MakeUnique<ServiceTestClient>(this); |
| 36 } | 46 } |
| 37 | 47 |
| 38 std::unique_ptr<base::MessageLoop> ServiceTest::CreateMessageLoop() { | 48 std::unique_ptr<base::MessageLoop> ServiceTest::CreateMessageLoop() { |
| 39 return base::WrapUnique(new base::MessageLoop); | 49 return base::MakeUnique<base::MessageLoop>(); |
| 40 } | 50 } |
| 41 | 51 |
| 42 void ServiceTest::OnStartCalled(Connector* connector, | 52 void ServiceTest::OnStartCalled(Connector* connector, |
| 43 const std::string& name, | 53 const std::string& name, |
| 44 const std::string& user_id) { | 54 const std::string& user_id) { |
| 45 DCHECK_EQ(connector_, connector); | 55 DCHECK_EQ(connector_, connector); |
| 46 initialize_name_ = name; | 56 initialize_name_ = name; |
| 47 initialize_userid_ = user_id; | 57 initialize_userid_ = user_id; |
| 48 initialize_called_.Run(); | 58 initialize_called_.Run(); |
| 49 } | 59 } |
| 50 | 60 |
| 51 void ServiceTest::SetUp() { | 61 void ServiceTest::SetUp() { |
| 52 service_ = CreateService(); | |
| 53 message_loop_ = CreateMessageLoop(); | 62 message_loop_ = CreateMessageLoop(); |
| 54 background_service_manager_.reset( | 63 background_service_manager_.reset( |
| 55 new service_manager::BackgroundServiceManager); | 64 new service_manager::BackgroundServiceManager); |
| 56 background_service_manager_->Init(nullptr); | 65 background_service_manager_->Init(nullptr); |
| 57 | 66 |
| 58 // Create the service manager connection. We don't proceed until we get our | 67 // Create the service manager connection. We don't proceed until we get our |
| 59 // Service's OnStart() method is called. | 68 // Service's OnStart() method is called. |
| 60 base::RunLoop run_loop; | 69 base::RunLoop run_loop; |
| 61 base::MessageLoop::ScopedNestableTaskAllower allow( | 70 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 62 base::MessageLoop::current()); | 71 base::MessageLoop::current()); |
| 63 initialize_called_ = run_loop.QuitClosure(); | 72 initialize_called_ = run_loop.QuitClosure(); |
| 64 | 73 |
| 65 service_->set_context(base::MakeUnique<ServiceContext>( | 74 context_.reset(new ServiceContext( |
| 66 service_.get(), | 75 CreateService(), |
| 67 background_service_manager_->CreateServiceRequest(test_name_))); | 76 background_service_manager_->CreateServiceRequest(test_name_))); |
| 68 connector_ = service_->connector(); | 77 connector_ = context_->connector(); |
| 69 | 78 |
| 70 run_loop.Run(); | 79 run_loop.Run(); |
| 71 } | 80 } |
| 72 | 81 |
| 73 void ServiceTest::TearDown() { | 82 void ServiceTest::TearDown() { |
| 74 background_service_manager_.reset(); | 83 background_service_manager_.reset(); |
| 75 message_loop_.reset(); | 84 message_loop_.reset(); |
| 76 service_.reset(); | 85 context_.reset(); |
| 77 } | 86 } |
| 78 | 87 |
| 79 } // namespace test | 88 } // namespace test |
| 80 } // namespace service_manager | 89 } // namespace service_manager |
| OLD | NEW |