OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "mojo/public/cpp/environment/environment.h" | 5 #include "mojo/public/cpp/environment/environment.h" |
6 #include "mojo/public/cpp/test_support/test_utils.h" | 6 #include "mojo/public/cpp/test_support/test_utils.h" |
7 #include "mojo/public/cpp/utility/run_loop.h" | 7 #include "mojo/public/cpp/utility/run_loop.h" |
8 #include "mojo/public/interfaces/bindings/tests/sample_factory.mojom.h" | 8 #include "mojo/public/interfaces/bindings/tests/sample_factory.mojom.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 namespace test { | 12 namespace test { |
13 namespace { | 13 namespace { |
14 | 14 |
15 const char kText1[] = "hello"; | 15 const char kText1[] = "hello"; |
16 const char kText2[] = "world"; | 16 const char kText2[] = "world"; |
17 | 17 |
18 class StringRecorder { | 18 class StringRecorder { |
19 public: | 19 public: |
20 explicit StringRecorder(std::string* buf) : buf_(buf) { | 20 explicit StringRecorder(std::string* buf) : buf_(buf) { |
21 } | 21 } |
22 void Run(const String& a) const { | 22 void Run(const String& a) const { |
23 *buf_ = a.To<std::string>(); | 23 *buf_ = a.To<std::string>(); |
24 } | 24 } |
25 private: | 25 private: |
26 std::string* buf_; | 26 std::string* buf_; |
27 }; | 27 }; |
28 | 28 |
29 class ImportedInterfaceImpl | |
30 : public InterfaceImpl<imported::ImportedInterface> { | |
31 public: | |
32 virtual void OnConnectionError() MOJO_OVERRIDE { | |
33 delete this; | |
34 } | |
35 | |
36 virtual void DoSomething() MOJO_OVERRIDE { | |
37 do_something_count_++; | |
38 } | |
39 | |
40 static int do_something_count() { return do_something_count_; } | |
41 | |
42 private: | |
43 static int do_something_count_; | |
44 }; | |
45 int ImportedInterfaceImpl::do_something_count_ = 0; | |
46 | |
47 class SampleNamedObjectImpl : public InterfaceImpl<sample::NamedObject> { | 29 class SampleNamedObjectImpl : public InterfaceImpl<sample::NamedObject> { |
48 public: | 30 public: |
49 virtual void OnConnectionError() MOJO_OVERRIDE { | 31 virtual void OnConnectionError() MOJO_OVERRIDE { |
50 delete this; | 32 delete this; |
51 } | 33 } |
52 | 34 |
53 virtual void SetName(const mojo::String& name) MOJO_OVERRIDE { | 35 virtual void SetName(const mojo::String& name) MOJO_OVERRIDE { |
54 name_ = name; | 36 name_ = name; |
55 } | 37 } |
56 | 38 |
(...skipping 29 matching lines...) Expand all Loading... |
86 ScopedMessagePipeHandle pipe0; | 68 ScopedMessagePipeHandle pipe0; |
87 if (!text2.empty()) { | 69 if (!text2.empty()) { |
88 CreateMessagePipe(NULL, &pipe0, &pipe1_); | 70 CreateMessagePipe(NULL, &pipe0, &pipe1_); |
89 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2)); | 71 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2)); |
90 } | 72 } |
91 | 73 |
92 sample::ResponsePtr response(sample::Response::New()); | 74 sample::ResponsePtr response(sample::Response::New()); |
93 response->x = 2; | 75 response->x = 2; |
94 response->pipe = pipe0.Pass(); | 76 response->pipe = pipe0.Pass(); |
95 client()->DidStuff(response.Pass(), text1); | 77 client()->DidStuff(response.Pass(), text1); |
96 | |
97 if (request->obj.get()) | |
98 request->obj->DoSomething(); | |
99 } | 78 } |
100 | 79 |
101 virtual void DoStuff2(ScopedDataPipeConsumerHandle pipe) MOJO_OVERRIDE { | 80 virtual void DoStuff2(ScopedDataPipeConsumerHandle pipe) MOJO_OVERRIDE { |
102 // Read the data from the pipe, writing the response (as a string) to | 81 // Read the data from the pipe, writing the response (as a string) to |
103 // DidStuff2(). | 82 // DidStuff2(). |
104 ASSERT_TRUE(pipe.is_valid()); | 83 ASSERT_TRUE(pipe.is_valid()); |
105 uint32_t data_size = 0; | 84 uint32_t data_size = 0; |
106 ASSERT_EQ(MOJO_RESULT_OK, | 85 ASSERT_EQ(MOJO_RESULT_OK, |
107 ReadDataRaw(pipe.get(), NULL, &data_size, | 86 ReadDataRaw(pipe.get(), NULL, &data_size, |
108 MOJO_READ_DATA_FLAG_QUERY)); | 87 MOJO_READ_DATA_FLAG_QUERY)); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 factory_client.set_expected_text_reply(kText1); | 187 factory_client.set_expected_text_reply(kText1); |
209 | 188 |
210 factory.set_client(&factory_client); | 189 factory.set_client(&factory_client); |
211 | 190 |
212 MessagePipe pipe0; | 191 MessagePipe pipe0; |
213 EXPECT_TRUE(WriteTextMessage(pipe0.handle1.get(), kText1)); | 192 EXPECT_TRUE(WriteTextMessage(pipe0.handle1.get(), kText1)); |
214 | 193 |
215 MessagePipe pipe1; | 194 MessagePipe pipe1; |
216 EXPECT_TRUE(WriteTextMessage(pipe1.handle1.get(), kText2)); | 195 EXPECT_TRUE(WriteTextMessage(pipe1.handle1.get(), kText2)); |
217 | 196 |
218 imported::ImportedInterfacePtr imported; | |
219 BindToProxy(new ImportedInterfaceImpl(), &imported); | |
220 | |
221 sample::RequestPtr request(sample::Request::New()); | 197 sample::RequestPtr request(sample::Request::New()); |
222 request->x = 1; | 198 request->x = 1; |
223 request->pipe = pipe1.handle0.Pass(); | 199 request->pipe = pipe1.handle0.Pass(); |
224 request->obj = imported.Pass(); | |
225 factory->DoStuff(request.Pass(), pipe0.handle0.Pass()); | 200 factory->DoStuff(request.Pass(), pipe0.handle0.Pass()); |
226 | 201 |
227 EXPECT_FALSE(factory_client.got_response()); | 202 EXPECT_FALSE(factory_client.got_response()); |
228 int count_before = ImportedInterfaceImpl::do_something_count(); | |
229 | 203 |
230 PumpMessages(); | 204 PumpMessages(); |
231 | 205 |
232 EXPECT_TRUE(factory_client.got_response()); | 206 EXPECT_TRUE(factory_client.got_response()); |
233 EXPECT_EQ(1, ImportedInterfaceImpl::do_something_count() - count_before); | |
234 } | 207 } |
235 | 208 |
236 TEST_F(HandlePassingTest, PassInvalid) { | 209 TEST_F(HandlePassingTest, PassInvalid) { |
237 sample::FactoryPtr factory; | 210 sample::FactoryPtr factory; |
238 BindToProxy(new SampleFactoryImpl(), &factory); | 211 BindToProxy(new SampleFactoryImpl(), &factory); |
239 | 212 |
240 SampleFactoryClientImpl factory_client; | 213 SampleFactoryClientImpl factory_client; |
241 factory.set_client(&factory_client); | 214 factory.set_client(&factory_client); |
242 | 215 |
243 sample::RequestPtr request(sample::Request::New()); | 216 sample::RequestPtr request(sample::Request::New()); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 | 330 |
358 PumpMessages(); // Yield for results. | 331 PumpMessages(); // Yield for results. |
359 | 332 |
360 EXPECT_EQ(std::string("object1"), name1); | 333 EXPECT_EQ(std::string("object1"), name1); |
361 EXPECT_EQ(std::string("object2"), name2); | 334 EXPECT_EQ(std::string("object2"), name2); |
362 } | 335 } |
363 | 336 |
364 } // namespace | 337 } // namespace |
365 } // namespace test | 338 } // namespace test |
366 } // namespace mojo | 339 } // namespace mojo |
OLD | NEW |