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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 19 | #include "test/Common.h" |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | namespace aapt { |
| 24 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 25 | TEST(ResourceUtilsTest, ParseResourceName) { |
| 26 | ResourceNameRef actual; |
| 27 | bool actualPriv = false; |
| 28 | EXPECT_TRUE(ResourceUtils::parseResourceName(u"android:color/foo", &actual, &actualPriv)); |
| 29 | EXPECT_EQ(ResourceNameRef(u"android", ResourceType::kColor, u"foo"), actual); |
| 30 | EXPECT_FALSE(actualPriv); |
| 31 | |
| 32 | EXPECT_TRUE(ResourceUtils::parseResourceName(u"color/foo", &actual, &actualPriv)); |
| 33 | EXPECT_EQ(ResourceNameRef({}, ResourceType::kColor, u"foo"), actual); |
| 34 | EXPECT_FALSE(actualPriv); |
| 35 | |
| 36 | EXPECT_TRUE(ResourceUtils::parseResourceName(u"*android:color/foo", &actual, &actualPriv)); |
| 37 | EXPECT_EQ(ResourceNameRef(u"android", ResourceType::kColor, u"foo"), actual); |
| 38 | EXPECT_TRUE(actualPriv); |
| 39 | } |
| 40 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 42 | ResourceNameRef expected({}, ResourceType::kColor, u"foo"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 43 | ResourceNameRef actual; |
| 44 | bool create = false; |
| 45 | bool privateRef = false; |
| 46 | EXPECT_TRUE(ResourceUtils::tryParseReference(u"@color/foo", &actual, &create, &privateRef)); |
| 47 | EXPECT_EQ(expected, actual); |
| 48 | EXPECT_FALSE(create); |
| 49 | EXPECT_FALSE(privateRef); |
| 50 | } |
| 51 | |
| 52 | TEST(ResourceUtilsTest, ParseReferenceWithPackage) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 53 | ResourceNameRef expected(u"android", ResourceType::kColor, u"foo"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | ResourceNameRef actual; |
| 55 | bool create = false; |
| 56 | bool privateRef = false; |
| 57 | EXPECT_TRUE(ResourceUtils::tryParseReference(u"@android:color/foo", &actual, &create, |
| 58 | &privateRef)); |
| 59 | EXPECT_EQ(expected, actual); |
| 60 | EXPECT_FALSE(create); |
| 61 | EXPECT_FALSE(privateRef); |
| 62 | } |
| 63 | |
| 64 | TEST(ResourceUtilsTest, ParseReferenceWithSurroundingWhitespace) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 65 | ResourceNameRef expected(u"android", ResourceType::kColor, u"foo"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 66 | ResourceNameRef actual; |
| 67 | bool create = false; |
| 68 | bool privateRef = false; |
| 69 | EXPECT_TRUE(ResourceUtils::tryParseReference(u"\t @android:color/foo\n \n\t", &actual, |
| 70 | &create, &privateRef)); |
| 71 | EXPECT_EQ(expected, actual); |
| 72 | EXPECT_FALSE(create); |
| 73 | EXPECT_FALSE(privateRef); |
| 74 | } |
| 75 | |
| 76 | TEST(ResourceUtilsTest, ParseAutoCreateIdReference) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 77 | ResourceNameRef expected(u"android", ResourceType::kId, 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:id/foo", &actual, &create, |
| 82 | &privateRef)); |
| 83 | EXPECT_EQ(expected, actual); |
| 84 | EXPECT_TRUE(create); |
| 85 | EXPECT_FALSE(privateRef); |
| 86 | } |
| 87 | |
| 88 | TEST(ResourceUtilsTest, ParsePrivateReference) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 89 | ResourceNameRef expected(u"android", ResourceType::kId, 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"@*android:id/foo", &actual, &create, |
| 94 | &privateRef)); |
| 95 | EXPECT_EQ(expected, actual); |
| 96 | EXPECT_FALSE(create); |
| 97 | EXPECT_TRUE(privateRef); |
| 98 | } |
| 99 | |
| 100 | TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) { |
| 101 | bool create = false; |
| 102 | bool privateRef = false; |
| 103 | ResourceNameRef actual; |
| 104 | EXPECT_FALSE(ResourceUtils::tryParseReference(u"@+android:color/foo", &actual, &create, |
| 105 | &privateRef)); |
| 106 | } |
| 107 | |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 108 | TEST(ResourceUtilsTest, ParseAttributeReferences) { |
| 109 | EXPECT_TRUE(ResourceUtils::isAttributeReference(u"?android")); |
| 110 | EXPECT_TRUE(ResourceUtils::isAttributeReference(u"?android:foo")); |
| 111 | EXPECT_TRUE(ResourceUtils::isAttributeReference(u"?attr/foo")); |
| 112 | EXPECT_TRUE(ResourceUtils::isAttributeReference(u"?android:attr/foo")); |
| 113 | } |
| 114 | |
| 115 | TEST(ResourceUtilsTest, FailParseIncompleteReference) { |
| 116 | EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?style/foo")); |
| 117 | EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?android:style/foo")); |
| 118 | EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?android:")); |
| 119 | EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?android:attr/")); |
| 120 | EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?:attr/")); |
| 121 | EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?:attr/foo")); |
| 122 | EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?:/")); |
| 123 | EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?:/foo")); |
| 124 | EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?attr/")); |
| 125 | EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?/foo")); |
| 126 | } |
| 127 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 128 | TEST(ResourceUtilsTest, ParseStyleParentReference) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 129 | const ResourceName kAndroidStyleFooName(u"android", ResourceType::kStyle, u"foo"); |
| 130 | const ResourceName kStyleFooName({}, ResourceType::kStyle, u"foo"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | |
| 132 | std::string errStr; |
| 133 | Maybe<Reference> ref = ResourceUtils::parseStyleParentReference(u"@android:style/foo", &errStr); |
| 134 | AAPT_ASSERT_TRUE(ref); |
| 135 | EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName); |
| 136 | |
| 137 | ref = ResourceUtils::parseStyleParentReference(u"@style/foo", &errStr); |
| 138 | AAPT_ASSERT_TRUE(ref); |
| 139 | EXPECT_EQ(ref.value().name.value(), kStyleFooName); |
| 140 | |
| 141 | ref = ResourceUtils::parseStyleParentReference(u"?android:style/foo", &errStr); |
| 142 | AAPT_ASSERT_TRUE(ref); |
| 143 | EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName); |
| 144 | |
| 145 | ref = ResourceUtils::parseStyleParentReference(u"?style/foo", &errStr); |
| 146 | AAPT_ASSERT_TRUE(ref); |
| 147 | EXPECT_EQ(ref.value().name.value(), kStyleFooName); |
| 148 | |
| 149 | ref = ResourceUtils::parseStyleParentReference(u"android:style/foo", &errStr); |
| 150 | AAPT_ASSERT_TRUE(ref); |
| 151 | EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName); |
| 152 | |
| 153 | ref = ResourceUtils::parseStyleParentReference(u"android:foo", &errStr); |
| 154 | AAPT_ASSERT_TRUE(ref); |
| 155 | EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName); |
| 156 | |
| 157 | ref = ResourceUtils::parseStyleParentReference(u"foo", &errStr); |
| 158 | AAPT_ASSERT_TRUE(ref); |
| 159 | EXPECT_EQ(ref.value().name.value(), kStyleFooName); |
| 160 | } |
| 161 | |
| 162 | } // namespace aapt |