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 "shell/shell_test_base.h" | 5 #include "shell/shell_test_base.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/i18n/time_formatting.h" | 8 #include "base/i18n/time_formatting.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "mojo/public/cpp/bindings/error_handler.h" | 12 #include "mojo/public/cpp/bindings/error_handler.h" |
13 #include "mojo/public/cpp/bindings/interface_ptr.h" | 13 #include "mojo/public/cpp/bindings/interface_ptr.h" |
14 #include "mojo/public/cpp/system/core.h" | 14 #include "mojo/public/cpp/system/core.h" |
15 #include "services/test_service/test_request_tracker.mojom.h" | 15 #include "services/test_service/test_request_tracker.mojom.h" |
16 #include "services/test_service/test_service.mojom.h" | 16 #include "services/test_service/test_service.mojom.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 using mojo::test::ServiceReport; | 20 using mojo::test::ServiceReport; |
21 using mojo::test::ServiceReportPtr; | 21 using mojo::test::ServiceReportPtr; |
22 using mojo::test::TestService; | 22 using mojo::test::TestService; |
23 using mojo::test::TestTimeService; | 23 using mojo::test::TestTimeService; |
24 using mojo::test::TestServicePtr; | 24 using mojo::test::TestServicePtr; |
25 using mojo::test::TestTimeServicePtr; | 25 using mojo::test::TestTimeServicePtr; |
26 using mojo::test::TestTrackedRequestService; | 26 using mojo::test::TestTrackedRequestService; |
27 using mojo::test::TestTrackedRequestServicePtr; | 27 using mojo::test::TestTrackedRequestServicePtr; |
28 | 28 |
29 namespace mojo { | |
30 namespace shell { | 29 namespace shell { |
31 namespace test { | 30 namespace test { |
32 namespace { | 31 namespace { |
33 | 32 |
34 void GetReportCallback(base::MessageLoop* loop, | 33 void GetReportCallback(base::MessageLoop* loop, |
35 std::vector<ServiceReport>* reports_out, | 34 std::vector<ServiceReport>* reports_out, |
36 Array<ServiceReportPtr> report) { | 35 mojo::Array<ServiceReportPtr> report) { |
37 for (size_t i = 0; i < report.size(); i++) | 36 for (size_t i = 0; i < report.size(); i++) |
38 reports_out->push_back(*report[i]); | 37 reports_out->push_back(*report[i]); |
39 loop->QuitWhenIdle(); | 38 loop->QuitWhenIdle(); |
40 } | 39 } |
41 | 40 |
42 class ShellTestBaseTest : public ShellTestBase { | 41 class ShellTestBaseTest : public ShellTestBase { |
43 public: | 42 public: |
44 // Convenience helpers for use as callbacks in tests. | 43 // Convenience helpers for use as callbacks in tests. |
45 template <typename T> | 44 template <typename T> |
46 base::Callback<void()> SetAndQuit(T* val, T result) { | 45 base::Callback<void()> SetAndQuit(T* val, T result) { |
(...skipping 17 matching lines...) Expand all Loading... |
64 | 63 |
65 private: | 64 private: |
66 template <typename T> | 65 template <typename T> |
67 void SetAndQuitImpl(T* val, T result) { | 66 void SetAndQuitImpl(T* val, T result) { |
68 *val = result; | 67 *val = result; |
69 message_loop()->QuitWhenIdle(); | 68 message_loop()->QuitWhenIdle(); |
70 } | 69 } |
71 TestTrackedRequestServicePtr request_tracking_; | 70 TestTrackedRequestServicePtr request_tracking_; |
72 }; | 71 }; |
73 | 72 |
74 class QuitMessageLoopErrorHandler : public ErrorHandler { | 73 class QuitMessageLoopErrorHandler : public mojo::ErrorHandler { |
75 public: | 74 public: |
76 QuitMessageLoopErrorHandler() {} | 75 QuitMessageLoopErrorHandler() {} |
77 ~QuitMessageLoopErrorHandler() override {} | 76 ~QuitMessageLoopErrorHandler() override {} |
78 | 77 |
79 // |ErrorHandler| implementation: | 78 // |mojo::ErrorHandler| implementation: |
80 void OnConnectionError() override { | 79 void OnConnectionError() override { |
81 base::MessageLoop::current()->QuitWhenIdle(); | 80 base::MessageLoop::current()->QuitWhenIdle(); |
82 } | 81 } |
83 | 82 |
84 private: | 83 private: |
85 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler); | 84 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler); |
86 }; | 85 }; |
87 | 86 |
88 // Tests that we can connect to a single service within a single app. | 87 // Tests that we can connect to a single service within a single app. |
89 TEST_F(ShellTestBaseTest, ConnectBasic) { | 88 TEST_F(ShellTestBaseTest, ConnectBasic) { |
90 InterfacePtr<TestService> service; | 89 TestServicePtr service; |
91 ConnectToService(test_app_url(), &service); | 90 ConnectToService(test_app_url(), &service); |
92 | 91 |
93 bool was_run = false; | 92 bool was_run = false; |
94 service->Ping(SetAndQuit<bool>(&was_run, true)); | 93 service->Ping(SetAndQuit<bool>(&was_run, true)); |
95 message_loop()->Run(); | 94 message_loop()->Run(); |
96 EXPECT_TRUE(was_run); | 95 EXPECT_TRUE(was_run); |
97 EXPECT_FALSE(service.encountered_error()); | 96 EXPECT_FALSE(service.encountered_error()); |
98 | 97 |
99 service.reset(); | 98 service.reset(); |
100 | 99 |
101 // This will run until the test app has actually quit (which it will, | 100 // This will run until the test app has actually quit (which it will, |
102 // since we killed the only connection to it). | 101 // since we killed the only connection to it). |
103 message_loop()->Run(); | 102 message_loop()->Run(); |
104 } | 103 } |
105 | 104 |
106 // Tests that trying to connect to a service fails properly if the service | 105 // Tests that trying to connect to a service fails properly if the service |
107 // doesn't exist. Implicit in this test is verification that the shell | 106 // doesn't exist. Implicit in this test is verification that the shell |
108 // terminates if no services are running. | 107 // terminates if no services are running. |
109 TEST_F(ShellTestBaseTest, ConnectInvalidService) { | 108 TEST_F(ShellTestBaseTest, ConnectInvalidService) { |
110 InterfacePtr<TestService> test_service; | 109 TestServicePtr test_service; |
111 ConnectToService(GURL("mojo:non_existent_service"), &test_service); | 110 ConnectToService(GURL("mojo:non_existent_service"), &test_service); |
112 | 111 |
113 bool was_run = false; | 112 bool was_run = false; |
114 test_service->Ping(SetAndQuit<bool>(&was_run, true)); | 113 test_service->Ping(SetAndQuit<bool>(&was_run, true)); |
115 | 114 |
116 // This will quit because there's nothing running. | 115 // This will quit because there's nothing running. |
117 message_loop()->Run(); | 116 message_loop()->Run(); |
118 EXPECT_FALSE(was_run); | 117 EXPECT_FALSE(was_run); |
119 | 118 |
120 // It may have quit before an error was processed. | 119 // It may have quit before an error was processed. |
121 if (!test_service.encountered_error()) { | 120 if (!test_service.encountered_error()) { |
122 QuitMessageLoopErrorHandler quitter; | 121 QuitMessageLoopErrorHandler quitter; |
123 test_service.set_error_handler(&quitter); | 122 test_service.set_error_handler(&quitter); |
124 message_loop()->Run(); | 123 message_loop()->Run(); |
125 EXPECT_TRUE(test_service.encountered_error()); | 124 EXPECT_TRUE(test_service.encountered_error()); |
126 } | 125 } |
127 | 126 |
128 test_service.reset(); | 127 test_service.reset(); |
129 } | 128 } |
130 | 129 |
131 // Tests that we can connect to a single service within a single app using | 130 // Tests that we can connect to a single service within a single app using |
132 // a network based loader instead of local files. | 131 // a network based loader instead of local files. |
133 // TODO(tim): Disabled because network service leaks NSS at exit, meaning | 132 // TODO(tim): Disabled because network service leaks NSS at exit, meaning |
134 // subsequent tests can't init properly. | 133 // subsequent tests can't init properly. |
135 TEST_F(ShellTestBaseTest, DISABLED_ConnectBasicNetwork) { | 134 TEST_F(ShellTestBaseTest, DISABLED_ConnectBasicNetwork) { |
136 InterfacePtr<TestService> service; | 135 TestServicePtr service; |
137 ConnectToService(test_app_url(), &service); | 136 ConnectToService(test_app_url(), &service); |
138 | 137 |
139 bool was_run = false; | 138 bool was_run = false; |
140 service->Ping(SetAndQuit<bool>(&was_run, true)); | 139 service->Ping(SetAndQuit<bool>(&was_run, true)); |
141 message_loop()->Run(); | 140 message_loop()->Run(); |
142 EXPECT_TRUE(was_run); | 141 EXPECT_TRUE(was_run); |
143 EXPECT_FALSE(service.encountered_error()); | 142 EXPECT_FALSE(service.encountered_error()); |
144 | 143 |
145 // Note that use of the network service is implicit in this test. | 144 // Note that use of the network service is implicit in this test. |
146 // Since TestService is not the only service in use, the shell won't auto | 145 // Since TestService is not the only service in use, the shell won't auto |
147 // magically exit when TestService is destroyed (unlike ConnectBasic). | 146 // magically exit when TestService is destroyed (unlike ConnectBasic). |
148 // Tearing down the shell context will kill connections. The shell loop will | 147 // Tearing down the shell context will kill connections. The shell loop will |
149 // exit as soon as no more apps are connected. | 148 // exit as soon as no more apps are connected. |
150 // TODO(tim): crbug.com/392685. Calling this explicitly shouldn't be | 149 // TODO(tim): crbug.com/392685. Calling this explicitly shouldn't be |
151 // necessary once the shell terminates if the primordial app exits, which | 150 // necessary once the shell terminates if the primordial app exits, which |
152 // we could enforce here by resetting |service|. | 151 // we could enforce here by resetting |service|. |
153 shell_context()->application_manager()->TerminateShellConnections(); | 152 shell_context()->application_manager()->TerminateShellConnections(); |
154 message_loop()->Run(); // Waits for all connections to die. | 153 message_loop()->Run(); // Waits for all connections to die. |
155 } | 154 } |
156 | 155 |
157 // Tests that trying to connect to a service over network fails preoprly | 156 // Tests that trying to connect to a service over network fails preoprly |
158 // if the service doesn't exist. | 157 // if the service doesn't exist. |
159 // TODO(tim): Disabled because network service leaks NSS at exit, meaning | 158 // TODO(tim): Disabled because network service leaks NSS at exit, meaning |
160 // subsequent tests can't init properly. | 159 // subsequent tests can't init properly. |
161 TEST_F(ShellTestBaseTest, DISABLED_ConnectInvalidServiceNetwork) { | 160 TEST_F(ShellTestBaseTest, DISABLED_ConnectInvalidServiceNetwork) { |
162 InterfacePtr<TestService> test_service; | 161 TestServicePtr test_service; |
163 ConnectToService(GURL("http://example.com/non_existent_service"), | 162 ConnectToService(GURL("http://example.com/non_existent_service"), |
164 &test_service); | 163 &test_service); |
165 QuitMessageLoopErrorHandler quitter; | 164 QuitMessageLoopErrorHandler quitter; |
166 test_service.set_error_handler(&quitter); | 165 test_service.set_error_handler(&quitter); |
167 bool was_run = false; | 166 bool was_run = false; |
168 test_service->Ping(SetAndQuit<bool>(&was_run, true)); | 167 test_service->Ping(SetAndQuit<bool>(&was_run, true)); |
169 message_loop()->Run(); | 168 message_loop()->Run(); |
170 EXPECT_TRUE(test_service.encountered_error()); | 169 EXPECT_TRUE(test_service.encountered_error()); |
171 | 170 |
172 // TODO(tim): crbug.com/392685. Calling this explicitly shouldn't be | 171 // TODO(tim): crbug.com/392685. Calling this explicitly shouldn't be |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 message_loop()->Run(); | 236 message_loop()->Run(); |
238 | 237 |
239 EXPECT_EQ(time_message, party_time); | 238 EXPECT_EQ(time_message, party_time); |
240 } | 239 } |
241 | 240 |
242 // Tests that service A in App 1 can be a client of service B in App 2. | 241 // Tests that service A in App 1 can be a client of service B in App 2. |
243 TEST_F(ShellTestBaseTest, ConnectServiceAsClientOfSeparateApp) { | 242 TEST_F(ShellTestBaseTest, ConnectServiceAsClientOfSeparateApp) { |
244 TestServicePtr service; | 243 TestServicePtr service; |
245 ConnectToService(test_app_url(), &service); | 244 ConnectToService(test_app_url(), &service); |
246 service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); | 245 service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); |
247 service->Ping(Callback<void()>()); | 246 service->Ping(mojo::Callback<void()>()); |
248 message_loop()->Run(); | 247 message_loop()->Run(); |
249 | 248 |
250 for (int i = 0; i < 8; i++) | 249 for (int i = 0; i < 8; i++) |
251 service->Ping(Callback<void()>()); | 250 service->Ping(mojo::Callback<void()>()); |
252 service->Ping(message_loop()->QuitWhenIdleClosure()); | 251 service->Ping(message_loop()->QuitWhenIdleClosure()); |
253 message_loop()->Run(); | 252 message_loop()->Run(); |
254 | 253 |
255 // If everything worked properly, the tracking service should report | 254 // If everything worked properly, the tracking service should report |
256 // 10 pings to TestService. | 255 // 10 pings to TestService. |
257 std::vector<ServiceReport> reports; | 256 std::vector<ServiceReport> reports; |
258 GetReport(&reports); | 257 GetReport(&reports); |
259 ASSERT_EQ(1U, reports.size()); | 258 ASSERT_EQ(1U, reports.size()); |
260 EXPECT_EQ(TestService::Name_, reports[0].service_name); | 259 EXPECT_EQ(TestService::Name_, reports[0].service_name); |
261 EXPECT_EQ(10U, reports[0].total_requests); | 260 EXPECT_EQ(10U, reports[0].total_requests); |
262 } | 261 } |
263 | 262 |
264 // Connect several services together and use the tracking service to verify | 263 // Connect several services together and use the tracking service to verify |
265 // communication. | 264 // communication. |
266 TEST_F(ShellTestBaseTest, ConnectManyClientsAndServices) { | 265 TEST_F(ShellTestBaseTest, ConnectManyClientsAndServices) { |
267 TestServicePtr service; | 266 TestServicePtr service; |
268 TestTimeServicePtr time_service; | 267 TestTimeServicePtr time_service; |
269 | 268 |
270 // Make a request to the TestService and have it contact TimeService in the | 269 // Make a request to the TestService and have it contact TimeService in the |
271 // tracking app. Do all this with tracking enabled, meaning both services | 270 // tracking app. Do all this with tracking enabled, meaning both services |
272 // are connected as clients of the TrackedRequestService. | 271 // are connected as clients of the TrackedRequestService. |
273 ConnectToService(test_app_url(), &service); | 272 ConnectToService(test_app_url(), &service); |
274 service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); | 273 service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); |
275 message_loop()->Run(); | 274 message_loop()->Run(); |
276 for (int i = 0; i < 5; i++) | 275 for (int i = 0; i < 5; i++) |
277 service->Ping(Callback<void()>()); | 276 service->Ping(mojo::Callback<void()>()); |
278 int64 time_result; | 277 int64 time_result; |
279 service->ConnectToAppAndGetTime("mojo:test_request_tracker_app", | 278 service->ConnectToAppAndGetTime("mojo:test_request_tracker_app", |
280 SetAndQuit<int64>(&time_result)); | 279 SetAndQuit<int64>(&time_result)); |
281 message_loop()->Run(); | 280 message_loop()->Run(); |
282 | 281 |
283 // Also make a few requests to the TimeService in the test_app. | 282 // Also make a few requests to the TimeService in the test_app. |
284 ConnectToService(test_app_url(), &time_service); | 283 ConnectToService(test_app_url(), &time_service); |
285 time_service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); | 284 time_service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); |
286 time_service->GetPartyTime(Callback<void(uint64_t)>()); | 285 time_service->GetPartyTime(mojo::Callback<void(uint64_t)>()); |
287 message_loop()->Run(); | 286 message_loop()->Run(); |
288 for (int i = 0; i < 18; i++) | 287 for (int i = 0; i < 18; i++) |
289 time_service->GetPartyTime(Callback<void(uint64_t)>()); | 288 time_service->GetPartyTime(mojo::Callback<void(uint64_t)>()); |
290 // Flush the tasks with one more to quit. | 289 // Flush the tasks with one more to quit. |
291 int64 party_time = 0; | 290 int64 party_time = 0; |
292 time_service->GetPartyTime(SetAndQuit<int64>(&party_time)); | 291 time_service->GetPartyTime(SetAndQuit<int64>(&party_time)); |
293 message_loop()->Run(); | 292 message_loop()->Run(); |
294 | 293 |
295 std::vector<ServiceReport> reports; | 294 std::vector<ServiceReport> reports; |
296 GetReport(&reports); | 295 GetReport(&reports); |
297 ASSERT_EQ(3U, reports.size()); | 296 ASSERT_EQ(3U, reports.size()); |
298 EXPECT_EQ(TestService::Name_, reports[0].service_name); | 297 EXPECT_EQ(TestService::Name_, reports[0].service_name); |
299 EXPECT_EQ(6U, reports[0].total_requests); | 298 EXPECT_EQ(6U, reports[0].total_requests); |
300 EXPECT_EQ(TestTimeService::Name_, reports[1].service_name); | 299 EXPECT_EQ(TestTimeService::Name_, reports[1].service_name); |
301 EXPECT_EQ(1U, reports[1].total_requests); | 300 EXPECT_EQ(1U, reports[1].total_requests); |
302 EXPECT_EQ(TestTimeService::Name_, reports[2].service_name); | 301 EXPECT_EQ(TestTimeService::Name_, reports[2].service_name); |
303 EXPECT_EQ(20U, reports[2].total_requests); | 302 EXPECT_EQ(20U, reports[2].total_requests); |
304 } | 303 } |
305 | 304 |
306 } // namespace | 305 } // namespace |
307 } // namespace test | 306 } // namespace test |
308 } // namespace shell | 307 } // namespace shell |
309 } // namespace mojo | |
OLD | NEW |