| OLD | NEW |
| (Empty) |
| 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 | |
| 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/run_loop.h" | |
| 13 #include "base/threading/simple_thread.h" | |
| 14 #include "mojo/public/cpp/bindings/weak_binding_set.h" | |
| 15 #include "mojo/shell/application_package_apptest.mojom.h" | |
| 16 #include "mojo/shell/public/cpp/application_runner.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/interfaces/content_handler.mojom.h" | |
| 20 | |
| 21 // Tests that multiple applications can be packaged in a single Mojo application | |
| 22 // implementing ContentHandler; that these applications can be specified by | |
| 23 // the package's manifest and are thus registered with the PackageManager. | |
| 24 | |
| 25 namespace mojo { | |
| 26 namespace shell { | |
| 27 namespace { | |
| 28 | |
| 29 using GetNameCallback = | |
| 30 test::mojom::ApplicationPackageApptestService::GetNameCallback; | |
| 31 | |
| 32 class ProvidedApplicationDelegate | |
| 33 : public ShellClient, | |
| 34 public InterfaceFactory<test::mojom::ApplicationPackageApptestService>, | |
| 35 public test::mojom::ApplicationPackageApptestService, | |
| 36 public base::SimpleThread { | |
| 37 public: | |
| 38 ProvidedApplicationDelegate(const std::string& name, | |
| 39 InterfaceRequest<mojom::ShellClient> request, | |
| 40 const Callback<void()>& destruct_callback) | |
| 41 : base::SimpleThread(name), | |
| 42 name_(name), | |
| 43 request_(std::move(request)), | |
| 44 destruct_callback_(destruct_callback) { | |
| 45 Start(); | |
| 46 } | |
| 47 ~ProvidedApplicationDelegate() override { | |
| 48 Join(); | |
| 49 destruct_callback_.Run(); | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 // mojo::ShellClient: | |
| 54 void Initialize(Shell* shell, const std::string& url, uint32_t id) override {} | |
| 55 bool AcceptConnection(Connection* connection) override { | |
| 56 connection->AddInterface<test::mojom::ApplicationPackageApptestService>( | |
| 57 this); | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 // InterfaceFactory<test::mojom::ApplicationPackageApptestService>: | |
| 62 void Create( | |
| 63 Connection* connection, | |
| 64 InterfaceRequest<test::mojom::ApplicationPackageApptestService> request) | |
| 65 override { | |
| 66 bindings_.AddBinding(this, std::move(request)); | |
| 67 } | |
| 68 | |
| 69 // test::mojom::ApplicationPackageApptestService: | |
| 70 void GetName(const GetNameCallback& callback) override { | |
| 71 callback.Run(name_); | |
| 72 } | |
| 73 | |
| 74 // base::SimpleThread: | |
| 75 void Run() override { | |
| 76 ApplicationRunner(this).Run(request_.PassMessagePipe().release().value(), | |
| 77 false); | |
| 78 delete this; | |
| 79 } | |
| 80 | |
| 81 const std::string name_; | |
| 82 InterfaceRequest<mojom::ShellClient> request_; | |
| 83 const Callback<void()> destruct_callback_; | |
| 84 WeakBindingSet<test::mojom::ApplicationPackageApptestService> bindings_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(ProvidedApplicationDelegate); | |
| 87 }; | |
| 88 | |
| 89 class ApplicationPackageApptestDelegate | |
| 90 : public ShellClient, | |
| 91 public InterfaceFactory<mojom::ContentHandler>, | |
| 92 public InterfaceFactory<test::mojom::ApplicationPackageApptestService>, | |
| 93 public mojom::ContentHandler, | |
| 94 public test::mojom::ApplicationPackageApptestService { | |
| 95 public: | |
| 96 ApplicationPackageApptestDelegate() {} | |
| 97 ~ApplicationPackageApptestDelegate() override {} | |
| 98 | |
| 99 private: | |
| 100 // mojo::ShellClient: | |
| 101 void Initialize(Shell* shell, const std::string& url, uint32_t id) override {} | |
| 102 bool AcceptConnection(Connection* connection) override { | |
| 103 connection->AddInterface<ContentHandler>(this); | |
| 104 connection->AddInterface<test::mojom::ApplicationPackageApptestService>( | |
| 105 this); | |
| 106 return true; | |
| 107 } | |
| 108 | |
| 109 // InterfaceFactory<mojom::ContentHandler>: | |
| 110 void Create(Connection* connection, | |
| 111 InterfaceRequest<mojom::ContentHandler> request) override { | |
| 112 content_handler_bindings_.AddBinding(this, std::move(request)); | |
| 113 } | |
| 114 | |
| 115 // InterfaceFactory<test::mojom::ApplicationPackageApptestService>: | |
| 116 void Create(Connection* connection, | |
| 117 InterfaceRequest<test::mojom::ApplicationPackageApptestService> | |
| 118 request) override { | |
| 119 bindings_.AddBinding(this, std::move(request)); | |
| 120 } | |
| 121 | |
| 122 // mojom::ContentHandler: | |
| 123 void StartApplication(InterfaceRequest<mojom::ShellClient> request, | |
| 124 URLResponsePtr response, | |
| 125 const Callback<void()>& destruct_callback) override { | |
| 126 const std::string url = response->url; | |
| 127 if (url == "mojo://package_test_a/") { | |
| 128 new ProvidedApplicationDelegate("A", std::move(request), | |
| 129 destruct_callback); | |
| 130 } else if (url == "mojo://package_test_b/") { | |
| 131 new ProvidedApplicationDelegate("B", std::move(request), | |
| 132 destruct_callback); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 // test::mojom::ApplicationPackageApptestService: | |
| 137 void GetName(const GetNameCallback& callback) override { | |
| 138 callback.Run("ROOT"); | |
| 139 } | |
| 140 | |
| 141 std::vector<scoped_ptr<ShellClient>> delegates_; | |
| 142 WeakBindingSet<mojom::ContentHandler> content_handler_bindings_; | |
| 143 WeakBindingSet<test::mojom::ApplicationPackageApptestService> bindings_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(ApplicationPackageApptestDelegate); | |
| 146 }; | |
| 147 | |
| 148 void ReceiveName(std::string* out_name, | |
| 149 base::RunLoop* loop, | |
| 150 const String& name) { | |
| 151 *out_name = name; | |
| 152 loop->Quit(); | |
| 153 } | |
| 154 | |
| 155 } // namespace | |
| 156 | |
| 157 class ApplicationPackageApptest : public mojo::test::ApplicationTestBase { | |
| 158 public: | |
| 159 ApplicationPackageApptest() : delegate_(nullptr) {} | |
| 160 ~ApplicationPackageApptest() override {} | |
| 161 | |
| 162 private: | |
| 163 // test::ApplicationTestBase: | |
| 164 ShellClient* GetShellClient() override { | |
| 165 delegate_ = new ApplicationPackageApptestDelegate; | |
| 166 return delegate_; | |
| 167 } | |
| 168 | |
| 169 ApplicationPackageApptestDelegate* delegate_; | |
| 170 | |
| 171 DISALLOW_COPY_AND_ASSIGN(ApplicationPackageApptest); | |
| 172 }; | |
| 173 | |
| 174 TEST_F(ApplicationPackageApptest, Basic) { | |
| 175 { | |
| 176 // We need to do this to force the shell to read the test app's manifest and | |
| 177 // register aliases. | |
| 178 test::mojom::ApplicationPackageApptestServicePtr root_service; | |
| 179 shell()->ConnectToInterface("mojo:mojo_shell_apptests", &root_service); | |
| 180 base::RunLoop run_loop; | |
| 181 std::string root_name; | |
| 182 root_service->GetName(base::Bind(&ReceiveName, &root_name, &run_loop)); | |
| 183 run_loop.Run(); | |
| 184 } | |
| 185 | |
| 186 { | |
| 187 // Now subsequent connects to applications provided by the root app will be | |
| 188 // resolved correctly. | |
| 189 test::mojom::ApplicationPackageApptestServicePtr service_a; | |
| 190 shell()->ConnectToInterface("mojo:package_test_a", &service_a); | |
| 191 base::RunLoop run_loop; | |
| 192 std::string a_name; | |
| 193 service_a->GetName(base::Bind(&ReceiveName, &a_name, &run_loop)); | |
| 194 run_loop.Run(); | |
| 195 EXPECT_EQ("A", a_name); | |
| 196 } | |
| 197 | |
| 198 { | |
| 199 test::mojom::ApplicationPackageApptestServicePtr service_b; | |
| 200 shell()->ConnectToInterface("mojo:package_test_b", &service_b); | |
| 201 base::RunLoop run_loop; | |
| 202 std::string b_name; | |
| 203 service_b->GetName(base::Bind(&ReceiveName, &b_name, &run_loop)); | |
| 204 run_loop.Run(); | |
| 205 EXPECT_EQ("B", b_name); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 } // namespace shell | |
| 210 } // namespace mojo | |
| OLD | NEW |