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