OLD | NEW |
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 <stdint.h> | 5 #include <stdint.h> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" |
9 #include "mojo/message_pump/message_pump_mojo.h" | 10 #include "mojo/message_pump/message_pump_mojo.h" |
10 #include "mojo/public/cpp/bindings/binding.h" | 11 #include "mojo/public/cpp/bindings/binding.h" |
11 #include "mojo/public/cpp/test_support/test_utils.h" | 12 #include "mojo/public/cpp/test_support/test_utils.h" |
12 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h" | 13 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h" |
13 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | 14 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace mojo { | 17 namespace mojo { |
17 namespace test { | 18 namespace test { |
18 namespace { | 19 namespace { |
(...skipping 30 matching lines...) Expand all Loading... |
49 | 50 |
50 void EchoInt(int32_t a, const EchoIntCallback& callback) override { | 51 void EchoInt(int32_t a, const EchoIntCallback& callback) override { |
51 callback.Run(a); | 52 callback.Run(a); |
52 } | 53 } |
53 | 54 |
54 Binding<sample::Provider> binding_; | 55 Binding<sample::Provider> binding_; |
55 }; | 56 }; |
56 | 57 |
57 class StringRecorder { | 58 class StringRecorder { |
58 public: | 59 public: |
59 explicit StringRecorder(std::string* buf) : buf_(buf) {} | 60 StringRecorder(std::string* buf, const base::Closure& closure) |
60 void Run(const String& a) const { *buf_ = a; } | 61 : buf_(buf), closure_(closure) {} |
| 62 void Run(const String& a) const { |
| 63 *buf_ = a; |
| 64 closure_.Run(); |
| 65 } |
61 void Run(const String& a, const String& b) const { | 66 void Run(const String& a, const String& b) const { |
62 *buf_ = a.get() + b.get(); | 67 *buf_ = a.get() + b.get(); |
| 68 closure_.Run(); |
63 } | 69 } |
64 | 70 |
65 private: | 71 private: |
66 std::string* buf_; | 72 std::string* buf_; |
| 73 base::Closure closure_; |
67 }; | 74 }; |
68 | 75 |
69 class EnumRecorder { | 76 class EnumRecorder { |
70 public: | 77 public: |
71 explicit EnumRecorder(sample::Enum* value) : value_(value) {} | 78 explicit EnumRecorder(sample::Enum* value, const base::Closure& closure) |
72 void Run(sample::Enum a) const { *value_ = a; } | 79 : value_(value), closure_(closure) {} |
| 80 void Run(sample::Enum a) const { |
| 81 *value_ = a; |
| 82 closure_.Run(); |
| 83 } |
73 | 84 |
74 private: | 85 private: |
75 sample::Enum* value_; | 86 sample::Enum* value_; |
| 87 base::Closure closure_; |
76 }; | 88 }; |
77 | 89 |
78 class MessagePipeWriter { | 90 class MessagePipeWriter { |
79 public: | 91 public: |
80 explicit MessagePipeWriter(const char* text) : text_(text) {} | 92 MessagePipeWriter(const char* text, const base::Closure& closure) |
| 93 : text_(text), closure_(closure) {} |
81 void Run(ScopedMessagePipeHandle handle) const { | 94 void Run(ScopedMessagePipeHandle handle) const { |
82 WriteTextMessage(handle.get(), text_); | 95 WriteTextMessage(handle.get(), text_); |
| 96 closure_.Run(); |
83 } | 97 } |
84 | 98 |
85 private: | 99 private: |
86 std::string text_; | 100 std::string text_; |
| 101 base::Closure closure_; |
87 }; | 102 }; |
88 | 103 |
89 class RequestResponseTest : public testing::Test { | 104 class RequestResponseTest : public testing::Test { |
90 public: | 105 public: |
91 RequestResponseTest() : loop_(common::MessagePumpMojo::Create()) {} | 106 RequestResponseTest() : loop_(common::MessagePumpMojo::Create()) {} |
92 ~RequestResponseTest() override { loop_.RunUntilIdle(); } | 107 ~RequestResponseTest() override { loop_.RunUntilIdle(); } |
93 | 108 |
94 void PumpMessages() { loop_.RunUntilIdle(); } | 109 void PumpMessages() { loop_.RunUntilIdle(); } |
95 | 110 |
96 private: | 111 private: |
97 base::MessageLoop loop_; | 112 base::MessageLoop loop_; |
98 }; | 113 }; |
99 | 114 |
100 TEST_F(RequestResponseTest, EchoString) { | 115 TEST_F(RequestResponseTest, EchoString) { |
101 sample::ProviderPtr provider; | 116 sample::ProviderPtr provider; |
102 ProviderImpl provider_impl(GetProxy(&provider)); | 117 ProviderImpl provider_impl(GetProxy(&provider)); |
103 | 118 |
104 std::string buf; | 119 std::string buf; |
105 provider->EchoString(String::From("hello"), StringRecorder(&buf)); | 120 base::RunLoop run_loop; |
| 121 provider->EchoString(String::From("hello"), |
| 122 StringRecorder(&buf, run_loop.QuitClosure())); |
106 | 123 |
107 PumpMessages(); | 124 run_loop.Run(); |
108 | 125 |
109 EXPECT_EQ(std::string("hello"), buf); | 126 EXPECT_EQ(std::string("hello"), buf); |
110 } | 127 } |
111 | 128 |
112 TEST_F(RequestResponseTest, EchoStrings) { | 129 TEST_F(RequestResponseTest, EchoStrings) { |
113 sample::ProviderPtr provider; | 130 sample::ProviderPtr provider; |
114 ProviderImpl provider_impl(GetProxy(&provider)); | 131 ProviderImpl provider_impl(GetProxy(&provider)); |
115 | 132 |
116 std::string buf; | 133 std::string buf; |
| 134 base::RunLoop run_loop; |
117 provider->EchoStrings( | 135 provider->EchoStrings( |
118 String::From("hello"), String::From(" world"), StringRecorder(&buf)); | 136 String::From("hello"), String::From(" world"), |
| 137 StringRecorder(&buf, run_loop.QuitClosure())); |
119 | 138 |
120 PumpMessages(); | 139 run_loop.Run(); |
121 | 140 |
122 EXPECT_EQ(std::string("hello world"), buf); | 141 EXPECT_EQ(std::string("hello world"), buf); |
123 } | 142 } |
124 | 143 |
125 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { | 144 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { |
126 sample::ProviderPtr provider; | 145 sample::ProviderPtr provider; |
127 ProviderImpl provider_impl(GetProxy(&provider)); | 146 ProviderImpl provider_impl(GetProxy(&provider)); |
128 | 147 |
129 MessagePipe pipe2; | 148 MessagePipe pipe2; |
130 provider->EchoMessagePipeHandle(std::move(pipe2.handle1), | 149 base::RunLoop run_loop; |
131 MessagePipeWriter("hello")); | 150 provider->EchoMessagePipeHandle( |
| 151 std::move(pipe2.handle1), |
| 152 MessagePipeWriter("hello", run_loop.QuitClosure())); |
132 | 153 |
133 PumpMessages(); | 154 run_loop.Run(); |
134 | 155 |
135 std::string value; | 156 std::string value; |
136 ReadTextMessage(pipe2.handle0.get(), &value); | 157 ReadTextMessage(pipe2.handle0.get(), &value); |
137 | 158 |
138 EXPECT_EQ(std::string("hello"), value); | 159 EXPECT_EQ(std::string("hello"), value); |
139 } | 160 } |
140 | 161 |
141 TEST_F(RequestResponseTest, EchoEnum) { | 162 TEST_F(RequestResponseTest, EchoEnum) { |
142 sample::ProviderPtr provider; | 163 sample::ProviderPtr provider; |
143 ProviderImpl provider_impl(GetProxy(&provider)); | 164 ProviderImpl provider_impl(GetProxy(&provider)); |
144 | 165 |
145 sample::Enum value; | 166 sample::Enum value; |
146 provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value)); | 167 base::RunLoop run_loop; |
| 168 provider->EchoEnum(sample::ENUM_VALUE, |
| 169 EnumRecorder(&value, run_loop.QuitClosure())); |
147 | 170 |
148 PumpMessages(); | 171 run_loop.Run(); |
149 | 172 |
150 EXPECT_EQ(sample::ENUM_VALUE, value); | 173 EXPECT_EQ(sample::ENUM_VALUE, value); |
151 } | 174 } |
152 | 175 |
153 } // namespace | 176 } // namespace |
154 } // namespace test | 177 } // namespace test |
155 } // namespace mojo | 178 } // namespace mojo |
OLD | NEW |