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 | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 31 | TEST(ResourceTableTest, FailToAddResourceWithBadName) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 32 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | |
| 34 | EXPECT_FALSE(table.addResource( |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 35 | ResourceNameRef(u"android", ResourceType::kId, u"hey,there"), |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 36 | ConfigDescription{}, "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 37 | test::ValueBuilder<Id>().setSource("test.xml", 21u).build(), |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 38 | test::getDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 39 | |
| 40 | EXPECT_FALSE(table.addResource( |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 41 | ResourceNameRef(u"android", ResourceType::kId, u"hey:there"), |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 42 | ConfigDescription{}, "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 43 | test::ValueBuilder<Id>().setSource("test.xml", 21u).build(), |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 44 | test::getDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 47 | TEST(ResourceTableTest, AddOneResource) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 50 | EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/id"), |
| 51 | ConfigDescription{}, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 52 | "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 53 | test::ValueBuilder<Id>() |
| 54 | .setSource("test/path/file.xml", 23u).build(), |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 55 | test::getDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 56 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 57 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 60 | TEST(ResourceTableTest, AddMultipleResources) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 62 | |
| 63 | ConfigDescription config; |
| 64 | ConfigDescription languageConfig; |
| 65 | memcpy(languageConfig.language, "pl", sizeof(languageConfig.language)); |
| 66 | |
| 67 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 68 | test::parseNameOrDie(u"@android:attr/layout_width"), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 69 | config, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 70 | "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 71 | test::ValueBuilder<Id>().setSource("test/path/file.xml", 10u).build(), |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 72 | test::getDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 73 | |
| 74 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 75 | test::parseNameOrDie(u"@android:attr/id"), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 76 | config, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 77 | "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 78 | test::ValueBuilder<Id>().setSource("test/path/file.xml", 12u).build(), |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 79 | test::getDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 80 | |
| 81 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 82 | test::parseNameOrDie(u"@android:string/ok"), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 83 | config, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 84 | "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 85 | test::ValueBuilder<Id>().setSource("test/path/file.xml", 14u).build(), |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 86 | test::getDiagnostics())); |
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 | languageConfig, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 91 | "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 92 | test::ValueBuilder<BinaryPrimitive>(android::Res_value{}) |
| 93 | .setSource("test/path/file.xml", 20u) |
| 94 | .build(), |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 95 | test::getDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 96 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 97 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/layout_width")); |
| 98 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); |
| 99 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:string/ok")); |
| 100 | ASSERT_NE(nullptr, test::getValueForConfig<BinaryPrimitive>(&table, u"@android:string/ok", |
| 101 | languageConfig)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 104 | TEST(ResourceTableTest, OverrideWeakResourceValue) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 105 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 107 | ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{}, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 108 | "", util::make_unique<Attribute>(true), test::getDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 109 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 110 | Attribute* attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); |
| 111 | ASSERT_NE(nullptr, attr); |
| 112 | EXPECT_TRUE(attr->isWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 113 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 114 | ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{}, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 115 | "", util::make_unique<Attribute>(false), test::getDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 117 | attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); |
| 118 | ASSERT_NE(nullptr, attr); |
| 119 | EXPECT_FALSE(attr->isWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 122 | TEST(ResourceTableTest, ProductVaryingValues) { |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 123 | ResourceTable table; |
| 124 | |
| 125 | EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:string/foo"), |
| 126 | test::parseConfigOrDie("land"), |
| 127 | "tablet", |
| 128 | util::make_unique<Id>(), |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 129 | test::getDiagnostics())); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 130 | EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:string/foo"), |
| 131 | test::parseConfigOrDie("land"), |
| 132 | "phone", |
| 133 | util::make_unique<Id>(), |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 134 | test::getDiagnostics())); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 135 | |
| 136 | EXPECT_NE(nullptr, test::getValueForConfigAndProduct<Id>(&table, u"@android:string/foo", |
| 137 | test::parseConfigOrDie("land"), |
| 138 | "tablet")); |
| 139 | EXPECT_NE(nullptr, test::getValueForConfigAndProduct<Id>(&table, u"@android:string/foo", |
| 140 | test::parseConfigOrDie("land"), |
| 141 | "phone")); |
| 142 | |
| 143 | Maybe<ResourceTable::SearchResult> sr = table.findResource( |
| 144 | test::parseNameOrDie(u"@android:string/foo")); |
| 145 | AAPT_ASSERT_TRUE(sr); |
| 146 | std::vector<ResourceConfigValue*> values = sr.value().entry->findAllValues( |
| 147 | test::parseConfigOrDie("land")); |
| 148 | ASSERT_EQ(2u, values.size()); |
| 149 | EXPECT_EQ(std::string("phone"), values[0]->product); |
| 150 | EXPECT_EQ(std::string("tablet"), values[1]->product); |
| 151 | } |
| 152 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 153 | } // namespace aapt |