OLD | NEW |
---|---|
(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_native_types.mojom-blink.h" | |
17 #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom-chromium .h" | |
18 #include "mojo/public/interfaces/bindings/tests/test_native_types.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) { | |
yzshen1
2015/12/15 21:20:27
style nit: * should be adjacent to the type instea
Ken Rockot(use gerrit already)
2015/12/15 23:31:08
oops, Done
| |
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 ChromiumPicklePasserImpl chromium_service_; | |
97 mojo::WeakBindingSet<chromium::PicklePasser> chromium_bindings_; | |
98 BlinkPicklePasserImpl blink_service_; | |
99 mojo::WeakBindingSet<blink::PicklePasser> blink_bindings_; | |
100 }; | |
101 | |
102 } // namespace | |
103 | |
104 TEST_F(PickleTest, ChromiumProxyToChromiumService) { | |
105 chromium::PicklePasserPtr chromium_proxy; | |
106 BindToChromiumService(GetProxy(&chromium_proxy)); | |
107 { | |
108 base::RunLoop loop; | |
109 chromium_proxy->PassPickle( | |
110 PickledStructChromium(1, 2), | |
111 ExpectResult(PickledStructChromium(1, 2), loop.QuitClosure())); | |
112 loop.Run(); | |
113 } | |
114 { | |
115 base::RunLoop loop; | |
116 chromium_proxy->PassPickle( | |
117 PickledStructChromium(4, 5), | |
118 ExpectResult(PickledStructChromium(4, 5), loop.QuitClosure())); | |
119 loop.Run(); | |
120 } | |
121 } | |
122 | |
123 TEST_F(PickleTest, ChromiumProxyToBlinkService) { | |
124 chromium::PicklePasserPtr chromium_proxy; | |
125 BindToBlinkService(GetProxy(&chromium_proxy)); | |
126 { | |
127 base::RunLoop loop; | |
128 chromium_proxy->PassPickle( | |
129 PickledStructChromium(1, 2), | |
130 ExpectResult(PickledStructChromium(1, 2), loop.QuitClosure())); | |
131 loop.Run(); | |
132 } | |
133 { | |
134 base::RunLoop loop; | |
135 chromium_proxy->PassPickle( | |
136 PickledStructChromium(4, 5), | |
137 ExpectResult(PickledStructChromium(4, 5), loop.QuitClosure())); | |
138 loop.Run(); | |
139 } | |
140 // The Blink service should drop our connection because the | |
141 // PickledStructBlink ParamTraits deserializer rejects negative values. | |
142 { | |
143 base::RunLoop loop; | |
144 chromium_proxy->PassPickle( | |
145 PickledStructChromium(-1, -1), | |
146 Fail<PickledStructChromium>("Blink service should reject this.")); | |
147 ExpectError(&chromium_proxy, loop.QuitClosure()); | |
148 loop.Run(); | |
149 } | |
150 } | |
151 | |
152 TEST_F(PickleTest, BlinkProxyToBlinkService) { | |
153 blink::PicklePasserPtr blink_proxy; | |
154 BindToBlinkService(GetProxy(&blink_proxy)); | |
155 { | |
156 base::RunLoop loop; | |
157 blink_proxy->PassPickle( | |
158 PickledStructBlink(1, 1), | |
159 ExpectResult(PickledStructBlink(1, 1), loop.QuitClosure())); | |
160 loop.Run(); | |
161 } | |
162 } | |
163 | |
164 TEST_F(PickleTest, BlinkProxyToChromiumService) { | |
165 blink::PicklePasserPtr blink_proxy; | |
166 BindToChromiumService(GetProxy(&blink_proxy)); | |
167 { | |
168 base::RunLoop loop; | |
169 blink_proxy->PassPickle( | |
170 PickledStructBlink(1, 1), | |
171 ExpectResult(PickledStructBlink(1, 1), loop.QuitClosure())); | |
172 loop.Run(); | |
173 } | |
174 } | |
175 | |
176 } // namespace test | |
177 } // namespace mojo | |
OLD | NEW |