Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: mojo/shell/public/cpp/lib/application_test_base.cc

Issue 1877753003: Move mojo\shell to services\shell (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@62scan
Patch Set: . Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/bind.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "mojo/public/cpp/bindings/binding.h"
13 #include "mojo/public/cpp/system/message_pipe.h"
14 #include "mojo/shell/public/cpp/application_test_base.h"
15 #include "mojo/shell/public/cpp/shell_connection.h"
16 #include "mojo/shell/public/interfaces/shell_client.mojom.h"
17
18 namespace mojo {
19 namespace test {
20
21 namespace {
22
23 // Share the application name with multiple application tests.
24 shell::mojom::IdentityPtr g_identity;
25 uint32_t g_id = shell::mojom::kInvalidInstanceID;
26
27 // ShellClient request handle passed from the shell in MojoMain, stored in
28 // between SetUp()/TearDown() so we can (re-)intialize new ShellConnections.
29 InterfaceRequest<shell::mojom::ShellClient> g_shell_client_request;
30
31 // Connector pointer passed in the initial mojo.ShellClient.Initialize() call,
32 // stored in between initial setup and the first test and between SetUp/TearDown
33 // calls so we can (re-)initialize new ShellConnections.
34 shell::mojom::ConnectorPtr g_connector;
35
36 class ShellGrabber : public shell::mojom::ShellClient {
37 public:
38 explicit ShellGrabber(InterfaceRequest<shell::mojom::ShellClient> request)
39 : binding_(this, std::move(request)) {
40 binding_.set_connection_error_handler([] { _exit(1); });
41 }
42
43 void WaitForInitialize() {
44 // Initialize is always the first call made on ShellClient.
45 CHECK(binding_.WaitForIncomingMethodCall());
46 }
47
48 private:
49 // shell::mojom::ShellClient implementation.
50 void Initialize(shell::mojom::IdentityPtr identity,
51 uint32_t id,
52 const InitializeCallback& callback) override {
53 callback.Run(GetProxy(&g_connector));
54
55 g_identity = std::move(identity);
56 g_id = id;
57 g_shell_client_request = binding_.Unbind();
58 }
59
60 void AcceptConnection(
61 shell::mojom::IdentityPtr source,
62 uint32_t source_id,
63 shell::mojom::InterfaceProviderRequest local_interfaces,
64 shell::mojom::InterfaceProviderPtr remote_interfaces,
65 shell::mojom::CapabilityRequestPtr capability_spec,
66 const String& name) override {
67 CHECK(false);
68 }
69
70 Binding<ShellClient> binding_;
71 };
72
73 void IgnoreConnectorRequest(shell::mojom::ConnectorRequest) {}
74
75 } // namespace
76
77 MojoResult RunAllTests(MojoHandle shell_client_request_handle) {
78 {
79 // This loop is used for init, and then destroyed before running tests.
80 base::MessageLoop message_loop;
81
82 // Grab the shell handle.
83 ShellGrabber grabber(
84 MakeRequest<shell::mojom::ShellClient>(MakeScopedHandle(
85 MessagePipeHandle(shell_client_request_handle))));
86 grabber.WaitForInitialize();
87 CHECK(g_connector);
88 CHECK(g_shell_client_request.is_pending());
89
90 int argc = 0;
91 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
92 const char** argv = new const char* [cmd_line->argv().size() + 1];
93 #if defined(OS_WIN)
94 std::vector<std::string> local_strings;
95 #endif
96 for (auto& arg : cmd_line->argv()) {
97 #if defined(OS_WIN)
98 local_strings.push_back(base::WideToUTF8(arg));
99 argv[argc++] = local_strings.back().c_str();
100 #else
101 argv[argc++] = arg.c_str();
102 #endif
103 }
104 argv[argc] = nullptr;
105
106 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0])));
107 }
108
109 int result = RUN_ALL_TESTS();
110
111 // Shut down our message pipes before exiting.
112 (void)g_shell_client_request.PassMessagePipe();
113 g_connector.reset();
114
115 return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN;
116 }
117
118 TestHelper::TestHelper(ShellClient* client)
119 : shell_connection_(new ShellConnection(
120 client == nullptr ? &default_shell_client_ : client,
121 std::move(g_shell_client_request))),
122 name_(g_identity->name),
123 userid_(g_identity->user_id),
124 instance_id_(g_id) {
125 shell_connection_->SetAppTestConnectorForTesting(std::move(g_connector));
126
127 // Fake ShellClient initialization.
128 shell::mojom::ShellClient* shell_client = shell_connection_.get();
129 shell_client->Initialize(std::move(g_identity), g_id,
130 base::Bind(&IgnoreConnectorRequest));
131 }
132
133 TestHelper::~TestHelper() {
134 // We may have supplied a member as the client. Delete |shell_connection_|
135 // while still valid.
136 shell_connection_.reset();
137 }
138
139 ApplicationTestBase::ApplicationTestBase() : test_helper_(nullptr) {}
140
141 ApplicationTestBase::~ApplicationTestBase() {
142 }
143
144 ShellClient* ApplicationTestBase::GetShellClient() {
145 return nullptr;
146 }
147
148 void ApplicationTestBase::SetUp() {
149 // A run loop is recommended for ShellConnection initialization and
150 // communication.
151 if (ShouldCreateDefaultRunLoop()) {
152 CHECK(!base::MessageLoop::current());
153 // Not leaked: accessible from |base::MessageLoop::current()|.
154 base::MessageLoop* message_loop = new base::MessageLoop();
155 CHECK_EQ(message_loop, base::MessageLoop::current());
156 }
157
158 CHECK(g_shell_client_request.is_pending());
159 CHECK(g_connector);
160
161 // New applications are constructed for each test to avoid persisting state.
162 test_helper_.reset(new TestHelper(GetShellClient()));
163 }
164
165 void ApplicationTestBase::TearDown() {
166 CHECK(!g_shell_client_request.is_pending());
167 CHECK(!g_connector);
168
169 test_helper_.reset();
170
171 if (ShouldCreateDefaultRunLoop()) {
172 CHECK(base::MessageLoop::current());
173 delete base::MessageLoop::current();
174 CHECK(!base::MessageLoop::current());
175 }
176 }
177
178 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() {
179 return true;
180 }
181
182 } // namespace test
183 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/shell/public/cpp/lib/application_runner.cc ('k') | mojo/shell/public/cpp/lib/application_test_main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698