Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 17 | #include "ResourceUtils.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Nicholas Lativy | 79f0396 | 2019-01-16 16:19:09 +0000 | [diff] [blame] | 19 | #include "SdkConstants.h" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 20 | #include "Resource.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 21 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 23 | using ::aapt::test::ValueEq; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 24 | using ::android::Res_value; |
| 25 | using ::android::ResTable_map; |
| 26 | using ::testing::Eq; |
| 27 | using ::testing::NotNull; |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 28 | using ::testing::Pointee; |
| 29 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | namespace aapt { |
| 31 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 32 | TEST(ResourceUtilsTest, ParseBool) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 33 | EXPECT_THAT(ResourceUtils::ParseBool("true"), Eq(Maybe<bool>(true))); |
| 34 | EXPECT_THAT(ResourceUtils::ParseBool("TRUE"), Eq(Maybe<bool>(true))); |
| 35 | EXPECT_THAT(ResourceUtils::ParseBool("True"), Eq(Maybe<bool>(true))); |
| 36 | |
| 37 | EXPECT_THAT(ResourceUtils::ParseBool("false"), Eq(Maybe<bool>(false))); |
| 38 | EXPECT_THAT(ResourceUtils::ParseBool("FALSE"), Eq(Maybe<bool>(false))); |
| 39 | EXPECT_THAT(ResourceUtils::ParseBool("False"), Eq(Maybe<bool>(false))); |
Adam Lesinski | 8a3bffe | 2017-06-27 12:27:43 -0700 | [diff] [blame] | 40 | |
| 41 | EXPECT_THAT(ResourceUtils::ParseBool(" False\n "), Eq(Maybe<bool>(false))); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 44 | TEST(ResourceUtilsTest, ParseResourceName) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | ResourceNameRef actual; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | bool actual_priv = false; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 47 | EXPECT_TRUE(ResourceUtils::ParseResourceName("android:color/foo", &actual, &actual_priv)); |
| 48 | EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo"))); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | EXPECT_FALSE(actual_priv); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 50 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 51 | EXPECT_TRUE(ResourceUtils::ParseResourceName("color/foo", &actual, &actual_priv)); |
| 52 | EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo"))); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | EXPECT_FALSE(actual_priv); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 54 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 55 | EXPECT_TRUE(ResourceUtils::ParseResourceName("*android:color/foo", &actual, &actual_priv)); |
| 56 | EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo"))); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | EXPECT_TRUE(actual_priv); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 58 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 59 | EXPECT_FALSE(ResourceUtils::ParseResourceName(android::StringPiece(), &actual, &actual_priv)); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 62 | TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | ResourceNameRef actual; |
| 64 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | bool private_ref = false; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 66 | EXPECT_TRUE(ResourceUtils::ParseReference("@color/foo", &actual, &create, &private_ref)); |
| 67 | EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo"))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | EXPECT_FALSE(create); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | EXPECT_FALSE(private_ref); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST(ResourceUtilsTest, ParseReferenceWithPackage) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | ResourceNameRef actual; |
| 74 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | bool private_ref = false; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 76 | EXPECT_TRUE(ResourceUtils::ParseReference("@android:color/foo", &actual, &create, &private_ref)); |
| 77 | EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo"))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | EXPECT_FALSE(create); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | EXPECT_FALSE(private_ref); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST(ResourceUtilsTest, ParseReferenceWithSurroundingWhitespace) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | ResourceNameRef actual; |
| 84 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | bool private_ref = false; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 86 | EXPECT_TRUE(ResourceUtils::ParseReference("\t @android:color/foo\n \n\t", &actual, &create, &private_ref)); |
| 87 | EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo"))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | EXPECT_FALSE(create); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | EXPECT_FALSE(private_ref); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST(ResourceUtilsTest, ParseAutoCreateIdReference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | ResourceNameRef actual; |
| 94 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | bool private_ref = false; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 96 | EXPECT_TRUE(ResourceUtils::ParseReference("@+android:id/foo", &actual, &create, &private_ref)); |
| 97 | EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo"))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | EXPECT_TRUE(create); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 99 | EXPECT_FALSE(private_ref); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST(ResourceUtilsTest, ParsePrivateReference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | ResourceNameRef actual; |
| 104 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 105 | bool private_ref = false; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 106 | EXPECT_TRUE(ResourceUtils::ParseReference("@*android:id/foo", &actual, &create, &private_ref)); |
| 107 | EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo"))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | EXPECT_FALSE(create); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | EXPECT_TRUE(private_ref); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Clark DuVall | e9fedbe | 2020-01-09 11:52:52 -0800 | [diff] [blame] | 112 | TEST(ResourceUtilsTest, ParseBinaryDynamicReference) { |
| 113 | android::Res_value value = {}; |
| 114 | value.data = util::HostToDevice32(0x01); |
| 115 | value.dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE; |
| 116 | std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(ResourceType::kId, |
| 117 | android::ConfigDescription(), |
| 118 | android::ResStringPool(), value, |
| 119 | nullptr); |
| 120 | |
| 121 | Reference* ref = ValueCast<Reference>(item.get()); |
| 122 | EXPECT_TRUE(ref->is_dynamic); |
| 123 | EXPECT_EQ(ref->id.value().id, 0x01); |
| 124 | } |
| 125 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 126 | TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | bool private_ref = false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | ResourceNameRef actual; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 130 | EXPECT_FALSE(ResourceUtils::ParseReference("@+android:color/foo", &actual, &create, &private_ref)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 133 | TEST(ResourceUtilsTest, ParseAttributeReferences) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 134 | EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android")); |
| 135 | EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:foo")); |
| 136 | EXPECT_TRUE(ResourceUtils::IsAttributeReference("?attr/foo")); |
| 137 | EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:attr/foo")); |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | TEST(ResourceUtilsTest, FailParseIncompleteReference) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?style/foo")); |
| 142 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:style/foo")); |
| 143 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:")); |
| 144 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:attr/")); |
| 145 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/")); |
| 146 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/foo")); |
| 147 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/")); |
| 148 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/foo")); |
| 149 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?attr/")); |
| 150 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?/foo")); |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 153 | TEST(ResourceUtilsTest, ParseStyleParentReference) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 154 | const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle, "foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 156 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | std::string err_str; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 158 | Maybe<Reference> ref = ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str); |
| 159 | ASSERT_TRUE(ref); |
| 160 | EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 161 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 162 | ref = ResourceUtils::ParseStyleParentReference("@style/foo", &err_str); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 163 | ASSERT_TRUE(ref); |
| 164 | EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 165 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 166 | ref = ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str); |
| 167 | ASSERT_TRUE(ref); |
| 168 | EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 169 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 170 | ref = ResourceUtils::ParseStyleParentReference("?style/foo", &err_str); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 171 | ASSERT_TRUE(ref); |
| 172 | EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 173 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 175 | ASSERT_TRUE(ref); |
| 176 | EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | ref = ResourceUtils::ParseStyleParentReference("android:foo", &err_str); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 179 | ASSERT_TRUE(ref); |
| 180 | EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 181 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | ref = ResourceUtils::ParseStyleParentReference("@android:foo", &err_str); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 183 | ASSERT_TRUE(ref); |
| 184 | EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName))); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 185 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | ref = ResourceUtils::ParseStyleParentReference("foo", &err_str); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 187 | ASSERT_TRUE(ref); |
| 188 | EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName))); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 189 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 190 | ref = ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str); |
| 191 | ASSERT_TRUE(ref); |
| 192 | EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName))); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | EXPECT_TRUE(ref.value().private_reference); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 196 | TEST(ResourceUtilsTest, ParseEmptyFlag) { |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 197 | std::unique_ptr<Attribute> attr = test::AttributeBuilder() |
| 198 | .SetTypeMask(ResTable_map::TYPE_FLAGS) |
| 199 | .AddItem("one", 0x01) |
| 200 | .AddItem("two", 0x02) |
| 201 | .Build(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 202 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 203 | std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseFlagSymbol(attr.get(), ""); |
| 204 | ASSERT_THAT(result, NotNull()); |
| 205 | EXPECT_THAT(result->value.data, Eq(0u)); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 208 | TEST(ResourceUtilsTest, NullIsEmptyReference) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 209 | ASSERT_THAT(ResourceUtils::MakeNull(), Pointee(ValueEq(Reference()))); |
| 210 | ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@null"), Pointee(ValueEq(Reference()))); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | TEST(ResourceUtilsTest, EmptyIsBinaryPrimitive) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 214 | ASSERT_THAT(ResourceUtils::MakeEmpty(), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY)))); |
| 215 | ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@empty"), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY)))); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Adam Lesinski | 8a3bffe | 2017-06-27 12:27:43 -0700 | [diff] [blame] | 218 | TEST(ResourceUtilsTest, ItemsWithWhitespaceAreParsedCorrectly) { |
| 219 | EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12\n ", ResTable_map::TYPE_INTEGER), |
| 220 | Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_DEC, 12u)))); |
| 221 | EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" true\n ", ResTable_map::TYPE_BOOLEAN), |
| 222 | Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_BOOLEAN, 0xffffffffu)))); |
| 223 | |
| 224 | const float expected_float = 12.0f; |
| 225 | const uint32_t expected_float_flattened = *(uint32_t*)&expected_float; |
| 226 | EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12.0\n ", ResTable_map::TYPE_FLOAT), |
| 227 | Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened)))); |
| 228 | } |
| 229 | |
Nicholas Lativy | 79f0396 | 2019-01-16 16:19:09 +0000 | [diff] [blame] | 230 | TEST(ResourceUtilsTest, ParseSdkVersionWithCodename) { |
Ryan Mitchell | 48002f6 | 2019-04-11 14:29:25 -0700 | [diff] [blame] | 231 | EXPECT_THAT(ResourceUtils::ParseSdkVersion("Q"), Eq(Maybe<int>(10000))); |
Nicholas Lativy | 79f0396 | 2019-01-16 16:19:09 +0000 | [diff] [blame] | 232 | EXPECT_THAT( |
Ryan Mitchell | 48002f6 | 2019-04-11 14:29:25 -0700 | [diff] [blame] | 233 | ResourceUtils::ParseSdkVersion("Q.fingerprint"), |
| 234 | Eq(Maybe<int>(10000))); |
| 235 | |
| 236 | EXPECT_THAT(ResourceUtils::ParseSdkVersion("R"), Eq(Maybe<int>(10000))); |
| 237 | EXPECT_THAT( |
| 238 | ResourceUtils::ParseSdkVersion("R.fingerprint"), |
| 239 | Eq(Maybe<int>(10000))); |
Nicholas Lativy | 79f0396 | 2019-01-16 16:19:09 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 242 | TEST(ResourceUtilsTest, StringBuilderWhitespaceRemoval) { |
| 243 | EXPECT_THAT(ResourceUtils::StringBuilder() |
| 244 | .AppendText(" hey guys ") |
| 245 | .AppendText(" this is so cool ") |
| 246 | .to_string(), |
| 247 | Eq(" hey guys this is so cool ")); |
| 248 | EXPECT_THAT(ResourceUtils::StringBuilder() |
| 249 | .AppendText(" \" wow, so many \t ") |
| 250 | .AppendText("spaces. \"what? ") |
| 251 | .to_string(), |
| 252 | Eq(" wow, so many \t spaces. what? ")); |
| 253 | EXPECT_THAT(ResourceUtils::StringBuilder() |
| 254 | .AppendText(" where \t ") |
| 255 | .AppendText(" \nis the pie?") |
| 256 | .to_string(), |
| 257 | Eq(" where is the pie?")); |
| 258 | } |
| 259 | |
| 260 | TEST(ResourceUtilsTest, StringBuilderEscaping) { |
| 261 | EXPECT_THAT(ResourceUtils::StringBuilder() |
| 262 | .AppendText("hey guys\\n ") |
| 263 | .AppendText(" this \\t is so\\\\ cool") |
| 264 | .to_string(), |
| 265 | Eq("hey guys\n this \t is so\\ cool")); |
| 266 | EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\@\\?\\#\\\\\\'").to_string(), |
| 267 | Eq("@?#\\\'")); |
| 268 | } |
| 269 | |
| 270 | TEST(ResourceUtilsTest, StringBuilderMisplacedQuote) { |
| 271 | ResourceUtils::StringBuilder builder; |
| 272 | EXPECT_FALSE(builder.AppendText("they're coming!")); |
| 273 | } |
| 274 | |
| 275 | TEST(ResourceUtilsTest, StringBuilderUnicodeCodes) { |
| 276 | EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\u00AF\\u0AF0 woah").to_string(), |
| 277 | Eq("\u00AF\u0AF0 woah")); |
| 278 | EXPECT_FALSE(ResourceUtils::StringBuilder().AppendText("\\u00 yo")); |
| 279 | } |
| 280 | |
| 281 | TEST(ResourceUtilsTest, StringBuilderPreserveSpaces) { |
| 282 | EXPECT_THAT(ResourceUtils::StringBuilder(true /*preserve_spaces*/).AppendText("\"").to_string(), |
| 283 | Eq("\"")); |
| 284 | } |
| 285 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 286 | } // namespace aapt |