| 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 "mojo/public/cpp/application/application_test_base.h" | 5 #include "mojo/application/application_test_base_chromium.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 7 #include "mojo/public/cpp/application/application_impl.h" | 9 #include "mojo/public/cpp/application/application_impl.h" |
| 8 #include "mojo/public/cpp/bindings/binding.h" | 10 #include "mojo/public/cpp/bindings/binding.h" |
| 9 #include "mojo/public/cpp/environment/environment.h" | 11 #include "mojo/public/cpp/environment/environment.h" |
| 10 #include "mojo/public/cpp/system/message_pipe.h" | 12 #include "mojo/public/cpp/system/message_pipe.h" |
| 11 #include "mojo/public/interfaces/application/application.mojom.h" | 13 #include "mojo/public/interfaces/application/application.mojom.h" |
| 12 | 14 |
| 13 namespace mojo { | 15 namespace mojo { |
| 14 namespace test { | 16 namespace test { |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 // Share the application command-line arguments with multiple application tests. | |
| 18 Array<String> g_args; | |
| 19 | |
| 20 // Share the application URL with multiple application tests. | 19 // Share the application URL with multiple application tests. |
| 21 String g_url; | 20 String g_url; |
| 22 | 21 |
| 23 // Application request handle passed from the shell in MojoMain, stored in | 22 // Application request handle passed from the shell in MojoMain, stored in |
| 24 // between SetUp()/TearDown() so we can (re-)intialize new ApplicationImpls. | 23 // between SetUp()/TearDown() so we can (re-)intialize new ApplicationImpls. |
| 25 InterfaceRequest<Application> g_application_request; | 24 InterfaceRequest<Application> g_application_request; |
| 26 | 25 |
| 27 // Shell pointer passed in the initial mojo.Application.Initialize() call, | 26 // Shell pointer passed in the initial mojo.Application.Initialize() call, |
| 28 // stored in between initial setup and the first test and between SetUp/TearDown | 27 // stored in between initial setup and the first test and between SetUp/TearDown |
| 29 // calls so we can (re-)initialize new ApplicationImpls. | 28 // calls so we can (re-)initialize new ApplicationImpls. |
| 30 ShellPtr g_shell; | 29 ShellPtr g_shell; |
| 31 | 30 |
| 32 void InitializeArgs(int argc, std::vector<const char*> argv) { | 31 class ShellGrabber : public Application { |
| 33 MOJO_CHECK(g_args.is_null()); | |
| 34 for (const char* arg : argv) { | |
| 35 if (arg) | |
| 36 g_args.push_back(arg); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 class ShellAndArgumentGrabber : public Application { | |
| 41 public: | 32 public: |
| 42 ShellAndArgumentGrabber(Array<String>* args, | 33 explicit ShellGrabber(InterfaceRequest<Application> application_request) |
| 43 InterfaceRequest<Application> application_request) | 34 : binding_(this, application_request.Pass()) {} |
| 44 : args_(args), binding_(this, application_request.Pass()) {} | |
| 45 | 35 |
| 46 void WaitForInitialize() { | 36 void WaitForInitialize() { |
| 47 // Initialize is always the first call made on Application. | 37 // Initialize is always the first call made on Application. |
| 48 MOJO_CHECK(binding_.WaitForIncomingMethodCall()); | 38 MOJO_CHECK(binding_.WaitForIncomingMethodCall()); |
| 49 } | 39 } |
| 50 | 40 |
| 51 private: | 41 private: |
| 52 // Application implementation. | 42 // Application implementation. |
| 53 void Initialize(ShellPtr shell, | 43 void Initialize(ShellPtr shell, |
| 54 Array<String> args, | 44 Array<String> args, |
| 55 const mojo::String& url) override { | 45 const mojo::String& url) override { |
| 56 *args_ = args.Pass(); | |
| 57 g_url = url; | 46 g_url = url; |
| 58 g_application_request = binding_.Unbind(); | 47 g_application_request = binding_.Unbind(); |
| 59 g_shell = shell.Pass(); | 48 g_shell = shell.Pass(); |
| 60 } | 49 } |
| 61 | 50 |
| 62 void AcceptConnection(const String& requestor_url, | 51 void AcceptConnection(const String& requestor_url, |
| 63 InterfaceRequest<ServiceProvider> services, | 52 InterfaceRequest<ServiceProvider> services, |
| 64 ServiceProviderPtr exposed_services, | 53 ServiceProviderPtr exposed_services, |
| 65 const String& url) override { | 54 const String& url) override { |
| 66 MOJO_CHECK(false); | 55 MOJO_CHECK(false); |
| 67 } | 56 } |
| 68 | 57 |
| 69 void RequestQuit() override { MOJO_CHECK(false); } | 58 void RequestQuit() override { MOJO_CHECK(false); } |
| 70 | 59 |
| 71 Array<String>* args_; | |
| 72 Binding<Application> binding_; | 60 Binding<Application> binding_; |
| 73 }; | 61 }; |
| 74 | 62 |
| 75 } // namespace | 63 } // namespace |
| 76 | 64 |
| 77 const Array<String>& Args() { | |
| 78 return g_args; | |
| 79 } | |
| 80 | |
| 81 MojoResult RunAllTests(MojoHandle application_request_handle) { | 65 MojoResult RunAllTests(MojoHandle application_request_handle) { |
| 82 { | 66 { |
| 83 // This loop is used for init, and then destroyed before running tests. | 67 // This loop is used for init, and then destroyed before running tests. |
| 84 Environment::InstantiateDefaultRunLoop(); | 68 Environment::InstantiateDefaultRunLoop(); |
| 85 | 69 |
| 86 // Grab the shell handle and GTEST commandline arguments. | 70 // Grab the shell handle. |
| 87 // GTEST command line arguments are supported amid application arguments: | 71 ShellGrabber grabber( |
| 88 // $ mojo_shell mojo:example_apptests | 72 MakeRequest<Application>(MakeScopedHandle( |
| 89 // --args-for='mojo:example_apptests arg1 --gtest_filter=foo arg2' | 73 MessagePipeHandle(application_request_handle)))); |
| 90 Array<String> args; | |
| 91 ShellAndArgumentGrabber grabber( | |
| 92 &args, MakeRequest<Application>(MakeScopedHandle( | |
| 93 MessagePipeHandle(application_request_handle)))); | |
| 94 grabber.WaitForInitialize(); | 74 grabber.WaitForInitialize(); |
| 95 MOJO_CHECK(g_shell); | 75 MOJO_CHECK(g_shell); |
| 96 MOJO_CHECK(g_application_request.is_pending()); | 76 MOJO_CHECK(g_application_request.is_pending()); |
| 97 | 77 |
| 98 // InitGoogleTest expects (argc + 1) elements, including a terminating null. | 78 int argc = 0; |
| 99 // It also removes GTEST arguments from |argv| and updates the |argc| count. | 79 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 100 MOJO_CHECK(args.size() < | 80 const char** argv = new const char* [cmd_line->argv().size()]; |
| 101 static_cast<size_t>(std::numeric_limits<int>::max())); | 81 #if defined(OS_WIN) |
| 102 int argc = static_cast<int>(args.size()); | 82 std::vector<std::string> local_strings; |
| 103 std::vector<const char*> argv(argc + 1); | 83 #endif |
| 104 for (int i = 0; i < argc; ++i) | 84 for (auto& arg : cmd_line->argv()) { |
| 105 argv[i] = args[i].get().c_str(); | 85 #if defined(OS_WIN) |
| 106 argv[argc] = nullptr; | 86 local_strings.push_back(base::WideToUTF8(arg)); |
| 87 argv[argc++] = local_strings.back().c_str(); |
| 88 #else |
| 89 argv[argc++] = arg.c_str(); |
| 90 #endif |
| 91 } |
| 107 | 92 |
| 108 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0]))); | 93 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0]))); |
| 109 InitializeArgs(argc, argv); | |
| 110 | 94 |
| 111 Environment::DestroyDefaultRunLoop(); | 95 Environment::DestroyDefaultRunLoop(); |
| 112 } | 96 } |
| 113 | 97 |
| 114 int result = RUN_ALL_TESTS(); | 98 int result = RUN_ALL_TESTS(); |
| 115 | 99 |
| 116 // Shut down our message pipes before exiting. | 100 // Shut down our message pipes before exiting. |
| 117 (void)g_application_request.PassMessagePipe(); | 101 (void)g_application_request.PassMessagePipe(); |
| 118 (void)g_shell.PassMessagePipe(); | 102 (void)g_shell.PassMessagePipe(); |
| 119 | 103 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 137 Environment::InstantiateDefaultRunLoop(); | 121 Environment::InstantiateDefaultRunLoop(); |
| 138 | 122 |
| 139 MOJO_CHECK(g_application_request.is_pending()); | 123 MOJO_CHECK(g_application_request.is_pending()); |
| 140 MOJO_CHECK(g_shell); | 124 MOJO_CHECK(g_shell); |
| 141 | 125 |
| 142 // New applications are constructed for each test to avoid persisting state. | 126 // New applications are constructed for each test to avoid persisting state. |
| 143 application_impl_ = new ApplicationImpl(GetApplicationDelegate(), | 127 application_impl_ = new ApplicationImpl(GetApplicationDelegate(), |
| 144 g_application_request.Pass()); | 128 g_application_request.Pass()); |
| 145 | 129 |
| 146 // Fake application initialization with the given command line arguments. | 130 // Fake application initialization with the given command line arguments. |
| 147 application_impl_->Initialize(g_shell.Pass(), g_args.Clone(), g_url); | 131 Array<String> empty_args; |
| 132 application_impl_->Initialize(g_shell.Pass(), empty_args.Clone(), g_url); |
| 148 } | 133 } |
| 149 | 134 |
| 150 void ApplicationTestBase::TearDown() { | 135 void ApplicationTestBase::TearDown() { |
| 151 MOJO_CHECK(!g_application_request.is_pending()); | 136 MOJO_CHECK(!g_application_request.is_pending()); |
| 152 MOJO_CHECK(!g_shell); | 137 MOJO_CHECK(!g_shell); |
| 153 | 138 |
| 154 application_impl_->UnbindConnections(&g_application_request, &g_shell); | 139 application_impl_->UnbindConnections(&g_application_request, &g_shell); |
| 155 delete application_impl_; | 140 delete application_impl_; |
| 156 if (ShouldCreateDefaultRunLoop()) | 141 if (ShouldCreateDefaultRunLoop()) |
| 157 Environment::DestroyDefaultRunLoop(); | 142 Environment::DestroyDefaultRunLoop(); |
| 158 } | 143 } |
| 159 | 144 |
| 160 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() { | 145 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() { |
| 161 return true; | 146 return true; |
| 162 } | 147 } |
| 163 | 148 |
| 164 } // namespace test | 149 } // namespace test |
| 165 } // namespace mojo | 150 } // namespace mojo |
| OLD | NEW |