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