OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <utility> | |
6 | |
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/cpp/application_test_base.h" | |
11 #include "mojo/application/public/interfaces/application.mojom.h" | |
12 #include "mojo/public/cpp/bindings/binding.h" | |
13 #include "mojo/public/cpp/environment/environment.h" | |
14 #include "mojo/public/cpp/system/message_pipe.h" | |
15 | |
16 namespace mojo { | |
17 namespace test { | |
18 | |
19 namespace { | |
20 // Share the application URL with multiple application tests. | |
21 String g_url; | |
22 | |
23 // Application request handle passed from the shell in MojoMain, stored in | |
24 // between SetUp()/TearDown() so we can (re-)intialize new ApplicationImpls. | |
25 InterfaceRequest<Application> g_application_request; | |
26 | |
27 // Shell pointer passed in the initial mojo.Application.Initialize() call, | |
28 // stored in between initial setup and the first test and between SetUp/TearDown | |
29 // calls so we can (re-)initialize new ApplicationImpls. | |
30 ShellPtr g_shell; | |
31 | |
32 class ShellGrabber : public Application { | |
33 public: | |
34 explicit ShellGrabber(InterfaceRequest<Application> application_request) | |
35 : binding_(this, std::move(application_request)) {} | |
36 | |
37 void WaitForInitialize() { | |
38 // Initialize is always the first call made on Application. | |
39 MOJO_CHECK(binding_.WaitForIncomingMethodCall()); | |
40 } | |
41 | |
42 private: | |
43 // Application implementation. | |
44 void Initialize(ShellPtr shell, const mojo::String& url) override { | |
45 g_url = url; | |
46 g_application_request = binding_.Unbind(); | |
47 g_shell = std::move(shell); | |
48 } | |
49 | |
50 void AcceptConnection(const String& requestor_url, | |
51 InterfaceRequest<ServiceProvider> services, | |
52 ServiceProviderPtr exposed_services, | |
53 Array<String> allowed_interfaces, | |
54 const String& url) override { | |
55 MOJO_CHECK(false); | |
56 } | |
57 | |
58 void OnQuitRequested(const Callback<void(bool)>& callback) override { | |
59 MOJO_CHECK(false); | |
60 } | |
61 | |
62 Binding<Application> binding_; | |
63 }; | |
64 | |
65 } // namespace | |
66 | |
67 MojoResult RunAllTests(MojoHandle application_request_handle) { | |
68 { | |
69 // This loop is used for init, and then destroyed before running tests. | |
70 Environment::InstantiateDefaultRunLoop(); | |
71 | |
72 // Grab the shell handle. | |
73 ShellGrabber grabber( | |
74 MakeRequest<Application>(MakeScopedHandle( | |
75 MessagePipeHandle(application_request_handle)))); | |
76 grabber.WaitForInitialize(); | |
77 MOJO_CHECK(g_shell); | |
78 MOJO_CHECK(g_application_request.is_pending()); | |
79 | |
80 int argc = 0; | |
81 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
82 const char** argv = new const char* [cmd_line->argv().size() + 1]; | |
83 #if defined(OS_WIN) | |
84 std::vector<std::string> local_strings; | |
85 #endif | |
86 for (auto& arg : cmd_line->argv()) { | |
87 #if defined(OS_WIN) | |
88 local_strings.push_back(base::WideToUTF8(arg)); | |
89 argv[argc++] = local_strings.back().c_str(); | |
90 #else | |
91 argv[argc++] = arg.c_str(); | |
92 #endif | |
93 } | |
94 argv[argc] = nullptr; | |
95 | |
96 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0]))); | |
97 | |
98 Environment::DestroyDefaultRunLoop(); | |
99 } | |
100 | |
101 int result = RUN_ALL_TESTS(); | |
102 | |
103 // Shut down our message pipes before exiting. | |
104 (void)g_application_request.PassMessagePipe(); | |
105 g_shell.reset(); | |
106 | |
107 return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN; | |
108 } | |
109 | |
110 ApplicationTestBase::ApplicationTestBase() : application_impl_(nullptr) { | |
111 } | |
112 | |
113 ApplicationTestBase::~ApplicationTestBase() { | |
114 } | |
115 | |
116 ApplicationDelegate* ApplicationTestBase::GetApplicationDelegate() { | |
117 return &default_application_delegate_; | |
118 } | |
119 | |
120 void ApplicationTestBase::SetUp() { | |
121 // A run loop is recommended for ApplicationImpl initialization and | |
122 // communication. | |
123 if (ShouldCreateDefaultRunLoop()) | |
124 Environment::InstantiateDefaultRunLoop(); | |
125 | |
126 MOJO_CHECK(g_application_request.is_pending()); | |
127 MOJO_CHECK(g_shell); | |
128 | |
129 // New applications are constructed for each test to avoid persisting state. | |
130 application_impl_ = new ApplicationImpl(GetApplicationDelegate(), | |
131 std::move(g_application_request)); | |
132 | |
133 // Fake application initialization. | |
134 Application* application = application_impl_; | |
135 application->Initialize(std::move(g_shell), g_url); | |
136 } | |
137 | |
138 void ApplicationTestBase::TearDown() { | |
139 MOJO_CHECK(!g_application_request.is_pending()); | |
140 MOJO_CHECK(!g_shell); | |
141 | |
142 // TODO: commented out until http://crbug.com/533107 is solved. | |
143 // { | |
144 // ApplicationImpl::TestApi test_api(application_impl_); | |
145 // test_api.UnbindConnections(&g_application_request, &g_shell); | |
146 // } | |
147 delete application_impl_; | |
148 if (ShouldCreateDefaultRunLoop()) | |
149 Environment::DestroyDefaultRunLoop(); | |
150 } | |
151 | |
152 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() { | |
153 return true; | |
154 } | |
155 | |
156 | |
157 } // namespace test | |
158 } // namespace mojo | |
OLD | NEW |