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

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

Issue 2013433002: [Mojo] Add [Native] type traits for base::Values (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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 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/bind.h"
5 #include "base/files/file_path.h" 6 #include "base/files/file_path.h"
7 #include "base/json/json_writer.h"
6 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/values.h"
8 #include "mojo/common/common_custom_types.mojom.h" 12 #include "mojo/common/common_custom_types.mojom.h"
9 #include "mojo/common/test_common_custom_types.mojom.h" 13 #include "mojo/common/test_common_custom_types.mojom.h"
10 #include "mojo/public/cpp/bindings/binding.h" 14 #include "mojo/public/cpp/bindings/binding.h"
11 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
12 16
13 namespace mojo { 17 namespace mojo {
14 namespace common { 18 namespace common {
15 namespace test { 19 namespace test {
20 namespace {
16 21
17 class TestFilePathImpl : public TestFilePath { 22 class TestFilePathImpl : public TestFilePath {
18 public: 23 public:
19 explicit TestFilePathImpl(TestFilePathRequest request) 24 explicit TestFilePathImpl(TestFilePathRequest request)
20 : binding_(this, std::move(request)) {} 25 : binding_(this, std::move(request)) {}
21 26
22 // TestFilePath implementation: 27 // TestFilePath implementation:
23 void BounceFilePath(const base::FilePath& in, 28 void BounceFilePath(const base::FilePath& in,
24 const BounceFilePathCallback& callback) override { 29 const BounceFilePathCallback& callback) override {
25 callback.Run(in); 30 callback.Run(in);
26 } 31 }
27 32
28 private: 33 private:
29 mojo::Binding<TestFilePath> binding_; 34 mojo::Binding<TestFilePath> binding_;
30 }; 35 };
31 36
37 class TestValueImpl : public TestValue {
38 public:
39 explicit TestValueImpl(TestValueRequest request)
40 : binding_(this, std::move(request)) {}
41
42 // TestValue implementation:
43 void BounceDictionaryValue(
44 const base::DictionaryValue& in,
45 const BounceDictionaryValueCallback& callback) override {
46 callback.Run(in);
47 }
48 void BounceListValue(
49 const base::ListValue& in,
50 const BounceListValueCallback& callback) override {
51 callback.Run(in);
52 }
53
54 private:
55 mojo::Binding<TestValue> binding_;
56 };
57
58 template<typename C, typename V>
59 bool IsBouncedValueEqual(const C& callback,
60 const V& value,
61 std::string* error) {
62 base::RunLoop run_loop;
63 bool is_equal = false;
64 callback.Run(value, [&run_loop, &value, &is_equal, &error](
65 const V& out) {
66 is_equal = value.Equals(&out);
67 if (!is_equal) {
68 std::string in_json;
69 base::JSONWriter::Write(value, &in_json);
70 std::string out_json;
71 base::JSONWriter::Write(out, &out_json);
72 *error = base::StringPrintf("Expected: '%s'; Found: '%s'",
73 in_json.c_str(), out_json.c_str());
74 }
75 run_loop.Quit();
76 });
77 run_loop.Run();
78 return is_equal;
79 }
80
81 } // namespace
82
32 TEST(CommonCustomTypesTest, FilePath) { 83 TEST(CommonCustomTypesTest, FilePath) {
33 base::MessageLoop message_loop; 84 base::MessageLoop message_loop;
34 base::RunLoop run_loop; 85 base::RunLoop run_loop;
35 86
36 TestFilePathPtr ptr; 87 TestFilePathPtr ptr;
37 TestFilePathImpl impl(GetProxy(&ptr)); 88 TestFilePathImpl impl(GetProxy(&ptr));
38 89
39 base::FilePath dir(FILE_PATH_LITERAL("hello")); 90 base::FilePath dir(FILE_PATH_LITERAL("hello"));
40 base::FilePath file = dir.Append(FILE_PATH_LITERAL("world")); 91 base::FilePath file = dir.Append(FILE_PATH_LITERAL("world"));
41 92
42 ptr->BounceFilePath(file, [&run_loop, &file](const base::FilePath& out) { 93 ptr->BounceFilePath(file, [&run_loop, &file](const base::FilePath& out) {
43 EXPECT_EQ(file, out); 94 EXPECT_EQ(file, out);
44 run_loop.Quit(); 95 run_loop.Quit();
45 }); 96 });
46 97
47 run_loop.Run(); 98 run_loop.Run();
48 } 99 }
49 100
101 TEST(CommonCustomTypesTest, Value) {
102 base::MessageLoop message_loop;
yzshen1 2016/05/24 17:54:41 Please consider moving this into a test fixture cl
Devlin 2016/05/24 18:05:56 Done.
103
104 TestValuePtr ptr;
105 TestValueImpl impl(GetProxy(&ptr));
106 std::string error;
107
108 auto TestDictEqual = [&ptr, &error](const base::DictionaryValue& value) {
yzshen1 2016/05/24 17:54:41 I feel that this testing code is fairly hard to re
Devlin 2016/05/24 18:05:56 Agreed. This had a lot more value when we were te
109 return IsBouncedValueEqual(
110 base::Bind(&TestValue::BounceDictionaryValue,
111 base::Unretained(ptr.get())),
112 value, &error);
113 };
114 auto TestListEqual = [&ptr, &error](const base::ListValue& value) {
115 return IsBouncedValueEqual(
116 base::Bind(&TestValue::BounceListValue,
117 base::Unretained(ptr.get())),
118 value, &error);
119 };
120
121 base::DictionaryValue dict;
122 dict.SetBoolean("bool", false);
123 dict.SetInteger("int", 2);
124 dict.SetString("string", "some string");
125 dict.SetBoolean("nested.bool", true);
126 dict.SetInteger("nested.int", 9);
127 dict.Set("some_binary", base::WrapUnique(
128 base::BinaryValue::CreateWithCopiedBuffer("mojo", 4)));
129 {
130 std::unique_ptr<base::ListValue> dict_list(new base::ListValue());
131 dict_list->AppendString("string");
132 dict_list->AppendBoolean(true);
133 dict.Set("list", std::move(dict_list));
134 }
135 EXPECT_TRUE(TestDictEqual(dict)) << error;
136
137 base::ListValue list;
138 list.AppendString("string");
139 list.AppendDouble(42.1);
140 list.AppendBoolean(true);
141 list.Append(base::WrapUnique(
142 base::BinaryValue::CreateWithCopiedBuffer("mojo", 4)));
143 {
144 std::unique_ptr<base::DictionaryValue> list_dict(
145 new base::DictionaryValue());
146 list_dict->SetString("string", "str");
147 list.Append(std::move(list_dict));
148 }
149 EXPECT_TRUE(TestListEqual(list)) << error;
150 }
151
50 } // namespace test 152 } // namespace test
51 } // namespace common 153 } // namespace common
52 } // namespace mojo 154 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698