OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <utility> | 5 #include <utility> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "mojo/public/cpp/bindings/binding.h" | 10 #include "mojo/public/cpp/bindings/binding.h" |
11 #include "mojo/public/cpp/environment/environment.h" | 11 #include "mojo/public/cpp/environment/environment.h" |
12 #include "mojo/public/cpp/system/message_pipe.h" | 12 #include "mojo/public/cpp/system/message_pipe.h" |
13 #include "mojo/shell/public/cpp/application_test_base.h" | 13 #include "mojo/shell/public/cpp/application_test_base.h" |
14 #include "mojo/shell/public/cpp/shell_connection.h" | 14 #include "mojo/shell/public/cpp/shell_connection.h" |
15 #include "mojo/shell/public/interfaces/shell_client.mojom.h" | 15 #include "mojo/shell/public/interfaces/shell_client.mojom.h" |
16 | 16 |
17 namespace mojo { | 17 namespace mojo { |
18 namespace test { | 18 namespace test { |
19 | 19 |
20 namespace { | 20 namespace { |
21 // Share the application URL with multiple application tests. | 21 // Share the application URL with multiple application tests. |
22 String g_url; | 22 String g_url; |
23 uint32_t g_id = shell::mojom::Connector::kInvalidApplicationID; | 23 uint32_t g_id = shell::mojom::Connector::kInvalidApplicationID; |
24 uint32_t g_user_id = shell::mojom::Connector::kUserRoot; | 24 uint32_t g_user_id = shell::mojom::Connector::kUserRoot; |
25 | 25 |
26 // ShellClient request handle passed from the shell in MojoMain, stored in | 26 // ShellClient request handle passed from the shell in MojoMain, stored in |
27 // between SetUp()/TearDown() so we can (re-)intialize new ShellConnections. | 27 // between SetUp()/TearDown() so we can (re-)intialize new ShellConnections. |
28 InterfaceRequest<shell::mojom::ShellClient> g_shell_client_request; | 28 InterfaceRequest<shell::mojom::ShellClient> g_shell_client_request; |
29 | 29 |
30 // Shell pointer passed in the initial mojo.ShellClient.Initialize() call, | 30 // Connector pointer passed in the initial mojo.ShellClient.Initialize() call, |
31 // stored in between initial setup and the first test and between SetUp/TearDown | 31 // stored in between initial setup and the first test and between SetUp/TearDown |
32 // calls so we can (re-)initialize new ShellConnections. | 32 // calls so we can (re-)initialize new ShellConnections. |
33 shell::mojom::ShellPtr g_shell; | 33 shell::mojom::ConnectorPtr g_connector; |
34 | 34 |
35 class ShellGrabber : public shell::mojom::ShellClient { | 35 class ShellGrabber : public shell::mojom::ShellClient { |
36 public: | 36 public: |
37 explicit ShellGrabber(InterfaceRequest<shell::mojom::ShellClient> request) | 37 explicit ShellGrabber(InterfaceRequest<shell::mojom::ShellClient> request) |
38 : binding_(this, std::move(request)) {} | 38 : binding_(this, std::move(request)) {} |
39 | 39 |
40 void WaitForInitialize() { | 40 void WaitForInitialize() { |
41 // Initialize is always the first call made on ShellClient. | 41 // Initialize is always the first call made on ShellClient. |
42 CHECK(binding_.WaitForIncomingMethodCall()); | 42 CHECK(binding_.WaitForIncomingMethodCall()); |
43 } | 43 } |
44 | 44 |
45 private: | 45 private: |
46 // shell::mojom::ShellClient implementation. | 46 // shell::mojom::ShellClient implementation. |
47 void Initialize(shell::mojom::ShellPtr shell, | 47 void Initialize(shell::mojom::ConnectorPtr connector, |
48 const mojo::String& url, | 48 const mojo::String& url, |
49 uint32_t id, | 49 uint32_t id, |
50 uint32_t user_id) override { | 50 uint32_t user_id) override { |
51 g_url = url; | 51 g_url = url; |
52 g_id = id; | 52 g_id = id; |
53 g_user_id = user_id; | 53 g_user_id = user_id; |
54 g_shell_client_request = binding_.Unbind(); | 54 g_shell_client_request = binding_.Unbind(); |
55 g_shell = std::move(shell); | 55 g_connector = std::move(connector); |
56 } | 56 } |
57 | 57 |
58 void AcceptConnection( | 58 void AcceptConnection( |
59 const String& requestor_url, | 59 const String& requestor_url, |
60 uint32_t requestor_user_id, | 60 uint32_t requestor_user_id, |
61 uint32_t requestor_id, | 61 uint32_t requestor_id, |
62 shell::mojom::InterfaceProviderRequest local_interfaces, | 62 shell::mojom::InterfaceProviderRequest local_interfaces, |
63 shell::mojom::InterfaceProviderPtr remote_interfaces, | 63 shell::mojom::InterfaceProviderPtr remote_interfaces, |
64 Array<String> allowed_interfaces, | 64 Array<String> allowed_interfaces, |
65 const String& url) override { | 65 const String& url) override { |
66 CHECK(false); | 66 CHECK(false); |
67 } | 67 } |
68 | 68 |
69 void OnQuitRequested(const Callback<void(bool)>& callback) override { | |
70 CHECK(false); | |
71 } | |
72 | |
73 Binding<ShellClient> binding_; | 69 Binding<ShellClient> binding_; |
74 }; | 70 }; |
75 | 71 |
76 } // namespace | 72 } // namespace |
77 | 73 |
78 MojoResult RunAllTests(MojoHandle shell_client_request_handle) { | 74 MojoResult RunAllTests(MojoHandle shell_client_request_handle) { |
79 { | 75 { |
80 // This loop is used for init, and then destroyed before running tests. | 76 // This loop is used for init, and then destroyed before running tests. |
81 Environment::InstantiateDefaultRunLoop(); | 77 Environment::InstantiateDefaultRunLoop(); |
82 | 78 |
83 // Grab the shell handle. | 79 // Grab the shell handle. |
84 ShellGrabber grabber( | 80 ShellGrabber grabber( |
85 MakeRequest<shell::mojom::ShellClient>(MakeScopedHandle( | 81 MakeRequest<shell::mojom::ShellClient>(MakeScopedHandle( |
86 MessagePipeHandle(shell_client_request_handle)))); | 82 MessagePipeHandle(shell_client_request_handle)))); |
87 grabber.WaitForInitialize(); | 83 grabber.WaitForInitialize(); |
88 CHECK(g_shell); | 84 CHECK(g_connector); |
89 CHECK(g_shell_client_request.is_pending()); | 85 CHECK(g_shell_client_request.is_pending()); |
90 | 86 |
91 int argc = 0; | 87 int argc = 0; |
92 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 88 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
93 const char** argv = new const char* [cmd_line->argv().size() + 1]; | 89 const char** argv = new const char* [cmd_line->argv().size() + 1]; |
94 #if defined(OS_WIN) | 90 #if defined(OS_WIN) |
95 std::vector<std::string> local_strings; | 91 std::vector<std::string> local_strings; |
96 #endif | 92 #endif |
97 for (auto& arg : cmd_line->argv()) { | 93 for (auto& arg : cmd_line->argv()) { |
98 #if defined(OS_WIN) | 94 #if defined(OS_WIN) |
99 local_strings.push_back(base::WideToUTF8(arg)); | 95 local_strings.push_back(base::WideToUTF8(arg)); |
100 argv[argc++] = local_strings.back().c_str(); | 96 argv[argc++] = local_strings.back().c_str(); |
101 #else | 97 #else |
102 argv[argc++] = arg.c_str(); | 98 argv[argc++] = arg.c_str(); |
103 #endif | 99 #endif |
104 } | 100 } |
105 argv[argc] = nullptr; | 101 argv[argc] = nullptr; |
106 | 102 |
107 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0]))); | 103 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0]))); |
108 | 104 |
109 Environment::DestroyDefaultRunLoop(); | 105 Environment::DestroyDefaultRunLoop(); |
110 } | 106 } |
111 | 107 |
112 int result = RUN_ALL_TESTS(); | 108 int result = RUN_ALL_TESTS(); |
113 | 109 |
114 // Shut down our message pipes before exiting. | 110 // Shut down our message pipes before exiting. |
115 (void)g_shell_client_request.PassMessagePipe(); | 111 (void)g_shell_client_request.PassMessagePipe(); |
116 g_shell.reset(); | 112 g_connector.reset(); |
117 | 113 |
118 return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN; | 114 return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN; |
119 } | 115 } |
120 | 116 |
121 TestHelper::TestHelper(ShellClient* client) | 117 TestHelper::TestHelper(ShellClient* client) |
122 : shell_connection_(new ShellConnection( | 118 : shell_connection_(new ShellConnection( |
123 client == nullptr ? &default_shell_client_ : client, | 119 client == nullptr ? &default_shell_client_ : client, |
124 std::move(g_shell_client_request))), | 120 std::move(g_shell_client_request))), |
125 url_(g_url) { | 121 url_(g_url) { |
126 // Fake ShellClient initialization. | 122 // Fake ShellClient initialization. |
127 shell::mojom::ShellClient* shell_client = shell_connection_.get(); | 123 shell::mojom::ShellClient* shell_client = shell_connection_.get(); |
128 shell_client->Initialize(std::move(g_shell), g_url, g_id, g_user_id); | 124 shell_client->Initialize(std::move(g_connector), g_url, g_id, g_user_id); |
129 } | 125 } |
130 | 126 |
131 TestHelper::~TestHelper() { | 127 TestHelper::~TestHelper() { |
132 // TODO: commented out until http://crbug.com/533107 is solved. | |
133 // { | |
134 // ShellConnection::TestApi test_api(shell_connection_); | |
135 // test_api.UnbindConnections(&g_shell_client_request, &g_shell); | |
136 // } | |
137 // We may have supplied a member as the client. Delete |shell_connection_| | 128 // We may have supplied a member as the client. Delete |shell_connection_| |
138 // while still valid. | 129 // while still valid. |
139 shell_connection_.reset(); | 130 shell_connection_.reset(); |
140 } | 131 } |
141 | 132 |
142 ApplicationTestBase::ApplicationTestBase() : test_helper_(nullptr) {} | 133 ApplicationTestBase::ApplicationTestBase() : test_helper_(nullptr) {} |
143 | 134 |
144 ApplicationTestBase::~ApplicationTestBase() { | 135 ApplicationTestBase::~ApplicationTestBase() { |
145 } | 136 } |
146 | 137 |
147 ShellClient* ApplicationTestBase::GetShellClient() { | 138 ShellClient* ApplicationTestBase::GetShellClient() { |
148 return nullptr; | 139 return nullptr; |
149 } | 140 } |
150 | 141 |
151 void ApplicationTestBase::SetUp() { | 142 void ApplicationTestBase::SetUp() { |
152 // A run loop is recommended for ShellConnection initialization and | 143 // A run loop is recommended for ShellConnection initialization and |
153 // communication. | 144 // communication. |
154 if (ShouldCreateDefaultRunLoop()) | 145 if (ShouldCreateDefaultRunLoop()) |
155 Environment::InstantiateDefaultRunLoop(); | 146 Environment::InstantiateDefaultRunLoop(); |
156 | 147 |
157 CHECK(g_shell_client_request.is_pending()); | 148 CHECK(g_shell_client_request.is_pending()); |
158 CHECK(g_shell); | 149 CHECK(g_connector); |
159 | 150 |
160 // New applications are constructed for each test to avoid persisting state. | 151 // New applications are constructed for each test to avoid persisting state. |
161 test_helper_.reset(new TestHelper(GetShellClient())); | 152 test_helper_.reset(new TestHelper(GetShellClient())); |
162 } | 153 } |
163 | 154 |
164 void ApplicationTestBase::TearDown() { | 155 void ApplicationTestBase::TearDown() { |
165 CHECK(!g_shell_client_request.is_pending()); | 156 CHECK(!g_shell_client_request.is_pending()); |
166 CHECK(!g_shell); | 157 CHECK(!g_connector); |
167 | 158 |
168 test_helper_.reset(); | 159 test_helper_.reset(); |
169 | 160 |
170 if (ShouldCreateDefaultRunLoop()) | 161 if (ShouldCreateDefaultRunLoop()) |
171 Environment::DestroyDefaultRunLoop(); | 162 Environment::DestroyDefaultRunLoop(); |
172 } | 163 } |
173 | 164 |
174 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() { | 165 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() { |
175 return true; | 166 return true; |
176 } | 167 } |
177 | 168 |
178 } // namespace test | 169 } // namespace test |
179 } // namespace mojo | 170 } // namespace mojo |
OLD | NEW |