Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / mojo / application / public / cpp / lib / application_test_base.cc
blobe5d1aa58f81292ef59651edd7f330f64e4bbc90f
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"
15 namespace mojo {
16 namespace test {
18 namespace {
19 // Share the application URL with multiple application tests.
20 String g_url;
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.
29 ShellPtr g_shell;
31 class ShellGrabber : public Application {
32 public:
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());
41 private:
42 // Application implementation.
43 void Initialize(ShellPtr shell, const mojo::String& url) override {
44 g_url = url;
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 {
54 MOJO_CHECK(false);
57 void OnQuitRequested(const Callback<void(bool)>& callback) override {
58 MOJO_CHECK(false);
61 Binding<Application> binding_;
64 } // namespace
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.
72 ShellGrabber grabber(
73 MakeRequest<Application>(MakeScopedHandle(
74 MessagePipeHandle(application_request_handle))));
75 grabber.WaitForInitialize();
76 MOJO_CHECK(g_shell);
77 MOJO_CHECK(g_application_request.is_pending());
79 int argc = 0;
80 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
81 const char** argv = new const char* [cmd_line->argv().size() + 1];
82 #if defined(OS_WIN)
83 std::vector<std::string> local_strings;
84 #endif
85 for (auto& arg : cmd_line->argv()) {
86 #if defined(OS_WIN)
87 local_strings.push_back(base::WideToUTF8(arg));
88 argv[argc++] = local_strings.back().c_str();
89 #else
90 argv[argc++] = arg.c_str();
91 #endif
93 argv[argc] = nullptr;
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
121 // communication.
122 if (ShouldCreateDefaultRunLoop())
123 Environment::InstantiateDefaultRunLoop();
125 MOJO_CHECK(g_application_request.is_pending());
126 MOJO_CHECK(g_shell);
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() {
151 return true;
155 } // namespace test
156 } // namespace mojo