| Index: services/service_manager/tests/service_manager/service_manager_unittest.cc
|
| diff --git a/services/service_manager/tests/service_manager/service_manager_unittest.cc b/services/service_manager/tests/service_manager/service_manager_unittest.cc
|
| index 5347c1eabd164611c8ce5b16a66cad11e7131023..015e7abcb13e714f3e624cbdc82de57db5c779d6 100644
|
| --- a/services/service_manager/tests/service_manager/service_manager_unittest.cc
|
| +++ b/services/service_manager/tests/service_manager/service_manager_unittest.cc
|
| @@ -9,6 +9,7 @@
|
| #include <utility>
|
|
|
| #include "base/bind.h"
|
| +#include "base/callback.h"
|
| #include "base/macros.h"
|
| #include "base/memory/ptr_util.h"
|
| #include "base/message_loop/message_loop.h"
|
| @@ -36,6 +37,11 @@ class ServiceManagerTestClient
|
|
|
| const Identity& target_identity() const { return target_identity_; }
|
|
|
| + void WaitForTargetIdentityCall() {
|
| + wait_for_target_identity_loop_ = base::MakeUnique<base::RunLoop>();
|
| + wait_for_target_identity_loop_->Run();
|
| + }
|
| +
|
| private:
|
| // test::ServiceTestClient:
|
| bool OnConnect(const ServiceInfo& remote_info,
|
| @@ -53,10 +59,14 @@ class ServiceManagerTestClient
|
| // test::mojom::CreateInstanceTest:
|
| void SetTargetIdentity(const service_manager::Identity& identity) override {
|
| target_identity_ = identity;
|
| - base::MessageLoop::current()->QuitWhenIdle();
|
| + if (!wait_for_target_identity_loop_)
|
| + LOG(ERROR) << "SetTargetIdentity call received when not waiting for it.";
|
| + else
|
| + wait_for_target_identity_loop_->Quit();
|
| }
|
|
|
| service_manager::Identity target_identity_;
|
| + std::unique_ptr<base::RunLoop> wait_for_target_identity_loop_;
|
|
|
| mojo::Binding<test::mojom::CreateInstanceTest> binding_;
|
|
|
| @@ -74,8 +84,6 @@ class ServiceManagerTest : public test::ServiceTest,
|
| binding_(this) {}
|
| ~ServiceManagerTest() override {}
|
|
|
| - void OnDriverQuit() { base::MessageLoop::current()->QuitNow(); }
|
| -
|
| protected:
|
| struct InstanceInfo {
|
| explicit InstanceInfo(const Identity& identity)
|
| @@ -92,7 +100,7 @@ class ServiceManagerTest : public test::ServiceTest,
|
|
|
| service_manager->AddListener(binding_.CreateInterfacePtrAndBind());
|
|
|
| - wait_for_instances_loop_.reset(new base::RunLoop);
|
| + wait_for_instances_loop_ = base::MakeUnique<base::RunLoop>();
|
| wait_for_instances_loop_->Run();
|
| }
|
|
|
| @@ -108,6 +116,10 @@ class ServiceManagerTest : public test::ServiceTest,
|
| return false;
|
| }
|
|
|
| + void WaitForTargetIdentityCall() {
|
| + service_->WaitForTargetIdentityCall();
|
| + }
|
| +
|
| const Identity& target_identity() const {
|
| DCHECK(service_);
|
| return service_->target_identity();
|
| @@ -115,6 +127,19 @@ class ServiceManagerTest : public test::ServiceTest,
|
|
|
| const std::vector<InstanceInfo>& instances() const { return instances_; }
|
|
|
| + using ServiceStartedCallback =
|
| + base::Callback<void(const service_manager::Identity&)>;
|
| + void set_service_started_callback(const ServiceStartedCallback& callback) {
|
| + service_started_callback_ = callback;
|
| + }
|
| +
|
| + using ServiceFailedToStartCallback =
|
| + base::Callback<void(const service_manager::Identity&)>;
|
| + void set_service_failed_to_start_callback(
|
| + const ServiceFailedToStartCallback& callback) {
|
| + service_failed_to_start_callback_ = callback;
|
| + }
|
| +
|
| private:
|
| // test::ServiceTest:
|
| std::unique_ptr<Service> CreateService() override {
|
| @@ -141,6 +166,13 @@ class ServiceManagerTest : public test::ServiceTest,
|
| break;
|
| }
|
| }
|
| + if (!service_started_callback_.is_null())
|
| + service_started_callback_.Run(identity);
|
| + }
|
| + void OnServiceFailedToStart(
|
| + const service_manager::Identity& identity) override {
|
| + if (!service_failed_to_start_callback_.is_null())
|
| + service_failed_to_start_callback_.Run(identity);
|
| }
|
| void OnServiceStopped(const service_manager::Identity& identity) override {
|
| for (auto it = instances_.begin(); it != instances_.end(); ++it) {
|
| @@ -157,6 +189,8 @@ class ServiceManagerTest : public test::ServiceTest,
|
| std::vector<InstanceInfo> instances_;
|
| std::vector<InstanceInfo> initial_instances_;
|
| std::unique_ptr<base::RunLoop> wait_for_instances_loop_;
|
| + ServiceStartedCallback service_started_callback_;
|
| + ServiceFailedToStartCallback service_failed_to_start_callback_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(ServiceManagerTest);
|
| };
|
| @@ -173,7 +207,7 @@ TEST_F(ServiceManagerTest, CreateInstance) {
|
|
|
| // 2. Wait for the target to connect to us. (via
|
| // service:service_manager_unittest)
|
| - base::RunLoop().Run();
|
| + WaitForTargetIdentityCall();
|
|
|
| EXPECT_FALSE(connection->IsPending());
|
| Identity remote_identity = connection->GetRemoteIdentity();
|
| @@ -200,10 +234,79 @@ TEST_F(ServiceManagerTest, CreateInstance) {
|
| EXPECT_NE(base::kNullProcessId, instance.pid);
|
| }
|
|
|
| - driver.set_connection_error_handler(
|
| - base::Bind(&ServiceManagerTest::OnDriverQuit, base::Unretained(this)));
|
| - driver->QuitDriver();
|
| - base::RunLoop().Run();
|
| + {
|
| + base::RunLoop loop;
|
| + driver.set_connection_error_handler(
|
| + base::Bind(&base::RunLoop::Quit, base::Unretained(&loop)));
|
| + driver->QuitDriver();
|
| + loop.Run();
|
| + }
|
| +}
|
| +
|
| +void OnServiceStartedCallback(int* start_count,
|
| + std::string* service_name,
|
| + const base::Closure& continuation,
|
| + const service_manager::Identity& identity) {
|
| + (*start_count)++;
|
| + *service_name = identity.name();
|
| + continuation.Run();
|
| +}
|
| +
|
| +void OnServiceFailedToStartCallback(
|
| + bool* run,
|
| + const base::Closure& continuation,
|
| + const service_manager::Identity& identity) {
|
| + *run = true;
|
| + continuation.Run();
|
| +}
|
| +
|
| +// Tests that creating connecting to a singleton packaged service work.
|
| +TEST_F(ServiceManagerTest, CreatePackagedSingletonInstance) {
|
| + AddListenerAndWaitForApplications();
|
| +
|
| + // Connect to the embedder service first.
|
| + {
|
| + base::RunLoop loop;
|
| + int start_count = 0;
|
| + std::string service_name;
|
| + set_service_started_callback(base::BindRepeating(
|
| + &OnServiceStartedCallback,
|
| + &start_count, &service_name, loop.QuitClosure()));
|
| + bool failed_to_start = false;
|
| + set_service_failed_to_start_callback(base::BindRepeating(
|
| + &OnServiceFailedToStartCallback,
|
| + &failed_to_start, loop.QuitClosure()));
|
| +
|
| + std::unique_ptr<Connection> embedder_connection =
|
| + connector()->Connect("exe:service_manager_unittest_embedder");
|
| + loop.Run();
|
| + EXPECT_FALSE(failed_to_start);
|
| + EXPECT_FALSE(embedder_connection->IsPending());
|
| + EXPECT_EQ(1, start_count);
|
| + EXPECT_EQ("exe:service_manager_unittest_embedder", service_name);
|
| + }
|
| +
|
| + {
|
| + base::RunLoop loop;
|
| + int start_count = 0;
|
| + std::string service_name;
|
| + set_service_started_callback(base::BindRepeating(
|
| + &OnServiceStartedCallback,
|
| + &start_count, &service_name, loop.QuitClosure()));
|
| + bool failed_to_start = false;
|
| + set_service_failed_to_start_callback(base::BindRepeating(
|
| + &OnServiceFailedToStartCallback,
|
| + &failed_to_start, loop.QuitClosure()));
|
| +
|
| + // Connect to the packaged singleton service.
|
| + std::unique_ptr<Connection> singleton_connection =
|
| + connector()->Connect("service:service_manager_unittest_singleton");
|
| + loop.Run();
|
| + EXPECT_FALSE(failed_to_start);
|
| + EXPECT_FALSE(singleton_connection->IsPending());
|
| + EXPECT_EQ(1, start_count);
|
| + EXPECT_EQ("service:service_manager_unittest_singleton", service_name);
|
| + }
|
| }
|
|
|
| } // namespace service_manager
|
|
|