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/public/cpp/application_test_base.h"
7 #include "base/command_line.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "mojo/application/public/cpp/application_impl.h"
10 #include "mojo/application/public/interfaces/application.mojom.h"
11 #include "mojo/public/cpp/bindings/binding.h"
12 #include "mojo/public/cpp/environment/environment.h"
13 #include "mojo/public/cpp/system/message_pipe.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
, const mojo::String
& url
) override
{
45 g_application_request
= binding_
.Unbind();
46 g_shell
= shell
.Pass();
49 void AcceptConnection(const String
& requestor_url
,
50 InterfaceRequest
<ServiceProvider
> services
,
51 ServiceProviderPtr exposed_services
,
52 Array
<String
> allowed_interfaces
,
53 const String
& url
) override
{
57 void OnQuitRequested(const Callback
<void(bool)>& callback
) override
{
61 Binding
<Application
> binding_
;
66 MojoResult
RunAllTests(MojoHandle application_request_handle
) {
68 // This loop is used for init, and then destroyed before running tests.
69 Environment::InstantiateDefaultRunLoop();
71 // Grab the shell handle.
73 MakeRequest
<Application
>(MakeScopedHandle(
74 MessagePipeHandle(application_request_handle
))));
75 grabber
.WaitForInitialize();
77 MOJO_CHECK(g_application_request
.is_pending());
80 base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
81 const char** argv
= new const char* [cmd_line
->argv().size() + 1];
83 std::vector
<std::string
> local_strings
;
85 for (auto& arg
: cmd_line
->argv()) {
87 local_strings
.push_back(base::WideToUTF8(arg
));
88 argv
[argc
++] = local_strings
.back().c_str();
90 argv
[argc
++] = arg
.c_str();
95 testing::InitGoogleTest(&argc
, const_cast<char**>(&(argv
[0])));
97 Environment::DestroyDefaultRunLoop();
100 int result
= RUN_ALL_TESTS();
102 // Shut down our message pipes before exiting.
103 (void)g_application_request
.PassMessagePipe();
104 (void)g_shell
.PassInterface();
106 return (result
== 0) ? MOJO_RESULT_OK
: MOJO_RESULT_UNKNOWN
;
109 ApplicationTestBase::ApplicationTestBase() : application_impl_(nullptr) {
112 ApplicationTestBase::~ApplicationTestBase() {
115 ApplicationDelegate
* ApplicationTestBase::GetApplicationDelegate() {
116 return &default_application_delegate_
;
119 void ApplicationTestBase::SetUp() {
120 // A run loop is recommended for ApplicationImpl initialization and
122 if (ShouldCreateDefaultRunLoop())
123 Environment::InstantiateDefaultRunLoop();
125 MOJO_CHECK(g_application_request
.is_pending());
128 // New applications are constructed for each test to avoid persisting state.
129 application_impl_
= new ApplicationImpl(GetApplicationDelegate(),
130 g_application_request
.Pass());
132 // Fake application initialization.
133 Application
* application
= application_impl_
;
134 application
->Initialize(g_shell
.Pass(), g_url
);
137 void ApplicationTestBase::TearDown() {
138 MOJO_CHECK(!g_application_request
.is_pending());
139 MOJO_CHECK(!g_shell
);
142 ApplicationImpl::TestApi
test_api(application_impl_
);
143 test_api
.UnbindConnections(&g_application_request
, &g_shell
);
145 delete application_impl_
;
146 if (ShouldCreateDefaultRunLoop())
147 Environment::DestroyDefaultRunLoop();
150 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() {