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

Side by Side Diff: mojo/common/common_custom_types_unittest.cc

Issue 2577563002: Add struct traits for base::Value. (Closed)
Patch Set: rebase Created 4 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
« no previous file with comments | « media/base/video_frame_metadata.cc ('k') | mojo/common/test_common_custom_types.mojom » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "base/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/files/scoped_temp_dir.h" 6 #include "base/files/scoped_temp_dir.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/numerics/safe_math.h" 8 #include "base/numerics/safe_math.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "mojo/common/test_common_custom_types.mojom.h" 12 #include "mojo/common/test_common_custom_types.mojom.h"
13 #include "mojo/public/cpp/bindings/binding.h" 13 #include "mojo/public/cpp/bindings/binding.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 namespace mojo { 16 namespace mojo {
17 namespace common { 17 namespace common {
18 namespace test { 18 namespace test {
19 namespace { 19 namespace {
20 20
21 template <typename T> 21 template <typename T>
22 struct BounceTestTraits { 22 struct BounceTestTraits {
23 static void ExpectEquality(const T& a, const T& b) { 23 static void ExpectEquality(const T& a, const T& b) {
24 EXPECT_EQ(a, b); 24 EXPECT_EQ(a, b);
25 } 25 }
26 }; 26 };
27 27
28 template <>
29 struct BounceTestTraits<base::DictionaryValue> {
30 static void ExpectEquality(const base::DictionaryValue& a,
31 const base::DictionaryValue& b) {
32 EXPECT_TRUE(a.Equals(&b));
33 }
34 };
35
36 template <>
37 struct BounceTestTraits<base::ListValue> {
38 static void ExpectEquality(const base::ListValue& a,
39 const base::ListValue& b) {
40 EXPECT_TRUE(a.Equals(&b));
41 }
42 };
43
44 template <typename T> 28 template <typename T>
45 struct PassTraits { 29 struct PassTraits {
46 using Type = const T&; 30 using Type = const T&;
47 }; 31 };
48 32
49 template <> 33 template <>
50 struct PassTraits<base::Time> { 34 struct PassTraits<base::Time> {
51 using Type = base::Time; 35 using Type = base::Time;
52 }; 36 };
53 37
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 mojo::Binding<TestTime> binding_; 114 mojo::Binding<TestTime> binding_;
131 }; 115 };
132 116
133 class TestValueImpl : public TestValue { 117 class TestValueImpl : public TestValue {
134 public: 118 public:
135 explicit TestValueImpl(TestValueRequest request) 119 explicit TestValueImpl(TestValueRequest request)
136 : binding_(this, std::move(request)) {} 120 : binding_(this, std::move(request)) {}
137 121
138 // TestValue implementation: 122 // TestValue implementation:
139 void BounceDictionaryValue( 123 void BounceDictionaryValue(
140 const base::DictionaryValue& in, 124 std::unique_ptr<base::DictionaryValue> in,
141 const BounceDictionaryValueCallback& callback) override { 125 const BounceDictionaryValueCallback& callback) override {
142 callback.Run(in); 126 callback.Run(std::move(in));
143 } 127 }
144 void BounceListValue(const base::ListValue& in, 128
129 void BounceListValue(std::unique_ptr<base::ListValue> in,
145 const BounceListValueCallback& callback) override { 130 const BounceListValueCallback& callback) override {
146 callback.Run(in); 131 callback.Run(std::move(in));
132 }
133
134 void BounceValue(std::unique_ptr<base::Value> in,
135 const BounceValueCallback& callback) override {
136 callback.Run(std::move(in));
147 } 137 }
148 138
149 private: 139 private:
150 mojo::Binding<TestValue> binding_; 140 mojo::Binding<TestValue> binding_;
151 }; 141 };
152 142
153 class TestString16Impl : public TestString16 { 143 class TestString16Impl : public TestString16 {
154 public: 144 public:
155 explicit TestString16Impl(TestString16Request request) 145 explicit TestString16Impl(TestString16Request request)
156 : binding_(this, std::move(request)) {} 146 : binding_(this, std::move(request)) {}
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 245
256 ptr->BounceTimeTicks(t, ExpectResponse(&t, run_loop.QuitClosure())); 246 ptr->BounceTimeTicks(t, ExpectResponse(&t, run_loop.QuitClosure()));
257 247
258 run_loop.Run(); 248 run_loop.Run();
259 } 249 }
260 250
261 TEST_F(CommonCustomTypesTest, Value) { 251 TEST_F(CommonCustomTypesTest, Value) {
262 TestValuePtr ptr; 252 TestValuePtr ptr;
263 TestValueImpl impl(MakeRequest(&ptr)); 253 TestValueImpl impl(MakeRequest(&ptr));
264 254
265 base::DictionaryValue dict; 255 std::unique_ptr<base::Value> output;
266 dict.SetBoolean("bool", false); 256
267 dict.SetInteger("int", 2); 257 ASSERT_TRUE(ptr->BounceValue(nullptr, &output));
268 dict.SetString("string", "some string"); 258 EXPECT_FALSE(output);
269 dict.SetBoolean("nested.bool", true); 259
270 dict.SetInteger("nested.int", 9); 260 std::unique_ptr<base::Value> input = base::Value::CreateNullValue();
271 dict.Set("some_binary", base::BinaryValue::CreateWithCopiedBuffer("mojo", 4)); 261 ASSERT_TRUE(ptr->BounceValue(input->CreateDeepCopy(), &output));
262 EXPECT_TRUE(base::Value::Equals(input.get(), output.get()));
263
264 input = base::MakeUnique<base::FundamentalValue>(123);
265 ASSERT_TRUE(ptr->BounceValue(input->CreateDeepCopy(), &output));
266 EXPECT_TRUE(base::Value::Equals(input.get(), output.get()));
267
268 input = base::MakeUnique<base::FundamentalValue>(1.23);
269 ASSERT_TRUE(ptr->BounceValue(input->CreateDeepCopy(), &output));
270 EXPECT_TRUE(base::Value::Equals(input.get(), output.get()));
271
272 input = base::MakeUnique<base::FundamentalValue>(false);
273 ASSERT_TRUE(ptr->BounceValue(input->CreateDeepCopy(), &output));
274 EXPECT_TRUE(base::Value::Equals(input.get(), output.get()));
275
276 input = base::MakeUnique<base::StringValue>("test string");
277 ASSERT_TRUE(ptr->BounceValue(input->CreateDeepCopy(), &output));
278 EXPECT_TRUE(base::Value::Equals(input.get(), output.get()));
279
280 input = base::BinaryValue::CreateWithCopiedBuffer("mojo", 4);
281 ASSERT_TRUE(ptr->BounceValue(input->CreateDeepCopy(), &output));
282 EXPECT_TRUE(base::Value::Equals(input.get(), output.get()));
283
284 auto dict = base::MakeUnique<base::DictionaryValue>();
285 dict->SetBoolean("bool", false);
286 dict->SetInteger("int", 2);
287 dict->SetString("string", "some string");
288 dict->SetBoolean("nested.bool", true);
289 dict->SetInteger("nested.int", 9);
290 dict->Set("some_binary",
291 base::BinaryValue::CreateWithCopiedBuffer("mojo", 4));
292 dict->Set("null_value", base::Value::CreateNullValue());
272 { 293 {
273 std::unique_ptr<base::ListValue> dict_list(new base::ListValue()); 294 std::unique_ptr<base::ListValue> dict_list(new base::ListValue());
274 dict_list->AppendString("string"); 295 dict_list->AppendString("string");
275 dict_list->AppendBoolean(true); 296 dict_list->AppendBoolean(true);
276 dict.Set("list", std::move(dict_list)); 297 dict->Set("list", std::move(dict_list));
277 }
278 {
279 base::RunLoop run_loop;
280 ptr->BounceDictionaryValue(
281 dict, ExpectResponse(&dict, run_loop.QuitClosure()));
282 run_loop.Run();
283 } 298 }
284 299
285 base::ListValue list; 300 std::unique_ptr<base::DictionaryValue> dict_output;
286 list.AppendString("string"); 301 ASSERT_TRUE(ptr->BounceDictionaryValue(dict->CreateDeepCopy(), &dict_output));
287 list.AppendDouble(42.1); 302 EXPECT_TRUE(base::Value::Equals(dict.get(), dict_output.get()));
288 list.AppendBoolean(true); 303
289 list.Append(base::BinaryValue::CreateWithCopiedBuffer("mojo", 4)); 304 input = std::move(dict);
305 ASSERT_TRUE(ptr->BounceValue(input->CreateDeepCopy(), &output));
306 EXPECT_TRUE(base::Value::Equals(input.get(), output.get()));
307
308 auto list = base::MakeUnique<base::ListValue>();
309 list->AppendString("string");
310 list->AppendDouble(42.1);
311 list->AppendBoolean(true);
312 list->Append(base::BinaryValue::CreateWithCopiedBuffer("mojo", 4));
313 list->Append(base::Value::CreateNullValue());
290 { 314 {
291 std::unique_ptr<base::DictionaryValue> list_dict( 315 std::unique_ptr<base::DictionaryValue> list_dict(
292 new base::DictionaryValue()); 316 new base::DictionaryValue());
293 list_dict->SetString("string", "str"); 317 list_dict->SetString("string", "str");
294 list.Append(std::move(list_dict)); 318 list->Append(std::move(list_dict));
295 } 319 }
296 { 320 std::unique_ptr<base::ListValue> list_output;
297 base::RunLoop run_loop; 321 ASSERT_TRUE(ptr->BounceListValue(list->CreateDeepCopy(), &list_output));
298 ptr->BounceListValue(list, ExpectResponse(&list, run_loop.QuitClosure())); 322 EXPECT_TRUE(base::Value::Equals(list.get(), list_output.get()));
299 run_loop.Run(); 323
300 } 324 input = std::move(list);
325 ASSERT_TRUE(ptr->BounceValue(input->CreateDeepCopy(), &output));
326 ASSERT_TRUE(base::Value::Equals(input.get(), output.get()));
301 } 327 }
302 328
303 TEST_F(CommonCustomTypesTest, String16) { 329 TEST_F(CommonCustomTypesTest, String16) {
304 base::RunLoop run_loop; 330 base::RunLoop run_loop;
305 331
306 TestString16Ptr ptr; 332 TestString16Ptr ptr;
307 TestString16Impl impl(MakeRequest(&ptr)); 333 TestString16Impl impl(MakeRequest(&ptr));
308 334
309 base::string16 str16 = base::ASCIIToUTF16("hello world"); 335 base::string16 str16 = base::ASCIIToUTF16("hello world");
310 336
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 temp_dir.GetPath().AppendASCII("test_file.txt"), 391 temp_dir.GetPath().AppendASCII("test_file.txt"),
366 base::File::FLAG_CREATE | base::File::FLAG_WRITE | base::File::FLAG_READ); 392 base::File::FLAG_CREATE | base::File::FLAG_WRITE | base::File::FLAG_READ);
367 393
368 ASSERT_TRUE(ptr->BounceFile(base::File(), &file_out)); 394 ASSERT_TRUE(ptr->BounceFile(base::File(), &file_out));
369 EXPECT_FALSE(file_out.IsValid()); 395 EXPECT_FALSE(file_out.IsValid());
370 } 396 }
371 397
372 } // namespace test 398 } // namespace test
373 } // namespace common 399 } // namespace common
374 } // namespace mojo 400 } // namespace mojo
OLDNEW
« no previous file with comments | « media/base/video_frame_metadata.cc ('k') | mojo/common/test_common_custom_types.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698