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