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