| 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 "mojo/runner/shell_test_base.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/i18n/time_formatting.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
| 16 #include "mojo/public/cpp/system/core.h" | |
| 17 #include "mojo/services/test_service/test_request_tracker.mojom.h" | |
| 18 #include "mojo/services/test_service/test_service.mojom.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 using mojo::test::ServiceReport; | |
| 23 using mojo::test::ServiceReportPtr; | |
| 24 using mojo::test::TestService; | |
| 25 using mojo::test::TestTimeService; | |
| 26 using mojo::test::TestServicePtr; | |
| 27 using mojo::test::TestTimeServicePtr; | |
| 28 using mojo::test::TestTrackedRequestService; | |
| 29 using mojo::test::TestTrackedRequestServicePtr; | |
| 30 | |
| 31 namespace mojo { | |
| 32 namespace runner { | |
| 33 namespace test { | |
| 34 namespace { | |
| 35 | |
| 36 void GetReportCallback(base::MessageLoop* loop, | |
| 37 std::vector<ServiceReport>* reports_out, | |
| 38 Array<ServiceReportPtr> report) { | |
| 39 for (size_t i = 0; i < report.size(); i++) | |
| 40 reports_out->push_back(*report[i]); | |
| 41 loop->QuitWhenIdle(); | |
| 42 } | |
| 43 | |
| 44 class ShellTestBaseTest : public ShellTestBase { | |
| 45 public: | |
| 46 // Convenience helpers for use as callbacks in tests. | |
| 47 template <typename T> | |
| 48 base::Callback<void()> SetAndQuit(T* val, T result) { | |
| 49 return base::Bind(&ShellTestBaseTest::SetAndQuitImpl<T>, | |
| 50 base::Unretained(this), val, result); | |
| 51 } | |
| 52 template <typename T> | |
| 53 base::Callback<void(T result)> SetAndQuit(T* val) { | |
| 54 return base::Bind(&ShellTestBaseTest::SetAndQuitImpl<T>, | |
| 55 base::Unretained(this), val); | |
| 56 } | |
| 57 static GURL test_app_url() { return GURL("mojo:test_app"); } | |
| 58 | |
| 59 void GetReport(std::vector<ServiceReport>* report) { | |
| 60 ConnectToService(GURL("mojo:test_request_tracker_app"), &request_tracking_); | |
| 61 request_tracking_->GetReport(base::Bind(&GetReportCallback, | |
| 62 base::Unretained(message_loop()), | |
| 63 base::Unretained(report))); | |
| 64 message_loop()->Run(); | |
| 65 } | |
| 66 | |
| 67 private: | |
| 68 template <typename T> | |
| 69 void SetAndQuitImpl(T* val, T result) { | |
| 70 *val = result; | |
| 71 message_loop()->QuitWhenIdle(); | |
| 72 } | |
| 73 TestTrackedRequestServicePtr request_tracking_; | |
| 74 }; | |
| 75 | |
| 76 // Tests that we can connect to a single service within a single app. | |
| 77 TEST_F(ShellTestBaseTest, ConnectBasic) { | |
| 78 InterfacePtr<TestService> service; | |
| 79 ConnectToService(test_app_url(), &service); | |
| 80 | |
| 81 bool was_run = false; | |
| 82 service->Ping(SetAndQuit<bool>(&was_run, true)); | |
| 83 message_loop()->Run(); | |
| 84 EXPECT_TRUE(was_run); | |
| 85 EXPECT_FALSE(service.encountered_error()); | |
| 86 | |
| 87 service.reset(); | |
| 88 | |
| 89 // This will run until the test app has actually quit (which it will, | |
| 90 // since we killed the only connection to it). | |
| 91 message_loop()->Run(); | |
| 92 } | |
| 93 | |
| 94 // Tests that trying to connect to a service fails properly if the service | |
| 95 // doesn't exist. Implicit in this test is verification that the shell | |
| 96 // terminates if no services are running. | |
| 97 TEST_F(ShellTestBaseTest, ConnectInvalidService) { | |
| 98 InterfacePtr<TestService> test_service; | |
| 99 ConnectToService(GURL("mojo:non_existent_service"), &test_service); | |
| 100 | |
| 101 bool was_run = false; | |
| 102 test_service->Ping(SetAndQuit<bool>(&was_run, true)); | |
| 103 | |
| 104 // This will quit because there's nothing running. | |
| 105 message_loop()->Run(); | |
| 106 EXPECT_FALSE(was_run); | |
| 107 | |
| 108 // It may have quit before an error was processed. | |
| 109 if (!test_service.encountered_error()) { | |
| 110 test_service.set_connection_error_handler( | |
| 111 []() { base::MessageLoop::current()->QuitWhenIdle(); }); | |
| 112 message_loop()->Run(); | |
| 113 EXPECT_TRUE(test_service.encountered_error()); | |
| 114 } | |
| 115 | |
| 116 test_service.reset(); | |
| 117 } | |
| 118 | |
| 119 // Tests that we can connect to a single service within a single app using | |
| 120 // a network based loader instead of local files. | |
| 121 // TODO(tim): Disabled because network service leaks NSS at exit, meaning | |
| 122 // subsequent tests can't init properly. | |
| 123 TEST_F(ShellTestBaseTest, DISABLED_ConnectBasicNetwork) { | |
| 124 InterfacePtr<TestService> service; | |
| 125 ConnectToService(test_app_url(), &service); | |
| 126 | |
| 127 bool was_run = false; | |
| 128 service->Ping(SetAndQuit<bool>(&was_run, true)); | |
| 129 message_loop()->Run(); | |
| 130 EXPECT_TRUE(was_run); | |
| 131 EXPECT_FALSE(service.encountered_error()); | |
| 132 | |
| 133 // Note that use of the network service is implicit in this test. | |
| 134 // Since TestService is not the only service in use, the shell won't auto | |
| 135 // magically exit when TestService is destroyed (unlike ConnectBasic). | |
| 136 // Tearing down the shell context will kill connections. The shell loop will | |
| 137 // exit as soon as no more apps are connected. | |
| 138 // TODO(tim): crbug.com/392685. Calling this explicitly shouldn't be | |
| 139 // necessary once the shell terminates if the primordial app exits, which | |
| 140 // we could enforce here by resetting |service|. | |
| 141 shell_context()->application_manager()->TerminateShellConnections(); | |
| 142 message_loop()->Run(); // Waits for all connections to die. | |
| 143 } | |
| 144 | |
| 145 // Tests that trying to connect to a service over network fails preoprly | |
| 146 // if the service doesn't exist. | |
| 147 // TODO(tim): Disabled because network service leaks NSS at exit, meaning | |
| 148 // subsequent tests can't init properly. | |
| 149 TEST_F(ShellTestBaseTest, DISABLED_ConnectInvalidServiceNetwork) { | |
| 150 InterfacePtr<TestService> test_service; | |
| 151 ConnectToService(GURL("http://example.com/non_existent_service"), | |
| 152 &test_service); | |
| 153 test_service.set_connection_error_handler( | |
| 154 []() { base::MessageLoop::current()->QuitWhenIdle(); }); | |
| 155 bool was_run = false; | |
| 156 test_service->Ping(SetAndQuit<bool>(&was_run, true)); | |
| 157 message_loop()->Run(); | |
| 158 EXPECT_TRUE(test_service.encountered_error()); | |
| 159 | |
| 160 // TODO(tim): crbug.com/392685. Calling this explicitly shouldn't be | |
| 161 // necessary once the shell terminates if the primordial app exits, which | |
| 162 // we could enforce here by resetting |service|. | |
| 163 shell_context()->application_manager()->TerminateShellConnections(); | |
| 164 message_loop()->Run(); // Waits for all connections to die. | |
| 165 } | |
| 166 | |
| 167 // Similar to ConnectBasic, but causes the app to instantiate multiple | |
| 168 // service implementation objects and verifies the shell can reach both. | |
| 169 TEST_F(ShellTestBaseTest, ConnectMultipleInstancesPerApp) { | |
| 170 { | |
| 171 TestServicePtr service1, service2; | |
| 172 ConnectToService(test_app_url(), &service1); | |
| 173 ConnectToService(test_app_url(), &service2); | |
| 174 | |
| 175 bool was_run1 = false; | |
| 176 bool was_run2 = false; | |
| 177 service1->Ping(SetAndQuit<bool>(&was_run1, true)); | |
| 178 message_loop()->Run(); | |
| 179 service2->Ping(SetAndQuit<bool>(&was_run2, true)); | |
| 180 message_loop()->Run(); | |
| 181 EXPECT_TRUE(was_run1); | |
| 182 EXPECT_TRUE(was_run2); | |
| 183 EXPECT_FALSE(service1.encountered_error()); | |
| 184 EXPECT_FALSE(service2.encountered_error()); | |
| 185 } | |
| 186 message_loop()->Run(); | |
| 187 } | |
| 188 | |
| 189 // Tests that service A and service B, both in App 1, can talk to each other | |
| 190 // and parameters are passed around properly. | |
| 191 TEST_F(ShellTestBaseTest, ConnectDifferentServicesInSingleApp) { | |
| 192 // Have a TestService GetPartyTime on a TestTimeService in the same app. | |
| 193 int64_t time_message; | |
| 194 TestServicePtr service; | |
| 195 ConnectToService(test_app_url(), &service); | |
| 196 service->ConnectToAppAndGetTime(test_app_url().spec(), | |
| 197 SetAndQuit<int64_t>(&time_message)); | |
| 198 message_loop()->Run(); | |
| 199 | |
| 200 // Verify by hitting the TimeService directly. | |
| 201 TestTimeServicePtr time_service; | |
| 202 ConnectToService(test_app_url(), &time_service); | |
| 203 int64_t party_time; | |
| 204 time_service->GetPartyTime(SetAndQuit<int64_t>(&party_time)); | |
| 205 message_loop()->Run(); | |
| 206 | |
| 207 EXPECT_EQ(time_message, party_time); | |
| 208 } | |
| 209 | |
| 210 // Tests that a service A in App 1 can talk to service B in App 2 and | |
| 211 // parameters are passed around properly. | |
| 212 TEST_F(ShellTestBaseTest, ConnectDifferentServicesInDifferentApps) { | |
| 213 int64_t time_message; | |
| 214 TestServicePtr service; | |
| 215 ConnectToService(test_app_url(), &service); | |
| 216 service->ConnectToAppAndGetTime("mojo:test_request_tracker_app", | |
| 217 SetAndQuit<int64_t>(&time_message)); | |
| 218 message_loop()->Run(); | |
| 219 | |
| 220 // Verify by hitting the TimeService in the request tracker app directly. | |
| 221 TestTimeServicePtr time_service; | |
| 222 ConnectToService(GURL("mojo:test_request_tracker_app"), &time_service); | |
| 223 int64_t party_time; | |
| 224 time_service->GetPartyTime(SetAndQuit<int64_t>(&party_time)); | |
| 225 message_loop()->Run(); | |
| 226 | |
| 227 EXPECT_EQ(time_message, party_time); | |
| 228 } | |
| 229 | |
| 230 // Tests that service A in App 1 can be a client of service B in App 2. | |
| 231 TEST_F(ShellTestBaseTest, ConnectServiceAsClientOfSeparateApp) { | |
| 232 TestServicePtr service; | |
| 233 ConnectToService(test_app_url(), &service); | |
| 234 service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); | |
| 235 service->Ping(Callback<void()>()); | |
| 236 message_loop()->Run(); | |
| 237 | |
| 238 for (int i = 0; i < 8; i++) | |
| 239 service->Ping(Callback<void()>()); | |
| 240 service->Ping(message_loop()->QuitWhenIdleClosure()); | |
| 241 message_loop()->Run(); | |
| 242 | |
| 243 // If everything worked properly, the tracking service should report | |
| 244 // 10 pings to TestService. | |
| 245 std::vector<ServiceReport> reports; | |
| 246 GetReport(&reports); | |
| 247 ASSERT_EQ(1U, reports.size()); | |
| 248 EXPECT_EQ(TestService::Name_, reports[0].service_name); | |
| 249 EXPECT_EQ(10U, reports[0].total_requests); | |
| 250 } | |
| 251 | |
| 252 // Connect several services together and use the tracking service to verify | |
| 253 // communication. | |
| 254 TEST_F(ShellTestBaseTest, ConnectManyClientsAndServices) { | |
| 255 TestServicePtr service; | |
| 256 TestTimeServicePtr time_service; | |
| 257 | |
| 258 // Make a request to the TestService and have it contact TimeService in the | |
| 259 // tracking app. Do all this with tracking enabled, meaning both services | |
| 260 // are connected as clients of the TrackedRequestService. | |
| 261 ConnectToService(test_app_url(), &service); | |
| 262 service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); | |
| 263 message_loop()->Run(); | |
| 264 for (int i = 0; i < 5; i++) | |
| 265 service->Ping(Callback<void()>()); | |
| 266 int64_t time_result; | |
| 267 service->ConnectToAppAndGetTime("mojo:test_request_tracker_app", | |
| 268 SetAndQuit<int64_t>(&time_result)); | |
| 269 message_loop()->Run(); | |
| 270 | |
| 271 // Also make a few requests to the TimeService in the test_app. | |
| 272 ConnectToService(test_app_url(), &time_service); | |
| 273 time_service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); | |
| 274 time_service->GetPartyTime(Callback<void(uint64_t)>()); | |
| 275 message_loop()->Run(); | |
| 276 for (int i = 0; i < 18; i++) | |
| 277 time_service->GetPartyTime(Callback<void(uint64_t)>()); | |
| 278 // Flush the tasks with one more to quit. | |
| 279 int64_t party_time = 0; | |
| 280 time_service->GetPartyTime(SetAndQuit<int64_t>(&party_time)); | |
| 281 message_loop()->Run(); | |
| 282 | |
| 283 std::vector<ServiceReport> reports; | |
| 284 GetReport(&reports); | |
| 285 ASSERT_EQ(3U, reports.size()); | |
| 286 EXPECT_EQ(TestService::Name_, reports[0].service_name); | |
| 287 EXPECT_EQ(6U, reports[0].total_requests); | |
| 288 EXPECT_EQ(TestTimeService::Name_, reports[1].service_name); | |
| 289 EXPECT_EQ(1U, reports[1].total_requests); | |
| 290 EXPECT_EQ(TestTimeService::Name_, reports[2].service_name); | |
| 291 EXPECT_EQ(20U, reports[2].total_requests); | |
| 292 } | |
| 293 | |
| 294 } // namespace | |
| 295 } // namespace test | |
| 296 } // namespace runner | |
| 297 } // namespace mojo | |
| OLD | NEW |