Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: shell/application_manager/application_manager_unittest.cc

Issue 1074963002: Add some |using mojo::...;|s to //shell .cc files. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "base/at_exit.h" 5 #include "base/at_exit.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/memory/scoped_vector.h" 8 #include "base/memory/scoped_vector.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "mojo/public/cpp/application/application_connection.h" 10 #include "mojo/public/cpp/application/application_connection.h"
11 #include "mojo/public/cpp/application/application_delegate.h" 11 #include "mojo/public/cpp/application/application_delegate.h"
12 #include "mojo/public/cpp/application/application_impl.h" 12 #include "mojo/public/cpp/application/application_impl.h"
13 #include "mojo/public/cpp/application/interface_factory.h" 13 #include "mojo/public/cpp/application/interface_factory.h"
14 #include "mojo/public/cpp/bindings/strong_binding.h" 14 #include "mojo/public/cpp/bindings/strong_binding.h"
15 #include "mojo/public/interfaces/application/service_provider.mojom.h" 15 #include "mojo/public/interfaces/application/service_provider.mojom.h"
16 #include "shell/application_manager/application_loader.h" 16 #include "shell/application_manager/application_loader.h"
17 #include "shell/application_manager/application_manager.h" 17 #include "shell/application_manager/application_manager.h"
18 #include "shell/application_manager/test.mojom.h" 18 #include "shell/application_manager/test.mojom.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 using mojo::Application;
22 using mojo::ApplicationConnection;
23 using mojo::ApplicationDelegate;
24 using mojo::ApplicationImpl;
25 using mojo::Callback;
26 using mojo::InterfaceFactory;
27 using mojo::InterfaceRequest;
28 using mojo::StrongBinding;
29
21 namespace shell { 30 namespace shell {
22 namespace { 31 namespace {
23 32
24 const char kTestURLString[] = "test:testService"; 33 const char kTestURLString[] = "test:testService";
25 const char kTestAURLString[] = "test:TestA"; 34 const char kTestAURLString[] = "test:TestA";
26 const char kTestBURLString[] = "test:TestB"; 35 const char kTestBURLString[] = "test:TestB";
27 36
28 struct TestContext { 37 struct TestContext {
29 TestContext() : num_impls(0), num_loader_deletes(0) {} 38 TestContext() : num_impls(0), num_loader_deletes(0) {}
30 std::string last_test_string; 39 std::string last_test_string;
(...skipping 15 matching lines...) Expand all
46 void OnConnectionError() override { 55 void OnConnectionError() override {
47 base::MessageLoop::current()->QuitWhenIdle(); 56 base::MessageLoop::current()->QuitWhenIdle();
48 } 57 }
49 58
50 private: 59 private:
51 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler); 60 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler);
52 }; 61 };
53 62
54 class TestServiceImpl : public TestService { 63 class TestServiceImpl : public TestService {
55 public: 64 public:
56 TestServiceImpl(TestContext* context, 65 TestServiceImpl(TestContext* context, InterfaceRequest<TestService> request)
57 mojo::InterfaceRequest<TestService> request)
58 : context_(context), binding_(this, request.Pass()) { 66 : context_(context), binding_(this, request.Pass()) {
59 ++context_->num_impls; 67 ++context_->num_impls;
60 } 68 }
61 69
62 ~TestServiceImpl() override { 70 ~TestServiceImpl() override {
63 --context_->num_impls; 71 --context_->num_impls;
64 if (!base::MessageLoop::current()->is_running()) 72 if (!base::MessageLoop::current()->is_running())
65 return; 73 return;
66 base::MessageLoop::current()->Quit(); 74 base::MessageLoop::current()->Quit();
67 } 75 }
68 76
69 // TestService implementation: 77 // TestService implementation:
70 void Test(const mojo::String& test_string, 78 void Test(const mojo::String& test_string,
71 const mojo::Callback<void()>& callback) override { 79 const Callback<void()>& callback) override {
72 context_->last_test_string = test_string; 80 context_->last_test_string = test_string;
73 callback.Run(); 81 callback.Run();
74 } 82 }
75 83
76 private: 84 private:
77 TestContext* context_; 85 TestContext* context_;
78 mojo::StrongBinding<TestService> binding_; 86 StrongBinding<TestService> binding_;
79 }; 87 };
80 88
81 class TestClient { 89 class TestClient {
82 public: 90 public:
83 explicit TestClient(TestServicePtr service) 91 explicit TestClient(TestServicePtr service)
84 : service_(service.Pass()), quit_after_ack_(false) {} 92 : service_(service.Pass()), quit_after_ack_(false) {}
85 93
86 void AckTest() { 94 void AckTest() {
87 if (quit_after_ack_) 95 if (quit_after_ack_)
88 base::MessageLoop::current()->Quit(); 96 base::MessageLoop::current()->Quit();
89 } 97 }
90 98
91 void Test(const std::string& test_string) { 99 void Test(const std::string& test_string) {
92 quit_after_ack_ = true; 100 quit_after_ack_ = true;
93 service_->Test(test_string, 101 service_->Test(test_string,
94 base::Bind(&TestClient::AckTest, base::Unretained(this))); 102 base::Bind(&TestClient::AckTest, base::Unretained(this)));
95 } 103 }
96 104
97 private: 105 private:
98 TestServicePtr service_; 106 TestServicePtr service_;
99 bool quit_after_ack_; 107 bool quit_after_ack_;
100 DISALLOW_COPY_AND_ASSIGN(TestClient); 108 DISALLOW_COPY_AND_ASSIGN(TestClient);
101 }; 109 };
102 110
103 class TestApplicationLoader : public ApplicationLoader, 111 class TestApplicationLoader : public ApplicationLoader,
104 public mojo::ApplicationDelegate, 112 public ApplicationDelegate,
105 public mojo::InterfaceFactory<TestService> { 113 public InterfaceFactory<TestService> {
106 public: 114 public:
107 TestApplicationLoader() : context_(nullptr), num_loads_(0) {} 115 TestApplicationLoader() : context_(nullptr), num_loads_(0) {}
108 116
109 ~TestApplicationLoader() override { 117 ~TestApplicationLoader() override {
110 if (context_) 118 if (context_)
111 ++context_->num_loader_deletes; 119 ++context_->num_loader_deletes;
112 test_app_.reset(); 120 test_app_.reset();
113 } 121 }
114 122
115 void set_context(TestContext* context) { context_ = context; } 123 void set_context(TestContext* context) { context_ = context; }
116 int num_loads() const { return num_loads_; } 124 int num_loads() const { return num_loads_; }
117 const std::vector<std::string>& GetArgs() const { return test_app_->args(); } 125 const std::vector<std::string>& GetArgs() const { return test_app_->args(); }
118 126
119 private: 127 private:
120 // ApplicationLoader implementation. 128 // ApplicationLoader implementation.
121 void Load( 129 void Load(const GURL& url,
122 const GURL& url, 130 InterfaceRequest<Application> application_request) override {
123 mojo::InterfaceRequest<mojo::Application> application_request) override {
124 ++num_loads_; 131 ++num_loads_;
125 test_app_.reset( 132 test_app_.reset(new ApplicationImpl(this, application_request.Pass()));
126 new mojo::ApplicationImpl(this, application_request.Pass()));
127 } 133 }
128 134
129 // mojo::ApplicationDelegate implementation. 135 // ApplicationDelegate implementation.
130 bool ConfigureIncomingConnection( 136 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
131 mojo::ApplicationConnection* connection) override {
132 connection->AddService(this); 137 connection->AddService(this);
133 return true; 138 return true;
134 } 139 }
135 140
136 // mojo::InterfaceFactory implementation. 141 // InterfaceFactory implementation.
137 void Create(mojo::ApplicationConnection* connection, 142 void Create(ApplicationConnection* connection,
138 mojo::InterfaceRequest<TestService> request) override { 143 InterfaceRequest<TestService> request) override {
139 new TestServiceImpl(context_, request.Pass()); 144 new TestServiceImpl(context_, request.Pass());
140 } 145 }
141 146
142 scoped_ptr<mojo::ApplicationImpl> test_app_; 147 scoped_ptr<ApplicationImpl> test_app_;
143 TestContext* context_; 148 TestContext* context_;
144 int num_loads_; 149 int num_loads_;
145 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); 150 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader);
146 }; 151 };
147 152
148 class ClosingApplicationLoader : public ApplicationLoader { 153 class ClosingApplicationLoader : public ApplicationLoader {
149 private: 154 private:
150 // ApplicationLoader implementation. 155 // ApplicationLoader implementation.
151 void Load( 156 void Load(const GURL& url,
152 const GURL& url, 157 InterfaceRequest<Application> application_request) override {}
153 mojo::InterfaceRequest<mojo::Application> application_request) override {}
154 }; 158 };
155 159
156 class TesterContext { 160 class TesterContext {
157 public: 161 public:
158 explicit TesterContext(base::MessageLoop* loop) 162 explicit TesterContext(base::MessageLoop* loop)
159 : num_b_calls_(0), 163 : num_b_calls_(0),
160 num_c_calls_(0), 164 num_c_calls_(0),
161 num_a_deletes_(0), 165 num_a_deletes_(0),
162 num_b_deletes_(0), 166 num_b_deletes_(0),
163 num_c_deletes_(0), 167 num_c_deletes_(0),
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 int num_c_deletes_; 248 int num_c_deletes_;
245 bool tester_called_quit_; 249 bool tester_called_quit_;
246 bool a_called_quit_; 250 bool a_called_quit_;
247 251
248 base::MessageLoop* loop_; 252 base::MessageLoop* loop_;
249 }; 253 };
250 254
251 // Used to test that the requestor url will be correctly passed. 255 // Used to test that the requestor url will be correctly passed.
252 class TestAImpl : public TestA { 256 class TestAImpl : public TestA {
253 public: 257 public:
254 TestAImpl(mojo::ApplicationImpl* app_impl, 258 TestAImpl(ApplicationImpl* app_impl,
255 TesterContext* test_context, 259 TesterContext* test_context,
256 mojo::InterfaceRequest<TestA> request) 260 InterfaceRequest<TestA> request)
257 : test_context_(test_context), binding_(this, request.Pass()) { 261 : test_context_(test_context), binding_(this, request.Pass()) {
258 app_impl->ConnectToApplication(kTestBURLString)->ConnectToService(&b_); 262 app_impl->ConnectToApplication(kTestBURLString)->ConnectToService(&b_);
259 } 263 }
260 264
261 ~TestAImpl() override { 265 ~TestAImpl() override {
262 test_context_->IncrementNumADeletes(); 266 test_context_->IncrementNumADeletes();
263 if (base::MessageLoop::current()->is_running()) 267 if (base::MessageLoop::current()->is_running())
264 Quit(); 268 Quit();
265 } 269 }
266 270
267 private: 271 private:
268 void CallB() override { 272 void CallB() override {
269 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this))); 273 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this)));
270 } 274 }
271 275
272 void CallCFromB() override { 276 void CallCFromB() override {
273 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this))); 277 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this)));
274 } 278 }
275 279
276 void Quit() { 280 void Quit() {
277 base::MessageLoop::current()->Quit(); 281 base::MessageLoop::current()->Quit();
278 test_context_->set_a_called_quit(); 282 test_context_->set_a_called_quit();
279 test_context_->QuitSoon(); 283 test_context_->QuitSoon();
280 } 284 }
281 285
282 TesterContext* test_context_; 286 TesterContext* test_context_;
283 TestBPtr b_; 287 TestBPtr b_;
284 mojo::StrongBinding<TestA> binding_; 288 StrongBinding<TestA> binding_;
285 }; 289 };
286 290
287 class TestBImpl : public TestB { 291 class TestBImpl : public TestB {
288 public: 292 public:
289 TestBImpl(mojo::ApplicationConnection* connection, 293 TestBImpl(ApplicationConnection* connection,
290 TesterContext* test_context, 294 TesterContext* test_context,
291 mojo::InterfaceRequest<TestB> request) 295 InterfaceRequest<TestB> request)
292 : test_context_(test_context), binding_(this, request.Pass()) { 296 : test_context_(test_context), binding_(this, request.Pass()) {
293 connection->ConnectToService(&c_); 297 connection->ConnectToService(&c_);
294 } 298 }
295 299
296 ~TestBImpl() override { 300 ~TestBImpl() override {
297 test_context_->IncrementNumBDeletes(); 301 test_context_->IncrementNumBDeletes();
298 if (base::MessageLoop::current()->is_running()) 302 if (base::MessageLoop::current()->is_running())
299 base::MessageLoop::current()->Quit(); 303 base::MessageLoop::current()->Quit();
300 test_context_->QuitSoon(); 304 test_context_->QuitSoon();
301 } 305 }
302 306
303 private: 307 private:
304 void B(const mojo::Callback<void()>& callback) override { 308 void B(const Callback<void()>& callback) override {
305 test_context_->IncrementNumBCalls(); 309 test_context_->IncrementNumBCalls();
306 callback.Run(); 310 callback.Run();
307 } 311 }
308 312
309 void CallC(const mojo::Callback<void()>& callback) override { 313 void CallC(const Callback<void()>& callback) override {
310 test_context_->IncrementNumBCalls(); 314 test_context_->IncrementNumBCalls();
311 c_->C(callback); 315 c_->C(callback);
312 } 316 }
313 317
314 TesterContext* test_context_; 318 TesterContext* test_context_;
315 TestCPtr c_; 319 TestCPtr c_;
316 mojo::StrongBinding<TestB> binding_; 320 StrongBinding<TestB> binding_;
317 }; 321 };
318 322
319 class TestCImpl : public TestC { 323 class TestCImpl : public TestC {
320 public: 324 public:
321 TestCImpl(mojo::ApplicationConnection* connection, 325 TestCImpl(ApplicationConnection* connection,
322 TesterContext* test_context, 326 TesterContext* test_context,
323 mojo::InterfaceRequest<TestC> request) 327 InterfaceRequest<TestC> request)
324 : test_context_(test_context), binding_(this, request.Pass()) {} 328 : test_context_(test_context), binding_(this, request.Pass()) {}
325 329
326 ~TestCImpl() override { test_context_->IncrementNumCDeletes(); } 330 ~TestCImpl() override { test_context_->IncrementNumCDeletes(); }
327 331
328 private: 332 private:
329 void C(const mojo::Callback<void()>& callback) override { 333 void C(const Callback<void()>& callback) override {
330 test_context_->IncrementNumCCalls(); 334 test_context_->IncrementNumCCalls();
331 callback.Run(); 335 callback.Run();
332 } 336 }
333 337
334 TesterContext* test_context_; 338 TesterContext* test_context_;
335 mojo::StrongBinding<TestC> binding_; 339 StrongBinding<TestC> binding_;
336 }; 340 };
337 341
338 class Tester : public mojo::ApplicationDelegate, 342 class Tester : public ApplicationDelegate,
339 public ApplicationLoader, 343 public ApplicationLoader,
340 public mojo::InterfaceFactory<TestA>, 344 public InterfaceFactory<TestA>,
341 public mojo::InterfaceFactory<TestB>, 345 public InterfaceFactory<TestB>,
342 public mojo::InterfaceFactory<TestC> { 346 public InterfaceFactory<TestC> {
343 public: 347 public:
344 Tester(TesterContext* context, const std::string& requestor_url) 348 Tester(TesterContext* context, const std::string& requestor_url)
345 : context_(context), requestor_url_(requestor_url) {} 349 : context_(context), requestor_url_(requestor_url) {}
346 ~Tester() override {} 350 ~Tester() override {}
347 351
348 private: 352 private:
349 void Load( 353 void Load(const GURL& url,
350 const GURL& url, 354 InterfaceRequest<Application> application_request) override {
351 mojo::InterfaceRequest<mojo::Application> application_request) override { 355 app_.reset(new ApplicationImpl(this, application_request.Pass()));
352 app_.reset(new mojo::ApplicationImpl(this, application_request.Pass()));
353 } 356 }
354 357
355 bool ConfigureIncomingConnection( 358 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
356 mojo::ApplicationConnection* connection) override {
357 if (!requestor_url_.empty() && 359 if (!requestor_url_.empty() &&
358 requestor_url_ != connection->GetRemoteApplicationURL()) { 360 requestor_url_ != connection->GetRemoteApplicationURL()) {
359 context_->set_tester_called_quit(); 361 context_->set_tester_called_quit();
360 context_->QuitSoon(); 362 context_->QuitSoon();
361 base::MessageLoop::current()->Quit(); 363 base::MessageLoop::current()->Quit();
362 return false; 364 return false;
363 } 365 }
364 // If we're coming from A, then add B, otherwise A. 366 // If we're coming from A, then add B, otherwise A.
365 if (connection->GetRemoteApplicationURL() == kTestAURLString) 367 if (connection->GetRemoteApplicationURL() == kTestAURLString)
366 connection->AddService<TestB>(this); 368 connection->AddService<TestB>(this);
367 else 369 else
368 connection->AddService<TestA>(this); 370 connection->AddService<TestA>(this);
369 return true; 371 return true;
370 } 372 }
371 373
372 bool ConfigureOutgoingConnection( 374 bool ConfigureOutgoingConnection(ApplicationConnection* connection) override {
373 mojo::ApplicationConnection* connection) override {
374 // If we're connecting to B, then add C. 375 // If we're connecting to B, then add C.
375 if (connection->GetRemoteApplicationURL() == kTestBURLString) 376 if (connection->GetRemoteApplicationURL() == kTestBURLString)
376 connection->AddService<TestC>(this); 377 connection->AddService<TestC>(this);
377 return true; 378 return true;
378 } 379 }
379 380
380 void Create(mojo::ApplicationConnection* connection, 381 void Create(ApplicationConnection* connection,
381 mojo::InterfaceRequest<TestA> request) override { 382 InterfaceRequest<TestA> request) override {
382 a_bindings_.push_back(new TestAImpl(app_.get(), context_, request.Pass())); 383 a_bindings_.push_back(new TestAImpl(app_.get(), context_, request.Pass()));
383 } 384 }
384 385
385 void Create(mojo::ApplicationConnection* connection, 386 void Create(ApplicationConnection* connection,
386 mojo::InterfaceRequest<TestB> request) override { 387 InterfaceRequest<TestB> request) override {
387 new TestBImpl(connection, context_, request.Pass()); 388 new TestBImpl(connection, context_, request.Pass());
388 } 389 }
389 390
390 void Create(mojo::ApplicationConnection* connection, 391 void Create(ApplicationConnection* connection,
391 mojo::InterfaceRequest<TestC> request) override { 392 InterfaceRequest<TestC> request) override {
392 new TestCImpl(connection, context_, request.Pass()); 393 new TestCImpl(connection, context_, request.Pass());
393 } 394 }
394 395
395 TesterContext* context_; 396 TesterContext* context_;
396 scoped_ptr<mojo::ApplicationImpl> app_; 397 scoped_ptr<ApplicationImpl> app_;
397 std::string requestor_url_; 398 std::string requestor_url_;
398 ScopedVector<TestAImpl> a_bindings_; 399 ScopedVector<TestAImpl> a_bindings_;
399 }; 400 };
400 401
401 class TestDelegate : public ApplicationManager::Delegate { 402 class TestDelegate : public ApplicationManager::Delegate {
402 public: 403 public:
403 void AddMapping(const GURL& from, const GURL& to) { mappings_[from] = to; } 404 void AddMapping(const GURL& from, const GURL& to) { mappings_[from] = to; }
404 405
405 // ApplicationManager::Delegate 406 // ApplicationManager::Delegate
406 GURL ResolveMappings(const GURL& url) override { 407 GURL ResolveMappings(const GURL& url) override {
(...skipping 12 matching lines...) Expand all
419 replacements.SetScheme("file", url::Component(0, 4)); 420 replacements.SetScheme("file", url::Component(0, 4));
420 mapped_url = mapped_url.ReplaceComponents(replacements); 421 mapped_url = mapped_url.ReplaceComponents(replacements);
421 } 422 }
422 return mapped_url; 423 return mapped_url;
423 } 424 }
424 425
425 private: 426 private:
426 std::map<GURL, GURL> mappings_; 427 std::map<GURL, GURL> mappings_;
427 }; 428 };
428 429
429 class TestExternal : public mojo::ApplicationDelegate { 430 class TestExternal : public ApplicationDelegate {
430 public: 431 public:
431 TestExternal() : configure_incoming_connection_called_(false) {} 432 TestExternal() : configure_incoming_connection_called_(false) {}
432 433
433 void Initialize(mojo::ApplicationImpl* app) override { 434 void Initialize(ApplicationImpl* app) override {
434 initialize_args_ = app->args(); 435 initialize_args_ = app->args();
435 base::MessageLoop::current()->Quit(); 436 base::MessageLoop::current()->Quit();
436 } 437 }
437 438
438 bool ConfigureIncomingConnection( 439 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
439 mojo::ApplicationConnection* connection) override {
440 configure_incoming_connection_called_ = true; 440 configure_incoming_connection_called_ = true;
441 base::MessageLoop::current()->Quit(); 441 base::MessageLoop::current()->Quit();
442 return true; 442 return true;
443 } 443 }
444 444
445 const std::vector<std::string>& initialize_args() const { 445 const std::vector<std::string>& initialize_args() const {
446 return initialize_args_; 446 return initialize_args_;
447 } 447 }
448 448
449 bool configure_incoming_connection_called() const { 449 bool configure_incoming_connection_called() const {
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
811 bool called = false; 811 bool called = false;
812 application_manager_->ConnectToApplication( 812 application_manager_->ConnectToApplication(
813 GURL("test:test"), GURL(), nullptr, nullptr, 813 GURL("test:test"), GURL(), nullptr, nullptr,
814 base::Bind(&QuitClosure, base::Unretained(&called))); 814 base::Bind(&QuitClosure, base::Unretained(&called)));
815 loop_.Run(); 815 loop_.Run();
816 EXPECT_TRUE(called); 816 EXPECT_TRUE(called);
817 } 817 }
818 818
819 } // namespace 819 } // namespace
820 } // namespace shell 820 } // namespace shell
OLDNEW
« no previous file with comments | « shell/application_manager/application_manager.cc ('k') | shell/application_manager/shell_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698