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

Side by Side Diff: headless/public/domains/types_unittest.cc

Issue 2783983002: Cleanup headless templates and message_dispatcher.h. (Closed)
Patch Set: build fix Created 3 years, 8 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/json/json_reader.h" 5 #include "base/json/json_reader.h"
6 #include "base/json/json_string_value_serializer.h" 6 #include "base/json/json_string_value_serializer.h"
7 #include "headless/public/devtools/domains/accessibility.h" 7 #include "headless/public/devtools/domains/accessibility.h"
8 #include "headless/public/devtools/domains/dom.h" 8 #include "headless/public/devtools/domains/dom.h"
9 #include "headless/public/devtools/domains/memory.h" 9 #include "headless/public/devtools/domains/memory.h"
10 #include "headless/public/devtools/domains/page.h" 10 #include "headless/public/devtools/domains/page.h"
11 #include "headless/public/util/error_reporter.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 13
13 namespace headless { 14 namespace headless {
14 15
15 TEST(TypesTest, IntegerProperty) { 16 TEST(TypesTest, IntegerProperty) {
16 std::unique_ptr<page::NavigateToHistoryEntryParams> object( 17 std::unique_ptr<page::NavigateToHistoryEntryParams> object(
17 page::NavigateToHistoryEntryParams::Builder().SetEntryId(123).Build()); 18 page::NavigateToHistoryEntryParams::Builder().SetEntryId(123).Build());
18 EXPECT_TRUE(object); 19 ASSERT_TRUE(object);
19 EXPECT_EQ(123, object->GetEntryId()); 20 EXPECT_EQ(123, object->GetEntryId());
20 21
21 std::unique_ptr<page::NavigateToHistoryEntryParams> clone(object->Clone()); 22 std::unique_ptr<page::NavigateToHistoryEntryParams> clone(object->Clone());
22 EXPECT_TRUE(clone); 23 ASSERT_TRUE(clone);
23 EXPECT_EQ(123, clone->GetEntryId()); 24 EXPECT_EQ(123, clone->GetEntryId());
24 } 25 }
25 26
26 TEST(TypesTest, IntegerPropertyParseError) { 27 TEST(TypesTest, IntegerPropertyParseError) {
27 const char* json = "{\"entryId\": \"foo\"}"; 28 const char json[] = "{\"entryId\": \"foo\"}";
28 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); 29 std::unique_ptr<base::Value> object = base::JSONReader::Read(json);
29 EXPECT_TRUE(object); 30 ASSERT_TRUE(object);
30 31
31 #if DCHECK_IS_ON() 32 #if DCHECK_IS_ON()
32 ErrorReporter errors; 33 ErrorReporter errors;
33 EXPECT_FALSE(page::NavigateToHistoryEntryParams::Parse(*object, &errors)); 34 EXPECT_FALSE(page::NavigateToHistoryEntryParams::Parse(*object, &errors));
34 EXPECT_TRUE(errors.HasErrors()); 35 EXPECT_TRUE(errors.HasErrors());
35 #endif // DCHECK_IS_ON() 36 #endif // DCHECK_IS_ON()
36 } 37 }
37 38
38 TEST(TypesTest, BooleanProperty) { 39 TEST(TypesTest, BooleanProperty) {
39 std::unique_ptr<memory::SetPressureNotificationsSuppressedParams> object( 40 std::unique_ptr<memory::SetPressureNotificationsSuppressedParams> object(
40 memory::SetPressureNotificationsSuppressedParams::Builder() 41 memory::SetPressureNotificationsSuppressedParams::Builder()
41 .SetSuppressed(true) 42 .SetSuppressed(true)
42 .Build()); 43 .Build());
43 EXPECT_TRUE(object); 44 ASSERT_TRUE(object);
44 EXPECT_TRUE(object->GetSuppressed()); 45 EXPECT_TRUE(object->GetSuppressed());
45 46
46 std::unique_ptr<memory::SetPressureNotificationsSuppressedParams> clone( 47 std::unique_ptr<memory::SetPressureNotificationsSuppressedParams> clone(
47 object->Clone()); 48 object->Clone());
48 EXPECT_TRUE(clone); 49 ASSERT_TRUE(clone);
49 EXPECT_TRUE(clone->GetSuppressed()); 50 EXPECT_TRUE(clone->GetSuppressed());
50 } 51 }
51 52
52 TEST(TypesTest, BooleanPropertyParseError) { 53 TEST(TypesTest, BooleanPropertyParseError) {
53 const char* json = "{\"suppressed\": \"foo\"}"; 54 const char json[] = "{\"suppressed\": \"foo\"}";
54 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); 55 std::unique_ptr<base::Value> object = base::JSONReader::Read(json);
55 EXPECT_TRUE(object); 56 ASSERT_TRUE(object);
56 57
57 #if DCHECK_IS_ON() 58 #if DCHECK_IS_ON()
58 ErrorReporter errors; 59 ErrorReporter errors;
59 EXPECT_FALSE(memory::SetPressureNotificationsSuppressedParams::Parse( 60 EXPECT_FALSE(memory::SetPressureNotificationsSuppressedParams::Parse(
60 *object, &errors)); 61 *object, &errors));
61 EXPECT_TRUE(errors.HasErrors()); 62 EXPECT_TRUE(errors.HasErrors());
62 #endif // DCHECK_IS_ON() 63 #endif // DCHECK_IS_ON()
63 } 64 }
64 65
65 TEST(TypesTest, DoubleProperty) { 66 TEST(TypesTest, DoubleProperty) {
66 std::unique_ptr<page::SetGeolocationOverrideParams> object( 67 std::unique_ptr<page::SetGeolocationOverrideParams> object(
67 page::SetGeolocationOverrideParams::Builder().SetLatitude(3.14).Build()); 68 page::SetGeolocationOverrideParams::Builder().SetLatitude(3.14).Build());
68 EXPECT_TRUE(object); 69 ASSERT_TRUE(object);
69 EXPECT_EQ(3.14, object->GetLatitude()); 70 EXPECT_EQ(3.14, object->GetLatitude());
70 71
71 std::unique_ptr<page::SetGeolocationOverrideParams> clone(object->Clone()); 72 std::unique_ptr<page::SetGeolocationOverrideParams> clone(object->Clone());
72 EXPECT_TRUE(clone); 73 ASSERT_TRUE(clone);
73 EXPECT_EQ(3.14, clone->GetLatitude()); 74 EXPECT_EQ(3.14, clone->GetLatitude());
74 } 75 }
75 76
76 TEST(TypesTest, DoublePropertyParseError) { 77 TEST(TypesTest, DoublePropertyParseError) {
77 const char* json = "{\"latitude\": \"foo\"}"; 78 const char json[] = "{\"latitude\": \"foo\"}";
78 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); 79 std::unique_ptr<base::Value> object = base::JSONReader::Read(json);
79 EXPECT_TRUE(object); 80 ASSERT_TRUE(object);
80 81
81 #if DCHECK_IS_ON() 82 #if DCHECK_IS_ON()
82 ErrorReporter errors; 83 ErrorReporter errors;
83 EXPECT_FALSE(page::SetGeolocationOverrideParams::Parse(*object, &errors)); 84 EXPECT_FALSE(page::SetGeolocationOverrideParams::Parse(*object, &errors));
84 EXPECT_TRUE(errors.HasErrors()); 85 EXPECT_TRUE(errors.HasErrors());
85 #endif // DCHECK_IS_ON() 86 #endif // DCHECK_IS_ON()
86 } 87 }
87 88
88 TEST(TypesTest, StringProperty) { 89 TEST(TypesTest, StringProperty) {
89 std::unique_ptr<page::NavigateParams> object( 90 std::unique_ptr<page::NavigateParams> object(
90 page::NavigateParams::Builder().SetUrl("url").Build()); 91 page::NavigateParams::Builder().SetUrl("url").Build());
91 EXPECT_TRUE(object); 92 ASSERT_TRUE(object);
92 EXPECT_EQ("url", object->GetUrl()); 93 EXPECT_EQ("url", object->GetUrl());
93 94
94 std::unique_ptr<page::NavigateParams> clone(object->Clone()); 95 std::unique_ptr<page::NavigateParams> clone(object->Clone());
95 EXPECT_TRUE(clone); 96 ASSERT_TRUE(clone);
96 EXPECT_EQ("url", clone->GetUrl()); 97 EXPECT_EQ("url", clone->GetUrl());
97 } 98 }
98 99
99 TEST(TypesTest, StringPropertyParseError) { 100 TEST(TypesTest, StringPropertyParseError) {
100 const char* json = "{\"url\": false}"; 101 const char json[] = "{\"url\": false}";
101 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); 102 std::unique_ptr<base::Value> object = base::JSONReader::Read(json);
102 EXPECT_TRUE(object); 103 ASSERT_TRUE(object);
103 104
104 #if DCHECK_IS_ON() 105 #if DCHECK_IS_ON()
105 ErrorReporter errors; 106 ErrorReporter errors;
106 EXPECT_FALSE(page::NavigateParams::Parse(*object, &errors)); 107 EXPECT_FALSE(page::NavigateParams::Parse(*object, &errors));
107 EXPECT_TRUE(errors.HasErrors()); 108 EXPECT_TRUE(errors.HasErrors());
108 #endif // DCHECK_IS_ON() 109 #endif // DCHECK_IS_ON()
109 } 110 }
110 111
111 TEST(TypesTest, EnumProperty) { 112 TEST(TypesTest, EnumProperty) {
112 std::unique_ptr<runtime::RemoteObject> object( 113 std::unique_ptr<runtime::RemoteObject> object(
113 runtime::RemoteObject::Builder() 114 runtime::RemoteObject::Builder()
114 .SetType(runtime::RemoteObjectType::UNDEFINED) 115 .SetType(runtime::RemoteObjectType::UNDEFINED)
115 .Build()); 116 .Build());
116 EXPECT_TRUE(object); 117 ASSERT_TRUE(object);
117 EXPECT_EQ(runtime::RemoteObjectType::UNDEFINED, object->GetType()); 118 EXPECT_EQ(runtime::RemoteObjectType::UNDEFINED, object->GetType());
118 119
119 std::unique_ptr<runtime::RemoteObject> clone(object->Clone()); 120 std::unique_ptr<runtime::RemoteObject> clone(object->Clone());
120 EXPECT_TRUE(clone); 121 ASSERT_TRUE(clone);
121 EXPECT_EQ(runtime::RemoteObjectType::UNDEFINED, clone->GetType()); 122 EXPECT_EQ(runtime::RemoteObjectType::UNDEFINED, clone->GetType());
122 } 123 }
123 124
124 TEST(TypesTest, EnumPropertyParseError) { 125 TEST(TypesTest, EnumPropertyParseError) {
125 const char* json = "{\"type\": false}"; 126 const char json[] = "{\"type\": false}";
126 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); 127 std::unique_ptr<base::Value> object = base::JSONReader::Read(json);
127 EXPECT_TRUE(object); 128 ASSERT_TRUE(object);
128 129
129 #if DCHECK_IS_ON() 130 #if DCHECK_IS_ON()
130 ErrorReporter errors; 131 ErrorReporter errors;
131 EXPECT_FALSE(runtime::RemoteObject::Parse(*object, &errors)); 132 EXPECT_FALSE(runtime::RemoteObject::Parse(*object, &errors));
132 EXPECT_TRUE(errors.HasErrors()); 133 EXPECT_TRUE(errors.HasErrors());
133 #endif // DCHECK_IS_ON() 134 #endif // DCHECK_IS_ON()
134 } 135 }
135 136
136 TEST(TypesTest, ArrayProperty) { 137 TEST(TypesTest, ArrayProperty) {
137 std::vector<int> values; 138 std::vector<int> values;
138 values.push_back(1); 139 values.push_back(1);
139 values.push_back(2); 140 values.push_back(2);
140 values.push_back(3); 141 values.push_back(3);
141 142
142 std::unique_ptr<dom::QuerySelectorAllResult> object( 143 std::unique_ptr<dom::QuerySelectorAllResult> object(
143 dom::QuerySelectorAllResult::Builder().SetNodeIds(values).Build()); 144 dom::QuerySelectorAllResult::Builder().SetNodeIds(values).Build());
144 EXPECT_TRUE(object); 145 ASSERT_TRUE(object);
145 EXPECT_EQ(3u, object->GetNodeIds()->size()); 146 ASSERT_TRUE(object->GetNodeIds());
146 EXPECT_EQ(1, object->GetNodeIds()->at(0)); 147 const auto& object_node_ids = *object->GetNodeIds();
147 EXPECT_EQ(2, object->GetNodeIds()->at(1)); 148 ASSERT_EQ(3u, object_node_ids.size());
148 EXPECT_EQ(3, object->GetNodeIds()->at(2)); 149 EXPECT_EQ(1, object_node_ids[0]);
150 EXPECT_EQ(2, object_node_ids[1]);
151 EXPECT_EQ(3, object_node_ids[2]);
149 152
150 std::unique_ptr<dom::QuerySelectorAllResult> clone(object->Clone()); 153 std::unique_ptr<dom::QuerySelectorAllResult> clone(object->Clone());
151 EXPECT_TRUE(clone); 154 ASSERT_TRUE(clone);
152 EXPECT_EQ(3u, clone->GetNodeIds()->size()); 155 ASSERT_TRUE(clone->GetNodeIds());
153 EXPECT_EQ(1, clone->GetNodeIds()->at(0)); 156 const auto& clone_node_ids = *object->GetNodeIds();
154 EXPECT_EQ(2, clone->GetNodeIds()->at(1)); 157 ASSERT_EQ(3u, clone_node_ids.size());
155 EXPECT_EQ(3, clone->GetNodeIds()->at(2)); 158 EXPECT_EQ(1, clone_node_ids[0]);
159 EXPECT_EQ(2, clone_node_ids[1]);
160 EXPECT_EQ(3, clone_node_ids[2]);
156 } 161 }
157 162
158 TEST(TypesTest, ArrayPropertyParseError) { 163 TEST(TypesTest, ArrayPropertyParseError) {
159 const char* json = "{\"nodeIds\": true}"; 164 const char json[] = "{\"nodeIds\": true}";
160 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); 165 std::unique_ptr<base::Value> object = base::JSONReader::Read(json);
161 EXPECT_TRUE(object); 166 ASSERT_TRUE(object);
162 167
163 #if DCHECK_IS_ON() 168 #if DCHECK_IS_ON()
164 ErrorReporter errors; 169 ErrorReporter errors;
165 EXPECT_FALSE(dom::QuerySelectorAllResult::Parse(*object, &errors)); 170 EXPECT_FALSE(dom::QuerySelectorAllResult::Parse(*object, &errors));
166 EXPECT_TRUE(errors.HasErrors()); 171 EXPECT_TRUE(errors.HasErrors());
167 #endif // DCHECK_IS_ON() 172 #endif // DCHECK_IS_ON()
168 } 173 }
169 174
170 TEST(TypesTest, ObjectProperty) { 175 TEST(TypesTest, ObjectProperty) {
171 std::unique_ptr<runtime::RemoteObject> subobject( 176 std::unique_ptr<runtime::RemoteObject> subobject(
172 runtime::RemoteObject::Builder() 177 runtime::RemoteObject::Builder()
173 .SetType(runtime::RemoteObjectType::SYMBOL) 178 .SetType(runtime::RemoteObjectType::SYMBOL)
174 .Build()); 179 .Build());
175 std::unique_ptr<runtime::EvaluateResult> object( 180 std::unique_ptr<runtime::EvaluateResult> object(
176 runtime::EvaluateResult::Builder() 181 runtime::EvaluateResult::Builder()
177 .SetResult(std::move(subobject)) 182 .SetResult(std::move(subobject))
178 .Build()); 183 .Build());
179 EXPECT_TRUE(object); 184 ASSERT_TRUE(object);
180 EXPECT_EQ(runtime::RemoteObjectType::SYMBOL, object->GetResult()->GetType()); 185 EXPECT_EQ(runtime::RemoteObjectType::SYMBOL, object->GetResult()->GetType());
181 186
182 std::unique_ptr<runtime::EvaluateResult> clone(object->Clone()); 187 std::unique_ptr<runtime::EvaluateResult> clone(object->Clone());
183 EXPECT_TRUE(clone); 188 ASSERT_TRUE(clone);
184 EXPECT_EQ(runtime::RemoteObjectType::SYMBOL, clone->GetResult()->GetType()); 189 EXPECT_EQ(runtime::RemoteObjectType::SYMBOL, clone->GetResult()->GetType());
185 } 190 }
186 191
187 TEST(TypesTest, ObjectPropertyParseError) { 192 TEST(TypesTest, ObjectPropertyParseError) {
188 const char* json = "{\"result\": 42}"; 193 const char json[] = "{\"result\": 42}";
189 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); 194 std::unique_ptr<base::Value> object = base::JSONReader::Read(json);
190 EXPECT_TRUE(object); 195 ASSERT_TRUE(object);
191 196
192 #if DCHECK_IS_ON() 197 #if DCHECK_IS_ON()
193 ErrorReporter errors; 198 ErrorReporter errors;
194 EXPECT_FALSE(runtime::EvaluateResult::Parse(*object, &errors)); 199 EXPECT_FALSE(runtime::EvaluateResult::Parse(*object, &errors));
195 EXPECT_TRUE(errors.HasErrors()); 200 EXPECT_TRUE(errors.HasErrors());
196 #endif // DCHECK_IS_ON() 201 #endif // DCHECK_IS_ON()
197 } 202 }
198 203
199 TEST(TypesTest, AnyProperty) { 204 TEST(TypesTest, AnyProperty) {
200 std::unique_ptr<base::Value> value(new base::Value(123)); 205 std::unique_ptr<base::Value> value(new base::Value(123));
201 std::unique_ptr<accessibility::AXValue> object( 206 std::unique_ptr<accessibility::AXValue> object(
202 accessibility::AXValue::Builder() 207 accessibility::AXValue::Builder()
203 .SetType(accessibility::AXValueType::INTEGER) 208 .SetType(accessibility::AXValueType::INTEGER)
204 .SetValue(std::move(value)) 209 .SetValue(std::move(value))
205 .Build()); 210 .Build());
206 EXPECT_TRUE(object); 211 ASSERT_TRUE(object);
207 EXPECT_EQ(base::Value::Type::INTEGER, object->GetValue()->GetType()); 212 EXPECT_EQ(base::Value::Type::INTEGER, object->GetValue()->GetType());
208 213
209 std::unique_ptr<accessibility::AXValue> clone(object->Clone()); 214 std::unique_ptr<accessibility::AXValue> clone(object->Clone());
210 EXPECT_TRUE(clone); 215 ASSERT_TRUE(clone);
211 EXPECT_EQ(base::Value::Type::INTEGER, clone->GetValue()->GetType()); 216 EXPECT_EQ(base::Value::Type::INTEGER, clone->GetValue()->GetType());
212 217
213 int clone_value; 218 int clone_value;
214 EXPECT_TRUE(clone->GetValue()->GetAsInteger(&clone_value)); 219 ASSERT_TRUE(clone->GetValue()->GetAsInteger(&clone_value));
215 EXPECT_EQ(123, clone_value); 220 EXPECT_EQ(123, clone_value);
216 } 221 }
217 222
218 TEST(TypesTest, ComplexObjectClone) { 223 TEST(TypesTest, ComplexObjectClone) {
219 std::vector<std::unique_ptr<dom::Node>> child_nodes; 224 std::vector<std::unique_ptr<dom::Node>> child_nodes;
220 child_nodes.emplace_back(dom::Node::Builder() 225 child_nodes.emplace_back(dom::Node::Builder()
221 .SetNodeId(1) 226 .SetNodeId(1)
222 .SetBackendNodeId(2) 227 .SetBackendNodeId(2)
223 .SetNodeType(3) 228 .SetNodeType(3)
224 .SetNodeName("-blink-blink") 229 .SetNodeName("-blink-blink")
225 .SetLocalName("-blink-blink") 230 .SetLocalName("-blink-blink")
226 .SetNodeValue("-blink-blink") 231 .SetNodeValue("-blink-blink")
227 .Build()); 232 .Build());
228 std::unique_ptr<dom::SetChildNodesParams> params = 233 std::unique_ptr<dom::SetChildNodesParams> params =
229 dom::SetChildNodesParams::Builder() 234 dom::SetChildNodesParams::Builder()
230 .SetParentId(123) 235 .SetParentId(123)
231 .SetNodes(std::move(child_nodes)) 236 .SetNodes(std::move(child_nodes))
232 .Build(); 237 .Build();
233 std::unique_ptr<dom::SetChildNodesParams> clone = params->Clone(); 238 std::unique_ptr<dom::SetChildNodesParams> clone = params->Clone();
234 ASSERT_NE(nullptr, clone); 239 ASSERT_TRUE(clone);
235 240
236 std::string orig; 241 std::string orig;
237 JSONStringValueSerializer(&orig).Serialize(*params->Serialize()); 242 JSONStringValueSerializer(&orig).Serialize(*params->Serialize());
238 std::string clone_value; 243 std::string clone_value;
239 JSONStringValueSerializer(&clone_value).Serialize(*clone->Serialize()); 244 JSONStringValueSerializer(&clone_value).Serialize(*clone->Serialize());
240 EXPECT_EQ(orig, clone_value); 245 EXPECT_EQ(orig, clone_value);
241 } 246 }
242 247
243 } // namespace headless 248 } // namespace headless
OLDNEW
« no previous file with comments | « headless/lib/browser/headless_devtools_client_impl.cc ('k') | headless/public/internal/message_dispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698