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 "base/bind.h" |
| 6 #include "base/logging.h" |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "base/time/time.h" |
| 12 #include "content/public/browser/mojo_app_connection.h" |
| 13 #include "content/public/browser/mojo_shell_context.h" |
| 14 #include "content/public/common/application_registry.h" |
| 15 #include "content/public/common/content_client.h" |
| 16 #include "content/public/common/static_mojo_application_loader.h" |
| 17 #include "content/public/test/content_browser_test.h" |
| 18 #include "content/public/test/test_utils.h" |
| 19 #include "content/test/test_content_utility_client.h" |
| 20 #include "content/test/test_service.mojom.h" |
| 21 #include "mojo/application/public/cpp/application_connection.h" |
| 22 #include "mojo/application/public/cpp/application_delegate.h" |
| 23 #include "mojo/application/public/cpp/application_impl.h" |
| 24 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" |
| 25 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" |
| 26 |
| 27 namespace content { |
| 28 |
| 29 // Simple TestApp which provides a TestService impl. The app terminates itself |
| 30 // after its TestService fulfills a single DoSomething call. |
| 31 class TestApp : public mojo::ApplicationDelegate, |
| 32 public mojo::InterfaceFactory<TestService>, |
| 33 public TestService { |
| 34 public: |
| 35 TestApp() : service_binding_(this), app_(nullptr) {} |
| 36 |
| 37 ~TestApp() override {} |
| 38 |
| 39 private: |
| 40 // mojo::ApplicationDelegate: |
| 41 void Initialize(mojo::ApplicationImpl* app) override { |
| 42 app_ = app; |
| 43 } |
| 44 |
| 45 bool ConfigureIncomingConnection( |
| 46 mojo::ApplicationConnection* connection) override { |
| 47 connection->AddService<TestService>(this); |
| 48 return true; |
| 49 } |
| 50 |
| 51 // mojo::InterfaceFactory<TestService>: |
| 52 void Create(mojo::ApplicationConnection* connection, |
| 53 mojo::InterfaceRequest<TestService> request) override { |
| 54 DCHECK(!service_binding_.is_bound()); |
| 55 service_binding_.Bind(request.Pass()); |
| 56 } |
| 57 |
| 58 // TestService: |
| 59 void DoSomething(const DoSomethingCallback& callback) override { |
| 60 callback.Run(); |
| 61 DCHECK(app_); |
| 62 app_->Terminate(); |
| 63 } |
| 64 |
| 65 mojo::Binding<TestService> service_binding_; |
| 66 |
| 67 // Not owned. |
| 68 mojo::ApplicationImpl* app_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(TestApp); |
| 71 }; |
| 72 |
| 73 class MojoShellTestUtilityClient : public TestContentUtilityClient { |
| 74 public: |
| 75 MojoShellTestUtilityClient( |
| 76 const base::Callback<void(ApplicationRegistry*)>& registration_callback) |
| 77 : registration_callback_(registration_callback) {} |
| 78 |
| 79 // ContentUtilityClient: |
| 80 void RegisterMojoApplications(ApplicationRegistry* registry) override { |
| 81 registration_callback_.Run(registry); |
| 82 } |
| 83 |
| 84 private: |
| 85 base::Callback<void(ApplicationRegistry*)> registration_callback_; |
| 86 }; |
| 87 |
| 88 class MojoShellTest : public ContentBrowserTest { |
| 89 public: |
| 90 MojoShellTest() |
| 91 : utility_client_(new MojoShellTestUtilityClient( |
| 92 base::Bind(&MojoShellTest::SetUpUtilityApplications, |
| 93 base::Unretained(this)))) { |
| 94 MojoShellContext::SetApplicationRegistryInitializerForTesting( |
| 95 base::Bind(&MojoShellTest::SetUpBrowserApplications, |
| 96 base::Unretained(this))); |
| 97 } |
| 98 |
| 99 protected: |
| 100 virtual void SetUpBrowserApplications(ApplicationRegistry* registry) { |
| 101 registry->RegisterStaticAppForURL( |
| 102 GURL("https://test.example.com/in-browser"), |
| 103 base::Bind(&CreateTestApp)); |
| 104 } |
| 105 |
| 106 virtual void SetUpUtilityApplications(ApplicationRegistry* registry) { |
| 107 registry->RegisterStaticAppForURL( |
| 108 GURL("https://test.example.com/in-utility"), |
| 109 base::Bind(&CreateTestApp)); |
| 110 } |
| 111 |
| 112 static void ValidateResult(int32_t expected_value, |
| 113 const base::Closure& callback, |
| 114 int32_t value) { |
| 115 EXPECT_EQ(expected_value, value); |
| 116 callback.Run(); |
| 117 } |
| 118 |
| 119 private: |
| 120 void SetUpOnMainThread() override { |
| 121 ContentBrowserTest::SetUpOnMainThread(); |
| 122 SetUtilityClientForTesting(utility_client_.get()); |
| 123 in_process_utility_thread_helper_.reset(new InProcessUtilityThreadHelper); |
| 124 } |
| 125 |
| 126 void TearDownOnMainThread() override { |
| 127 in_process_utility_thread_helper_.reset(); |
| 128 } |
| 129 |
| 130 static scoped_ptr<mojo::ApplicationDelegate> CreateTestApp() { |
| 131 return scoped_ptr<mojo::ApplicationDelegate>(new TestApp); |
| 132 } |
| 133 |
| 134 scoped_ptr<InProcessUtilityThreadHelper> in_process_utility_thread_helper_; |
| 135 scoped_ptr<MojoShellTestUtilityClient> utility_client_; |
| 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(MojoShellTest); |
| 138 }; |
| 139 |
| 140 IN_PROC_BROWSER_TEST_F(MojoShellTest, TestBrowserConnection) { |
| 141 auto test_app = MojoAppConnection::Create( |
| 142 GURL("https://test.example.com/in-browser")); |
| 143 TestServicePtr test_service; |
| 144 test_app->ConnectToService(&test_service); |
| 145 |
| 146 base::RunLoop run_loop; |
| 147 test_service->DoSomething(run_loop.QuitClosure()); |
| 148 run_loop.Run(); |
| 149 } |
| 150 |
| 151 IN_PROC_BROWSER_TEST_F(MojoShellTest, TestUtilityConnection) { |
| 152 auto test_app = MojoAppConnection::Create( |
| 153 GURL("https://test.example.com/in-utility")); |
| 154 TestServicePtr test_service; |
| 155 test_app->ConnectToService(&test_service); |
| 156 |
| 157 base::RunLoop run_loop; |
| 158 test_service->DoSomething(run_loop.QuitClosure()); |
| 159 run_loop.Run(); |
| 160 } |
| 161 |
| 162 } // namespace content |
OLD | NEW |