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

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

Issue 2689513003: Add field-initializing constructors to generated mojo structs. (Closed)
Patch Set: rebase Created 3 years, 9 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 2015 The Chromium Authors. All rights reserved. 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 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 <string> 5 #include <string>
6 #include <utility> 6 #include <utility>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "mojo/public/cpp/bindings/binding_set.h" 13 #include "mojo/public/cpp/bindings/binding_set.h"
14 #include "mojo/public/cpp/bindings/interface_request.h" 14 #include "mojo/public/cpp/bindings/interface_request.h"
15 #include "mojo/public/cpp/bindings/tests/pickled_types_blink.h" 15 #include "mojo/public/cpp/bindings/tests/pickled_types_blink.h"
16 #include "mojo/public/cpp/bindings/tests/pickled_types_chromium.h" 16 #include "mojo/public/cpp/bindings/tests/pickled_types_chromium.h"
17 #include "mojo/public/cpp/bindings/tests/variant_test_util.h" 17 #include "mojo/public/cpp/bindings/tests/variant_test_util.h"
18 #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom-blink.h" 18 #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom-blink.h"
19 #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom.h" 19 #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 namespace mojo { 22 namespace mojo {
23 namespace test { 23 namespace test {
24 namespace { 24 namespace {
25 25
26 template <typename T> 26 template <typename T>
27 void DoExpectResult(int foo, 27 void DoExpectResult(int foo, int bar, const base::Closure& callback, T actual) {
28 int bar,
29 const base::Closure& callback,
30 const T& actual) {
31 EXPECT_EQ(foo, actual.foo()); 28 EXPECT_EQ(foo, actual.foo());
32 EXPECT_EQ(bar, actual.bar()); 29 EXPECT_EQ(bar, actual.bar());
33 callback.Run(); 30 callback.Run();
34 } 31 }
35 32
36 template <typename T> 33 template <typename T>
37 base::Callback<void(const T&)> ExpectResult(const T& t, 34 base::Callback<void(T)> ExpectResult(const T& t,
38 const base::Closure& callback) { 35 const base::Closure& callback) {
39 return base::Bind(&DoExpectResult<T>, t.foo(), t.bar(), callback); 36 return base::Bind(&DoExpectResult<T>, t.foo(), t.bar(), callback);
40 } 37 }
41 38
42 template <typename T> 39 template <typename T>
43 void DoFail(const std::string& reason, const T&) { 40 void DoFail(const std::string& reason, T) {
44 EXPECT_TRUE(false) << reason; 41 EXPECT_TRUE(false) << reason;
45 } 42 }
46 43
47 template <typename T> 44 template <typename T>
48 base::Callback<void(const T&)> Fail(const std::string& reason) { 45 base::Callback<void(T)> Fail(const std::string& reason) {
49 return base::Bind(&DoFail<T>, reason); 46 return base::Bind(&DoFail<T>, reason);
50 } 47 }
51 48
52 template <typename T> 49 template <typename T>
53 void DoExpectEnumResult(T expected, const base::Closure& callback, T actual) { 50 void DoExpectEnumResult(T expected, const base::Closure& callback, T actual) {
54 EXPECT_EQ(expected, actual); 51 EXPECT_EQ(expected, actual);
55 callback.Run(); 52 callback.Run();
56 } 53 }
57 54
58 template <typename T> 55 template <typename T>
(...skipping 23 matching lines...) Expand all
82 base::Callback<void(Arg)> BindSimpleLambda(Func func) { 79 base::Callback<void(Arg)> BindSimpleLambda(Func func) {
83 return base::Bind(&RunSimpleLambda<Func, Arg>, func); 80 return base::Bind(&RunSimpleLambda<Func, Arg>, func);
84 } 81 }
85 82
86 // This implements the generated Chromium variant of PicklePasser. 83 // This implements the generated Chromium variant of PicklePasser.
87 class ChromiumPicklePasserImpl : public PicklePasser { 84 class ChromiumPicklePasserImpl : public PicklePasser {
88 public: 85 public:
89 ChromiumPicklePasserImpl() {} 86 ChromiumPicklePasserImpl() {}
90 87
91 // mojo::test::PicklePasser: 88 // mojo::test::PicklePasser:
92 void PassPickledStruct(const PickledStructChromium& pickle, 89 void PassPickledStruct(PickledStructChromium pickle,
93 const PassPickledStructCallback& callback) override { 90 const PassPickledStructCallback& callback) override {
94 callback.Run(pickle); 91 callback.Run(std::move(pickle));
95 } 92 }
96 93
97 void PassPickledEnum(PickledEnumChromium pickle, 94 void PassPickledEnum(PickledEnumChromium pickle,
98 const PassPickledEnumCallback& callback) override { 95 const PassPickledEnumCallback& callback) override {
99 callback.Run(pickle); 96 callback.Run(pickle);
100 } 97 }
101 98
102 void PassPickleContainer( 99 void PassPickleContainer(
103 PickleContainerPtr container, 100 PickleContainerPtr container,
104 const PassPickleContainerCallback& callback) override { 101 const PassPickleContainerCallback& callback) override {
105 callback.Run(std::move(container)); 102 callback.Run(std::move(container));
106 } 103 }
107 104
108 void PassPickles(const std::vector<PickledStructChromium>& pickles, 105 void PassPickles(std::vector<PickledStructChromium> pickles,
109 const PassPicklesCallback& callback) override { 106 const PassPicklesCallback& callback) override {
110 callback.Run(pickles); 107 callback.Run(std::move(pickles));
111 } 108 }
112 109
113 void PassPickleArrays( 110 void PassPickleArrays(
114 const std::vector<std::vector<PickledStructChromium>>& pickle_arrays, 111 std::vector<std::vector<PickledStructChromium>> pickle_arrays,
115 const PassPickleArraysCallback& callback) override { 112 const PassPickleArraysCallback& callback) override {
116 callback.Run(pickle_arrays); 113 callback.Run(std::move(pickle_arrays));
117 } 114 }
118 }; 115 };
119 116
120 // This implements the generated Blink variant of PicklePasser. 117 // This implements the generated Blink variant of PicklePasser.
121 class BlinkPicklePasserImpl : public blink::PicklePasser { 118 class BlinkPicklePasserImpl : public blink::PicklePasser {
122 public: 119 public:
123 BlinkPicklePasserImpl() {} 120 BlinkPicklePasserImpl() {}
124 121
125 // mojo::test::blink::PicklePasser: 122 // mojo::test::blink::PicklePasser:
126 void PassPickledStruct(const PickledStructBlink& pickle, 123 void PassPickledStruct(PickledStructBlink pickle,
127 const PassPickledStructCallback& callback) override { 124 const PassPickledStructCallback& callback) override {
128 callback.Run(pickle); 125 callback.Run(std::move(pickle));
129 } 126 }
130 127
131 void PassPickledEnum(PickledEnumBlink pickle, 128 void PassPickledEnum(PickledEnumBlink pickle,
132 const PassPickledEnumCallback& callback) override { 129 const PassPickledEnumCallback& callback) override {
133 callback.Run(pickle); 130 callback.Run(pickle);
134 } 131 }
135 132
136 void PassPickleContainer( 133 void PassPickleContainer(
137 blink::PickleContainerPtr container, 134 blink::PickleContainerPtr container,
138 const PassPickleContainerCallback& callback) override { 135 const PassPickleContainerCallback& callback) override {
139 callback.Run(std::move(container)); 136 callback.Run(std::move(container));
140 } 137 }
141 138
142 void PassPickles(const WTF::Vector<PickledStructBlink>& pickles, 139 void PassPickles(WTF::Vector<PickledStructBlink> pickles,
143 const PassPicklesCallback& callback) override { 140 const PassPicklesCallback& callback) override {
144 callback.Run(pickles); 141 callback.Run(std::move(pickles));
145 } 142 }
146 143
147 void PassPickleArrays( 144 void PassPickleArrays(
148 const WTF::Vector<WTF::Vector<PickledStructBlink>>& pickle_arrays, 145 WTF::Vector<WTF::Vector<PickledStructBlink>> pickle_arrays,
149 const PassPickleArraysCallback& callback) override { 146 const PassPickleArraysCallback& callback) override {
150 callback.Run(pickle_arrays); 147 callback.Run(std::move(pickle_arrays));
151 } 148 }
152 }; 149 };
153 150
154 // A test which runs both Chromium and Blink implementations of the 151 // A test which runs both Chromium and Blink implementations of the
155 // PicklePasser service. 152 // PicklePasser service.
156 class PickleTest : public testing::Test { 153 class PickleTest : public testing::Test {
157 public: 154 public:
158 PickleTest() {} 155 PickleTest() {}
159 156
160 template <typename ProxyType = PicklePasser> 157 template <typename ProxyType = PicklePasser>
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 pickles[0].set_baz(100); 304 pickles[0].set_baz(100);
308 pickles[1].set_foo(3); 305 pickles[1].set_foo(3);
309 pickles[1].set_bar(4); 306 pickles[1].set_bar(4);
310 pickles[1].set_baz(100); 307 pickles[1].set_baz(100);
311 { 308 {
312 base::RunLoop run_loop; 309 base::RunLoop run_loop;
313 // Verify that the array of pickled structs can be serialized and 310 // Verify that the array of pickled structs can be serialized and
314 // deserialized intact. This ensures that the ParamTraits are actually used 311 // deserialized intact. This ensures that the ParamTraits are actually used
315 // rather than doing a byte-for-byte copy of the element data, beacuse the 312 // rather than doing a byte-for-byte copy of the element data, beacuse the
316 // |baz| field should never be serialized. 313 // |baz| field should never be serialized.
317 proxy->PassPickles( 314 proxy->PassPickles(std::move(pickles),
318 std::move(pickles), 315 BindSimpleLambda<std::vector<PickledStructChromium>>(
319 BindSimpleLambda<const std::vector<PickledStructChromium>&>( 316 [&](std::vector<PickledStructChromium> passed) {
320 [&](const std::vector<PickledStructChromium>& passed) { 317 ASSERT_EQ(2u, passed.size());
321 ASSERT_EQ(2u, passed.size()); 318 EXPECT_EQ(1, passed[0].foo());
322 EXPECT_EQ(1, passed[0].foo()); 319 EXPECT_EQ(2, passed[0].bar());
323 EXPECT_EQ(2, passed[0].bar()); 320 EXPECT_EQ(0, passed[0].baz());
324 EXPECT_EQ(0, passed[0].baz()); 321 EXPECT_EQ(3, passed[1].foo());
325 EXPECT_EQ(3, passed[1].foo()); 322 EXPECT_EQ(4, passed[1].bar());
326 EXPECT_EQ(4, passed[1].bar()); 323 EXPECT_EQ(0, passed[1].baz());
327 EXPECT_EQ(0, passed[1].baz()); 324 run_loop.Quit();
328 run_loop.Quit(); 325 }));
329 }));
330 run_loop.Run(); 326 run_loop.Run();
331 } 327 }
332 } 328 }
333 329
334 TEST_F(PickleTest, PickleArrayArray) { 330 TEST_F(PickleTest, PickleArrayArray) {
335 auto proxy = ConnectToChromiumService(); 331 auto proxy = ConnectToChromiumService();
336 auto pickle_arrays = std::vector<std::vector<PickledStructChromium>>(2); 332 auto pickle_arrays = std::vector<std::vector<PickledStructChromium>>(2);
337 for (size_t i = 0; i < 2; ++i) 333 for (size_t i = 0; i < 2; ++i)
338 pickle_arrays[i] = std::vector<PickledStructChromium>(2); 334 pickle_arrays[i] = std::vector<PickledStructChromium>(2);
339 335
340 pickle_arrays[0][0].set_foo(1); 336 pickle_arrays[0][0].set_foo(1);
341 pickle_arrays[0][0].set_bar(2); 337 pickle_arrays[0][0].set_bar(2);
342 pickle_arrays[0][0].set_baz(100); 338 pickle_arrays[0][0].set_baz(100);
343 pickle_arrays[0][1].set_foo(3); 339 pickle_arrays[0][1].set_foo(3);
344 pickle_arrays[0][1].set_bar(4); 340 pickle_arrays[0][1].set_bar(4);
345 pickle_arrays[0][1].set_baz(100); 341 pickle_arrays[0][1].set_baz(100);
346 pickle_arrays[1][0].set_foo(5); 342 pickle_arrays[1][0].set_foo(5);
347 pickle_arrays[1][0].set_bar(6); 343 pickle_arrays[1][0].set_bar(6);
348 pickle_arrays[1][0].set_baz(100); 344 pickle_arrays[1][0].set_baz(100);
349 pickle_arrays[1][1].set_foo(7); 345 pickle_arrays[1][1].set_foo(7);
350 pickle_arrays[1][1].set_bar(8); 346 pickle_arrays[1][1].set_bar(8);
351 pickle_arrays[1][1].set_baz(100); 347 pickle_arrays[1][1].set_baz(100);
352 { 348 {
353 base::RunLoop run_loop; 349 base::RunLoop run_loop;
354 // Verify that the array-of-arrays serializes and deserializes properly. 350 // Verify that the array-of-arrays serializes and deserializes properly.
355 proxy->PassPickleArrays( 351 proxy->PassPickleArrays(
356 pickle_arrays, 352 std::move(pickle_arrays),
357 BindSimpleLambda< 353 BindSimpleLambda<std::vector<std::vector<PickledStructChromium>>>(
358 const std::vector<std::vector<PickledStructChromium>>&>( 354 [&](std::vector<std::vector<PickledStructChromium>> passed) {
359 [&](const std::vector<std::vector<PickledStructChromium>>& passed) {
360 ASSERT_EQ(2u, passed.size()); 355 ASSERT_EQ(2u, passed.size());
361 ASSERT_EQ(2u, passed[0].size()); 356 ASSERT_EQ(2u, passed[0].size());
362 ASSERT_EQ(2u, passed[1].size()); 357 ASSERT_EQ(2u, passed[1].size());
363 EXPECT_EQ(1, passed[0][0].foo()); 358 EXPECT_EQ(1, passed[0][0].foo());
364 EXPECT_EQ(2, passed[0][0].bar()); 359 EXPECT_EQ(2, passed[0][0].bar());
365 EXPECT_EQ(0, passed[0][0].baz()); 360 EXPECT_EQ(0, passed[0][0].baz());
366 EXPECT_EQ(3, passed[0][1].foo()); 361 EXPECT_EQ(3, passed[0][1].foo());
367 EXPECT_EQ(4, passed[0][1].bar()); 362 EXPECT_EQ(4, passed[0][1].bar());
368 EXPECT_EQ(0, passed[0][1].baz()); 363 EXPECT_EQ(0, passed[0][1].baz());
369 EXPECT_EQ(5, passed[1][0].foo()); 364 EXPECT_EQ(5, passed[1][0].foo());
(...skipping 29 matching lines...) Expand all
399 EXPECT_EQ(PickledEnumChromium::VALUE_1, 394 EXPECT_EQ(PickledEnumChromium::VALUE_1,
400 passed->f_enum); 395 passed->f_enum);
401 run_loop.Quit(); 396 run_loop.Quit();
402 })); 397 }));
403 run_loop.Run(); 398 run_loop.Run();
404 } 399 }
405 } 400 }
406 401
407 } // namespace test 402 } // namespace test
408 } // namespace mojo 403 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/hash_unittest.cc ('k') | mojo/public/cpp/bindings/tests/pickled_types_blink.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698