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/public/bindings/allocation_scope.h" | |
6 #include "mojo/public/bindings/remote_ptr.h" | |
7 #include "mojo/public/environment/environment.h" | |
8 #include "mojo/public/shell/application.h" | |
9 #include "mojo/public/utility/run_loop.h" | |
10 #include "mojo/shell/service_connector.h" | |
11 #include "mojom/shell.h" | |
12 #include "mojom/test.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace mojo { | |
16 namespace shell { | |
17 namespace { | |
18 | |
19 const char kTestURLString[] = "test:testService"; | |
20 | |
21 struct TestContext { | |
22 TestContext() : num_impls(0) {} | |
23 std::string last_test_string; | |
24 int num_impls; | |
25 }; | |
26 | |
27 class TestServiceImpl : | |
28 public Service<TestService, TestServiceImpl, TestContext> { | |
29 public: | |
30 TestServiceImpl() {} | |
31 | |
32 virtual ~TestServiceImpl() { | |
33 --context()->num_impls; | |
34 } | |
35 | |
36 virtual void Test(const mojo::String& test_string) OVERRIDE { | |
37 context()->last_test_string = test_string.To<std::string>(); | |
38 client()->AckTest(); | |
39 } | |
40 | |
41 void Initialize(ServiceFactory<TestServiceImpl, TestContext>* service_factory, | |
42 ScopedMessagePipeHandle client_handle) { | |
43 Service<TestService, TestServiceImpl, TestContext>::Initialize( | |
44 service_factory, client_handle.Pass()); | |
45 ++context()->num_impls; | |
46 } | |
47 }; | |
48 | |
49 class TestClientImpl : public TestClient { | |
50 public: | |
51 explicit TestClientImpl(ScopedTestServiceHandle service_handle) | |
52 : service_(service_handle.Pass(), this), | |
53 quit_after_ack_(false) { | |
54 } | |
55 | |
56 virtual ~TestClientImpl() {} | |
57 | |
58 virtual void AckTest() OVERRIDE { | |
59 if (quit_after_ack_) | |
60 mojo::RunLoop::current()->Quit(); | |
61 } | |
62 | |
63 void Test(std::string test_string) { | |
64 AllocationScope scope; | |
65 quit_after_ack_ = true; | |
66 service_->Test(mojo::String(test_string)); | |
67 } | |
68 | |
69 private: | |
70 RemotePtr<TestService> service_; | |
71 bool quit_after_ack_; | |
72 DISALLOW_COPY_AND_ASSIGN(TestClientImpl); | |
73 }; | |
74 } // namespace | |
75 | |
76 class ServiceConnectorTest : public testing::Test, | |
77 public ServiceConnector::Loader { | |
78 public: | |
79 ServiceConnectorTest() {} | |
80 | |
81 virtual ~ServiceConnectorTest() {} | |
82 | |
83 virtual void SetUp() OVERRIDE { | |
84 GURL test_url(kTestURLString); | |
85 service_connector_.reset(new ServiceConnector); | |
86 service_connector_->SetLoaderForURL(this, test_url); | |
87 | |
88 InterfacePipe<TestService, AnyInterface> pipe; | |
89 test_client_.reset(new TestClientImpl(pipe.handle_to_self.Pass())); | |
90 service_connector_->Connect(test_url, pipe.handle_to_peer.Pass()); | |
91 } | |
92 | |
93 virtual void TearDown() OVERRIDE { | |
94 test_client_.reset(NULL); | |
95 test_app_.reset(NULL); | |
96 service_connector_.reset(NULL); | |
97 } | |
98 | |
99 virtual void Load(const GURL& url, | |
100 ScopedShellHandle shell_handle) OVERRIDE { | |
101 test_app_.reset(new Application(shell_handle.Pass())); | |
102 test_app_->AddServiceFactory( | |
103 new ServiceFactory<TestServiceImpl, TestContext>(&context_)); | |
104 } | |
105 | |
106 bool HasFactoryForTestURL() { | |
107 ServiceConnector::TestAPI connector_test_api(service_connector_.get()); | |
108 return connector_test_api.HasFactoryForURL(GURL(kTestURLString)); | |
109 } | |
110 | |
111 protected: | |
112 mojo::Environment env_; | |
113 mojo::RunLoop loop_; | |
114 TestContext context_; | |
115 scoped_ptr<Application> test_app_; | |
116 scoped_ptr<TestClientImpl> test_client_; | |
117 scoped_ptr<ServiceConnector> service_connector_; | |
118 DISALLOW_COPY_AND_ASSIGN(ServiceConnectorTest); | |
119 }; | |
120 | |
121 TEST_F(ServiceConnectorTest, Basic) { | |
122 test_client_->Test("test"); | |
123 loop_.Run(); | |
124 EXPECT_EQ(std::string("test"), context_.last_test_string); | |
125 } | |
126 | |
127 TEST_F(ServiceConnectorTest, ClientError) { | |
128 test_client_->Test("test"); | |
129 EXPECT_TRUE(HasFactoryForTestURL()); | |
130 loop_.Run(); | |
131 EXPECT_EQ(1, context_.num_impls); | |
132 test_client_.reset(NULL); | |
133 loop_.Run(); | |
134 EXPECT_EQ(0, context_.num_impls); | |
135 EXPECT_FALSE(HasFactoryForTestURL()); | |
136 } | |
137 } // namespace shell | |
138 } // namespace mojo | |
OLD | NEW |