| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/process/process_handle.h" | 15 #include "base/process/process_handle.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "mojo/public/cpp/bindings/binding_set.h" | 17 #include "mojo/public/cpp/bindings/binding_set.h" |
| 18 #include "services/shell/public/cpp/interface_factory.h" | 18 #include "services/shell/public/cpp/interface_factory.h" |
| 19 #include "services/shell/public/cpp/service.h" | 19 #include "services/shell/public/cpp/service.h" |
| 20 #include "services/shell/public/cpp/service_test.h" | 20 #include "services/shell/public/cpp/service_test.h" |
| 21 #include "services/shell/public/interfaces/service_manager.mojom.h" | 21 #include "services/shell/public/interfaces/service_manager.mojom.h" |
| 22 #include "services/shell/tests/shell/shell_unittest.mojom.h" | 22 #include "services/shell/tests/shell/shell_unittest.mojom.h" |
| 23 | 23 |
| 24 namespace shell { | 24 namespace shell { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 class ServiceTestClient | 28 class ShellTestClient |
| 29 : public test::ServiceTestClient, | 29 : public test::ServiceTestClient, |
| 30 public InterfaceFactory<test::mojom::CreateInstanceTest>, | 30 public InterfaceFactory<test::mojom::CreateInstanceTest>, |
| 31 public test::mojom::CreateInstanceTest { | 31 public test::mojom::CreateInstanceTest { |
| 32 public: | 32 public: |
| 33 explicit ServiceTestClient(test::ServiceTest* test) | 33 explicit ShellTestClient(test::ServiceTest* test) |
| 34 : test::ServiceTestClient(test), | 34 : test::ServiceTestClient(test), |
| 35 target_id_(shell::mojom::kInvalidInstanceID), | 35 target_id_(shell::mojom::kInvalidInstanceID), |
| 36 binding_(this) {} | 36 binding_(this) {} |
| 37 ~ServiceTestClient() override {} | 37 ~ShellTestClient() override {} |
| 38 | 38 |
| 39 uint32_t target_id() const { return target_id_; } | 39 uint32_t target_id() const { return target_id_; } |
| 40 | 40 |
| 41 private: | 41 private: |
| 42 // test::ServiceTestClient: | 42 // test::ServiceTestClient: |
| 43 bool OnConnect(Connection* connection) override { | 43 bool OnConnect(Connection* connection) override { |
| 44 connection->AddInterface<test::mojom::CreateInstanceTest>(this); | 44 connection->AddInterface<test::mojom::CreateInstanceTest>(this); |
| 45 return true; | 45 return true; |
| 46 } | 46 } |
| 47 | 47 |
| 48 // InterfaceFactory<test::mojom::CreateInstanceTest>: | 48 // InterfaceFactory<test::mojom::CreateInstanceTest>: |
| 49 void Create( | 49 void Create( |
| 50 Connection* connection, | 50 Connection* connection, |
| 51 test::mojom::CreateInstanceTestRequest request) override { | 51 test::mojom::CreateInstanceTestRequest request) override { |
| 52 binding_.Bind(std::move(request)); | 52 binding_.Bind(std::move(request)); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // test::mojom::CreateInstanceTest: | 55 // test::mojom::CreateInstanceTest: |
| 56 void SetTargetID(uint32_t target_id) override { | 56 void SetTargetID(uint32_t target_id) override { |
| 57 target_id_ = target_id; | 57 target_id_ = target_id; |
| 58 base::MessageLoop::current()->QuitWhenIdle(); | 58 base::MessageLoop::current()->QuitWhenIdle(); |
| 59 } | 59 } |
| 60 | 60 |
| 61 uint32_t target_id_; | 61 uint32_t target_id_; |
| 62 | 62 |
| 63 mojo::Binding<test::mojom::CreateInstanceTest> binding_; | 63 mojo::Binding<test::mojom::CreateInstanceTest> binding_; |
| 64 | 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(ServiceTestClient); | 65 DISALLOW_COPY_AND_ASSIGN(ShellTestClient); |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 } // namespace | 68 } // namespace |
| 69 | 69 |
| 70 class ServiceTest : public test::ServiceTest, | 70 class ShellTest : public test::ServiceTest, |
| 71 public mojom::ServiceManagerListener { | 71 public mojom::ServiceManagerListener { |
| 72 public: | 72 public: |
| 73 ServiceTest() | 73 ShellTest() |
| 74 : test::ServiceTest("mojo:shell_unittest"), | 74 : test::ServiceTest("mojo:shell_unittest"), |
| 75 service_(nullptr), | 75 service_(nullptr), |
| 76 binding_(this) {} | 76 binding_(this) {} |
| 77 ~ServiceTest() override {} | 77 ~ShellTest() override {} |
| 78 | 78 |
| 79 void OnDriverQuit() { | 79 void OnDriverQuit() { |
| 80 base::MessageLoop::current()->QuitNow(); | 80 base::MessageLoop::current()->QuitNow(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 protected: | 83 protected: |
| 84 struct InstanceInfo { | 84 struct InstanceInfo { |
| 85 InstanceInfo(uint32_t id, const std::string& name) | 85 InstanceInfo(uint32_t id, const std::string& name) |
| 86 : id(id), name(name), pid(base::kNullProcessId) {} | 86 : id(id), name(name), pid(base::kNullProcessId) {} |
| 87 | 87 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 117 return service_->target_id(); | 117 return service_->target_id(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 const std::vector<InstanceInfo>& instances() const { | 120 const std::vector<InstanceInfo>& instances() const { |
| 121 return instances_; | 121 return instances_; |
| 122 } | 122 } |
| 123 | 123 |
| 124 private: | 124 private: |
| 125 // test::ServiceTest: | 125 // test::ServiceTest: |
| 126 std::unique_ptr<Service> CreateService() override { | 126 std::unique_ptr<Service> CreateService() override { |
| 127 service_ = new ServiceTestClient(this); | 127 service_ = new ShellTestClient(this); |
| 128 return base::WrapUnique(service_); | 128 return base::WrapUnique(service_); |
| 129 } | 129 } |
| 130 | 130 |
| 131 // mojom::ServiceManagerListener: | 131 // mojom::ServiceManagerListener: |
| 132 void OnInit(mojo::Array<mojom::ServiceInfoPtr> instances) override { | 132 void OnInit(mojo::Array<mojom::ServiceInfoPtr> instances) override { |
| 133 for (size_t i = 0; i < instances.size(); ++i) { | 133 for (size_t i = 0; i < instances.size(); ++i) { |
| 134 initial_instances_.push_back(InstanceInfo(instances[i]->id, | 134 initial_instances_.push_back(InstanceInfo(instances[i]->id, |
| 135 instances[i]->identity->name)); | 135 instances[i]->identity->name)); |
| 136 } | 136 } |
| 137 | 137 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 152 void OnServiceStopped(uint32_t id) override { | 152 void OnServiceStopped(uint32_t id) override { |
| 153 for (auto it = instances_.begin(); it != instances_.end(); ++it) { | 153 for (auto it = instances_.begin(); it != instances_.end(); ++it) { |
| 154 auto& instance = *it; | 154 auto& instance = *it; |
| 155 if (instance.id == id) { | 155 if (instance.id == id) { |
| 156 instances_.erase(it); | 156 instances_.erase(it); |
| 157 break; | 157 break; |
| 158 } | 158 } |
| 159 } | 159 } |
| 160 } | 160 } |
| 161 | 161 |
| 162 ServiceTestClient* service_; | 162 ShellTestClient* service_; |
| 163 mojo::Binding<mojom::ServiceManagerListener> binding_; | 163 mojo::Binding<mojom::ServiceManagerListener> binding_; |
| 164 std::vector<InstanceInfo> instances_; | 164 std::vector<InstanceInfo> instances_; |
| 165 std::vector<InstanceInfo> initial_instances_; | 165 std::vector<InstanceInfo> initial_instances_; |
| 166 std::unique_ptr<base::RunLoop> wait_for_instances_loop_; | 166 std::unique_ptr<base::RunLoop> wait_for_instances_loop_; |
| 167 | 167 |
| 168 DISALLOW_COPY_AND_ASSIGN(ServiceTest); | 168 DISALLOW_COPY_AND_ASSIGN(ShellTest); |
| 169 }; | 169 }; |
| 170 | 170 |
| 171 TEST_F(ServiceTest, CreateInstance) { | 171 TEST_F(ShellTest, CreateInstance) { |
| 172 AddListenerAndWaitForApplications(); | 172 AddListenerAndWaitForApplications(); |
| 173 | 173 |
| 174 // 1. Launch a process. (Actually, have the runner launch a process that | 174 // 1. Launch a process. (Actually, have the runner launch a process that |
| 175 // launches a process.) | 175 // launches a process.) |
| 176 test::mojom::DriverPtr driver; | 176 test::mojom::DriverPtr driver; |
| 177 std::unique_ptr<Connection> connection = | 177 std::unique_ptr<Connection> connection = |
| 178 connector()->Connect("exe:shell_unittest_driver"); | 178 connector()->Connect("exe:shell_unittest_driver"); |
| 179 connection->GetInterface(&driver); | 179 connection->GetInterface(&driver); |
| 180 | 180 |
| 181 // 2. Wait for the target to connect to us. (via | 181 // 2. Wait for the target to connect to us. (via |
| (...skipping 20 matching lines...) Expand all Loading... |
| 202 } | 202 } |
| 203 { | 203 { |
| 204 auto& instance = instances().back(); | 204 auto& instance = instances().back(); |
| 205 // We learn about the target process id via a ping from it. | 205 // We learn about the target process id via a ping from it. |
| 206 EXPECT_EQ(target_id(), instance.id); | 206 EXPECT_EQ(target_id(), instance.id); |
| 207 EXPECT_EQ("exe:shell_unittest_target", instance.name); | 207 EXPECT_EQ("exe:shell_unittest_target", instance.name); |
| 208 EXPECT_NE(base::kNullProcessId, instance.pid); | 208 EXPECT_NE(base::kNullProcessId, instance.pid); |
| 209 } | 209 } |
| 210 | 210 |
| 211 driver.set_connection_error_handler( | 211 driver.set_connection_error_handler( |
| 212 base::Bind(&ServiceTest::OnDriverQuit, | 212 base::Bind(&ShellTest::OnDriverQuit, |
| 213 base::Unretained(this))); | 213 base::Unretained(this))); |
| 214 driver->QuitDriver(); | 214 driver->QuitDriver(); |
| 215 base::RunLoop().Run(); | 215 base::RunLoop().Run(); |
| 216 } | 216 } |
| 217 | 217 |
| 218 } // namespace shell | 218 } // namespace shell |
| OLD | NEW |