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