| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <stddef.h> | |
| 6 #include <stdint.h> | |
| 7 | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/process/process_handle.h" | |
| 14 #include "mojo/converters/network/network_type_converters.h" | |
| 15 #include "mojo/public/cpp/bindings/weak_binding_set.h" | |
| 16 #include "mojo/shell/application_manager_apptests.mojom.h" | |
| 17 #include "mojo/shell/public/cpp/application_test_base.h" | |
| 18 #include "mojo/shell/public/cpp/interface_factory.h" | |
| 19 #include "mojo/shell/public/cpp/shell.h" | |
| 20 #include "mojo/shell/public/interfaces/application_manager.mojom.h" | |
| 21 | |
| 22 using mojo::shell::test::mojom::CreateInstanceForHandleTest; | |
| 23 | |
| 24 namespace mojo { | |
| 25 namespace shell { | |
| 26 namespace { | |
| 27 | |
| 28 class ApplicationManagerAppTestDelegate | |
| 29 : public ShellClient, | |
| 30 public InterfaceFactory<CreateInstanceForHandleTest>, | |
| 31 public CreateInstanceForHandleTest { | |
| 32 public: | |
| 33 ApplicationManagerAppTestDelegate() | |
| 34 : target_id_(mojom::Shell::kInvalidApplicationID), | |
| 35 binding_(this) {} | |
| 36 ~ApplicationManagerAppTestDelegate() override {} | |
| 37 | |
| 38 uint32_t target_id() const { return target_id_; } | |
| 39 | |
| 40 private: | |
| 41 // mojo::ShellClient: | |
| 42 void Initialize(Shell* shell, const std::string& url, uint32_t id) override {} | |
| 43 bool AcceptConnection(Connection* connection) override { | |
| 44 connection->AddInterface<CreateInstanceForHandleTest>(this); | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 // InterfaceFactory<CreateInstanceForHandleTest>: | |
| 49 void Create(Connection* connection, | |
| 50 InterfaceRequest<CreateInstanceForHandleTest> request) override { | |
| 51 binding_.Bind(std::move(request)); | |
| 52 } | |
| 53 | |
| 54 // CreateInstanceForHandleTest: | |
| 55 void SetTargetID(uint32_t target_id) override { | |
| 56 target_id_ = target_id; | |
| 57 base::MessageLoop::current()->QuitWhenIdle(); | |
| 58 } | |
| 59 | |
| 60 uint32_t target_id_; | |
| 61 | |
| 62 Binding<CreateInstanceForHandleTest> binding_; | |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerAppTestDelegate); | |
| 65 }; | |
| 66 | |
| 67 } // namespace | |
| 68 | |
| 69 class ApplicationManagerAppTest : public mojo::test::ApplicationTestBase, | |
| 70 public mojom::ApplicationManagerListener { | |
| 71 public: | |
| 72 ApplicationManagerAppTest() : delegate_(nullptr), binding_(this) {} | |
| 73 ~ApplicationManagerAppTest() override {} | |
| 74 | |
| 75 void OnDriverQuit() { | |
| 76 base::MessageLoop::current()->QuitNow(); | |
| 77 } | |
| 78 | |
| 79 protected: | |
| 80 struct ApplicationInfo { | |
| 81 ApplicationInfo(uint32_t id, | |
| 82 const std::string& url, | |
| 83 const std::string& name) | |
| 84 : id(id), url(url), pid(base::kNullProcessId), name(name) {} | |
| 85 | |
| 86 uint32_t id; | |
| 87 std::string url; | |
| 88 base::ProcessId pid; | |
| 89 std::string name; | |
| 90 }; | |
| 91 | |
| 92 void AddListenerAndWaitForApplications() { | |
| 93 mojom::ApplicationManagerPtr application_manager; | |
| 94 shell()->ConnectToInterface("mojo:shell", &application_manager); | |
| 95 | |
| 96 application_manager->AddListener(binding_.CreateInterfacePtrAndBind()); | |
| 97 binding_.WaitForIncomingMethodCall(); | |
| 98 } | |
| 99 | |
| 100 bool ContainsApplicationNamed(const std::string& name) const { | |
| 101 for (const auto& application : initial_applications_) { | |
| 102 if (application.name == name) | |
| 103 return true; | |
| 104 } | |
| 105 for (const auto& application : applications_) { | |
| 106 if (application.name == name) | |
| 107 return true; | |
| 108 } | |
| 109 return false; | |
| 110 } | |
| 111 | |
| 112 uint32_t target_id() const { | |
| 113 DCHECK(delegate_); | |
| 114 return delegate_->target_id(); | |
| 115 } | |
| 116 | |
| 117 const std::vector<ApplicationInfo>& applications() const { | |
| 118 return applications_; | |
| 119 } | |
| 120 | |
| 121 ApplicationManagerAppTestDelegate* delegate() { return delegate_; } | |
| 122 | |
| 123 private: | |
| 124 // test::ApplicationTestBase: | |
| 125 ShellClient* GetShellClient() override { | |
| 126 delegate_ = new ApplicationManagerAppTestDelegate; | |
| 127 return delegate_; | |
| 128 } | |
| 129 | |
| 130 // mojom::ApplicationManagerListener: | |
| 131 void SetRunningApplications( | |
| 132 Array<mojom::ApplicationInfoPtr> applications) override { | |
| 133 for (size_t i = 0; i < applications.size(); ++i) { | |
| 134 initial_applications_.push_back(ApplicationInfo(applications[i]->id, | |
| 135 applications[i]->url, | |
| 136 applications[i]->name)); | |
| 137 } | |
| 138 } | |
| 139 void ApplicationInstanceCreated( | |
| 140 mojom::ApplicationInfoPtr application) override { | |
| 141 applications_.push_back(ApplicationInfo(application->id, application->url, | |
| 142 application->name)); | |
| 143 } | |
| 144 void ApplicationInstanceDestroyed(uint32_t id) override { | |
| 145 for (auto it = applications_.begin(); it != applications_.end(); ++it) { | |
| 146 auto& application = *it; | |
| 147 if (application.id == id) { | |
| 148 applications_.erase(it); | |
| 149 break; | |
| 150 } | |
| 151 } | |
| 152 } | |
| 153 void ApplicationPIDAvailable(uint32_t id, uint32_t pid) override { | |
| 154 for (auto& application : applications_) { | |
| 155 if (application.id == id) { | |
| 156 application.pid = pid; | |
| 157 break; | |
| 158 } | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 ApplicationManagerAppTestDelegate* delegate_; | |
| 163 Binding<mojom::ApplicationManagerListener> binding_; | |
| 164 std::vector<ApplicationInfo> applications_; | |
| 165 std::vector<ApplicationInfo> initial_applications_; | |
| 166 | |
| 167 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerAppTest); | |
| 168 }; | |
| 169 | |
| 170 TEST_F(ApplicationManagerAppTest, CreateInstanceForHandle) { | |
| 171 AddListenerAndWaitForApplications(); | |
| 172 | |
| 173 // 1. Launch a process. (Actually, have the runner launch a process that | |
| 174 // launches a process. #becauselinkerrors). | |
| 175 mojo::shell::test::mojom::DriverPtr driver; | |
| 176 scoped_ptr<Connection> connection = | |
| 177 shell()->Connect("exe:application_manager_apptest_driver"); | |
| 178 connection->GetInterface(&driver); | |
| 179 | |
| 180 // 2. Wait for the target to connect to us. (via | |
| 181 // mojo:application_manager_apptests) | |
| 182 base::MessageLoop::current()->Run(); | |
| 183 | |
| 184 uint32_t remote_id = mojom::Shell::kInvalidApplicationID; | |
| 185 EXPECT_TRUE(connection->GetRemoteApplicationID(&remote_id)); | |
| 186 EXPECT_NE(mojom::Shell::kInvalidApplicationID, remote_id); | |
| 187 | |
| 188 // 3. Validate that this test suite's pretty name was consumed from its | |
| 189 // manifest. | |
| 190 EXPECT_TRUE(ContainsApplicationNamed("Application Manager Apptests")); | |
| 191 | |
| 192 // 4. Validate that the right applications/processes were created. | |
| 193 // Note that the target process will be created even if the tests are | |
| 194 // run with --single-process. | |
| 195 EXPECT_EQ(2u, applications().size()); | |
| 196 { | |
| 197 auto& application = applications().front(); | |
| 198 EXPECT_EQ(remote_id, application.id); | |
| 199 EXPECT_EQ("exe://application_manager_apptest_driver/", application.url); | |
| 200 EXPECT_NE(base::kNullProcessId, application.pid); | |
| 201 } | |
| 202 { | |
| 203 auto& application = applications().back(); | |
| 204 // We learn about the target process id via a ping from it. | |
| 205 EXPECT_EQ(target_id(), application.id); | |
| 206 EXPECT_EQ("exe://application_manager_apptest_target/", application.url); | |
| 207 EXPECT_NE(base::kNullProcessId, application.pid); | |
| 208 } | |
| 209 | |
| 210 driver.set_connection_error_handler( | |
| 211 base::Bind(&ApplicationManagerAppTest::OnDriverQuit, | |
| 212 base::Unretained(this))); | |
| 213 driver->QuitDriver(); | |
| 214 base::MessageLoop::current()->Run(); | |
| 215 } | |
| 216 | |
| 217 } // namespace shell | |
| 218 } // namespace mojo | |
| OLD | NEW |