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 | |
| 22 | #include "test/Common.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( |
| 45 | ResourceNameRef{ u"android", ResourceType::kId, u"hey,there" }, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | {}, Source{ "test.xml", 21 }, |
| 47 | util::make_unique<Id>(), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | |
| 49 | EXPECT_FALSE(table.addResource( |
| 50 | ResourceNameRef{ u"android", ResourceType::kId, u"hey:there" }, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | {}, Source{ "test.xml", 21 }, |
| 52 | util::make_unique<Id>(), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 55 | TEST_F(ResourceTableTest, AddOneResource) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 56 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 57 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/id"), {}, |
| 59 | Source{ "test/path/file.xml", 23 }, |
| 60 | util::make_unique<Id>(), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 62 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 65 | TEST_F(ResourceTableTest, AddMultipleResources) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 66 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | |
| 68 | ConfigDescription config; |
| 69 | ConfigDescription languageConfig; |
| 70 | memcpy(languageConfig.language, "pl", sizeof(languageConfig.language)); |
| 71 | |
| 72 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 73 | test::parseNameOrDie(u"@android:attr/layout_width"), |
| 74 | config, Source{ "test/path/file.xml", 10 }, |
| 75 | util::make_unique<Id>(), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | |
| 77 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 78 | test::parseNameOrDie(u"@android:attr/id"), |
| 79 | config, Source{ "test/path/file.xml", 12 }, |
| 80 | util::make_unique<Id>(), &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:string/ok"), |
| 84 | config, Source{ "test/path/file.xml", 14 }, |
| 85 | util::make_unique<Id>(), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 86 | |
| 87 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 88 | test::parseNameOrDie(u"@android:string/ok"), |
| 89 | languageConfig, Source{ "test/path/file.xml", 20 }, |
| 90 | util::make_unique<BinaryPrimitive>(android::Res_value{}), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 91 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/layout_width")); |
| 93 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); |
| 94 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:string/ok")); |
| 95 | ASSERT_NE(nullptr, test::getValueForConfig<BinaryPrimitive>(&table, u"@android:string/ok", |
| 96 | languageConfig)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 99 | TEST_F(ResourceTableTest, OverrideWeakResourceValue) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 100 | ResourceTable table; |
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_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), {}, {}, |
| 103 | util::make_unique<Attribute>(true), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 104 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 105 | Attribute* attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); |
| 106 | ASSERT_NE(nullptr, attr); |
| 107 | EXPECT_TRUE(attr->isWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 108 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 109 | ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), {}, {}, |
| 110 | util::make_unique<Attribute>(false), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); |
| 113 | ASSERT_NE(nullptr, attr); |
| 114 | EXPECT_FALSE(attr->isWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | } // namespace aapt |