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

Side by Side Diff: mojo/public/cpp/bindings/tests/request_response_unittest.cc

Issue 265793015: Mojo: Replace RemotePtr with InterfacePtr and InterfaceImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: snapshot 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 "mojo/public/cpp/bindings/allocation_scope.h" 5 #include "mojo/public/cpp/bindings/allocation_scope.h"
6 #include "mojo/public/cpp/bindings/remote_ptr.h" 6 #include "mojo/public/cpp/bindings/tests/test_bindings_utils.h"
7 #include "mojo/public/cpp/environment/environment.h" 7 #include "mojo/public/cpp/environment/environment.h"
8 #include "mojo/public/cpp/test_support/test_utils.h" 8 #include "mojo/public/cpp/test_support/test_utils.h"
9 #include "mojo/public/cpp/utility/run_loop.h" 9 #include "mojo/public/cpp/utility/run_loop.h"
10 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h" 10 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h"
11 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" 11 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace mojo { 14 namespace mojo {
15 namespace test { 15 namespace test {
16 namespace { 16 namespace {
17 17
18 class ProviderImpl : public sample::Provider { 18 class ProviderImpl : public sample::Provider {
19 public: 19 public:
20 explicit ProviderImpl(sample::ScopedProviderClientHandle handle)
21 : client_(handle.Pass(), this) {
22 }
23
24 virtual void EchoString( 20 virtual void EchoString(
25 const String& a, 21 const String& a,
26 const Callback<void(String)>& callback) MOJO_OVERRIDE { 22 const Callback<void(String)>& callback) MOJO_OVERRIDE {
27 AllocationScope scope; 23 AllocationScope scope;
28 Callback<void(String)> callback_copy; 24 Callback<void(String)> callback_copy;
29 // Make sure operator= is used. 25 // Make sure operator= is used.
30 callback_copy = callback; 26 callback_copy = callback;
31 callback_copy.Run(a); 27 callback_copy.Run(a);
32 } 28 }
33 29
(...skipping 10 matching lines...) Expand all
44 const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE { 40 const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE {
45 AllocationScope scope; 41 AllocationScope scope;
46 callback.Run(a.Pass()); 42 callback.Run(a.Pass());
47 } 43 }
48 44
49 virtual void EchoEnum(sample::Enum a, 45 virtual void EchoEnum(sample::Enum a,
50 const Callback<void(sample::Enum)>& callback) 46 const Callback<void(sample::Enum)>& callback)
51 MOJO_OVERRIDE { 47 MOJO_OVERRIDE {
52 callback.Run(a); 48 callback.Run(a);
53 } 49 }
54
55 private:
56 RemotePtr<sample::ProviderClient> client_;
57 }; 50 };
58 51
59 class StringRecorder { 52 class StringRecorder {
60 public: 53 public:
61 StringRecorder(std::string* buf) : buf_(buf) { 54 StringRecorder(std::string* buf) : buf_(buf) {
62 } 55 }
63 void Run(const String& a) const { 56 void Run(const String& a) const {
64 *buf_ = a.To<std::string>(); 57 *buf_ = a.To<std::string>();
65 } 58 }
66 void Run(const String& a, const String& b) const { 59 void Run(const String& a, const String& b) const {
(...skipping 20 matching lines...) Expand all
87 } 80 }
88 void Run(ScopedMessagePipeHandle handle) const { 81 void Run(ScopedMessagePipeHandle handle) const {
89 WriteTextMessage(handle.get(), text_); 82 WriteTextMessage(handle.get(), text_);
90 } 83 }
91 private: 84 private:
92 std::string text_; 85 std::string text_;
93 }; 86 };
94 87
95 class RequestResponseTest : public testing::Test { 88 class RequestResponseTest : public testing::Test {
96 public: 89 public:
90 virtual void TearDown() {
91 PumpMessages();
92 EXPECT_EQ(0, internal::GetDetachedStateCount());
93 }
94
97 void PumpMessages() { 95 void PumpMessages() {
98 loop_.RunUntilIdle(); 96 loop_.RunUntilIdle();
99 } 97 }
100 98
101 private: 99 private:
102 Environment env_; 100 Environment env_;
103 RunLoop loop_; 101 RunLoop loop_;
104 }; 102 };
105 103
106 TEST_F(RequestResponseTest, EchoString) { 104 TEST_F(RequestResponseTest, EchoString) {
107 InterfacePipe<sample::Provider> pipe; 105 sample::ProviderPtr provider;
108 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 106 MakeRemote(new ProviderImpl(), &provider);
109 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
110 107
111 std::string buf; 108 std::string buf;
112 { 109 {
113 AllocationScope scope; 110 AllocationScope scope;
114 provider->EchoString("hello", StringRecorder(&buf)); 111 provider->EchoString("hello", StringRecorder(&buf));
115 } 112 }
116 113
117 PumpMessages(); 114 PumpMessages();
118 115
119 EXPECT_EQ(std::string("hello"), buf); 116 EXPECT_EQ(std::string("hello"), buf);
120 } 117 }
121 118
122 TEST_F(RequestResponseTest, EchoStrings) { 119 TEST_F(RequestResponseTest, EchoStrings) {
123 InterfacePipe<sample::Provider> pipe; 120 sample::ProviderPtr provider;
124 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 121 MakeRemote(new ProviderImpl(), &provider);
125 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
126 122
127 std::string buf; 123 std::string buf;
128 { 124 {
129 AllocationScope scope; 125 AllocationScope scope;
130 provider->EchoStrings("hello", " world", StringRecorder(&buf)); 126 provider->EchoStrings("hello", " world", StringRecorder(&buf));
131 } 127 }
132 128
133 PumpMessages(); 129 PumpMessages();
134 130
135 EXPECT_EQ(std::string("hello world"), buf); 131 EXPECT_EQ(std::string("hello world"), buf);
136 } 132 }
137 133
138 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { 134 TEST_F(RequestResponseTest, EchoMessagePipeHandle) {
139 InterfacePipe<sample::Provider> pipe; 135 sample::ProviderPtr provider;
140 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 136 MakeRemote(new ProviderImpl(), &provider);
141 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
142 137
143 MessagePipe pipe2; 138 MessagePipe pipe2;
144 { 139 {
145 AllocationScope scope; 140 AllocationScope scope;
146 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(), 141 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(),
147 MessagePipeWriter("hello")); 142 MessagePipeWriter("hello"));
148 } 143 }
149 144
150 PumpMessages(); 145 PumpMessages();
151 146
152 std::string value; 147 std::string value;
153 ReadTextMessage(pipe2.handle0.get(), &value); 148 ReadTextMessage(pipe2.handle0.get(), &value);
154 149
155 EXPECT_EQ(std::string("hello"), value); 150 EXPECT_EQ(std::string("hello"), value);
156 } 151 }
157 152
158 TEST_F(RequestResponseTest, EchoEnum) { 153 TEST_F(RequestResponseTest, EchoEnum) {
159 InterfacePipe<sample::Provider> pipe; 154 sample::ProviderPtr provider;
160 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 155 MakeRemote(new ProviderImpl(), &provider);
161 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
162 156
163 sample::Enum value; 157 sample::Enum value;
164 { 158 {
165 AllocationScope scope; 159 AllocationScope scope;
166 provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value)); 160 provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value));
167 } 161 }
168 162
169 PumpMessages(); 163 PumpMessages();
170 164
171 EXPECT_EQ(sample::ENUM_VALUE, value); 165 EXPECT_EQ(sample::ENUM_VALUE, value);
172 } 166 }
173 167
174 } // namespace 168 } // namespace
175 } // namespace test 169 } // namespace test
176 } // namespace mojo 170 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698