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

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

Issue 1524703002: [mojo] Support native types with mojom wire format (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bindings-5-pickles
Patch Set: doc Created 5 years 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
(Empty)
1 // Copyright 2015 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 "base/bind.h"
6 #include "base/callback.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "mojo/common/weak_binding_set.h"
11 #include "mojo/public/cpp/bindings/interface_request.h"
12 #include "mojo/public/cpp/bindings/tests/rect_blink.h"
13 #include "mojo/public/cpp/bindings/tests/rect_chromium.h"
14 #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom-blink.h"
15 #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom-chromium .h"
16 #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace mojo {
20 namespace test {
21 namespace {
22
23 template <typename T>
24 void DoExpectResult(const T& expected,
25 const base::Closure& callback,
26 const T& actual) {
27 EXPECT_EQ(expected.x(), actual.x());
28 EXPECT_EQ(expected.y(), actual.y());
29 EXPECT_EQ(expected.width(), actual.width());
30 EXPECT_EQ(expected.height(), actual.height());
31 callback.Run();
32 }
33
34 template <typename T>
35 base::Callback<void(const T&)> ExpectResult(const T& r,
36 const base::Closure& callback) {
37 return base::Bind(&DoExpectResult<T>, r, callback);
38 }
39
40 template <typename T>
41 void DoFail(const std::string& reason, const T&) {
42 EXPECT_TRUE(false) << reason;
43 }
44
45 template <typename T>
46 base::Callback<void(const T&)> Fail(const std::string& reason) {
47 return base::Bind(&DoFail<T>, reason);
48 }
49
50 template <typename T>
51 void ExpectError(InterfacePtr<T> *proxy, const base::Closure& callback) {
52 proxy->set_connection_error_handler(callback);
53 }
54
55 // This implements the generated Chromium variant of RectService.
56 class ChromiumRectServiceImpl : public chromium::RectService {
57 public:
58 ChromiumRectServiceImpl() {}
59
60 // mojo::test::chromium::RectService:
61 void AddRect(const RectChromium& r) override {
62 if (r.GetArea() > largest_rect_.GetArea())
63 largest_rect_ = r;
64 }
65
66 void GetLargestRect(const GetLargestRectCallback& callback) override {
67 callback.Run(largest_rect_);
68 }
69
70 private:
71 RectChromium largest_rect_;
72 };
73
74 // This implements the generated Blink variant of RectService.
75 class BlinkRectServiceImpl : public blink::RectService {
76 public:
77 BlinkRectServiceImpl() {}
78
79 // mojo::test::blink::RectService:
80 void AddRect(const RectBlink& r) override {
81 if (r.computeArea() > largest_rect_.computeArea()) {
82 largest_rect_.setX(r.x());
83 largest_rect_.setY(r.y());
84 largest_rect_.setWidth(r.width());
85 largest_rect_.setHeight(r.height());
86 }
87 }
88
89 void GetLargestRect(const GetLargestRectCallback& callback) override {
90 callback.Run(largest_rect_);
91 }
92
93 private:
94 RectBlink largest_rect_;
95 };
96
97 // A test which runs both Chromium and Blink implementations of a RectService.
98 class StructTraitsTest : public testing::Test {
99 public:
100 StructTraitsTest() {}
101
102 void BindToChromiumService(mojo::InterfaceRequest<RectService> request) {
103 chromium_bindings_.AddBinding(&chromium_service_, std::move(request));
104 }
105
106 void BindToBlinkService(mojo::InterfaceRequest<RectService> request) {
107 blink_bindings_.AddBinding(&blink_service_, std::move(request));
108 }
109
110 private:
111 base::MessageLoop loop_;
112
113 ChromiumRectServiceImpl chromium_service_;
114 mojo::WeakBindingSet<chromium::RectService> chromium_bindings_;
115
116 BlinkRectServiceImpl blink_service_;
117 mojo::WeakBindingSet<blink::RectService> blink_bindings_;
118 };
119
120 } // namespace
121
122 TEST_F(StructTraitsTest, ChromiumProxyToChromiumService) {
123 chromium::RectServicePtr chromium_proxy;
124 BindToChromiumService(GetProxy(&chromium_proxy));
125 {
126 base::RunLoop loop;
127 chromium_proxy->AddRect(RectChromium(1, 1, 4, 5));
128 chromium_proxy->AddRect(RectChromium(-1, -1, 2, 2));
129 chromium_proxy->GetLargestRect(
130 ExpectResult(RectChromium(1, 1, 4, 5), loop.QuitClosure()));
131 loop.Run();
132 }
133 }
134
135 TEST_F(StructTraitsTest, ChromiumToBlinkService) {
136 chromium::RectServicePtr chromium_proxy;
137 BindToBlinkService(GetProxy(&chromium_proxy));
138 {
139 base::RunLoop loop;
140 chromium_proxy->AddRect(RectChromium(1, 1, 4, 5));
141 chromium_proxy->AddRect(RectChromium(2, 2, 5, 5));
142 chromium_proxy->GetLargestRect(
143 ExpectResult(RectChromium(2, 2, 5, 5), loop.QuitClosure()));
144 loop.Run();
145 }
146 // The Blink service should drop our connection because RectBlink's
147 // deserializer rejects negative origins.
148 {
149 base::RunLoop loop;
150 ExpectError(&chromium_proxy, loop.QuitClosure());
151 chromium_proxy->AddRect(RectChromium(-1, -1, 2, 2));
152 chromium_proxy->GetLargestRect(
153 Fail<RectChromium>("The pipe should have been closed."));
154 loop.Run();
155 }
156 }
157
158 TEST_F(StructTraitsTest, BlinkProxyToBlinkService) {
159 blink::RectServicePtr blink_proxy;
160 BindToBlinkService(GetProxy(&blink_proxy));
161 {
162 base::RunLoop loop;
163 blink_proxy->AddRect(RectBlink(1, 1, 4, 5));
164 blink_proxy->AddRect(RectBlink(10, 10, 20, 20));
165 blink_proxy->GetLargestRect(
166 ExpectResult(RectBlink(10, 10, 20, 20), loop.QuitClosure()));
167 loop.Run();
168 }
169 }
170
171 TEST_F(StructTraitsTest, BlinkProxyToChromiumService) {
172 blink::RectServicePtr blink_proxy;
173 BindToChromiumService(GetProxy(&blink_proxy));
174 {
175 base::RunLoop loop;
176 blink_proxy->AddRect(RectBlink(1, 1, 4, 5));
177 blink_proxy->AddRect(RectBlink(10, 10, 2, 2));
178 blink_proxy->GetLargestRect(
179 ExpectResult(RectBlink(1, 1, 4, 5), loop.QuitClosure()));
180 loop.Run();
181 }
182 }
183
184 } // namespace test
185 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698