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

Unified Diff: mojo/shell/shell_test_base_unittest.cc

Issue 349303006: mojo: add some end-to-end shell tests and a new test sample app (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: no icu Created 6 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/shell/shell_test_base.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/shell/shell_test_base_unittest.cc
diff --git a/mojo/shell/shell_test_base_unittest.cc b/mojo/shell/shell_test_base_unittest.cc
index 4b861f90ff97bde7c699c56312ab51f178ddd968..a5529f731d4abe2c02ddbc79ec56a583a76cb264 100644
--- a/mojo/shell/shell_test_base_unittest.cc
+++ b/mojo/shell/shell_test_base_unittest.cc
@@ -5,21 +5,75 @@
#include "mojo/shell/shell_test_base.h"
#include "base/bind.h"
+#include "base/i18n/time_formatting.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
+#include "base/strings/utf_string_conversions.h"
#include "mojo/public/cpp/bindings/error_handler.h"
#include "mojo/public/cpp/bindings/interface_ptr.h"
#include "mojo/public/cpp/system/core.h"
+#include "mojo/services/test_service/test_request_tracker.mojom.h"
#include "mojo/services/test_service/test_service.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
+using mojo::test::ServiceReport;
+using mojo::test::ServiceReportPtr;
+using mojo::test::TestService;
+using mojo::test::TestTimeService;
+using mojo::test::TestServicePtr;
+using mojo::test::TestTimeServicePtr;
+using mojo::test::TestTrackedRequestService;
+using mojo::test::TestTrackedRequestServicePtr;
+
namespace mojo {
namespace shell {
namespace test {
namespace {
-typedef ShellTestBase ShellTestBaseTest;
+void GetReportCallback(base::MessageLoop* loop,
+ std::vector<ServiceReport>* reports_out,
+ mojo::Array<ServiceReportPtr> report) {
+ for (size_t i = 0; i < report.size(); i++)
+ reports_out->push_back(*report[i]);
+ loop->QuitWhenIdle();
+}
+
+class ShellTestBaseTest : public ShellTestBase {
+ public:
+ // Convenience helpers for use as callbacks in tests.
+ template <typename T>
+ base::Callback<void()> SetAndQuit(T* val, T result) {
+ return base::Bind(&ShellTestBaseTest::SetAndQuitImpl<T>,
+ base::Unretained(this), val, result);
+ }
+ template <typename T>
+ base::Callback<void(T result)> SetAndQuit(T* val) {
+ return base::Bind(&ShellTestBaseTest::SetAndQuitImpl<T>,
+ base::Unretained(this), val);
+ }
+ static GURL test_app_url() {
+ return GURL("mojo:mojo_test_app");
+ }
+
+ void GetReport(std::vector<ServiceReport>* report) {
+ request_tracking_.Bind(
+ ConnectToService(GURL("mojo:mojo_test_request_tracker_app"),
+ TestTrackedRequestService::Name_).Pass());
+ request_tracking_->GetReport(base::Bind(&GetReportCallback,
+ base::Unretained(message_loop()),
+ base::Unretained(report)));
+ message_loop()->Run();
+ }
+
+ private:
+ template<typename T>
+ void SetAndQuitImpl(T* val, T result) {
+ *val = result;
+ message_loop()->QuitWhenIdle();
+ }
+ TestTrackedRequestServicePtr request_tracking_;
+};
class QuitMessageLoopErrorHandler : public ErrorHandler {
public:
@@ -35,53 +89,33 @@ class QuitMessageLoopErrorHandler : public ErrorHandler {
DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler);
};
-void PingCallback(base::MessageLoop* message_loop, bool* was_run) {
- *was_run = true;
- VLOG(2) << "Ping callback";
- message_loop->QuitWhenIdle();
-}
-
-TEST_F(ShellTestBaseTest, LaunchServiceInProcess) {
- InterfacePtr<mojo::test::ITestService> test_service;
-
- {
- ScopedMessagePipeHandle service_handle =
- LaunchServiceInProcess(GURL("mojo:mojo_test_service"),
- mojo::test::ITestService::Name_);
- test_service.Bind(service_handle.Pass());
- }
+// Tests that we can connect to a single service within a single app.
+TEST_F(ShellTestBaseTest, ConnectBasic) {
+ InterfacePtr<TestService> service;
+ service.Bind(ConnectToService(test_app_url(), TestService::Name_).Pass());
bool was_run = false;
- test_service->Ping(base::Bind(&PingCallback,
- base::Unretained(message_loop()),
- base::Unretained(&was_run)));
+ service->Ping(SetAndQuit<bool>(&was_run, true));
message_loop()->Run();
EXPECT_TRUE(was_run);
- EXPECT_FALSE(test_service.encountered_error());
+ EXPECT_FALSE(service.encountered_error());
- test_service.reset();
+ service.reset();
- // This will run until the test service has actually quit (which it will,
+ // This will run until the test app has actually quit (which it will,
// since we killed the only connection to it).
message_loop()->Run();
}
-// Tests that launching a service in process fails properly if the service
+// Tests that trying to connect to a service fails properly if the service
// doesn't exist.
-TEST_F(ShellTestBaseTest, LaunchServiceInProcessInvalidService) {
- InterfacePtr<mojo::test::ITestService> test_service;
-
- {
- ScopedMessagePipeHandle service_handle =
- LaunchServiceInProcess(GURL("mojo:non_existent_service"),
- mojo::test::ITestService::Name_);
- test_service.Bind(service_handle.Pass());
- }
+TEST_F(ShellTestBaseTest, ConnectInvalidService) {
+ InterfacePtr<TestService> test_service;
+ test_service.Bind(ConnectToService(GURL("mojo:non_existent_service"),
+ TestService::Name_).Pass());
bool was_run = false;
- test_service->Ping(base::Bind(&PingCallback,
- base::Unretained(message_loop()),
- base::Unretained(&was_run)));
+ test_service->Ping(SetAndQuit<bool>(&was_run, true));
// This will quit because there's nothing running.
message_loop()->Run();
@@ -98,6 +132,136 @@ TEST_F(ShellTestBaseTest, LaunchServiceInProcessInvalidService) {
test_service.reset();
}
+// Similar to ConnectBasic, but causes the app to instantiate multiple
+// service implementation objects and verifies the shell can reach both.
+TEST_F(ShellTestBaseTest, ConnectMultipleInstancesPerApp) {
+ {
+ TestServicePtr service1, service2;
+ service1.Bind(ConnectToService(test_app_url(), TestService::Name_).Pass());
+ service2.Bind(ConnectToService(test_app_url(), TestService::Name_).Pass());
+
+ bool was_run1 = false;
+ bool was_run2 = false;
+ service1->Ping(SetAndQuit<bool>(&was_run1, true));
+ message_loop()->Run();
+ service2->Ping(SetAndQuit<bool>(&was_run2, true));
+ message_loop()->Run();
+ EXPECT_TRUE(was_run1);
+ EXPECT_TRUE(was_run2);
+ EXPECT_FALSE(service1.encountered_error());
+ EXPECT_FALSE(service2.encountered_error());
+ }
+ message_loop()->Run();
+}
+
+// Tests that service A and service B, both in App 1, can talk to each other
+// and parameters are passed around properly.
+TEST_F(ShellTestBaseTest, ConnectDifferentServicesInSingleApp) {
+ // Have a TestService GetPartyTime on a TestTimeService in the same app.
+ int64 time_message;
+ TestServicePtr service;
+ service.Bind(ConnectToService(test_app_url(), TestService::Name_).Pass());
+ service->ConnectToAppAndGetTime(test_app_url().spec(),
+ SetAndQuit<int64>(&time_message));
+ message_loop()->Run();
+
+ // Verify by hitting the TimeService directly.
+ TestTimeServicePtr time_service;
+ time_service.Bind(
+ ConnectToService(test_app_url(), TestTimeService::Name_).Pass());
+ int64 party_time;
+ time_service->GetPartyTime(SetAndQuit<int64>(&party_time));
+ message_loop()->Run();
+
+ EXPECT_EQ(time_message, party_time);
+}
+
+// Tests that a service A in App 1 can talk to service B in App 2 and
+// parameters are passed around properly.
+TEST_F(ShellTestBaseTest, ConnectDifferentServicesInDifferentApps) {
+ int64 time_message;
+ TestServicePtr service;
+ service.Bind(ConnectToService(test_app_url(), TestService::Name_).Pass());
+ service->ConnectToAppAndGetTime("mojo:mojo_test_request_tracker_app",
+ SetAndQuit<int64>(&time_message));
+ message_loop()->Run();
+
+ // Verify by hitting the TimeService in the request tracker app directly.
+ TestTimeServicePtr time_service;
+ time_service.Bind(ConnectToService(GURL("mojo:mojo_test_request_tracker_app"),
+ TestTimeService::Name_).Pass());
+ int64 party_time;
+ time_service->GetPartyTime(SetAndQuit<int64>(&party_time));
+ message_loop()->Run();
+
+ EXPECT_EQ(time_message, party_time);
+}
+
+// Tests that service A in App 1 can be a client of service B in App 2.
+TEST_F(ShellTestBaseTest, ConnectServiceAsClientOfSeparateApp) {
+ TestServicePtr service;
+ service.Bind(ConnectToService(test_app_url(), TestService::Name_).Pass());
+ service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure());
+ service->Ping(mojo::Callback<void()>());
+ message_loop()->Run();
+
+ for (int i = 0; i < 8; i++)
+ service->Ping(mojo::Callback<void()>());
+ service->Ping(message_loop()->QuitWhenIdleClosure());
+ message_loop()->Run();
+
+ // If everything worked properly, the tracking service should report
+ // 10 pings to TestService.
+ std::vector<ServiceReport> reports;
+ GetReport(&reports);
+ ASSERT_EQ(1U, reports.size());
+ EXPECT_EQ(TestService::Name_, reports[0].service_name);
+ EXPECT_EQ(10U, reports[0].total_requests);
+}
+
+// Connect several services together and use the tracking service to verify
+// communication.
+TEST_F(ShellTestBaseTest, ConnectManyClientsAndServices) {
+ TestServicePtr service;
+ TestTimeServicePtr time_service;
+
+ // Make a request to the TestService and have it contact TimeService in the
+ // tracking app. Do all this with tracking enabled, meaning both services
+ // are connected as clients of the TrackedRequestService.
+ service.Bind(ConnectToService(test_app_url(), TestService::Name_).Pass());
+ service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure());
+ message_loop()->Run();
+ for (int i = 0; i < 5; i++)
+ service->Ping(mojo::Callback<void()>());
+ int64 time_result;
+ service->ConnectToAppAndGetTime("mojo:mojo_test_request_tracker_app",
+ SetAndQuit<int64>(&time_result));
+ message_loop()->Run();
+
+ // Also make a few requests to the TimeService in the test_app.
+ time_service.Bind(
+ ConnectToService(test_app_url(), TestTimeService::Name_).Pass());
+ time_service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure());
+ time_service->GetPartyTime(mojo::Callback<void(uint64_t)>());
+ message_loop()->Run();
+ for (int i = 0; i < 18; i++)
+ time_service->GetPartyTime(mojo::Callback<void(uint64_t)>());
+ // Flush the tasks with one more to quit.
+ int64 party_time = 0;
+ time_service->GetPartyTime(SetAndQuit<int64>(&party_time));
+ message_loop()->Run();
+
+ std::vector<ServiceReport> reports;
+ GetReport(&reports);
+ ASSERT_EQ(3U, reports.size());
+ EXPECT_EQ(TestService::Name_, reports[0].service_name);
+ EXPECT_EQ(6U, reports[0].total_requests);
+ EXPECT_EQ(TestTimeService::Name_, reports[1].service_name);
+ EXPECT_EQ(1U, reports[1].total_requests);
+ EXPECT_EQ(TestTimeService::Name_, reports[2].service_name);
+ EXPECT_EQ(20U, reports[2].total_requests);
+}
+
} // namespace
} // namespace test
} // namespace shell
« no previous file with comments | « mojo/shell/shell_test_base.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698