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

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 virtual void SetClient(TestClient* client) OVERRIDE {
44 client_ = client;
45 }
46 virtual void Test(const mojo::String& test_string) OVERRIDE {
47 context()->last_test_string = test_string.To<std::string>();
48 client_->AckTest();
49 }
50
51 private:
52 TestClient* client_;
51 }; 53 };
52 54
53 class TestClientImpl : public TestClient { 55 class TestClientImpl : public TestClient {
54 public: 56 public:
55 explicit TestClientImpl(ScopedTestServiceHandle service_handle) 57 explicit TestClientImpl(TestServicePtr service)
56 : service_(service_handle.Pass(), this), 58 : service_(service.Pass()),
57 quit_after_ack_(false) { 59 quit_after_ack_(false) {
60 service_->SetClient(this);
58 } 61 }
59 62
60 virtual ~TestClientImpl() {} 63 virtual ~TestClientImpl() {}
61 64
62 virtual void AckTest() OVERRIDE { 65 virtual void AckTest() OVERRIDE {
63 if (quit_after_ack_) 66 if (quit_after_ack_)
64 base::MessageLoop::current()->Quit(); 67 base::MessageLoop::current()->Quit();
65 } 68 }
66 69
67 void Test(std::string test_string) { 70 void Test(std::string test_string) {
68 AllocationScope scope; 71 AllocationScope scope;
69 quit_after_ack_ = true; 72 quit_after_ack_ = true;
70 service_->Test(mojo::String(test_string)); 73 service_->Test(test_string);
71 } 74 }
72 75
73 private: 76 private:
74 RemotePtr<TestService> service_; 77 TestServicePtr service_;
75 bool quit_after_ack_; 78 bool quit_after_ack_;
76 DISALLOW_COPY_AND_ASSIGN(TestClientImpl); 79 DISALLOW_COPY_AND_ASSIGN(TestClientImpl);
77 }; 80 };
78 81
79 class TestServiceLoader : public ServiceLoader { 82 class TestServiceLoader : public ServiceLoader {
80 public: 83 public:
81 TestServiceLoader() 84 TestServiceLoader()
82 : context_(NULL), 85 : context_(NULL),
83 num_loads_(0), 86 num_loads_(0),
84 quit_after_error_(false) { 87 quit_after_error_(false) {
85 } 88 }
86 89
87 virtual ~TestServiceLoader() { 90 virtual ~TestServiceLoader() {
88 if (context_) 91 if (context_)
89 ++context_->num_loader_deletes; 92 ++context_->num_loader_deletes;
90 test_app_.reset(NULL); 93 test_app_.reset(NULL);
91 } 94 }
92 95
93 void set_context(TestContext* context) { context_ = context; } 96 void set_context(TestContext* context) { context_ = context; }
94 void set_quit_after_error(bool quit_after_error) { 97 void set_quit_after_error(bool quit_after_error) {
95 quit_after_error_ = quit_after_error; 98 quit_after_error_ = quit_after_error;
96 } 99 }
97 100
98 int num_loads() const { return num_loads_; } 101 int num_loads() const { return num_loads_; }
99 102
100 private: 103 private:
101 virtual void LoadService(ServiceManager* manager, 104 virtual void LoadService(ServiceManager* manager,
102 const GURL& url, 105 const GURL& url,
103 ScopedShellHandle shell_handle) OVERRIDE { 106 ScopedMessagePipeHandle shell_handle) OVERRIDE {
104 ++num_loads_; 107 ++num_loads_;
105 test_app_.reset(new Application(shell_handle.Pass())); 108 test_app_.reset(new Application(shell_handle.Pass()));
106 test_app_->AddServiceConnector( 109 test_app_->AddServiceConnector(
107 new ServiceConnector<TestServiceImpl, TestContext>(context_)); 110 new ServiceConnector<TestServiceImpl, TestContext>(context_));
108 } 111 }
109 112
110 virtual void OnServiceError(ServiceManager* manager, 113 virtual void OnServiceError(ServiceManager* manager,
111 const GURL& url) OVERRIDE { 114 const GURL& url) OVERRIDE {
112 if (quit_after_error_) { 115 if (quit_after_error_) {
113 base::MessageLoop::current()->PostTask(FROM_HERE, 116 base::MessageLoop::current()->PostTask(FROM_HERE,
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 class ServiceManagerTest : public testing::Test { 158 class ServiceManagerTest : public testing::Test {
156 public: 159 public:
157 ServiceManagerTest() {} 160 ServiceManagerTest() {}
158 161
159 virtual ~ServiceManagerTest() {} 162 virtual ~ServiceManagerTest() {}
160 163
161 virtual void SetUp() OVERRIDE { 164 virtual void SetUp() OVERRIDE {
162 GURL test_url(kTestURLString); 165 GURL test_url(kTestURLString);
163 service_manager_.reset(new ServiceManager); 166 service_manager_.reset(new ServiceManager);
164 167
165 InterfacePipe<TestService, AnyInterface> pipe; 168 MessagePipe pipe;
166 test_client_.reset(new TestClientImpl(pipe.handle_to_self.Pass())); 169 TestServicePtr service_proxy = MakeProxy<TestService>(pipe.handle0.Pass());
170 test_client_.reset(new TestClientImpl(service_proxy.Pass()));
171
167 TestServiceLoader* default_loader = new TestServiceLoader; 172 TestServiceLoader* default_loader = new TestServiceLoader;
168 default_loader->set_context(&context_); 173 default_loader->set_context(&context_);
169 default_loader->set_quit_after_error(true); 174 default_loader->set_quit_after_error(true);
170 service_manager_->set_default_loader( 175 service_manager_->set_default_loader(
171 scoped_ptr<ServiceLoader>(default_loader)); 176 scoped_ptr<ServiceLoader>(default_loader));
172 service_manager_->Connect(test_url, pipe.handle_to_peer.Pass()); 177
178 service_manager_->Connect(test_url, pipe.handle1.Pass());
173 } 179 }
174 180
175 virtual void TearDown() OVERRIDE { 181 virtual void TearDown() OVERRIDE {
176 test_client_.reset(NULL); 182 test_client_.reset(NULL);
177 service_manager_.reset(NULL); 183 service_manager_.reset(NULL);
178 } 184 }
179 185
180 bool HasFactoryForTestURL() { 186 bool HasFactoryForTestURL() {
181 ServiceManager::TestAPI manager_test_api(service_manager_.get()); 187 ServiceManager::TestAPI manager_test_api(service_manager_.get());
182 return manager_test_api.HasFactoryForURL(GURL(kTestURLString)); 188 return manager_test_api.HasFactoryForURL(GURL(kTestURLString));
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 TEST_F(ServiceManagerTest, SetLoaders) { 242 TEST_F(ServiceManagerTest, SetLoaders) {
237 ServiceManager sm; 243 ServiceManager sm;
238 TestServiceLoader* default_loader = new TestServiceLoader; 244 TestServiceLoader* default_loader = new TestServiceLoader;
239 TestServiceLoader* url_loader = new TestServiceLoader; 245 TestServiceLoader* url_loader = new TestServiceLoader;
240 TestServiceLoader* scheme_loader = new TestServiceLoader; 246 TestServiceLoader* scheme_loader = new TestServiceLoader;
241 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader)); 247 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader));
242 sm.SetLoaderForURL(scoped_ptr<ServiceLoader>(url_loader), GURL("test:test1")); 248 sm.SetLoaderForURL(scoped_ptr<ServiceLoader>(url_loader), GURL("test:test1"));
243 sm.SetLoaderForScheme(scoped_ptr<ServiceLoader>(scheme_loader), "test"); 249 sm.SetLoaderForScheme(scoped_ptr<ServiceLoader>(scheme_loader), "test");
244 250
245 // test::test1 should go to url_loader. 251 // test::test1 should go to url_loader.
246 InterfacePipe<TestService, AnyInterface> pipe1; 252 MessagePipe pipe1;
247 sm.Connect(GURL("test:test1"), pipe1.handle_to_peer.Pass()); 253 sm.Connect(GURL("test:test1"), pipe1.handle0.Pass());
248 EXPECT_EQ(1, url_loader->num_loads()); 254 EXPECT_EQ(1, url_loader->num_loads());
249 EXPECT_EQ(0, scheme_loader->num_loads()); 255 EXPECT_EQ(0, scheme_loader->num_loads());
250 EXPECT_EQ(0, default_loader->num_loads()); 256 EXPECT_EQ(0, default_loader->num_loads());
251 257
252 // test::test2 should go to scheme loader. 258 // test::test2 should go to scheme loader.
253 InterfacePipe<TestService, AnyInterface> pipe2; 259 MessagePipe pipe2;
254 sm.Connect(GURL("test:test2"), pipe2.handle_to_peer.Pass()); 260 sm.Connect(GURL("test:test2"), pipe2.handle0.Pass());
255 EXPECT_EQ(1, url_loader->num_loads()); 261 EXPECT_EQ(1, url_loader->num_loads());
256 EXPECT_EQ(1, scheme_loader->num_loads()); 262 EXPECT_EQ(1, scheme_loader->num_loads());
257 EXPECT_EQ(0, default_loader->num_loads()); 263 EXPECT_EQ(0, default_loader->num_loads());
258 264
259 // http::test1 should go to default loader. 265 // http::test1 should go to default loader.
260 InterfacePipe<TestService, AnyInterface> pipe3; 266 MessagePipe pipe3;
261 sm.Connect(GURL("http:test1"), pipe3.handle_to_peer.Pass()); 267 sm.Connect(GURL("http:test1"), pipe3.handle0.Pass());
262 EXPECT_EQ(1, url_loader->num_loads()); 268 EXPECT_EQ(1, url_loader->num_loads());
263 EXPECT_EQ(1, scheme_loader->num_loads()); 269 EXPECT_EQ(1, scheme_loader->num_loads());
264 EXPECT_EQ(1, default_loader->num_loads()); 270 EXPECT_EQ(1, default_loader->num_loads());
265 } 271 }
266 272
267 TEST_F(ServiceManagerTest, Interceptor) { 273 TEST_F(ServiceManagerTest, Interceptor) {
268 ServiceManager sm; 274 ServiceManager sm;
269 TestServiceInterceptor interceptor; 275 TestServiceInterceptor interceptor;
270 TestServiceLoader* default_loader = new TestServiceLoader; 276 TestServiceLoader* default_loader = new TestServiceLoader;
271 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader)); 277 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader));
272 sm.SetInterceptor(&interceptor); 278 sm.SetInterceptor(&interceptor);
273 279
274 std::string url("test:test3"); 280 std::string url("test:test3");
275 InterfacePipe<TestService, AnyInterface> pipe1; 281 MessagePipe pipe1;
276 sm.Connect(GURL(url), pipe1.handle_to_peer.Pass()); 282 sm.Connect(GURL(url), pipe1.handle0.Pass());
277 EXPECT_EQ(1, interceptor.call_count()); 283 EXPECT_EQ(1, interceptor.call_count());
278 EXPECT_EQ(url, interceptor.url_spec()); 284 EXPECT_EQ(url, interceptor.url_spec());
279 EXPECT_EQ(1, default_loader->num_loads()); 285 EXPECT_EQ(1, default_loader->num_loads());
280 } 286 }
281 287
282 } // namespace mojo 288 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698