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

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: rebase 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) 20 virtual void SetClient(sample::ProviderClient* client) MOJO_OVERRIDE {
21 : client_(handle.Pass(), this) { 21 // Ignored. TODO(darin): Eliminate ProviderClient.
22 } 22 }
23 23
24 virtual void EchoString( 24 virtual void EchoString(
25 const String& a, 25 const String& a,
26 const Callback<void(String)>& callback) MOJO_OVERRIDE { 26 const Callback<void(String)>& callback) MOJO_OVERRIDE {
27 AllocationScope scope; 27 AllocationScope scope;
28 Callback<void(String)> callback_copy; 28 Callback<void(String)> callback_copy;
29 // Make sure operator= is used. 29 // Make sure operator= is used.
30 callback_copy = callback; 30 callback_copy = callback;
31 callback_copy.Run(a); 31 callback_copy.Run(a);
(...skipping 12 matching lines...) Expand all
44 const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE { 44 const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE {
45 AllocationScope scope; 45 AllocationScope scope;
46 callback.Run(a.Pass()); 46 callback.Run(a.Pass());
47 } 47 }
48 48
49 virtual void EchoEnum(sample::Enum a, 49 virtual void EchoEnum(sample::Enum a,
50 const Callback<void(sample::Enum)>& callback) 50 const Callback<void(sample::Enum)>& callback)
51 MOJO_OVERRIDE { 51 MOJO_OVERRIDE {
52 callback.Run(a); 52 callback.Run(a);
53 } 53 }
54
55 private:
56 RemotePtr<sample::ProviderClient> client_;
57 }; 54 };
58 55
59 class StringRecorder { 56 class StringRecorder {
60 public: 57 public:
61 StringRecorder(std::string* buf) : buf_(buf) { 58 StringRecorder(std::string* buf) : buf_(buf) {
62 } 59 }
63 void Run(const String& a) const { 60 void Run(const String& a) const {
64 *buf_ = a.To<std::string>(); 61 *buf_ = a.To<std::string>();
65 } 62 }
66 void Run(const String& a, const String& b) const { 63 void Run(const String& a, const String& b) const {
(...skipping 20 matching lines...) Expand all
87 } 84 }
88 void Run(ScopedMessagePipeHandle handle) const { 85 void Run(ScopedMessagePipeHandle handle) const {
89 WriteTextMessage(handle.get(), text_); 86 WriteTextMessage(handle.get(), text_);
90 } 87 }
91 private: 88 private:
92 std::string text_; 89 std::string text_;
93 }; 90 };
94 91
95 class RequestResponseTest : public testing::Test { 92 class RequestResponseTest : public testing::Test {
96 public: 93 public:
94 virtual void TearDown() {
95 PumpMessages();
96 EXPECT_EQ(0, internal::GetDetachedStateCount());
97 }
98
97 void PumpMessages() { 99 void PumpMessages() {
98 loop_.RunUntilIdle(); 100 loop_.RunUntilIdle();
99 } 101 }
100 102
101 private: 103 private:
102 Environment env_; 104 Environment env_;
103 RunLoop loop_; 105 RunLoop loop_;
104 }; 106 };
105 107
106 TEST_F(RequestResponseTest, EchoString) { 108 TEST_F(RequestResponseTest, EchoString) {
107 InterfacePipe<sample::Provider> pipe; 109 sample::ProviderPtr provider;
108 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 110 MakeRemote(new ProviderImpl(), &provider);
109 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
110 111
111 std::string buf; 112 std::string buf;
112 { 113 {
113 AllocationScope scope; 114 AllocationScope scope;
114 provider->EchoString("hello", StringRecorder(&buf)); 115 provider->EchoString("hello", StringRecorder(&buf));
115 } 116 }
116 117
117 PumpMessages(); 118 PumpMessages();
118 119
119 EXPECT_EQ(std::string("hello"), buf); 120 EXPECT_EQ(std::string("hello"), buf);
120 } 121 }
121 122
122 TEST_F(RequestResponseTest, EchoStrings) { 123 TEST_F(RequestResponseTest, EchoStrings) {
123 InterfacePipe<sample::Provider> pipe; 124 sample::ProviderPtr provider;
124 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 125 MakeRemote(new ProviderImpl(), &provider);
125 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
126 126
127 std::string buf; 127 std::string buf;
128 { 128 {
129 AllocationScope scope; 129 AllocationScope scope;
130 provider->EchoStrings("hello", " world", StringRecorder(&buf)); 130 provider->EchoStrings("hello", " world", StringRecorder(&buf));
131 } 131 }
132 132
133 PumpMessages(); 133 PumpMessages();
134 134
135 EXPECT_EQ(std::string("hello world"), buf); 135 EXPECT_EQ(std::string("hello world"), buf);
136 } 136 }
137 137
138 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { 138 TEST_F(RequestResponseTest, EchoMessagePipeHandle) {
139 InterfacePipe<sample::Provider> pipe; 139 sample::ProviderPtr provider;
140 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 140 MakeRemote(new ProviderImpl(), &provider);
141 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
142 141
143 MessagePipe pipe2; 142 MessagePipe pipe2;
144 { 143 {
145 AllocationScope scope; 144 AllocationScope scope;
146 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(), 145 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(),
147 MessagePipeWriter("hello")); 146 MessagePipeWriter("hello"));
148 } 147 }
149 148
150 PumpMessages(); 149 PumpMessages();
151 150
152 std::string value; 151 std::string value;
153 ReadTextMessage(pipe2.handle0.get(), &value); 152 ReadTextMessage(pipe2.handle0.get(), &value);
154 153
155 EXPECT_EQ(std::string("hello"), value); 154 EXPECT_EQ(std::string("hello"), value);
156 } 155 }
157 156
158 TEST_F(RequestResponseTest, EchoEnum) { 157 TEST_F(RequestResponseTest, EchoEnum) {
159 InterfacePipe<sample::Provider> pipe; 158 sample::ProviderPtr provider;
160 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); 159 MakeRemote(new ProviderImpl(), &provider);
161 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass(), NULL);
162 160
163 sample::Enum value; 161 sample::Enum value;
164 { 162 {
165 AllocationScope scope; 163 AllocationScope scope;
166 provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value)); 164 provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value));
167 } 165 }
168 166
169 PumpMessages(); 167 PumpMessages();
170 168
171 EXPECT_EQ(sample::ENUM_VALUE, value); 169 EXPECT_EQ(sample::ENUM_VALUE, value);
172 } 170 }
173 171
174 } // namespace 172 } // namespace
175 } // namespace test 173 } // namespace test
176 } // namespace mojo 174 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698