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