Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [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 "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | #include "ResourceTable.h" |
| 19 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "util/Util.h" |
| 21 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame^] | 22 | #include "test/Builders.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | |
| 24 | #include <algorithm> |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <ostream> |
| 27 | #include <string> |
| 28 | |
| 29 | namespace aapt { |
| 30 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | struct ResourceTableTest : public ::testing::Test { |
| 32 | struct EmptyDiagnostics : public IDiagnostics { |
| 33 | void error(const DiagMessage& msg) override {} |
| 34 | void warn(const DiagMessage& msg) override {} |
| 35 | void note(const DiagMessage& msg) override {} |
| 36 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 37 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 38 | EmptyDiagnostics mDiagnostics; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | TEST_F(ResourceTableTest, FailToAddResourceWithBadName) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | |
| 44 | EXPECT_FALSE(table.addResource( |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame^] | 45 | ResourceNameRef(u"android", ResourceType::kId, u"hey,there"), |
| 46 | ConfigDescription{}, |
| 47 | test::ValueBuilder<Id>().setSource("test.xml", 21u).build(), |
| 48 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | |
| 50 | EXPECT_FALSE(table.addResource( |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame^] | 51 | ResourceNameRef(u"android", ResourceType::kId, u"hey:there"), |
| 52 | ConfigDescription{}, |
| 53 | test::ValueBuilder<Id>().setSource("test.xml", 21u).build(), |
| 54 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 57 | TEST_F(ResourceTableTest, AddOneResource) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame^] | 60 | EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/id"), |
| 61 | ConfigDescription{}, |
| 62 | test::ValueBuilder<Id>() |
| 63 | .setSource("test/path/file.xml", 23u).build(), |
| 64 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 65 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 66 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 69 | TEST_F(ResourceTableTest, AddMultipleResources) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 71 | |
| 72 | ConfigDescription config; |
| 73 | ConfigDescription languageConfig; |
| 74 | memcpy(languageConfig.language, "pl", sizeof(languageConfig.language)); |
| 75 | |
| 76 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 77 | test::parseNameOrDie(u"@android:attr/layout_width"), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame^] | 78 | config, |
| 79 | test::ValueBuilder<Id>().setSource("test/path/file.xml", 10u).build(), |
| 80 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 81 | |
| 82 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 83 | test::parseNameOrDie(u"@android:attr/id"), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame^] | 84 | config, |
| 85 | test::ValueBuilder<Id>().setSource("test/path/file.xml", 12u).build(), |
| 86 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 87 | |
| 88 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 89 | test::parseNameOrDie(u"@android:string/ok"), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame^] | 90 | config, |
| 91 | test::ValueBuilder<Id>().setSource("test/path/file.xml", 14u).build(), |
| 92 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 93 | |
| 94 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 95 | test::parseNameOrDie(u"@android:string/ok"), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame^] | 96 | languageConfig, |
| 97 | test::ValueBuilder<BinaryPrimitive>(android::Res_value{}) |
| 98 | .setSource("test/path/file.xml", 20u) |
| 99 | .build(), |
| 100 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 101 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 102 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/layout_width")); |
| 103 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); |
| 104 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:string/ok")); |
| 105 | ASSERT_NE(nullptr, test::getValueForConfig<BinaryPrimitive>(&table, u"@android:string/ok", |
| 106 | languageConfig)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 109 | TEST_F(ResourceTableTest, OverrideWeakResourceValue) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 110 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame^] | 112 | ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{}, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 113 | util::make_unique<Attribute>(true), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 114 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 115 | Attribute* attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); |
| 116 | ASSERT_NE(nullptr, attr); |
| 117 | EXPECT_TRUE(attr->isWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 118 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame^] | 119 | ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{}, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 120 | util::make_unique<Attribute>(false), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 121 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); |
| 123 | ASSERT_NE(nullptr, attr); |
| 124 | EXPECT_FALSE(attr->isWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | } // namespace aapt |