| OLD | NEW |
| 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 <utility> | 5 #include <utility> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "mojo/public/cpp/bindings/binding.h" | 9 #include "mojo/public/cpp/bindings/binding.h" |
| 10 #include "mojo/public/cpp/environment/environment.h" | 10 #include "mojo/public/cpp/environment/environment.h" |
| 11 #include "mojo/public/cpp/system/message_pipe.h" | 11 #include "mojo/public/cpp/system/message_pipe.h" |
| 12 #include "mojo/shell/public/cpp/application_impl.h" | |
| 13 #include "mojo/shell/public/cpp/application_test_base.h" | 12 #include "mojo/shell/public/cpp/application_test_base.h" |
| 14 #include "mojo/shell/public/interfaces/application.mojom.h" | 13 #include "mojo/shell/public/cpp/shell_connection.h" |
| 14 #include "mojo/shell/public/interfaces/shell_client.mojom.h" |
| 15 | 15 |
| 16 namespace mojo { | 16 namespace mojo { |
| 17 namespace test { | 17 namespace test { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 // Share the application URL with multiple application tests. | 20 // Share the application URL with multiple application tests. |
| 21 String g_url; | 21 String g_url; |
| 22 uint32_t g_id = shell::mojom::Shell::kInvalidApplicationID; | 22 uint32_t g_id = shell::mojom::Shell::kInvalidApplicationID; |
| 23 | 23 |
| 24 // Application request handle passed from the shell in MojoMain, stored in | 24 // ShellClient request handle passed from the shell in MojoMain, stored in |
| 25 // between SetUp()/TearDown() so we can (re-)intialize new ApplicationImpls. | 25 // between SetUp()/TearDown() so we can (re-)intialize new ShellConnections. |
| 26 InterfaceRequest<shell::mojom::Application> g_application_request; | 26 InterfaceRequest<shell::mojom::ShellClient> g_shell_client_request; |
| 27 | 27 |
| 28 // Shell pointer passed in the initial mojo.Application.Initialize() call, | 28 // Shell pointer passed in the initial mojo.ShellClient.Initialize() call, |
| 29 // stored in between initial setup and the first test and between SetUp/TearDown | 29 // stored in between initial setup and the first test and between SetUp/TearDown |
| 30 // calls so we can (re-)initialize new ApplicationImpls. | 30 // calls so we can (re-)initialize new ShellConnections. |
| 31 shell::mojom::ShellPtr g_shell; | 31 shell::mojom::ShellPtr g_shell; |
| 32 | 32 |
| 33 class ShellGrabber : public shell::mojom::Application { | 33 class ShellGrabber : public shell::mojom::ShellClient { |
| 34 public: | 34 public: |
| 35 explicit ShellGrabber( | 35 explicit ShellGrabber(InterfaceRequest<shell::mojom::ShellClient> request) |
| 36 InterfaceRequest<shell::mojom::Application> application_request) | 36 : binding_(this, std::move(request)) {} |
| 37 : binding_(this, std::move(application_request)) {} | |
| 38 | 37 |
| 39 void WaitForInitialize() { | 38 void WaitForInitialize() { |
| 40 // Initialize is always the first call made on Application. | 39 // Initialize is always the first call made on ShellClient. |
| 41 MOJO_CHECK(binding_.WaitForIncomingMethodCall()); | 40 MOJO_CHECK(binding_.WaitForIncomingMethodCall()); |
| 42 } | 41 } |
| 43 | 42 |
| 44 private: | 43 private: |
| 45 // Application implementation. | 44 // shell::mojom::ShellClient implementation. |
| 46 void Initialize(shell::mojom::ShellPtr shell, | 45 void Initialize(shell::mojom::ShellPtr shell, |
| 47 const mojo::String& url, | 46 const mojo::String& url, |
| 48 uint32_t id) override { | 47 uint32_t id) override { |
| 49 g_url = url; | 48 g_url = url; |
| 50 g_id = id; | 49 g_id = id; |
| 51 g_application_request = binding_.Unbind(); | 50 g_shell_client_request = binding_.Unbind(); |
| 52 g_shell = std::move(shell); | 51 g_shell = std::move(shell); |
| 53 } | 52 } |
| 54 | 53 |
| 55 void AcceptConnection(const String& requestor_url, | 54 void AcceptConnection(const String& requestor_url, |
| 56 uint32_t requestor_id, | 55 uint32_t requestor_id, |
| 57 InterfaceRequest<ServiceProvider> services, | 56 InterfaceRequest<ServiceProvider> services, |
| 58 ServiceProviderPtr exposed_services, | 57 ServiceProviderPtr exposed_services, |
| 59 Array<String> allowed_interfaces, | 58 Array<String> allowed_interfaces, |
| 60 const String& url) override { | 59 const String& url) override { |
| 61 MOJO_CHECK(false); | 60 MOJO_CHECK(false); |
| 62 } | 61 } |
| 63 | 62 |
| 64 void OnQuitRequested(const Callback<void(bool)>& callback) override { | 63 void OnQuitRequested(const Callback<void(bool)>& callback) override { |
| 65 MOJO_CHECK(false); | 64 MOJO_CHECK(false); |
| 66 } | 65 } |
| 67 | 66 |
| 68 Binding<Application> binding_; | 67 Binding<ShellClient> binding_; |
| 69 }; | 68 }; |
| 70 | 69 |
| 71 } // namespace | 70 } // namespace |
| 72 | 71 |
| 73 MojoResult RunAllTests(MojoHandle application_request_handle) { | 72 MojoResult RunAllTests(MojoHandle shell_client_request_handle) { |
| 74 { | 73 { |
| 75 // This loop is used for init, and then destroyed before running tests. | 74 // This loop is used for init, and then destroyed before running tests. |
| 76 Environment::InstantiateDefaultRunLoop(); | 75 Environment::InstantiateDefaultRunLoop(); |
| 77 | 76 |
| 78 // Grab the shell handle. | 77 // Grab the shell handle. |
| 79 ShellGrabber grabber( | 78 ShellGrabber grabber( |
| 80 MakeRequest<shell::mojom::Application>(MakeScopedHandle( | 79 MakeRequest<shell::mojom::ShellClient>(MakeScopedHandle( |
| 81 MessagePipeHandle(application_request_handle)))); | 80 MessagePipeHandle(shell_client_request_handle)))); |
| 82 grabber.WaitForInitialize(); | 81 grabber.WaitForInitialize(); |
| 83 MOJO_CHECK(g_shell); | 82 MOJO_CHECK(g_shell); |
| 84 MOJO_CHECK(g_application_request.is_pending()); | 83 MOJO_CHECK(g_shell_client_request.is_pending()); |
| 85 | 84 |
| 86 int argc = 0; | 85 int argc = 0; |
| 87 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 86 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 88 const char** argv = new const char* [cmd_line->argv().size() + 1]; | 87 const char** argv = new const char* [cmd_line->argv().size() + 1]; |
| 89 #if defined(OS_WIN) | 88 #if defined(OS_WIN) |
| 90 std::vector<std::string> local_strings; | 89 std::vector<std::string> local_strings; |
| 91 #endif | 90 #endif |
| 92 for (auto& arg : cmd_line->argv()) { | 91 for (auto& arg : cmd_line->argv()) { |
| 93 #if defined(OS_WIN) | 92 #if defined(OS_WIN) |
| 94 local_strings.push_back(base::WideToUTF8(arg)); | 93 local_strings.push_back(base::WideToUTF8(arg)); |
| 95 argv[argc++] = local_strings.back().c_str(); | 94 argv[argc++] = local_strings.back().c_str(); |
| 96 #else | 95 #else |
| 97 argv[argc++] = arg.c_str(); | 96 argv[argc++] = arg.c_str(); |
| 98 #endif | 97 #endif |
| 99 } | 98 } |
| 100 argv[argc] = nullptr; | 99 argv[argc] = nullptr; |
| 101 | 100 |
| 102 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0]))); | 101 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0]))); |
| 103 | 102 |
| 104 Environment::DestroyDefaultRunLoop(); | 103 Environment::DestroyDefaultRunLoop(); |
| 105 } | 104 } |
| 106 | 105 |
| 107 int result = RUN_ALL_TESTS(); | 106 int result = RUN_ALL_TESTS(); |
| 108 | 107 |
| 109 // Shut down our message pipes before exiting. | 108 // Shut down our message pipes before exiting. |
| 110 (void)g_application_request.PassMessagePipe(); | 109 (void)g_shell_client_request.PassMessagePipe(); |
| 111 g_shell.reset(); | 110 g_shell.reset(); |
| 112 | 111 |
| 113 return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN; | 112 return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN; |
| 114 } | 113 } |
| 115 | 114 |
| 116 TestHelper::TestHelper(ShellClient* client) | 115 TestHelper::TestHelper(ShellClient* client) |
| 117 : application_impl_(new ApplicationImpl( | 116 : shell_connection_(new ShellConnection( |
| 118 client == nullptr ? &default_shell_client_ : client, | 117 client == nullptr ? &default_shell_client_ : client, |
| 119 std::move(g_application_request))), | 118 std::move(g_shell_client_request))), |
| 120 url_(g_url) { | 119 url_(g_url) { |
| 121 // Fake application initialization. | 120 // Fake ShellClient initialization. |
| 122 shell::mojom::Application* application = application_impl_.get(); | 121 shell::mojom::ShellClient* shell_client = shell_connection_.get(); |
| 123 application->Initialize(std::move(g_shell), g_url, g_id); | 122 shell_client->Initialize(std::move(g_shell), g_url, g_id); |
| 124 } | 123 } |
| 125 | 124 |
| 126 TestHelper::~TestHelper() { | 125 TestHelper::~TestHelper() { |
| 127 // TODO: commented out until http://crbug.com/533107 is solved. | 126 // TODO: commented out until http://crbug.com/533107 is solved. |
| 128 // { | 127 // { |
| 129 // ApplicationImpl::TestApi test_api(application_impl_); | 128 // ShellConnection::TestApi test_api(shell_connection_); |
| 130 // test_api.UnbindConnections(&g_application_request, &g_shell); | 129 // test_api.UnbindConnections(&g_shell_client_request, &g_shell); |
| 131 // } | 130 // } |
| 132 // We may have supplied a member as the client. Delete |application_impl_| | 131 // We may have supplied a member as the client. Delete |shell_connection_| |
| 133 // while still valid. | 132 // while still valid. |
| 134 application_impl_.reset(); | 133 shell_connection_.reset(); |
| 135 } | 134 } |
| 136 | 135 |
| 137 ApplicationTestBase::ApplicationTestBase() : test_helper_(nullptr) {} | 136 ApplicationTestBase::ApplicationTestBase() : test_helper_(nullptr) {} |
| 138 | 137 |
| 139 ApplicationTestBase::~ApplicationTestBase() { | 138 ApplicationTestBase::~ApplicationTestBase() { |
| 140 } | 139 } |
| 141 | 140 |
| 142 ShellClient* ApplicationTestBase::GetShellClient() { | 141 ShellClient* ApplicationTestBase::GetShellClient() { |
| 143 return nullptr; | 142 return nullptr; |
| 144 } | 143 } |
| 145 | 144 |
| 146 void ApplicationTestBase::SetUp() { | 145 void ApplicationTestBase::SetUp() { |
| 147 // A run loop is recommended for ApplicationImpl initialization and | 146 // A run loop is recommended for ShellConnection initialization and |
| 148 // communication. | 147 // communication. |
| 149 if (ShouldCreateDefaultRunLoop()) | 148 if (ShouldCreateDefaultRunLoop()) |
| 150 Environment::InstantiateDefaultRunLoop(); | 149 Environment::InstantiateDefaultRunLoop(); |
| 151 | 150 |
| 152 MOJO_CHECK(g_application_request.is_pending()); | 151 MOJO_CHECK(g_shell_client_request.is_pending()); |
| 153 MOJO_CHECK(g_shell); | 152 MOJO_CHECK(g_shell); |
| 154 | 153 |
| 155 // New applications are constructed for each test to avoid persisting state. | 154 // New applications are constructed for each test to avoid persisting state. |
| 156 test_helper_.reset(new TestHelper(GetShellClient())); | 155 test_helper_.reset(new TestHelper(GetShellClient())); |
| 157 } | 156 } |
| 158 | 157 |
| 159 void ApplicationTestBase::TearDown() { | 158 void ApplicationTestBase::TearDown() { |
| 160 MOJO_CHECK(!g_application_request.is_pending()); | 159 MOJO_CHECK(!g_shell_client_request.is_pending()); |
| 161 MOJO_CHECK(!g_shell); | 160 MOJO_CHECK(!g_shell); |
| 162 | 161 |
| 163 test_helper_.reset(); | 162 test_helper_.reset(); |
| 164 | 163 |
| 165 if (ShouldCreateDefaultRunLoop()) | 164 if (ShouldCreateDefaultRunLoop()) |
| 166 Environment::DestroyDefaultRunLoop(); | 165 Environment::DestroyDefaultRunLoop(); |
| 167 } | 166 } |
| 168 | 167 |
| 169 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() { | 168 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() { |
| 170 return true; | 169 return true; |
| 171 } | 170 } |
| 172 | 171 |
| 173 } // namespace test | 172 } // namespace test |
| 174 } // namespace mojo | 173 } // namespace mojo |
| OLD | NEW |