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
3 // found in the LICENSE file.
5 #include "mojo/application/application_test_base_chromium.h"
7 #include "base/command_line.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "mojo/public/cpp/application/application_impl.h"
10 #include "mojo/public/cpp/bindings/binding.h"
11 #include "mojo/public/cpp/environment/environment.h"
12 #include "mojo/public/cpp/system/message_pipe.h"
13 #include "mojo/public/interfaces/application/application.mojom.h"
19 // Share the application URL with multiple application tests.
22 // Application request handle passed from the shell in MojoMain, stored in
23 // between SetUp()/TearDown() so we can (re-)intialize new ApplicationImpls.
24 InterfaceRequest
<Application
> g_application_request
;
26 // Shell pointer passed in the initial mojo.Application.Initialize() call,
27 // stored in between initial setup and the first test and between SetUp/TearDown
28 // calls so we can (re-)initialize new ApplicationImpls.
31 class ShellGrabber
: public Application
{
33 explicit ShellGrabber(InterfaceRequest
<Application
> application_request
)
34 : binding_(this, application_request
.Pass()) {}
36 void WaitForInitialize() {
37 // Initialize is always the first call made on Application.
38 MOJO_CHECK(binding_
.WaitForIncomingMethodCall());
42 // Application implementation.
43 void Initialize(ShellPtr shell
,
45 const mojo::String
& url
) override
{
47 g_application_request
= binding_
.Unbind();
48 g_shell
= shell
.Pass();
51 void AcceptConnection(const String
& requestor_url
,
52 InterfaceRequest
<ServiceProvider
> services
,
53 ServiceProviderPtr exposed_services
,
54 const String
& url
) override
{
58 void RequestQuit() override
{ MOJO_CHECK(false); }
60 Binding
<Application
> binding_
;
65 MojoResult
RunAllTests(MojoHandle application_request_handle
) {
67 // This loop is used for init, and then destroyed before running tests.
68 Environment::InstantiateDefaultRunLoop();
70 // Grab the shell handle.
72 MakeRequest
<Application
>(MakeScopedHandle(
73 MessagePipeHandle(application_request_handle
))));
74 grabber
.WaitForInitialize();
76 MOJO_CHECK(g_application_request
.is_pending());
79 base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
80 const char** argv
= new const char* [cmd_line
->argv().size()];
82 std::vector
<std::string
> local_strings
;
84 for (auto& arg
: cmd_line
->argv()) {
86 local_strings
.push_back(base::WideToUTF8(arg
));
87 argv
[argc
++] = local_strings
.back().c_str();
89 argv
[argc
++] = arg
.c_str();
93 testing::InitGoogleTest(&argc
, const_cast<char**>(&(argv
[0])));
95 Environment::DestroyDefaultRunLoop();
98 int result
= RUN_ALL_TESTS();
100 // Shut down our message pipes before exiting.
101 (void)g_application_request
.PassMessagePipe();
102 (void)g_shell
.PassMessagePipe();
104 return (result
== 0) ? MOJO_RESULT_OK
: MOJO_RESULT_UNKNOWN
;
107 ApplicationTestBase::ApplicationTestBase() : application_impl_(nullptr) {
110 ApplicationTestBase::~ApplicationTestBase() {
113 ApplicationDelegate
* ApplicationTestBase::GetApplicationDelegate() {
114 return &default_application_delegate_
;
117 void ApplicationTestBase::SetUp() {
118 // A run loop is recommended for ApplicationImpl initialization and
120 if (ShouldCreateDefaultRunLoop())
121 Environment::InstantiateDefaultRunLoop();
123 MOJO_CHECK(g_application_request
.is_pending());
126 // New applications are constructed for each test to avoid persisting state.
127 application_impl_
= new ApplicationImpl(GetApplicationDelegate(),
128 g_application_request
.Pass());
130 // Fake application initialization with the given command line arguments.
131 Array
<String
> empty_args
;
132 application_impl_
->Initialize(g_shell
.Pass(), empty_args
.Clone(), g_url
);
135 void ApplicationTestBase::TearDown() {
136 MOJO_CHECK(!g_application_request
.is_pending());
137 MOJO_CHECK(!g_shell
);
139 application_impl_
->UnbindConnections(&g_application_request
, &g_shell
);
140 delete application_impl_
;
141 if (ShouldCreateDefaultRunLoop())
142 Environment::DestroyDefaultRunLoop();
145 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() {