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 "gtest/gtest.h" | |
6 #include "mojo/public/cpp/bindings/binding.h" | |
7 #include "mojo/public/cpp/test_support/test_utils.h" | |
8 #include "mojo/public/cpp/utility/run_loop.h" | |
9 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h" | |
10 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | |
11 | |
12 namespace mojo { | |
13 namespace test { | |
14 namespace { | |
15 | |
16 class ProviderImpl : public sample::Provider { | |
17 public: | |
18 explicit ProviderImpl(InterfaceRequest<sample::Provider> request) | |
19 : binding_(this, request.Pass()) {} | |
20 | |
21 void EchoString(const String& a, | |
22 const Callback<void(String)>& callback) override { | |
23 Callback<void(String)> callback_copy; | |
24 // Make sure operator= is used. | |
25 callback_copy = callback; | |
26 callback_copy.Run(a); | |
27 } | |
28 | |
29 void EchoStrings(const String& a, | |
30 const String& b, | |
31 const Callback<void(String, String)>& callback) override { | |
32 callback.Run(a, b); | |
33 } | |
34 | |
35 void EchoMessagePipeHandle( | |
36 ScopedMessagePipeHandle a, | |
37 const Callback<void(ScopedMessagePipeHandle)>& callback) override { | |
38 callback.Run(a.Pass()); | |
39 } | |
40 | |
41 void EchoEnum(sample::Enum a, | |
42 const Callback<void(sample::Enum)>& callback) override { | |
43 callback.Run(a); | |
44 } | |
45 | |
46 void EchoInt(int32_t a, const EchoIntCallback& callback) override { | |
47 callback.Run(a); | |
48 } | |
49 | |
50 Binding<sample::Provider> binding_; | |
51 }; | |
52 | |
53 class StringRecorder { | |
54 public: | |
55 explicit StringRecorder(std::string* buf) : buf_(buf) {} | |
56 void Run(const String& a) const { *buf_ = a; } | |
57 void Run(const String& a, const String& b) const { | |
58 *buf_ = a.get() + b.get(); | |
59 } | |
60 | |
61 private: | |
62 std::string* buf_; | |
63 }; | |
64 | |
65 class EnumRecorder { | |
66 public: | |
67 explicit EnumRecorder(sample::Enum* value) : value_(value) {} | |
68 void Run(sample::Enum a) const { *value_ = a; } | |
69 | |
70 private: | |
71 sample::Enum* value_; | |
72 }; | |
73 | |
74 class MessagePipeWriter { | |
75 public: | |
76 explicit MessagePipeWriter(const char* text) : text_(text) {} | |
77 void Run(ScopedMessagePipeHandle handle) const { | |
78 WriteTextMessage(handle.get(), text_); | |
79 } | |
80 | |
81 private: | |
82 std::string text_; | |
83 }; | |
84 | |
85 class RequestResponseTest : public testing::Test { | |
86 public: | |
87 ~RequestResponseTest() override { loop_.RunUntilIdle(); } | |
88 | |
89 void PumpMessages() { loop_.RunUntilIdle(); } | |
90 | |
91 private: | |
92 RunLoop loop_; | |
93 }; | |
94 | |
95 TEST_F(RequestResponseTest, EchoString) { | |
96 sample::ProviderPtr provider; | |
97 ProviderImpl provider_impl(GetProxy(&provider)); | |
98 | |
99 std::string buf; | |
100 provider->EchoString(String::From("hello"), StringRecorder(&buf)); | |
101 | |
102 PumpMessages(); | |
103 | |
104 EXPECT_EQ(std::string("hello"), buf); | |
105 } | |
106 | |
107 TEST_F(RequestResponseTest, EchoStrings) { | |
108 sample::ProviderPtr provider; | |
109 ProviderImpl provider_impl(GetProxy(&provider)); | |
110 | |
111 std::string buf; | |
112 provider->EchoStrings( | |
113 String::From("hello"), String::From(" world"), StringRecorder(&buf)); | |
114 | |
115 PumpMessages(); | |
116 | |
117 EXPECT_EQ(std::string("hello world"), buf); | |
118 } | |
119 | |
120 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { | |
121 sample::ProviderPtr provider; | |
122 ProviderImpl provider_impl(GetProxy(&provider)); | |
123 | |
124 MessagePipe pipe2; | |
125 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(), | |
126 MessagePipeWriter("hello")); | |
127 | |
128 PumpMessages(); | |
129 | |
130 std::string value; | |
131 ReadTextMessage(pipe2.handle0.get(), &value); | |
132 | |
133 EXPECT_EQ(std::string("hello"), value); | |
134 } | |
135 | |
136 TEST_F(RequestResponseTest, EchoEnum) { | |
137 sample::ProviderPtr provider; | |
138 ProviderImpl provider_impl(GetProxy(&provider)); | |
139 | |
140 sample::Enum value; | |
141 provider->EchoEnum(sample::Enum::VALUE, EnumRecorder(&value)); | |
142 | |
143 PumpMessages(); | |
144 | |
145 EXPECT_EQ(sample::Enum::VALUE, value); | |
146 } | |
147 | |
148 } // namespace | |
149 } // namespace test | |
150 } // namespace mojo | |
OLD | NEW |