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

Side by Side Diff: mojo/service_manager/service_manager_unittest.cc

Issue 265793015: Mojo: Replace RemotePtr with InterfacePtr and InterfaceImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/message_loop/message_loop.h" 5 #include "base/message_loop/message_loop.h"
6 #include "mojo/public/cpp/bindings/allocation_scope.h" 6 #include "mojo/public/cpp/bindings/allocation_scope.h"
7 #include "mojo/public/cpp/bindings/remote_ptr.h"
8 #include "mojo/public/cpp/environment/environment.h" 7 #include "mojo/public/cpp/environment/environment.h"
9 #include "mojo/public/cpp/shell/application.h" 8 #include "mojo/public/cpp/shell/application.h"
10 #include "mojo/public/interfaces/shell/shell.mojom.h" 9 #include "mojo/public/interfaces/shell/shell.mojom.h"
11 #include "mojo/service_manager/service_loader.h" 10 #include "mojo/service_manager/service_loader.h"
12 #include "mojo/service_manager/service_manager.h" 11 #include "mojo/service_manager/service_manager.h"
13 #include "mojo/service_manager/test.mojom.h" 12 #include "mojo/service_manager/test.mojom.h"
14 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
15 14
16 namespace mojo { 15 namespace mojo {
17 namespace { 16 namespace {
18 17
19 const char kTestURLString[] = "test:testService"; 18 const char kTestURLString[] = "test:testService";
20 19
21 struct TestContext { 20 struct TestContext {
22 TestContext() : num_impls(0), num_loader_deletes(0) {} 21 TestContext() : num_impls(0), num_loader_deletes(0) {}
23 std::string last_test_string; 22 std::string last_test_string;
24 int num_impls; 23 int num_impls;
25 int num_loader_deletes; 24 int num_loader_deletes;
26 }; 25 };
27 26
28 class TestServiceImpl : 27 class TestServiceImpl :
29 public ServiceConnection<TestService, TestServiceImpl, TestContext> { 28 public ServiceConnection<TestService, TestServiceImpl, TestContext> {
30 public: 29 public:
31 TestServiceImpl() {} 30 TestServiceImpl() : client_(NULL) {}
32 31
33 virtual ~TestServiceImpl() { 32 virtual ~TestServiceImpl() {
34 if (context()) 33 if (context())
35 --context()->num_impls; 34 --context()->num_impls;
36 } 35 }
37 36
38 virtual void Test(const mojo::String& test_string) OVERRIDE { 37 void Initialize() {
39 context()->last_test_string = test_string.To<std::string>();
40 client()->AckTest();
41 }
42
43 void Initialize(
44 ServiceConnector<TestServiceImpl, TestContext>* service_factory,
45 ScopedMessagePipeHandle client_handle) {
46 ServiceConnection<TestService, TestServiceImpl, TestContext>::Initialize(
47 service_factory, client_handle.Pass());
48 if (context()) 38 if (context())
49 ++context()->num_impls; 39 ++context()->num_impls;
50 } 40 }
41
42 // TestService implementation:
43
44 virtual void SetClient(TestClient* client) OVERRIDE {
45 client_ = client;
46 }
47
48 virtual void Test(const mojo::String& test_string) OVERRIDE {
49 context()->last_test_string = test_string.To<std::string>();
50 client_->AckTest();
51 }
52
53 private:
54 TestClient* client_;
51 }; 55 };
52 56
53 class TestClientImpl : public TestClient { 57 class TestClientImpl : public TestClient {
54 public: 58 public:
55 explicit TestClientImpl(ScopedTestServiceHandle service_handle) 59 explicit TestClientImpl(TestServicePtr service)
56 : service_(service_handle.Pass(), this), 60 : service_(service.Pass()),
57 quit_after_ack_(false) { 61 quit_after_ack_(false) {
62 service_->SetClient(this);
58 } 63 }
59 64
60 virtual ~TestClientImpl() {} 65 virtual ~TestClientImpl() {}
61 66
62 virtual void AckTest() OVERRIDE { 67 virtual void AckTest() OVERRIDE {
63 if (quit_after_ack_) 68 if (quit_after_ack_)
64 base::MessageLoop::current()->Quit(); 69 base::MessageLoop::current()->Quit();
65 } 70 }
66 71
67 void Test(std::string test_string) { 72 void Test(std::string test_string) {
68 AllocationScope scope; 73 AllocationScope scope;
69 quit_after_ack_ = true; 74 quit_after_ack_ = true;
70 service_->Test(mojo::String(test_string)); 75 service_->Test(test_string);
71 } 76 }
72 77
73 private: 78 private:
74 RemotePtr<TestService> service_; 79 TestServicePtr service_;
75 bool quit_after_ack_; 80 bool quit_after_ack_;
76 DISALLOW_COPY_AND_ASSIGN(TestClientImpl); 81 DISALLOW_COPY_AND_ASSIGN(TestClientImpl);
77 }; 82 };
78 83
79 class TestServiceLoader : public ServiceLoader { 84 class TestServiceLoader : public ServiceLoader {
80 public: 85 public:
81 TestServiceLoader() 86 TestServiceLoader()
82 : context_(NULL), 87 : context_(NULL),
83 num_loads_(0), 88 num_loads_(0),
84 quit_after_error_(false) { 89 quit_after_error_(false) {
85 } 90 }
86 91
87 virtual ~TestServiceLoader() { 92 virtual ~TestServiceLoader() {
88 if (context_) 93 if (context_)
89 ++context_->num_loader_deletes; 94 ++context_->num_loader_deletes;
90 test_app_.reset(NULL); 95 test_app_.reset(NULL);
91 } 96 }
92 97
93 void set_context(TestContext* context) { context_ = context; } 98 void set_context(TestContext* context) { context_ = context; }
94 void set_quit_after_error(bool quit_after_error) { 99 void set_quit_after_error(bool quit_after_error) {
95 quit_after_error_ = quit_after_error; 100 quit_after_error_ = quit_after_error;
96 } 101 }
97 102
98 int num_loads() const { return num_loads_; } 103 int num_loads() const { return num_loads_; }
99 104
100 private: 105 private:
101 virtual void LoadService(ServiceManager* manager, 106 virtual void LoadService(ServiceManager* manager,
102 const GURL& url, 107 const GURL& url,
103 ScopedShellHandle shell_handle) OVERRIDE { 108 ScopedMessagePipeHandle shell_handle) OVERRIDE {
104 ++num_loads_; 109 ++num_loads_;
105 test_app_.reset(new Application(shell_handle.Pass())); 110 test_app_.reset(new Application(shell_handle.Pass()));
106 test_app_->AddServiceConnector( 111 test_app_->AddServiceConnector(
107 new ServiceConnector<TestServiceImpl, TestContext>(context_)); 112 new ServiceConnector<TestServiceImpl, TestContext>(context_));
108 } 113 }
109 114
110 virtual void OnServiceError(ServiceManager* manager, 115 virtual void OnServiceError(ServiceManager* manager,
111 const GURL& url) OVERRIDE { 116 const GURL& url) OVERRIDE {
112 if (quit_after_error_) { 117 if (quit_after_error_) {
113 base::MessageLoop::current()->PostTask(FROM_HERE, 118 base::MessageLoop::current()->PostTask(FROM_HERE,
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 class ServiceManagerTest : public testing::Test { 160 class ServiceManagerTest : public testing::Test {
156 public: 161 public:
157 ServiceManagerTest() {} 162 ServiceManagerTest() {}
158 163
159 virtual ~ServiceManagerTest() {} 164 virtual ~ServiceManagerTest() {}
160 165
161 virtual void SetUp() OVERRIDE { 166 virtual void SetUp() OVERRIDE {
162 GURL test_url(kTestURLString); 167 GURL test_url(kTestURLString);
163 service_manager_.reset(new ServiceManager); 168 service_manager_.reset(new ServiceManager);
164 169
165 InterfacePipe<TestService, AnyInterface> pipe; 170 MessagePipe pipe;
166 test_client_.reset(new TestClientImpl(pipe.handle_to_self.Pass())); 171 TestServicePtr service_proxy = MakeProxy<TestService>(pipe.handle0.Pass());
172 test_client_.reset(new TestClientImpl(service_proxy.Pass()));
173
167 TestServiceLoader* default_loader = new TestServiceLoader; 174 TestServiceLoader* default_loader = new TestServiceLoader;
168 default_loader->set_context(&context_); 175 default_loader->set_context(&context_);
169 default_loader->set_quit_after_error(true); 176 default_loader->set_quit_after_error(true);
170 service_manager_->set_default_loader( 177 service_manager_->set_default_loader(
171 scoped_ptr<ServiceLoader>(default_loader)); 178 scoped_ptr<ServiceLoader>(default_loader));
172 service_manager_->Connect(test_url, pipe.handle_to_peer.Pass()); 179
180 service_manager_->Connect(test_url, pipe.handle1.Pass());
173 } 181 }
174 182
175 virtual void TearDown() OVERRIDE { 183 virtual void TearDown() OVERRIDE {
176 test_client_.reset(NULL); 184 test_client_.reset(NULL);
177 service_manager_.reset(NULL); 185 service_manager_.reset(NULL);
178 } 186 }
179 187
180 bool HasFactoryForTestURL() { 188 bool HasFactoryForTestURL() {
181 ServiceManager::TestAPI manager_test_api(service_manager_.get()); 189 ServiceManager::TestAPI manager_test_api(service_manager_.get());
182 return manager_test_api.HasFactoryForURL(GURL(kTestURLString)); 190 return manager_test_api.HasFactoryForURL(GURL(kTestURLString));
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 TEST_F(ServiceManagerTest, SetLoaders) { 244 TEST_F(ServiceManagerTest, SetLoaders) {
237 ServiceManager sm; 245 ServiceManager sm;
238 TestServiceLoader* default_loader = new TestServiceLoader; 246 TestServiceLoader* default_loader = new TestServiceLoader;
239 TestServiceLoader* url_loader = new TestServiceLoader; 247 TestServiceLoader* url_loader = new TestServiceLoader;
240 TestServiceLoader* scheme_loader = new TestServiceLoader; 248 TestServiceLoader* scheme_loader = new TestServiceLoader;
241 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader)); 249 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader));
242 sm.SetLoaderForURL(scoped_ptr<ServiceLoader>(url_loader), GURL("test:test1")); 250 sm.SetLoaderForURL(scoped_ptr<ServiceLoader>(url_loader), GURL("test:test1"));
243 sm.SetLoaderForScheme(scoped_ptr<ServiceLoader>(scheme_loader), "test"); 251 sm.SetLoaderForScheme(scoped_ptr<ServiceLoader>(scheme_loader), "test");
244 252
245 // test::test1 should go to url_loader. 253 // test::test1 should go to url_loader.
246 InterfacePipe<TestService, AnyInterface> pipe1; 254 MessagePipe pipe1;
247 sm.Connect(GURL("test:test1"), pipe1.handle_to_peer.Pass()); 255 sm.Connect(GURL("test:test1"), pipe1.handle0.Pass());
248 EXPECT_EQ(1, url_loader->num_loads()); 256 EXPECT_EQ(1, url_loader->num_loads());
249 EXPECT_EQ(0, scheme_loader->num_loads()); 257 EXPECT_EQ(0, scheme_loader->num_loads());
250 EXPECT_EQ(0, default_loader->num_loads()); 258 EXPECT_EQ(0, default_loader->num_loads());
251 259
252 // test::test2 should go to scheme loader. 260 // test::test2 should go to scheme loader.
253 InterfacePipe<TestService, AnyInterface> pipe2; 261 MessagePipe pipe2;
254 sm.Connect(GURL("test:test2"), pipe2.handle_to_peer.Pass()); 262 sm.Connect(GURL("test:test2"), pipe2.handle0.Pass());
255 EXPECT_EQ(1, url_loader->num_loads()); 263 EXPECT_EQ(1, url_loader->num_loads());
256 EXPECT_EQ(1, scheme_loader->num_loads()); 264 EXPECT_EQ(1, scheme_loader->num_loads());
257 EXPECT_EQ(0, default_loader->num_loads()); 265 EXPECT_EQ(0, default_loader->num_loads());
258 266
259 // http::test1 should go to default loader. 267 // http::test1 should go to default loader.
260 InterfacePipe<TestService, AnyInterface> pipe3; 268 MessagePipe pipe3;
261 sm.Connect(GURL("http:test1"), pipe3.handle_to_peer.Pass()); 269 sm.Connect(GURL("http:test1"), pipe3.handle0.Pass());
262 EXPECT_EQ(1, url_loader->num_loads()); 270 EXPECT_EQ(1, url_loader->num_loads());
263 EXPECT_EQ(1, scheme_loader->num_loads()); 271 EXPECT_EQ(1, scheme_loader->num_loads());
264 EXPECT_EQ(1, default_loader->num_loads()); 272 EXPECT_EQ(1, default_loader->num_loads());
265 } 273 }
266 274
267 TEST_F(ServiceManagerTest, Interceptor) { 275 TEST_F(ServiceManagerTest, Interceptor) {
268 ServiceManager sm; 276 ServiceManager sm;
269 TestServiceInterceptor interceptor; 277 TestServiceInterceptor interceptor;
270 TestServiceLoader* default_loader = new TestServiceLoader; 278 TestServiceLoader* default_loader = new TestServiceLoader;
271 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader)); 279 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader));
272 sm.SetInterceptor(&interceptor); 280 sm.SetInterceptor(&interceptor);
273 281
274 std::string url("test:test3"); 282 std::string url("test:test3");
275 InterfacePipe<TestService, AnyInterface> pipe1; 283 MessagePipe pipe1;
276 sm.Connect(GURL(url), pipe1.handle_to_peer.Pass()); 284 sm.Connect(GURL(url), pipe1.handle0.Pass());
277 EXPECT_EQ(1, interceptor.call_count()); 285 EXPECT_EQ(1, interceptor.call_count());
278 EXPECT_EQ(url, interceptor.url_spec()); 286 EXPECT_EQ(url, interceptor.url_spec());
279 EXPECT_EQ(1, default_loader->num_loads()); 287 EXPECT_EQ(1, default_loader->num_loads());
280 } 288 }
281 289
282 } // namespace mojo 290 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698