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

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

Issue 1552983003: Fix a bunch of mojo_public_*_unittests with the new EDK. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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
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 <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
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 { *buf_ = a; closure_.Run(); }
yzshen1 2016/01/04 18:54:25 nit: please put the statements on separate lines.
jam 2016/01/04 21:00:06 Done.
61 void Run(const String& a, const String& b) const { 63 void Run(const String& a, const String& b) const {
62 *buf_ = a.get() + b.get(); 64 *buf_ = a.get() + b.get();
65 closure_.Run();
63 } 66 }
64 67
65 private: 68 private:
66 std::string* buf_; 69 std::string* buf_;
70 base::Closure closure_;
67 }; 71 };
68 72
69 class EnumRecorder { 73 class EnumRecorder {
70 public: 74 public:
71 explicit EnumRecorder(sample::Enum* value) : value_(value) {} 75 explicit EnumRecorder(sample::Enum* value, const base::Closure& closure)
72 void Run(sample::Enum a) const { *value_ = a; } 76 : value_(value), closure_(closure) {}
77 void Run(sample::Enum a) const { *value_ = a; closure_.Run(); }
73 78
74 private: 79 private:
75 sample::Enum* value_; 80 sample::Enum* value_;
81 base::Closure closure_;
76 }; 82 };
77 83
78 class MessagePipeWriter { 84 class MessagePipeWriter {
79 public: 85 public:
80 explicit MessagePipeWriter(const char* text) : text_(text) {} 86 MessagePipeWriter(const char* text, const base::Closure& closure)
87 : text_(text), closure_(closure) {}
81 void Run(ScopedMessagePipeHandle handle) const { 88 void Run(ScopedMessagePipeHandle handle) const {
82 WriteTextMessage(handle.get(), text_); 89 WriteTextMessage(handle.get(), text_);
90 closure_.Run();
83 } 91 }
84 92
85 private: 93 private:
86 std::string text_; 94 std::string text_;
95 base::Closure closure_;
87 }; 96 };
88 97
89 class RequestResponseTest : public testing::Test { 98 class RequestResponseTest : public testing::Test {
90 public: 99 public:
91 RequestResponseTest() : loop_(common::MessagePumpMojo::Create()) {} 100 RequestResponseTest() : loop_(common::MessagePumpMojo::Create()) {}
92 ~RequestResponseTest() override { loop_.RunUntilIdle(); } 101 ~RequestResponseTest() override { loop_.RunUntilIdle(); }
93 102
94 void PumpMessages() { loop_.RunUntilIdle(); } 103 void PumpMessages() { loop_.RunUntilIdle(); }
95 104
96 private: 105 private:
97 base::MessageLoop loop_; 106 base::MessageLoop loop_;
98 }; 107 };
99 108
100 TEST_F(RequestResponseTest, EchoString) { 109 TEST_F(RequestResponseTest, EchoString) {
101 sample::ProviderPtr provider; 110 sample::ProviderPtr provider;
102 ProviderImpl provider_impl(GetProxy(&provider)); 111 ProviderImpl provider_impl(GetProxy(&provider));
103 112
104 std::string buf; 113 std::string buf;
105 provider->EchoString(String::From("hello"), StringRecorder(&buf)); 114 base::RunLoop run_loop;
115 provider->EchoString(String::From("hello"),
116 StringRecorder(&buf, run_loop.QuitClosure()));
106 117
107 PumpMessages(); 118 run_loop.Run();
108 119
109 EXPECT_EQ(std::string("hello"), buf); 120 EXPECT_EQ(std::string("hello"), buf);
110 } 121 }
111 122
112 TEST_F(RequestResponseTest, EchoStrings) { 123 TEST_F(RequestResponseTest, EchoStrings) {
113 sample::ProviderPtr provider; 124 sample::ProviderPtr provider;
114 ProviderImpl provider_impl(GetProxy(&provider)); 125 ProviderImpl provider_impl(GetProxy(&provider));
115 126
116 std::string buf; 127 std::string buf;
128 base::RunLoop run_loop;
117 provider->EchoStrings( 129 provider->EchoStrings(
118 String::From("hello"), String::From(" world"), StringRecorder(&buf)); 130 String::From("hello"), String::From(" world"),
131 StringRecorder(&buf, run_loop.QuitClosure()));
119 132
120 PumpMessages(); 133 run_loop.Run();
121 134
122 EXPECT_EQ(std::string("hello world"), buf); 135 EXPECT_EQ(std::string("hello world"), buf);
123 } 136 }
124 137
125 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { 138 TEST_F(RequestResponseTest, EchoMessagePipeHandle) {
126 sample::ProviderPtr provider; 139 sample::ProviderPtr provider;
127 ProviderImpl provider_impl(GetProxy(&provider)); 140 ProviderImpl provider_impl(GetProxy(&provider));
128 141
129 MessagePipe pipe2; 142 MessagePipe pipe2;
130 provider->EchoMessagePipeHandle(std::move(pipe2.handle1), 143 base::RunLoop run_loop;
131 MessagePipeWriter("hello")); 144 provider->EchoMessagePipeHandle(
145 std::move(pipe2.handle1),
146 MessagePipeWriter("hello", run_loop.QuitClosure()));
132 147
133 PumpMessages(); 148 run_loop.Run();
134 149
135 std::string value; 150 std::string value;
136 ReadTextMessage(pipe2.handle0.get(), &value); 151 ReadTextMessage(pipe2.handle0.get(), &value);
137 152
138 EXPECT_EQ(std::string("hello"), value); 153 EXPECT_EQ(std::string("hello"), value);
139 } 154 }
140 155
141 TEST_F(RequestResponseTest, EchoEnum) { 156 TEST_F(RequestResponseTest, EchoEnum) {
142 sample::ProviderPtr provider; 157 sample::ProviderPtr provider;
143 ProviderImpl provider_impl(GetProxy(&provider)); 158 ProviderImpl provider_impl(GetProxy(&provider));
144 159
145 sample::Enum value; 160 sample::Enum value;
146 provider->EchoEnum(sample::ENUM_VALUE, EnumRecorder(&value)); 161 base::RunLoop run_loop;
162 provider->EchoEnum(sample::ENUM_VALUE,
163 EnumRecorder(&value, run_loop.QuitClosure()));
147 164
148 PumpMessages(); 165 run_loop.Run();
149 166
150 EXPECT_EQ(sample::ENUM_VALUE, value); 167 EXPECT_EQ(sample::ENUM_VALUE, value);
151 } 168 }
152 169
153 } // namespace 170 } // namespace
154 } // namespace test 171 } // namespace test
155 } // namespace mojo 172 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698