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 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 19 | #include "Resource.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 20 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | |
| 22 | namespace aapt { |
| 23 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 24 | TEST(ResourceUtilsTest, ParseBool) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | EXPECT_EQ(Maybe<bool>(true), ResourceUtils::ParseBool("true")); |
| 26 | EXPECT_EQ(Maybe<bool>(true), ResourceUtils::ParseBool("TRUE")); |
| 27 | EXPECT_EQ(Maybe<bool>(true), ResourceUtils::ParseBool("True")); |
| 28 | EXPECT_EQ(Maybe<bool>(false), ResourceUtils::ParseBool("false")); |
| 29 | EXPECT_EQ(Maybe<bool>(false), ResourceUtils::ParseBool("FALSE")); |
| 30 | EXPECT_EQ(Maybe<bool>(false), ResourceUtils::ParseBool("False")); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 33 | TEST(ResourceUtilsTest, ParseResourceName) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 34 | ResourceNameRef actual; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 35 | bool actual_priv = false; |
| 36 | EXPECT_TRUE(ResourceUtils::ParseResourceName("android:color/foo", &actual, |
| 37 | &actual_priv)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | EXPECT_EQ(ResourceNameRef("android", ResourceType::kColor, "foo"), actual); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | EXPECT_FALSE(actual_priv); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | EXPECT_TRUE( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | ResourceUtils::ParseResourceName("color/foo", &actual, &actual_priv)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | EXPECT_EQ(ResourceNameRef({}, ResourceType::kColor, "foo"), actual); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | EXPECT_FALSE(actual_priv); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 45 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | EXPECT_TRUE(ResourceUtils::ParseResourceName("*android:color/foo", &actual, |
| 47 | &actual_priv)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | EXPECT_EQ(ResourceNameRef("android", ResourceType::kColor, "foo"), actual); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | EXPECT_TRUE(actual_priv); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 50 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 51 | EXPECT_FALSE(ResourceUtils::ParseResourceName(android::StringPiece(), &actual, &actual_priv)); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | ResourceNameRef expected({}, ResourceType::kColor, "foo"); |
| 56 | ResourceNameRef actual; |
| 57 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | bool private_ref = false; |
| 59 | EXPECT_TRUE(ResourceUtils::ParseReference("@color/foo", &actual, &create, |
| 60 | &private_ref)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | EXPECT_EQ(expected, actual); |
| 62 | EXPECT_FALSE(create); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | EXPECT_FALSE(private_ref); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST(ResourceUtilsTest, ParseReferenceWithPackage) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | ResourceNameRef expected("android", ResourceType::kColor, "foo"); |
| 68 | ResourceNameRef actual; |
| 69 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | bool private_ref = false; |
| 71 | EXPECT_TRUE(ResourceUtils::ParseReference("@android:color/foo", &actual, |
| 72 | &create, &private_ref)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | EXPECT_EQ(expected, actual); |
| 74 | EXPECT_FALSE(create); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | EXPECT_FALSE(private_ref); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | TEST(ResourceUtilsTest, ParseReferenceWithSurroundingWhitespace) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | ResourceNameRef expected("android", ResourceType::kColor, "foo"); |
| 80 | ResourceNameRef actual; |
| 81 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | bool private_ref = false; |
| 83 | EXPECT_TRUE(ResourceUtils::ParseReference("\t @android:color/foo\n \n\t", |
| 84 | &actual, &create, &private_ref)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | EXPECT_EQ(expected, actual); |
| 86 | EXPECT_FALSE(create); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | EXPECT_FALSE(private_ref); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST(ResourceUtilsTest, ParseAutoCreateIdReference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | ResourceNameRef expected("android", ResourceType::kId, "foo"); |
| 92 | ResourceNameRef actual; |
| 93 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | bool private_ref = false; |
| 95 | EXPECT_TRUE(ResourceUtils::ParseReference("@+android:id/foo", &actual, |
| 96 | &create, &private_ref)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | EXPECT_EQ(expected, actual); |
| 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 expected("android", ResourceType::kId, "foo"); |
| 104 | ResourceNameRef actual; |
| 105 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | bool private_ref = false; |
| 107 | EXPECT_TRUE(ResourceUtils::ParseReference("@*android:id/foo", &actual, |
| 108 | &create, &private_ref)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | EXPECT_EQ(expected, actual); |
| 110 | EXPECT_FALSE(create); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 111 | EXPECT_TRUE(private_ref); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | bool private_ref = false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | ResourceNameRef actual; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | EXPECT_FALSE(ResourceUtils::ParseReference("@+android:color/foo", &actual, |
| 119 | &create, &private_ref)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 122 | TEST(ResourceUtilsTest, ParseAttributeReferences) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android")); |
| 124 | EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:foo")); |
| 125 | EXPECT_TRUE(ResourceUtils::IsAttributeReference("?attr/foo")); |
| 126 | EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:attr/foo")); |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | TEST(ResourceUtilsTest, FailParseIncompleteReference) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?style/foo")); |
| 131 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:style/foo")); |
| 132 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:")); |
| 133 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:attr/")); |
| 134 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/")); |
| 135 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/foo")); |
| 136 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/")); |
| 137 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/foo")); |
| 138 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?attr/")); |
| 139 | EXPECT_FALSE(ResourceUtils::IsAttributeReference("?/foo")); |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 142 | TEST(ResourceUtilsTest, ParseStyleParentReference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle, |
| 144 | "foo"); |
| 145 | const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 146 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 147 | std::string err_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | Maybe<Reference> ref = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | AAPT_ASSERT_TRUE(ref); |
| 151 | EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 152 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 153 | ref = ResourceUtils::ParseStyleParentReference("@style/foo", &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | AAPT_ASSERT_TRUE(ref); |
| 155 | EXPECT_EQ(ref.value().name.value(), kStyleFooName); |
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 | ref = |
| 158 | ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | AAPT_ASSERT_TRUE(ref); |
| 160 | EXPECT_EQ(ref.value().name.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 | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | AAPT_ASSERT_TRUE(ref); |
| 164 | EXPECT_EQ(ref.value().name.value(), kStyleFooName); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 165 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 166 | ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 167 | AAPT_ASSERT_TRUE(ref); |
| 168 | EXPECT_EQ(ref.value().name.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("android:foo", &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | AAPT_ASSERT_TRUE(ref); |
| 172 | EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName); |
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:foo", &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 175 | AAPT_ASSERT_TRUE(ref); |
| 176 | EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 177 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | ref = ResourceUtils::ParseStyleParentReference("foo", &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | AAPT_ASSERT_TRUE(ref); |
| 180 | EXPECT_EQ(ref.value().name.value(), kStyleFooName); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 181 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | ref = |
| 183 | ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | AAPT_ASSERT_TRUE(ref); |
| 185 | EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | EXPECT_TRUE(ref.value().private_reference); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 189 | TEST(ResourceUtilsTest, ParseEmptyFlag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 190 | std::unique_ptr<Attribute> attr = |
| 191 | test::AttributeBuilder(false) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 192 | .SetTypeMask(android::ResTable_map::TYPE_FLAGS) |
| 193 | .AddItem("one", 0x01) |
| 194 | .AddItem("two", 0x02) |
| 195 | .Build(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 196 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 197 | std::unique_ptr<BinaryPrimitive> result = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 198 | ResourceUtils::TryParseFlagSymbol(attr.get(), ""); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | ASSERT_NE(nullptr, result); |
| 200 | EXPECT_EQ(0u, result->value.data); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | } // namespace aapt |