James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Viet-Trung Luu | 7ab2213 | 2016-04-26 21:09:03 -0700 | [diff] [blame] | 5 | #include "shell/application_manager/application_manager.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 9 | #include "base/at_exit.h" |
| 10 | #include "base/bind.h" |
| 11 | #include "base/macros.h" |
Scott Violet | dd035f5 | 2014-12-18 08:47:12 -0800 | [diff] [blame] | 12 | #include "base/memory/scoped_vector.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 14 | #include "mojo/public/cpp/application/application_delegate.h" |
| 15 | #include "mojo/public/cpp/application/application_impl.h" |
Viet-Trung Luu | 7ab2213 | 2016-04-26 21:09:03 -0700 | [diff] [blame] | 16 | #include "mojo/public/cpp/application/connect.h" |
Viet-Trung Luu | b237bca | 2016-05-13 16:35:11 -0700 | [diff] [blame] | 17 | #include "mojo/public/cpp/application/service_provider_impl.h" |
Dave Moore | 2abdce5 | 2015-01-26 15:41:12 -0800 | [diff] [blame] | 18 | #include "mojo/public/cpp/bindings/strong_binding.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 19 | #include "mojo/public/interfaces/application/service_provider.mojom.h" |
Przemysław Pietrzkiewicz | 6384d09 | 2015-01-15 16:03:17 -0800 | [diff] [blame] | 20 | #include "shell/application_manager/application_loader.h" |
Przemysław Pietrzkiewicz | 6384d09 | 2015-01-15 16:03:17 -0800 | [diff] [blame] | 21 | #include "shell/application_manager/test.mojom.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 24 | using mojo::Application; |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 25 | using mojo::ApplicationDelegate; |
| 26 | using mojo::ApplicationImpl; |
| 27 | using mojo::Callback; |
Viet-Trung Luu | d21f93b | 2016-05-12 16:35:38 -0700 | [diff] [blame] | 28 | using mojo::ConnectionContext; |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 29 | using mojo::InterfaceRequest; |
| 30 | using mojo::StrongBinding; |
| 31 | |
Viet-Trung Luu | 36faa4d | 2015-03-04 18:08:18 -0800 | [diff] [blame] | 32 | namespace shell { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | const char kTestURLString[] = "test:testService"; |
| 36 | const char kTestAURLString[] = "test:TestA"; |
| 37 | const char kTestBURLString[] = "test:TestB"; |
| 38 | |
| 39 | struct TestContext { |
| 40 | TestContext() : num_impls(0), num_loader_deletes(0) {} |
| 41 | std::string last_test_string; |
| 42 | int num_impls; |
| 43 | int num_loader_deletes; |
| 44 | }; |
| 45 | |
Benjamin Lerman | 993869e | 2015-03-24 13:35:28 +0100 | [diff] [blame] | 46 | void QuitClosure(bool* value) { |
| 47 | *value = true; |
| 48 | base::MessageLoop::current()->QuitWhenIdle(); |
| 49 | } |
| 50 | |
Dave Moore | 2abdce5 | 2015-01-26 15:41:12 -0800 | [diff] [blame] | 51 | class TestServiceImpl : public TestService { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 52 | public: |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 53 | TestServiceImpl(TestContext* context, InterfaceRequest<TestService> request) |
Dave Moore | 2abdce5 | 2015-01-26 15:41:12 -0800 | [diff] [blame] | 54 | : context_(context), binding_(this, request.Pass()) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 55 | ++context_->num_impls; |
| 56 | } |
| 57 | |
Dave Moore | 2abdce5 | 2015-01-26 15:41:12 -0800 | [diff] [blame] | 58 | ~TestServiceImpl() override { |
| 59 | --context_->num_impls; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 60 | if (!base::MessageLoop::current()->is_running()) |
| 61 | return; |
| 62 | base::MessageLoop::current()->Quit(); |
| 63 | } |
| 64 | |
| 65 | // TestService implementation: |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 66 | void Test(const mojo::String& test_string, |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 67 | const Callback<void()>& callback) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 68 | context_->last_test_string = test_string; |
Aaron Boodman | 8b78ea3 | 2015-01-21 09:28:10 -0800 | [diff] [blame] | 69 | callback.Run(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | private: |
| 73 | TestContext* context_; |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 74 | StrongBinding<TestService> binding_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Aaron Boodman | 8b78ea3 | 2015-01-21 09:28:10 -0800 | [diff] [blame] | 77 | class TestClient { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 78 | public: |
Aaron Boodman | 8b78ea3 | 2015-01-21 09:28:10 -0800 | [diff] [blame] | 79 | explicit TestClient(TestServicePtr service) |
| 80 | : service_(service.Pass()), quit_after_ack_(false) {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 81 | |
Aaron Boodman | 8b78ea3 | 2015-01-21 09:28:10 -0800 | [diff] [blame] | 82 | void AckTest() { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 83 | if (quit_after_ack_) |
| 84 | base::MessageLoop::current()->Quit(); |
| 85 | } |
| 86 | |
Scott Violet | dd035f5 | 2014-12-18 08:47:12 -0800 | [diff] [blame] | 87 | void Test(const std::string& test_string) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 88 | quit_after_ack_ = true; |
Aaron Boodman | 8b78ea3 | 2015-01-21 09:28:10 -0800 | [diff] [blame] | 89 | service_->Test(test_string, |
| 90 | base::Bind(&TestClient::AckTest, base::Unretained(this))); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private: |
| 94 | TestServicePtr service_; |
| 95 | bool quit_after_ack_; |
Aaron Boodman | 8b78ea3 | 2015-01-21 09:28:10 -0800 | [diff] [blame] | 96 | DISALLOW_COPY_AND_ASSIGN(TestClient); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | class TestApplicationLoader : public ApplicationLoader, |
Viet-Trung Luu | 5e30a07 | 2016-05-13 14:12:59 -0700 | [diff] [blame] | 100 | public ApplicationDelegate { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 101 | public: |
Viet-Trung Luu | a417b14 | 2015-03-31 11:24:44 -0700 | [diff] [blame] | 102 | TestApplicationLoader() : context_(nullptr), num_loads_(0) {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 103 | |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 104 | ~TestApplicationLoader() override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 105 | if (context_) |
| 106 | ++context_->num_loader_deletes; |
Viet-Trung Luu | a417b14 | 2015-03-31 11:24:44 -0700 | [diff] [blame] | 107 | test_app_.reset(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void set_context(TestContext* context) { context_ = context; } |
| 111 | int num_loads() const { return num_loads_; } |
James Robinson | d453188 | 2014-10-17 16:14:32 -0700 | [diff] [blame] | 112 | const std::vector<std::string>& GetArgs() const { return test_app_->args(); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | // ApplicationLoader implementation. |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 116 | void Load(const GURL& url, |
| 117 | InterfaceRequest<Application> application_request) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 118 | ++num_loads_; |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 119 | test_app_.reset(new ApplicationImpl(this, application_request.Pass())); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 122 | // ApplicationDelegate implementation. |
Viet-Trung Luu | 22e78b3 | 2016-05-13 15:27:15 -0700 | [diff] [blame] | 123 | bool ConfigureIncomingConnection( |
| 124 | mojo::ServiceProviderImpl* service_provider_impl) override { |
| 125 | service_provider_impl->AddService<TestService>( |
Viet-Trung Luu | 5e30a07 | 2016-05-13 14:12:59 -0700 | [diff] [blame] | 126 | [this](const ConnectionContext& connection_context, |
| 127 | InterfaceRequest<TestService> request) { |
| 128 | new TestServiceImpl(context_, request.Pass()); |
| 129 | }); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 133 | scoped_ptr<ApplicationImpl> test_app_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 134 | TestContext* context_; |
| 135 | int num_loads_; |
| 136 | DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); |
| 137 | }; |
| 138 | |
Benjamin Lerman | 993869e | 2015-03-24 13:35:28 +0100 | [diff] [blame] | 139 | class ClosingApplicationLoader : public ApplicationLoader { |
| 140 | private: |
| 141 | // ApplicationLoader implementation. |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 142 | void Load(const GURL& url, |
| 143 | InterfaceRequest<Application> application_request) override {} |
Benjamin Lerman | 993869e | 2015-03-24 13:35:28 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 146 | class TesterContext { |
| 147 | public: |
| 148 | explicit TesterContext(base::MessageLoop* loop) |
| 149 | : num_b_calls_(0), |
| 150 | num_c_calls_(0), |
| 151 | num_a_deletes_(0), |
| 152 | num_b_deletes_(0), |
| 153 | num_c_deletes_(0), |
| 154 | tester_called_quit_(false), |
| 155 | a_called_quit_(false), |
| 156 | loop_(loop) {} |
| 157 | |
| 158 | void IncrementNumBCalls() { |
| 159 | base::AutoLock lock(lock_); |
| 160 | num_b_calls_++; |
| 161 | } |
| 162 | |
| 163 | void IncrementNumCCalls() { |
| 164 | base::AutoLock lock(lock_); |
| 165 | num_c_calls_++; |
| 166 | } |
| 167 | |
| 168 | void IncrementNumADeletes() { |
| 169 | base::AutoLock lock(lock_); |
| 170 | num_a_deletes_++; |
| 171 | } |
| 172 | |
| 173 | void IncrementNumBDeletes() { |
| 174 | base::AutoLock lock(lock_); |
| 175 | num_b_deletes_++; |
| 176 | } |
| 177 | |
| 178 | void IncrementNumCDeletes() { |
| 179 | base::AutoLock lock(lock_); |
| 180 | num_c_deletes_++; |
| 181 | } |
| 182 | |
| 183 | void set_tester_called_quit() { |
| 184 | base::AutoLock lock(lock_); |
| 185 | tester_called_quit_ = true; |
| 186 | } |
| 187 | |
| 188 | void set_a_called_quit() { |
| 189 | base::AutoLock lock(lock_); |
| 190 | a_called_quit_ = true; |
| 191 | } |
| 192 | |
| 193 | int num_b_calls() { |
| 194 | base::AutoLock lock(lock_); |
| 195 | return num_b_calls_; |
| 196 | } |
| 197 | int num_c_calls() { |
| 198 | base::AutoLock lock(lock_); |
| 199 | return num_c_calls_; |
| 200 | } |
| 201 | int num_a_deletes() { |
| 202 | base::AutoLock lock(lock_); |
| 203 | return num_a_deletes_; |
| 204 | } |
| 205 | int num_b_deletes() { |
| 206 | base::AutoLock lock(lock_); |
| 207 | return num_b_deletes_; |
| 208 | } |
| 209 | int num_c_deletes() { |
| 210 | base::AutoLock lock(lock_); |
| 211 | return num_c_deletes_; |
| 212 | } |
| 213 | bool tester_called_quit() { |
| 214 | base::AutoLock lock(lock_); |
| 215 | return tester_called_quit_; |
| 216 | } |
| 217 | bool a_called_quit() { |
| 218 | base::AutoLock lock(lock_); |
| 219 | return a_called_quit_; |
| 220 | } |
| 221 | |
| 222 | void QuitSoon() { |
| 223 | loop_->PostTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 224 | } |
| 225 | |
| 226 | private: |
| 227 | // lock_ protects all members except for loop_ which must be unchanged for the |
| 228 | // lifetime of this class. |
| 229 | base::Lock lock_; |
| 230 | int num_b_calls_; |
| 231 | int num_c_calls_; |
| 232 | int num_a_deletes_; |
| 233 | int num_b_deletes_; |
| 234 | int num_c_deletes_; |
| 235 | bool tester_called_quit_; |
| 236 | bool a_called_quit_; |
| 237 | |
| 238 | base::MessageLoop* loop_; |
| 239 | }; |
| 240 | |
| 241 | // Used to test that the requestor url will be correctly passed. |
Scott Violet | dd035f5 | 2014-12-18 08:47:12 -0800 | [diff] [blame] | 242 | class TestAImpl : public TestA { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 243 | public: |
Viet-Trung Luu | 7ab2213 | 2016-04-26 21:09:03 -0700 | [diff] [blame] | 244 | TestAImpl(mojo::InterfaceHandle<mojo::ServiceProvider> b_sp_handle, |
Scott Violet | dd035f5 | 2014-12-18 08:47:12 -0800 | [diff] [blame] | 245 | TesterContext* test_context, |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 246 | InterfaceRequest<TestA> request) |
Geoffrey Gowan | 8e12b96 | 2015-02-09 16:45:05 -0800 | [diff] [blame] | 247 | : test_context_(test_context), binding_(this, request.Pass()) { |
Viet-Trung Luu | 7ab2213 | 2016-04-26 21:09:03 -0700 | [diff] [blame] | 248 | auto b_sp = mojo::ServiceProviderPtr::Create(b_sp_handle.Pass()); |
| 249 | mojo::ConnectToService(b_sp.get(), GetProxy(&b_)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 250 | } |
Dave Moore | 2abdce5 | 2015-01-26 15:41:12 -0800 | [diff] [blame] | 251 | |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 252 | ~TestAImpl() override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 253 | test_context_->IncrementNumADeletes(); |
| 254 | if (base::MessageLoop::current()->is_running()) |
| 255 | Quit(); |
| 256 | } |
| 257 | |
| 258 | private: |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 259 | void CallB() override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 260 | b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this))); |
| 261 | } |
| 262 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 263 | void Quit() { |
| 264 | base::MessageLoop::current()->Quit(); |
| 265 | test_context_->set_a_called_quit(); |
| 266 | test_context_->QuitSoon(); |
| 267 | } |
| 268 | |
| 269 | TesterContext* test_context_; |
| 270 | TestBPtr b_; |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 271 | StrongBinding<TestA> binding_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 272 | }; |
| 273 | |
Dave Moore | 2abdce5 | 2015-01-26 15:41:12 -0800 | [diff] [blame] | 274 | class TestBImpl : public TestB { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 275 | public: |
Viet-Trung Luu | d21f93b | 2016-05-12 16:35:38 -0700 | [diff] [blame] | 276 | TestBImpl(TesterContext* test_context, InterfaceRequest<TestB> request) |
Viet-Trung Luu | 7f38d69 | 2016-05-09 12:53:14 -0700 | [diff] [blame] | 277 | : test_context_(test_context), binding_(this, request.Pass()) {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 278 | |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 279 | ~TestBImpl() override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 280 | test_context_->IncrementNumBDeletes(); |
| 281 | if (base::MessageLoop::current()->is_running()) |
| 282 | base::MessageLoop::current()->Quit(); |
| 283 | test_context_->QuitSoon(); |
| 284 | } |
| 285 | |
| 286 | private: |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 287 | void B(const Callback<void()>& callback) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 288 | test_context_->IncrementNumBCalls(); |
| 289 | callback.Run(); |
| 290 | } |
| 291 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 292 | TesterContext* test_context_; |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 293 | StrongBinding<TestB> binding_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 294 | }; |
| 295 | |
Viet-Trung Luu | 5e30a07 | 2016-05-13 14:12:59 -0700 | [diff] [blame] | 296 | class Tester : public ApplicationDelegate, public ApplicationLoader { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 297 | public: |
| 298 | Tester(TesterContext* context, const std::string& requestor_url) |
| 299 | : context_(context), requestor_url_(requestor_url) {} |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 300 | ~Tester() override {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 301 | |
| 302 | private: |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 303 | void Load(const GURL& url, |
| 304 | InterfaceRequest<Application> application_request) override { |
| 305 | app_.reset(new ApplicationImpl(this, application_request.Pass())); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Viet-Trung Luu | 22e78b3 | 2016-05-13 15:27:15 -0700 | [diff] [blame] | 308 | bool ConfigureIncomingConnection( |
| 309 | mojo::ServiceProviderImpl* service_provider_impl) override { |
Viet-Trung Luu | 087fc16 | 2016-05-13 11:15:55 -0700 | [diff] [blame] | 310 | const std::string& remote_url = |
Viet-Trung Luu | 22e78b3 | 2016-05-13 15:27:15 -0700 | [diff] [blame] | 311 | service_provider_impl->connection_context().remote_url; |
Viet-Trung Luu | 087fc16 | 2016-05-13 11:15:55 -0700 | [diff] [blame] | 312 | if (!requestor_url_.empty() && requestor_url_ != remote_url) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 313 | context_->set_tester_called_quit(); |
| 314 | context_->QuitSoon(); |
| 315 | base::MessageLoop::current()->Quit(); |
| 316 | return false; |
| 317 | } |
| 318 | // If we're coming from A, then add B, otherwise A. |
Viet-Trung Luu | 5e30a07 | 2016-05-13 14:12:59 -0700 | [diff] [blame] | 319 | if (remote_url == kTestAURLString) { |
Viet-Trung Luu | 22e78b3 | 2016-05-13 15:27:15 -0700 | [diff] [blame] | 320 | service_provider_impl->AddService<TestB>( |
Viet-Trung Luu | 5e30a07 | 2016-05-13 14:12:59 -0700 | [diff] [blame] | 321 | [this](const ConnectionContext& connection_context, |
| 322 | InterfaceRequest<TestB> test_b_request) { |
| 323 | new TestBImpl(context_, test_b_request.Pass()); |
| 324 | }); |
| 325 | } else { |
Viet-Trung Luu | 22e78b3 | 2016-05-13 15:27:15 -0700 | [diff] [blame] | 326 | service_provider_impl->AddService<TestA>( |
Viet-Trung Luu | 5e30a07 | 2016-05-13 14:12:59 -0700 | [diff] [blame] | 327 | [this](const ConnectionContext& connection_context, |
| 328 | InterfaceRequest<TestA> test_a_request) { |
| 329 | mojo::InterfaceHandle<mojo::ServiceProvider> incoming_sp_handle; |
| 330 | app_->shell()->ConnectToApplication( |
| 331 | kTestBURLString, GetProxy(&incoming_sp_handle), nullptr); |
| 332 | a_bindings_.push_back(new TestAImpl( |
| 333 | incoming_sp_handle.Pass(), context_, test_a_request.Pass())); |
| 334 | }); |
| 335 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 339 | TesterContext* context_; |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 340 | scoped_ptr<ApplicationImpl> app_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 341 | std::string requestor_url_; |
Scott Violet | dd035f5 | 2014-12-18 08:47:12 -0800 | [diff] [blame] | 342 | ScopedVector<TestAImpl> a_bindings_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 343 | }; |
| 344 | |
Aaron Boodman | cd7b911 | 2014-11-06 21:44:49 -0800 | [diff] [blame] | 345 | class TestDelegate : public ApplicationManager::Delegate { |
| 346 | public: |
Viet-Trung Luu | 3295227 | 2015-02-11 16:32:03 -0800 | [diff] [blame] | 347 | void AddMapping(const GURL& from, const GURL& to) { mappings_[from] = to; } |
Aaron Boodman | cd7b911 | 2014-11-06 21:44:49 -0800 | [diff] [blame] | 348 | |
| 349 | // ApplicationManager::Delegate |
Viet-Trung Luu | 3295227 | 2015-02-11 16:32:03 -0800 | [diff] [blame] | 350 | GURL ResolveMappings(const GURL& url) override { |
Aaron Boodman | cd7b911 | 2014-11-06 21:44:49 -0800 | [diff] [blame] | 351 | auto it = mappings_.find(url); |
| 352 | if (it != mappings_.end()) |
| 353 | return it->second; |
| 354 | return url; |
| 355 | } |
Benjamin Lerman | 605969e | 2015-04-30 13:51:04 +0200 | [diff] [blame] | 356 | GURL ResolveMojoURL(const GURL& url) override { |
Benjamin Lerman | 75aef1d | 2015-01-21 12:04:39 +0100 | [diff] [blame] | 357 | GURL mapped_url = ResolveMappings(url); |
| 358 | // The shell automatically map mojo URLs. |
| 359 | if (mapped_url.scheme() == "mojo") { |
| 360 | url::Replacements<char> replacements; |
| 361 | replacements.SetScheme("file", url::Component(0, 4)); |
| 362 | mapped_url = mapped_url.ReplaceComponents(replacements); |
| 363 | } |
| 364 | return mapped_url; |
| 365 | } |
| 366 | |
Aaron Boodman | cd7b911 | 2014-11-06 21:44:49 -0800 | [diff] [blame] | 367 | private: |
| 368 | std::map<GURL, GURL> mappings_; |
| 369 | }; |
| 370 | |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 371 | class TestExternal : public ApplicationDelegate { |
Dave Moore | 0504d06 | 2015-01-23 15:33:29 -0800 | [diff] [blame] | 372 | public: |
Dave Moore | f2efe55 | 2015-01-26 14:31:45 -0800 | [diff] [blame] | 373 | TestExternal() : configure_incoming_connection_called_(false) {} |
Dave Moore | 0504d06 | 2015-01-23 15:33:29 -0800 | [diff] [blame] | 374 | |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 375 | void Initialize(ApplicationImpl* app) override { |
Dave Moore | f2efe55 | 2015-01-26 14:31:45 -0800 | [diff] [blame] | 376 | initialize_args_ = app->args(); |
Dave Moore | 0504d06 | 2015-01-23 15:33:29 -0800 | [diff] [blame] | 377 | base::MessageLoop::current()->Quit(); |
| 378 | } |
| 379 | |
Viet-Trung Luu | 22e78b3 | 2016-05-13 15:27:15 -0700 | [diff] [blame] | 380 | bool ConfigureIncomingConnection( |
| 381 | mojo::ServiceProviderImpl* service_provider_impl) override { |
Dave Moore | 0504d06 | 2015-01-23 15:33:29 -0800 | [diff] [blame] | 382 | configure_incoming_connection_called_ = true; |
| 383 | base::MessageLoop::current()->Quit(); |
| 384 | return true; |
| 385 | } |
| 386 | |
Dave Moore | f2efe55 | 2015-01-26 14:31:45 -0800 | [diff] [blame] | 387 | const std::vector<std::string>& initialize_args() const { |
| 388 | return initialize_args_; |
| 389 | } |
| 390 | |
Dave Moore | 0504d06 | 2015-01-23 15:33:29 -0800 | [diff] [blame] | 391 | bool configure_incoming_connection_called() const { |
| 392 | return configure_incoming_connection_called_; |
| 393 | } |
| 394 | |
| 395 | private: |
Dave Moore | f2efe55 | 2015-01-26 14:31:45 -0800 | [diff] [blame] | 396 | std::vector<std::string> initialize_args_; |
Dave Moore | 0504d06 | 2015-01-23 15:33:29 -0800 | [diff] [blame] | 397 | bool configure_incoming_connection_called_; |
| 398 | }; |
| 399 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 400 | class ApplicationManagerTest : public testing::Test { |
| 401 | public: |
| 402 | ApplicationManagerTest() : tester_context_(&loop_) {} |
| 403 | |
Viet-Trung Luu | 153dd21 | 2014-10-21 14:41:15 -0700 | [diff] [blame] | 404 | ~ApplicationManagerTest() override {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 405 | |
Viet-Trung Luu | 153dd21 | 2014-10-21 14:41:15 -0700 | [diff] [blame] | 406 | void SetUp() override { |
Viet-Trung Luu | 988b4be | 2015-05-15 13:00:15 -0700 | [diff] [blame] | 407 | application_manager_.reset( |
| 408 | new ApplicationManager(ApplicationManager::Options(), &test_delegate_)); |
Aaron Boodman | cd7b911 | 2014-11-06 21:44:49 -0800 | [diff] [blame] | 409 | test_loader_ = new TestApplicationLoader; |
| 410 | test_loader_->set_context(&context_); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 411 | application_manager_->set_default_loader( |
Aaron Boodman | cd7b911 | 2014-11-06 21:44:49 -0800 | [diff] [blame] | 412 | scoped_ptr<ApplicationLoader>(test_loader_)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 413 | |
| 414 | TestServicePtr service_proxy; |
| 415 | application_manager_->ConnectToService(GURL(kTestURLString), |
| 416 | &service_proxy); |
Aaron Boodman | 8b78ea3 | 2015-01-21 09:28:10 -0800 | [diff] [blame] | 417 | test_client_.reset(new TestClient(service_proxy.Pass())); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Viet-Trung Luu | 153dd21 | 2014-10-21 14:41:15 -0700 | [diff] [blame] | 420 | void TearDown() override { |
Viet-Trung Luu | a417b14 | 2015-03-31 11:24:44 -0700 | [diff] [blame] | 421 | test_client_.reset(); |
| 422 | application_manager_.reset(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 423 | } |
| 424 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 425 | void AddLoaderForURL(const GURL& url, const std::string& requestor_url) { |
Scott Violet | dd035f5 | 2014-12-18 08:47:12 -0800 | [diff] [blame] | 426 | application_manager_->SetLoaderForURL( |
| 427 | make_scoped_ptr(new Tester(&tester_context_, requestor_url)), url); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | bool HasFactoryForTestURL() { |
| 431 | ApplicationManager::TestAPI manager_test_api(application_manager_.get()); |
| 432 | return manager_test_api.HasFactoryForURL(GURL(kTestURLString)); |
| 433 | } |
| 434 | |
| 435 | protected: |
| 436 | base::ShadowingAtExitManager at_exit_; |
Aaron Boodman | cd7b911 | 2014-11-06 21:44:49 -0800 | [diff] [blame] | 437 | TestDelegate test_delegate_; |
| 438 | TestApplicationLoader* test_loader_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 439 | TesterContext tester_context_; |
| 440 | TestContext context_; |
| 441 | base::MessageLoop loop_; |
Aaron Boodman | 8b78ea3 | 2015-01-21 09:28:10 -0800 | [diff] [blame] | 442 | scoped_ptr<TestClient> test_client_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 443 | scoped_ptr<ApplicationManager> application_manager_; |
| 444 | DISALLOW_COPY_AND_ASSIGN(ApplicationManagerTest); |
| 445 | }; |
| 446 | |
| 447 | TEST_F(ApplicationManagerTest, Basic) { |
| 448 | test_client_->Test("test"); |
| 449 | loop_.Run(); |
| 450 | EXPECT_EQ(std::string("test"), context_.last_test_string); |
| 451 | } |
| 452 | |
| 453 | // Confirm that no arguments are sent to an application by default. |
| 454 | TEST_F(ApplicationManagerTest, NoArgs) { |
Viet-Trung Luu | 988b4be | 2015-05-15 13:00:15 -0700 | [diff] [blame] | 455 | ApplicationManager am(ApplicationManager::Options(), &test_delegate_); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 456 | GURL test_url("test:test"); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 457 | TestApplicationLoader* loader = new TestApplicationLoader; |
Scott Violet | ba1f42c | 2014-12-04 11:08:49 -0800 | [diff] [blame] | 458 | loader->set_context(&context_); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 459 | am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); |
| 460 | TestServicePtr test_service; |
| 461 | am.ConnectToService(test_url, &test_service); |
Aaron Boodman | 8b78ea3 | 2015-01-21 09:28:10 -0800 | [diff] [blame] | 462 | TestClient test_client(test_service.Pass()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 463 | test_client.Test("test"); |
| 464 | loop_.Run(); |
| 465 | std::vector<std::string> app_args = loader->GetArgs(); |
| 466 | EXPECT_EQ(0U, app_args.size()); |
| 467 | } |
| 468 | |
| 469 | // Confirm that arguments are sent to an application. |
| 470 | TEST_F(ApplicationManagerTest, Args) { |
Viet-Trung Luu | 988b4be | 2015-05-15 13:00:15 -0700 | [diff] [blame] | 471 | ApplicationManager am(ApplicationManager::Options(), &test_delegate_); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 472 | GURL test_url("test:test"); |
| 473 | std::vector<std::string> args; |
| 474 | args.push_back("test_arg1"); |
| 475 | args.push_back("test_arg2"); |
| 476 | am.SetArgsForURL(args, test_url); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 477 | TestApplicationLoader* loader = new TestApplicationLoader; |
Scott Violet | ba1f42c | 2014-12-04 11:08:49 -0800 | [diff] [blame] | 478 | loader->set_context(&context_); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 479 | am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); |
| 480 | TestServicePtr test_service; |
| 481 | am.ConnectToService(test_url, &test_service); |
Aaron Boodman | 8b78ea3 | 2015-01-21 09:28:10 -0800 | [diff] [blame] | 482 | TestClient test_client(test_service.Pass()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 483 | test_client.Test("test"); |
| 484 | loop_.Run(); |
| 485 | std::vector<std::string> app_args = loader->GetArgs(); |
Benjamin Lerman | f3b7f56 | 2015-11-23 18:18:03 +0100 | [diff] [blame] | 486 | ASSERT_EQ(args.size() + 1, app_args.size()); |
| 487 | EXPECT_EQ(args[0], app_args[1]); |
| 488 | EXPECT_EQ(args[1], app_args[2]); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Benjamin Lerman | aec7ef8 | 2015-11-23 15:34:06 +0100 | [diff] [blame] | 491 | // Confirm that arguments are sent to an application in the presence of query |
| 492 | // parameters. |
| 493 | TEST_F(ApplicationManagerTest, ArgsWithQuery) { |
| 494 | ApplicationManager am(ApplicationManager::Options(), &test_delegate_); |
| 495 | GURL test_url("test:test"); |
| 496 | GURL test_url_with_query("test:test?foo=bar"); |
| 497 | std::vector<std::string> args; |
| 498 | args.push_back("test_arg1"); |
| 499 | am.SetArgsForURL(args, test_url); |
| 500 | TestApplicationLoader* loader = new TestApplicationLoader; |
| 501 | loader->set_context(&context_); |
| 502 | am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); |
| 503 | TestServicePtr test_service; |
| 504 | am.ConnectToService(test_url_with_query, &test_service); |
| 505 | TestClient test_client(test_service.Pass()); |
| 506 | test_client.Test("test"); |
| 507 | loop_.Run(); |
| 508 | std::vector<std::string> app_args = loader->GetArgs(); |
Benjamin Lerman | f3b7f56 | 2015-11-23 18:18:03 +0100 | [diff] [blame] | 509 | ASSERT_EQ(args.size() + 1, app_args.size()); |
| 510 | EXPECT_EQ(args[0], app_args[1]); |
| 511 | } |
| 512 | |
| 513 | // Confirm that the URL is not duplicated when arguments are added in multiple |
| 514 | // phases. |
| 515 | TEST_F(ApplicationManagerTest, ArgsMultipleCalls) { |
| 516 | ApplicationManager am(ApplicationManager::Options(), &test_delegate_); |
| 517 | GURL test_url("test:test"); |
| 518 | std::vector<std::string> args1; |
| 519 | args1.push_back("test_arg1"); |
| 520 | am.SetArgsForURL(args1, test_url); |
| 521 | std::vector<std::string> args2; |
| 522 | args2.push_back("test_arg2"); |
| 523 | am.SetArgsForURL(args2, test_url); |
| 524 | TestApplicationLoader* loader = new TestApplicationLoader; |
| 525 | loader->set_context(&context_); |
| 526 | am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); |
| 527 | TestServicePtr test_service; |
| 528 | am.ConnectToService(test_url, &test_service); |
| 529 | TestClient test_client(test_service.Pass()); |
| 530 | test_client.Test("test"); |
| 531 | loop_.Run(); |
| 532 | std::vector<std::string> app_args = loader->GetArgs(); |
| 533 | ASSERT_EQ(args1.size() + args2.size() + 1, app_args.size()); |
| 534 | EXPECT_EQ(args1[0], app_args[1]); |
| 535 | EXPECT_EQ(args2[0], app_args[2]); |
Benjamin Lerman | aec7ef8 | 2015-11-23 15:34:06 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Benjamin Lerman | acf32fb | 2015-03-24 10:54:37 +0100 | [diff] [blame] | 538 | // Confirm that arguments are aggregated through mappings. |
| 539 | TEST_F(ApplicationManagerTest, ArgsAndMapping) { |
Viet-Trung Luu | 988b4be | 2015-05-15 13:00:15 -0700 | [diff] [blame] | 540 | ApplicationManager am(ApplicationManager::Options(), &test_delegate_); |
Benjamin Lerman | acf32fb | 2015-03-24 10:54:37 +0100 | [diff] [blame] | 541 | GURL test_url("test:test"); |
| 542 | GURL test_url2("test:test2"); |
| 543 | test_delegate_.AddMapping(test_url, test_url2); |
| 544 | std::vector<std::string> args; |
| 545 | args.push_back("test_arg1"); |
| 546 | args.push_back("test_arg2"); |
| 547 | am.SetArgsForURL(args, test_url); |
| 548 | std::vector<std::string> args2; |
| 549 | args2.push_back("test_arg3"); |
| 550 | args2.push_back("test_arg4"); |
| 551 | am.SetArgsForURL(args2, test_url2); |
| 552 | TestApplicationLoader* loader = new TestApplicationLoader; |
| 553 | loader->set_context(&context_); |
| 554 | am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url2); |
| 555 | { |
| 556 | // Connext to the mapped url |
| 557 | TestServicePtr test_service; |
| 558 | am.ConnectToService(test_url, &test_service); |
| 559 | TestClient test_client(test_service.Pass()); |
| 560 | test_client.Test("test"); |
| 561 | loop_.Run(); |
| 562 | std::vector<std::string> app_args = loader->GetArgs(); |
Benjamin Lerman | f3b7f56 | 2015-11-23 18:18:03 +0100 | [diff] [blame] | 563 | ASSERT_EQ(args.size() + args2.size() + 1, app_args.size()); |
| 564 | EXPECT_EQ(args[0], app_args[1]); |
| 565 | EXPECT_EQ(args[1], app_args[2]); |
| 566 | EXPECT_EQ(args2[0], app_args[3]); |
| 567 | EXPECT_EQ(args2[1], app_args[4]); |
Benjamin Lerman | acf32fb | 2015-03-24 10:54:37 +0100 | [diff] [blame] | 568 | } |
| 569 | { |
| 570 | // Connext to the target url |
| 571 | TestServicePtr test_service; |
| 572 | am.ConnectToService(test_url2, &test_service); |
| 573 | TestClient test_client(test_service.Pass()); |
| 574 | test_client.Test("test"); |
| 575 | loop_.Run(); |
| 576 | std::vector<std::string> app_args = loader->GetArgs(); |
Benjamin Lerman | f3b7f56 | 2015-11-23 18:18:03 +0100 | [diff] [blame] | 577 | ASSERT_EQ(args.size() + args2.size() + 1, app_args.size()); |
| 578 | EXPECT_EQ(args[0], app_args[1]); |
| 579 | EXPECT_EQ(args[1], app_args[2]); |
| 580 | EXPECT_EQ(args2[0], app_args[3]); |
| 581 | EXPECT_EQ(args2[1], app_args[4]); |
Benjamin Lerman | acf32fb | 2015-03-24 10:54:37 +0100 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 585 | TEST_F(ApplicationManagerTest, ClientError) { |
| 586 | test_client_->Test("test"); |
| 587 | EXPECT_TRUE(HasFactoryForTestURL()); |
| 588 | loop_.Run(); |
| 589 | EXPECT_EQ(1, context_.num_impls); |
Viet-Trung Luu | a417b14 | 2015-03-31 11:24:44 -0700 | [diff] [blame] | 590 | test_client_.reset(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 591 | loop_.Run(); |
| 592 | EXPECT_EQ(0, context_.num_impls); |
| 593 | EXPECT_TRUE(HasFactoryForTestURL()); |
| 594 | } |
| 595 | |
| 596 | TEST_F(ApplicationManagerTest, Deletes) { |
| 597 | { |
Viet-Trung Luu | 988b4be | 2015-05-15 13:00:15 -0700 | [diff] [blame] | 598 | ApplicationManager am(ApplicationManager::Options(), &test_delegate_); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 599 | TestApplicationLoader* default_loader = new TestApplicationLoader; |
| 600 | default_loader->set_context(&context_); |
| 601 | TestApplicationLoader* url_loader1 = new TestApplicationLoader; |
| 602 | TestApplicationLoader* url_loader2 = new TestApplicationLoader; |
| 603 | url_loader1->set_context(&context_); |
| 604 | url_loader2->set_context(&context_); |
| 605 | TestApplicationLoader* scheme_loader1 = new TestApplicationLoader; |
| 606 | TestApplicationLoader* scheme_loader2 = new TestApplicationLoader; |
| 607 | scheme_loader1->set_context(&context_); |
| 608 | scheme_loader2->set_context(&context_); |
| 609 | am.set_default_loader(scoped_ptr<ApplicationLoader>(default_loader)); |
| 610 | am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader1), |
| 611 | GURL("test:test1")); |
| 612 | am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader2), |
| 613 | GURL("test:test1")); |
| 614 | am.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader1), |
| 615 | "test"); |
| 616 | am.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader2), |
| 617 | "test"); |
| 618 | } |
| 619 | EXPECT_EQ(5, context_.num_loader_deletes); |
| 620 | } |
| 621 | |
| 622 | // Confirm that both urls and schemes can have their loaders explicitly set. |
| 623 | TEST_F(ApplicationManagerTest, SetLoaders) { |
| 624 | TestApplicationLoader* default_loader = new TestApplicationLoader; |
| 625 | TestApplicationLoader* url_loader = new TestApplicationLoader; |
| 626 | TestApplicationLoader* scheme_loader = new TestApplicationLoader; |
| 627 | application_manager_->set_default_loader( |
| 628 | scoped_ptr<ApplicationLoader>(default_loader)); |
| 629 | application_manager_->SetLoaderForURL( |
| 630 | scoped_ptr<ApplicationLoader>(url_loader), GURL("test:test1")); |
| 631 | application_manager_->SetLoaderForScheme( |
| 632 | scoped_ptr<ApplicationLoader>(scheme_loader), "test"); |
| 633 | |
| 634 | // test::test1 should go to url_loader. |
| 635 | TestServicePtr test_service; |
| 636 | application_manager_->ConnectToService(GURL("test:test1"), &test_service); |
| 637 | EXPECT_EQ(1, url_loader->num_loads()); |
| 638 | EXPECT_EQ(0, scheme_loader->num_loads()); |
| 639 | EXPECT_EQ(0, default_loader->num_loads()); |
| 640 | |
| 641 | // test::test2 should go to scheme loader. |
| 642 | application_manager_->ConnectToService(GURL("test:test2"), &test_service); |
| 643 | EXPECT_EQ(1, url_loader->num_loads()); |
| 644 | EXPECT_EQ(1, scheme_loader->num_loads()); |
| 645 | EXPECT_EQ(0, default_loader->num_loads()); |
| 646 | |
| 647 | // http::test1 should go to default loader. |
| 648 | application_manager_->ConnectToService(GURL("http:test1"), &test_service); |
| 649 | EXPECT_EQ(1, url_loader->num_loads()); |
| 650 | EXPECT_EQ(1, scheme_loader->num_loads()); |
| 651 | EXPECT_EQ(1, default_loader->num_loads()); |
| 652 | } |
| 653 | |
| 654 | // Confirm that the url of a service is correctly passed to another service that |
| 655 | // it loads. |
| 656 | TEST_F(ApplicationManagerTest, ACallB) { |
| 657 | // Any url can load a. |
| 658 | AddLoaderForURL(GURL(kTestAURLString), std::string()); |
| 659 | |
| 660 | // Only a can load b. |
| 661 | AddLoaderForURL(GURL(kTestBURLString), kTestAURLString); |
| 662 | |
| 663 | TestAPtr a; |
| 664 | application_manager_->ConnectToService(GURL(kTestAURLString), &a); |
| 665 | a->CallB(); |
| 666 | loop_.Run(); |
| 667 | EXPECT_EQ(1, tester_context_.num_b_calls()); |
| 668 | EXPECT_TRUE(tester_context_.a_called_quit()); |
| 669 | } |
| 670 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 671 | // Confirm that a service impl will be deleted if the app that connected to |
| 672 | // it goes away. |
| 673 | TEST_F(ApplicationManagerTest, BDeleted) { |
| 674 | AddLoaderForURL(GURL(kTestAURLString), std::string()); |
| 675 | AddLoaderForURL(GURL(kTestBURLString), std::string()); |
| 676 | |
| 677 | TestAPtr a; |
| 678 | application_manager_->ConnectToService(GURL(kTestAURLString), &a); |
| 679 | |
| 680 | a->CallB(); |
| 681 | loop_.Run(); |
| 682 | |
| 683 | // Kills the a app. |
| 684 | application_manager_->SetLoaderForURL(scoped_ptr<ApplicationLoader>(), |
| 685 | GURL(kTestAURLString)); |
| 686 | loop_.Run(); |
| 687 | |
| 688 | EXPECT_EQ(1, tester_context_.num_b_deletes()); |
| 689 | } |
| 690 | |
| 691 | // Confirm that the url of a service is correctly passed to another service that |
| 692 | // it loads, and that it can be rejected. |
| 693 | TEST_F(ApplicationManagerTest, ANoLoadB) { |
| 694 | // Any url can load a. |
| 695 | AddLoaderForURL(GURL(kTestAURLString), std::string()); |
| 696 | |
| 697 | // Only c can load b, so this will fail. |
| 698 | AddLoaderForURL(GURL(kTestBURLString), "test:TestC"); |
| 699 | |
| 700 | TestAPtr a; |
| 701 | application_manager_->ConnectToService(GURL(kTestAURLString), &a); |
| 702 | a->CallB(); |
| 703 | loop_.Run(); |
| 704 | EXPECT_EQ(0, tester_context_.num_b_calls()); |
| 705 | |
| 706 | EXPECT_FALSE(tester_context_.a_called_quit()); |
| 707 | EXPECT_TRUE(tester_context_.tester_called_quit()); |
| 708 | } |
| 709 | |
| 710 | TEST_F(ApplicationManagerTest, NoServiceNoLoad) { |
| 711 | AddLoaderForURL(GURL(kTestAURLString), std::string()); |
| 712 | |
| 713 | // There is no TestC service implementation registered with |
| 714 | // ApplicationManager, so this cannot succeed (but also shouldn't crash). |
| 715 | TestCPtr c; |
| 716 | application_manager_->ConnectToService(GURL(kTestAURLString), &c); |
Viet-Trung Luu | 01e64cf | 2015-07-08 09:55:53 -0700 | [diff] [blame] | 717 | c.set_connection_error_handler( |
| 718 | []() { base::MessageLoop::current()->QuitWhenIdle(); }); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 719 | |
| 720 | loop_.Run(); |
| 721 | EXPECT_TRUE(c.encountered_error()); |
| 722 | } |
| 723 | |
Aaron Boodman | cd7b911 | 2014-11-06 21:44:49 -0800 | [diff] [blame] | 724 | TEST_F(ApplicationManagerTest, MappedURLsShouldNotCauseDuplicateLoad) { |
| 725 | test_delegate_.AddMapping(GURL("foo:foo2"), GURL("foo:foo")); |
| 726 | // 1 because ApplicationManagerTest connects once at startup. |
| 727 | EXPECT_EQ(1, test_loader_->num_loads()); |
| 728 | |
| 729 | TestServicePtr test_service; |
| 730 | application_manager_->ConnectToService(GURL("foo:foo"), &test_service); |
| 731 | EXPECT_EQ(2, test_loader_->num_loads()); |
| 732 | |
| 733 | TestServicePtr test_service2; |
| 734 | application_manager_->ConnectToService(GURL("foo:foo2"), &test_service2); |
| 735 | EXPECT_EQ(2, test_loader_->num_loads()); |
| 736 | |
| 737 | TestServicePtr test_service3; |
| 738 | application_manager_->ConnectToService(GURL("bar:bar"), &test_service2); |
| 739 | EXPECT_EQ(3, test_loader_->num_loads()); |
| 740 | } |
| 741 | |
Benjamin Lerman | 75aef1d | 2015-01-21 12:04:39 +0100 | [diff] [blame] | 742 | TEST_F(ApplicationManagerTest, MappedURLsShouldWorkWithLoaders) { |
| 743 | TestApplicationLoader* custom_loader = new TestApplicationLoader; |
| 744 | TestContext context; |
| 745 | custom_loader->set_context(&context); |
| 746 | application_manager_->SetLoaderForURL(make_scoped_ptr(custom_loader), |
| 747 | GURL("mojo:foo")); |
| 748 | test_delegate_.AddMapping(GURL("mojo:foo2"), GURL("mojo:foo")); |
| 749 | |
| 750 | TestServicePtr test_service; |
| 751 | application_manager_->ConnectToService(GURL("mojo:foo2"), &test_service); |
| 752 | EXPECT_EQ(1, custom_loader->num_loads()); |
| 753 | custom_loader->set_context(nullptr); |
| 754 | } |
| 755 | |
Benjamin Lerman | c76642f | 2015-03-04 15:00:55 +0100 | [diff] [blame] | 756 | TEST_F(ApplicationManagerTest, TestQueryWithLoaders) { |
| 757 | TestApplicationLoader* url_loader = new TestApplicationLoader; |
| 758 | TestApplicationLoader* scheme_loader = new TestApplicationLoader; |
| 759 | application_manager_->SetLoaderForURL( |
| 760 | scoped_ptr<ApplicationLoader>(url_loader), GURL("test:test1")); |
| 761 | application_manager_->SetLoaderForScheme( |
| 762 | scoped_ptr<ApplicationLoader>(scheme_loader), "test"); |
| 763 | |
| 764 | // test::test1 should go to url_loader. |
| 765 | TestServicePtr test_service; |
| 766 | application_manager_->ConnectToService(GURL("test:test1?foo=bar"), |
| 767 | &test_service); |
| 768 | EXPECT_EQ(1, url_loader->num_loads()); |
| 769 | EXPECT_EQ(0, scheme_loader->num_loads()); |
| 770 | |
| 771 | // test::test2 should go to scheme loader. |
| 772 | application_manager_->ConnectToService(GURL("test:test2?foo=bar"), |
| 773 | &test_service); |
| 774 | EXPECT_EQ(1, url_loader->num_loads()); |
| 775 | EXPECT_EQ(1, scheme_loader->num_loads()); |
| 776 | } |
| 777 | |
Benjamin Lerman | 993869e | 2015-03-24 13:35:28 +0100 | [diff] [blame] | 778 | TEST_F(ApplicationManagerTest, TestEndApplicationClosure) { |
| 779 | ClosingApplicationLoader* loader = new ClosingApplicationLoader(); |
| 780 | application_manager_->SetLoaderForScheme( |
| 781 | scoped_ptr<ApplicationLoader>(loader), "test"); |
| 782 | |
| 783 | bool called = false; |
| 784 | application_manager_->ConnectToApplication( |
Viet-Trung Luu | d588c94 | 2016-05-09 16:49:45 -0700 | [diff] [blame] | 785 | GURL("test:test"), GURL(), nullptr, |
Benjamin Lerman | 993869e | 2015-03-24 13:35:28 +0100 | [diff] [blame] | 786 | base::Bind(&QuitClosure, base::Unretained(&called))); |
| 787 | loop_.Run(); |
| 788 | EXPECT_TRUE(called); |
| 789 | } |
| 790 | |
Viet-Trung Luu | 36faa4d | 2015-03-04 18:08:18 -0800 | [diff] [blame] | 791 | } // namespace |
| 792 | } // namespace shell |