| OLD | NEW |
| 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 "headless/public/domains/types.h" | 6 #include "headless/public/domains/types.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace headless { | 9 namespace headless { |
| 10 | 10 |
| 11 TEST(TypesTest, IntegerProperty) { | 11 TEST(TypesTest, IntegerProperty) { |
| 12 std::unique_ptr<page::NavigateToHistoryEntryParams> object( | 12 std::unique_ptr<page::NavigateToHistoryEntryParams> object( |
| 13 page::NavigateToHistoryEntryParams::Builder().SetEntryId(123).Build()); | 13 page::NavigateToHistoryEntryParams::Builder().SetEntryId(123).Build()); |
| 14 EXPECT_TRUE(object); | 14 EXPECT_TRUE(object); |
| 15 EXPECT_EQ(123, object->GetEntryId()); | 15 EXPECT_EQ(123, object->GetEntryId()); |
| 16 | 16 |
| 17 std::unique_ptr<page::NavigateToHistoryEntryParams> clone(object->Clone()); | 17 std::unique_ptr<page::NavigateToHistoryEntryParams> clone(object->Clone()); |
| 18 EXPECT_TRUE(clone); | 18 EXPECT_TRUE(clone); |
| 19 EXPECT_EQ(123, clone->GetEntryId()); | 19 EXPECT_EQ(123, clone->GetEntryId()); |
| 20 } | 20 } |
| 21 | 21 |
| 22 TEST(TypesTest, IntegerPropertyParseError) { | 22 TEST(TypesTest, IntegerPropertyParseError) { |
| 23 const char* json = "{\"entryId\": \"foo\"}"; | 23 const char* json = "{\"entryId\": \"foo\"}"; |
| 24 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); | 24 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); |
| 25 EXPECT_TRUE(object); | 25 EXPECT_TRUE(object); |
| 26 | 26 |
| 27 #if DCHECK_IS_ON() |
| 27 ErrorReporter errors; | 28 ErrorReporter errors; |
| 28 EXPECT_FALSE(page::NavigateToHistoryEntryParams::Parse(*object, &errors)); | 29 EXPECT_FALSE(page::NavigateToHistoryEntryParams::Parse(*object, &errors)); |
| 29 EXPECT_TRUE(errors.HasErrors()); | 30 EXPECT_TRUE(errors.HasErrors()); |
| 31 #endif // DCHECK_IS_ON() |
| 30 } | 32 } |
| 31 | 33 |
| 32 TEST(TypesTest, BooleanProperty) { | 34 TEST(TypesTest, BooleanProperty) { |
| 33 std::unique_ptr<memory::SetPressureNotificationsSuppressedParams> object( | 35 std::unique_ptr<memory::SetPressureNotificationsSuppressedParams> object( |
| 34 memory::SetPressureNotificationsSuppressedParams::Builder() | 36 memory::SetPressureNotificationsSuppressedParams::Builder() |
| 35 .SetSuppressed(true) | 37 .SetSuppressed(true) |
| 36 .Build()); | 38 .Build()); |
| 37 EXPECT_TRUE(object); | 39 EXPECT_TRUE(object); |
| 38 EXPECT_TRUE(object->GetSuppressed()); | 40 EXPECT_TRUE(object->GetSuppressed()); |
| 39 | 41 |
| 40 std::unique_ptr<memory::SetPressureNotificationsSuppressedParams> clone( | 42 std::unique_ptr<memory::SetPressureNotificationsSuppressedParams> clone( |
| 41 object->Clone()); | 43 object->Clone()); |
| 42 EXPECT_TRUE(clone); | 44 EXPECT_TRUE(clone); |
| 43 EXPECT_TRUE(clone->GetSuppressed()); | 45 EXPECT_TRUE(clone->GetSuppressed()); |
| 44 } | 46 } |
| 45 | 47 |
| 46 TEST(TypesTest, BooleanPropertyParseError) { | 48 TEST(TypesTest, BooleanPropertyParseError) { |
| 47 const char* json = "{\"suppressed\": \"foo\"}"; | 49 const char* json = "{\"suppressed\": \"foo\"}"; |
| 48 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); | 50 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); |
| 49 EXPECT_TRUE(object); | 51 EXPECT_TRUE(object); |
| 50 | 52 |
| 53 #if DCHECK_IS_ON() |
| 51 ErrorReporter errors; | 54 ErrorReporter errors; |
| 52 EXPECT_FALSE(memory::SetPressureNotificationsSuppressedParams::Parse( | 55 EXPECT_FALSE(memory::SetPressureNotificationsSuppressedParams::Parse( |
| 53 *object, &errors)); | 56 *object, &errors)); |
| 54 EXPECT_TRUE(errors.HasErrors()); | 57 EXPECT_TRUE(errors.HasErrors()); |
| 58 #endif // DCHECK_IS_ON() |
| 55 } | 59 } |
| 56 | 60 |
| 57 TEST(TypesTest, DoubleProperty) { | 61 TEST(TypesTest, DoubleProperty) { |
| 58 std::unique_ptr<page::SetGeolocationOverrideParams> object( | 62 std::unique_ptr<page::SetGeolocationOverrideParams> object( |
| 59 page::SetGeolocationOverrideParams::Builder().SetLatitude(3.14).Build()); | 63 page::SetGeolocationOverrideParams::Builder().SetLatitude(3.14).Build()); |
| 60 EXPECT_TRUE(object); | 64 EXPECT_TRUE(object); |
| 61 EXPECT_EQ(3.14, object->GetLatitude()); | 65 EXPECT_EQ(3.14, object->GetLatitude()); |
| 62 | 66 |
| 63 std::unique_ptr<page::SetGeolocationOverrideParams> clone(object->Clone()); | 67 std::unique_ptr<page::SetGeolocationOverrideParams> clone(object->Clone()); |
| 64 EXPECT_TRUE(clone); | 68 EXPECT_TRUE(clone); |
| 65 EXPECT_EQ(3.14, clone->GetLatitude()); | 69 EXPECT_EQ(3.14, clone->GetLatitude()); |
| 66 } | 70 } |
| 67 | 71 |
| 68 TEST(TypesTest, DoublePropertyParseError) { | 72 TEST(TypesTest, DoublePropertyParseError) { |
| 69 const char* json = "{\"latitude\": \"foo\"}"; | 73 const char* json = "{\"latitude\": \"foo\"}"; |
| 70 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); | 74 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); |
| 71 EXPECT_TRUE(object); | 75 EXPECT_TRUE(object); |
| 72 | 76 |
| 77 #if DCHECK_IS_ON() |
| 73 ErrorReporter errors; | 78 ErrorReporter errors; |
| 74 EXPECT_FALSE(page::SetGeolocationOverrideParams::Parse(*object, &errors)); | 79 EXPECT_FALSE(page::SetGeolocationOverrideParams::Parse(*object, &errors)); |
| 75 EXPECT_TRUE(errors.HasErrors()); | 80 EXPECT_TRUE(errors.HasErrors()); |
| 81 #endif // DCHECK_IS_ON() |
| 76 } | 82 } |
| 77 | 83 |
| 78 TEST(TypesTest, StringProperty) { | 84 TEST(TypesTest, StringProperty) { |
| 79 std::unique_ptr<page::NavigateParams> object( | 85 std::unique_ptr<page::NavigateParams> object( |
| 80 page::NavigateParams::Builder().SetUrl("url").Build()); | 86 page::NavigateParams::Builder().SetUrl("url").Build()); |
| 81 EXPECT_TRUE(object); | 87 EXPECT_TRUE(object); |
| 82 EXPECT_EQ("url", object->GetUrl()); | 88 EXPECT_EQ("url", object->GetUrl()); |
| 83 | 89 |
| 84 std::unique_ptr<page::NavigateParams> clone(object->Clone()); | 90 std::unique_ptr<page::NavigateParams> clone(object->Clone()); |
| 85 EXPECT_TRUE(clone); | 91 EXPECT_TRUE(clone); |
| 86 EXPECT_EQ("url", clone->GetUrl()); | 92 EXPECT_EQ("url", clone->GetUrl()); |
| 87 } | 93 } |
| 88 | 94 |
| 89 TEST(TypesTest, StringPropertyParseError) { | 95 TEST(TypesTest, StringPropertyParseError) { |
| 90 const char* json = "{\"url\": false}"; | 96 const char* json = "{\"url\": false}"; |
| 91 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); | 97 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); |
| 92 EXPECT_TRUE(object); | 98 EXPECT_TRUE(object); |
| 93 | 99 |
| 100 #if DCHECK_IS_ON() |
| 94 ErrorReporter errors; | 101 ErrorReporter errors; |
| 95 EXPECT_FALSE(page::NavigateParams::Parse(*object, &errors)); | 102 EXPECT_FALSE(page::NavigateParams::Parse(*object, &errors)); |
| 96 EXPECT_TRUE(errors.HasErrors()); | 103 EXPECT_TRUE(errors.HasErrors()); |
| 104 #endif // DCHECK_IS_ON() |
| 97 } | 105 } |
| 98 | 106 |
| 99 TEST(TypesTest, EnumProperty) { | 107 TEST(TypesTest, EnumProperty) { |
| 100 std::unique_ptr<runtime::RemoteObject> object( | 108 std::unique_ptr<runtime::RemoteObject> object( |
| 101 runtime::RemoteObject::Builder() | 109 runtime::RemoteObject::Builder() |
| 102 .SetType(runtime::RemoteObjectType::UNDEFINED) | 110 .SetType(runtime::RemoteObjectType::UNDEFINED) |
| 103 .Build()); | 111 .Build()); |
| 104 EXPECT_TRUE(object); | 112 EXPECT_TRUE(object); |
| 105 EXPECT_EQ(runtime::RemoteObjectType::UNDEFINED, object->GetType()); | 113 EXPECT_EQ(runtime::RemoteObjectType::UNDEFINED, object->GetType()); |
| 106 | 114 |
| 107 std::unique_ptr<runtime::RemoteObject> clone(object->Clone()); | 115 std::unique_ptr<runtime::RemoteObject> clone(object->Clone()); |
| 108 EXPECT_TRUE(clone); | 116 EXPECT_TRUE(clone); |
| 109 EXPECT_EQ(runtime::RemoteObjectType::UNDEFINED, clone->GetType()); | 117 EXPECT_EQ(runtime::RemoteObjectType::UNDEFINED, clone->GetType()); |
| 110 } | 118 } |
| 111 | 119 |
| 112 TEST(TypesTest, EnumPropertyParseError) { | 120 TEST(TypesTest, EnumPropertyParseError) { |
| 113 const char* json = "{\"type\": false}"; | 121 const char* json = "{\"type\": false}"; |
| 114 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); | 122 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); |
| 115 EXPECT_TRUE(object); | 123 EXPECT_TRUE(object); |
| 116 | 124 |
| 125 #if DCHECK_IS_ON() |
| 117 ErrorReporter errors; | 126 ErrorReporter errors; |
| 118 EXPECT_FALSE(runtime::RemoteObject::Parse(*object, &errors)); | 127 EXPECT_FALSE(runtime::RemoteObject::Parse(*object, &errors)); |
| 119 EXPECT_TRUE(errors.HasErrors()); | 128 EXPECT_TRUE(errors.HasErrors()); |
| 129 #endif // DCHECK_IS_ON() |
| 120 } | 130 } |
| 121 | 131 |
| 122 TEST(TypesTest, ArrayProperty) { | 132 TEST(TypesTest, ArrayProperty) { |
| 123 std::vector<int> values; | 133 std::vector<int> values; |
| 124 values.push_back(1); | 134 values.push_back(1); |
| 125 values.push_back(2); | 135 values.push_back(2); |
| 126 values.push_back(3); | 136 values.push_back(3); |
| 127 | 137 |
| 128 std::unique_ptr<dom::QuerySelectorAllResult> object( | 138 std::unique_ptr<dom::QuerySelectorAllResult> object( |
| 129 dom::QuerySelectorAllResult::Builder().SetNodeIds(values).Build()); | 139 dom::QuerySelectorAllResult::Builder().SetNodeIds(values).Build()); |
| 130 EXPECT_TRUE(object); | 140 EXPECT_TRUE(object); |
| 131 EXPECT_EQ(3u, object->GetNodeIds()->size()); | 141 EXPECT_EQ(3u, object->GetNodeIds()->size()); |
| 132 EXPECT_EQ(1, object->GetNodeIds()->at(0)); | 142 EXPECT_EQ(1, object->GetNodeIds()->at(0)); |
| 133 EXPECT_EQ(2, object->GetNodeIds()->at(1)); | 143 EXPECT_EQ(2, object->GetNodeIds()->at(1)); |
| 134 EXPECT_EQ(3, object->GetNodeIds()->at(2)); | 144 EXPECT_EQ(3, object->GetNodeIds()->at(2)); |
| 135 | 145 |
| 136 std::unique_ptr<dom::QuerySelectorAllResult> clone(object->Clone()); | 146 std::unique_ptr<dom::QuerySelectorAllResult> clone(object->Clone()); |
| 137 EXPECT_TRUE(clone); | 147 EXPECT_TRUE(clone); |
| 138 EXPECT_EQ(3u, clone->GetNodeIds()->size()); | 148 EXPECT_EQ(3u, clone->GetNodeIds()->size()); |
| 139 EXPECT_EQ(1, clone->GetNodeIds()->at(0)); | 149 EXPECT_EQ(1, clone->GetNodeIds()->at(0)); |
| 140 EXPECT_EQ(2, clone->GetNodeIds()->at(1)); | 150 EXPECT_EQ(2, clone->GetNodeIds()->at(1)); |
| 141 EXPECT_EQ(3, clone->GetNodeIds()->at(2)); | 151 EXPECT_EQ(3, clone->GetNodeIds()->at(2)); |
| 142 } | 152 } |
| 143 | 153 |
| 144 TEST(TypesTest, ArrayPropertyParseError) { | 154 TEST(TypesTest, ArrayPropertyParseError) { |
| 145 const char* json = "{\"nodeIds\": true}"; | 155 const char* json = "{\"nodeIds\": true}"; |
| 146 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); | 156 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); |
| 147 EXPECT_TRUE(object); | 157 EXPECT_TRUE(object); |
| 148 | 158 |
| 159 #if DCHECK_IS_ON() |
| 149 ErrorReporter errors; | 160 ErrorReporter errors; |
| 150 EXPECT_FALSE(dom::QuerySelectorAllResult::Parse(*object, &errors)); | 161 EXPECT_FALSE(dom::QuerySelectorAllResult::Parse(*object, &errors)); |
| 151 EXPECT_TRUE(errors.HasErrors()); | 162 EXPECT_TRUE(errors.HasErrors()); |
| 163 #endif // DCHECK_IS_ON() |
| 152 } | 164 } |
| 153 | 165 |
| 154 TEST(TypesTest, ObjectProperty) { | 166 TEST(TypesTest, ObjectProperty) { |
| 155 std::unique_ptr<runtime::RemoteObject> subobject( | 167 std::unique_ptr<runtime::RemoteObject> subobject( |
| 156 runtime::RemoteObject::Builder() | 168 runtime::RemoteObject::Builder() |
| 157 .SetType(runtime::RemoteObjectType::SYMBOL) | 169 .SetType(runtime::RemoteObjectType::SYMBOL) |
| 158 .Build()); | 170 .Build()); |
| 159 std::unique_ptr<runtime::EvaluateResult> object( | 171 std::unique_ptr<runtime::EvaluateResult> object( |
| 160 runtime::EvaluateResult::Builder() | 172 runtime::EvaluateResult::Builder() |
| 161 .SetResult(std::move(subobject)) | 173 .SetResult(std::move(subobject)) |
| 162 .Build()); | 174 .Build()); |
| 163 EXPECT_TRUE(object); | 175 EXPECT_TRUE(object); |
| 164 EXPECT_EQ(runtime::RemoteObjectType::SYMBOL, object->GetResult()->GetType()); | 176 EXPECT_EQ(runtime::RemoteObjectType::SYMBOL, object->GetResult()->GetType()); |
| 165 | 177 |
| 166 std::unique_ptr<runtime::EvaluateResult> clone(object->Clone()); | 178 std::unique_ptr<runtime::EvaluateResult> clone(object->Clone()); |
| 167 EXPECT_TRUE(clone); | 179 EXPECT_TRUE(clone); |
| 168 EXPECT_EQ(runtime::RemoteObjectType::SYMBOL, clone->GetResult()->GetType()); | 180 EXPECT_EQ(runtime::RemoteObjectType::SYMBOL, clone->GetResult()->GetType()); |
| 169 } | 181 } |
| 170 | 182 |
| 171 TEST(TypesTest, ObjectPropertyParseError) { | 183 TEST(TypesTest, ObjectPropertyParseError) { |
| 172 const char* json = "{\"result\": 42}"; | 184 const char* json = "{\"result\": 42}"; |
| 173 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); | 185 std::unique_ptr<base::Value> object = base::JSONReader::Read(json); |
| 174 EXPECT_TRUE(object); | 186 EXPECT_TRUE(object); |
| 175 | 187 |
| 188 #if DCHECK_IS_ON() |
| 176 ErrorReporter errors; | 189 ErrorReporter errors; |
| 177 EXPECT_FALSE(runtime::EvaluateResult::Parse(*object, &errors)); | 190 EXPECT_FALSE(runtime::EvaluateResult::Parse(*object, &errors)); |
| 178 EXPECT_TRUE(errors.HasErrors()); | 191 EXPECT_TRUE(errors.HasErrors()); |
| 192 #endif // DCHECK_IS_ON() |
| 179 } | 193 } |
| 180 | 194 |
| 181 TEST(TypesTest, AnyProperty) { | 195 TEST(TypesTest, AnyProperty) { |
| 182 std::unique_ptr<base::Value> value(new base::FundamentalValue(123)); | 196 std::unique_ptr<base::Value> value(new base::FundamentalValue(123)); |
| 183 std::unique_ptr<accessibility::AXValue> object( | 197 std::unique_ptr<accessibility::AXValue> object( |
| 184 accessibility::AXValue::Builder() | 198 accessibility::AXValue::Builder() |
| 185 .SetType(accessibility::AXValueType::INTEGER) | 199 .SetType(accessibility::AXValueType::INTEGER) |
| 186 .SetValue(std::move(value)) | 200 .SetValue(std::move(value)) |
| 187 .Build()); | 201 .Build()); |
| 188 EXPECT_TRUE(object); | 202 EXPECT_TRUE(object); |
| 189 EXPECT_EQ(base::Value::TYPE_INTEGER, object->GetValue()->GetType()); | 203 EXPECT_EQ(base::Value::TYPE_INTEGER, object->GetValue()->GetType()); |
| 190 | 204 |
| 191 std::unique_ptr<accessibility::AXValue> clone(object->Clone()); | 205 std::unique_ptr<accessibility::AXValue> clone(object->Clone()); |
| 192 EXPECT_TRUE(clone); | 206 EXPECT_TRUE(clone); |
| 193 EXPECT_EQ(base::Value::TYPE_INTEGER, clone->GetValue()->GetType()); | 207 EXPECT_EQ(base::Value::TYPE_INTEGER, clone->GetValue()->GetType()); |
| 194 | 208 |
| 195 int clone_value; | 209 int clone_value; |
| 196 EXPECT_TRUE(clone->GetValue()->GetAsInteger(&clone_value)); | 210 EXPECT_TRUE(clone->GetValue()->GetAsInteger(&clone_value)); |
| 197 EXPECT_EQ(123, clone_value); | 211 EXPECT_EQ(123, clone_value); |
| 198 } | 212 } |
| 199 | 213 |
| 200 } // namespace headless | 214 } // namespace headless |
| OLD | NEW |