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

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

Issue 1518293002: [mojo] Add Mojo bindings support for IPC::ParamTraits (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pickle4
Patch Set: 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 <string>
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "mojo/common/weak_binding_set.h"
13 #include "mojo/public/cpp/bindings/interface_request.h"
14 #include "mojo/public/cpp/bindings/tests/pickled_struct_blink.h"
15 #include "mojo/public/cpp/bindings/tests/pickled_struct_chromium.h"
16 #include "mojo/public/interfaces/bindings/tests/test_pickles.mojom-blink.h"
17 #include "mojo/public/interfaces/bindings/tests/test_pickles.mojom-chromium.h"
18 #include "mojo/public/interfaces/bindings/tests/test_pickles.mojom.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace mojo {
22 namespace test {
23 namespace {
24
25 template <typename T>
26 void DoExpectResult(int foo,
27 int bar,
28 const base::Closure& callback,
29 const T& actual) {
30 EXPECT_EQ(foo, actual.foo());
31 EXPECT_EQ(bar, actual.bar());
32 callback.Run();
33 }
34
35 template <typename T>
36 base::Callback<void(const T&)> ExpectResult(const T& t,
37 const base::Closure& callback) {
38 return base::Bind(&DoExpectResult<T>, t.foo(), t.bar(), callback);
39 }
40
41 template <typename T>
42 void DoFail(const std::string& reason, const T&) {
43 EXPECT_TRUE(false) << reason;
44 }
45
46 template <typename T>
47 base::Callback<void(const T&)> Fail(const std::string& reason) {
48 return base::Bind(&DoFail<T>, reason);
49 }
50
51 template <typename T>
52 void ExpectError(InterfacePtr<T> *proxy, const base::Closure& callback) {
53 proxy->set_connection_error_handler(callback);
54 }
55
56 // This implements the generated Chromium variant of PicklePasser.
57 class ChromiumPicklePasserImpl : public chromium::PicklePasser {
58 public:
59 ChromiumPicklePasserImpl() {}
60
61 // mojo::test::chromium::PicklePasser:
62 void PassPickle(const PickledStructChromium& pickle,
63 const PassPickleCallback& callback) override {
64 callback.Run(pickle);
65 }
66 };
67
68 // This implements the generated Blink variant of PicklePasser.
69 class BlinkPicklePasserImpl : public blink::PicklePasser {
70 public:
71 BlinkPicklePasserImpl() {}
72
73 // mojo::test::blink::PicklePasser:
74 void PassPickle(const PickledStructBlink& pickle,
75 const PassPickleCallback& callback) override {
76 callback.Run(pickle);
77 }
78 };
79
80 // A test which runs both Chromium and Blink implementations of the
81 // PicklePasser service.
82 class PickleTest : public testing::Test {
83 public:
84 PickleTest() {}
85
86 void BindToChromiumService(mojo::InterfaceRequest<PicklePasser> request) {
87 chromium_bindings_.AddBinding(&chromium_service_, std::move(request));
88 }
89
90 void BindToBlinkService(mojo::InterfaceRequest<PicklePasser> request) {
91 blink_bindings_.AddBinding(&blink_service_, std::move(request));
92 }
93
94 private:
95 base::MessageLoop loop_;
96
97 ChromiumPicklePasserImpl chromium_service_;
98 mojo::WeakBindingSet<chromium::PicklePasser> chromium_bindings_;
99
100 BlinkPicklePasserImpl blink_service_;
101 mojo::WeakBindingSet<blink::PicklePasser> blink_bindings_;
102 };
103
104 } // namespace
105
106 TEST_F(PickleTest, ChromiumProxyToChromiumService) {
107 chromium::PicklePasserPtr chromium_proxy;
108 BindToChromiumService(GetProxy(&chromium_proxy));
109 {
110 base::RunLoop loop;
111 chromium_proxy->PassPickle(
112 PickledStructChromium(1, 2),
113 ExpectResult(PickledStructChromium(1, 2), loop.QuitClosure()));
114 loop.Run();
115 }
116 {
117 base::RunLoop loop;
118 chromium_proxy->PassPickle(
119 PickledStructChromium(4, 5),
120 ExpectResult(PickledStructChromium(4, 5), loop.QuitClosure()));
121 loop.Run();
122 }
123 }
124
125 TEST_F(PickleTest, ChromiumProxyToBlinkService) {
126 chromium::PicklePasserPtr chromium_proxy;
127 BindToBlinkService(GetProxy(&chromium_proxy));
128 {
129 base::RunLoop loop;
130 chromium_proxy->PassPickle(
131 PickledStructChromium(1, 2),
132 ExpectResult(PickledStructChromium(1, 2), loop.QuitClosure()));
133 loop.Run();
134 }
135 {
136 base::RunLoop loop;
137 chromium_proxy->PassPickle(
138 PickledStructChromium(4, 5),
139 ExpectResult(PickledStructChromium(4, 5), loop.QuitClosure()));
140 loop.Run();
141 }
142 // The Blink service should drop our connection because the
143 // PickledStructBlink ParamTraits deserializer rejects negative values.
144 {
145 base::RunLoop loop;
146 chromium_proxy->PassPickle(
147 PickledStructChromium(-1, -1),
148 Fail<PickledStructChromium>("Blink service should reject this."));
149 ExpectError(&chromium_proxy, loop.QuitClosure());
150 loop.Run();
151 }
152 }
153
154 TEST_F(PickleTest, BlinkProxyToBlinkService) {
155 blink::PicklePasserPtr blink_proxy;
156 BindToBlinkService(GetProxy(&blink_proxy));
157 {
158 base::RunLoop loop;
159 blink_proxy->PassPickle(
160 PickledStructBlink(1, 1),
161 ExpectResult(PickledStructBlink(1, 1), loop.QuitClosure()));
162 loop.Run();
163 }
164 }
165
166 TEST_F(PickleTest, BlinkProxyToChromiumService) {
167 blink::PicklePasserPtr blink_proxy;
168 BindToChromiumService(GetProxy(&blink_proxy));
169 {
170 base::RunLoop loop;
171 blink_proxy->PassPickle(
172 PickledStructBlink(1, 1),
173 ExpectResult(PickledStructBlink(1, 1), loop.QuitClosure()));
174 loop.Run();
175 }
176 }
177
178 } // namespace test
179 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/BUILD.gn ('k') | mojo/public/cpp/bindings/tests/pickled_struct_blink.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698