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 | |
| 17 | #include "ResourceTable.h" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 18 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 19 | #include "ResourceValues.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 20 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "util/Util.h" |
| 22 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | #include <ostream> |
| 25 | #include <string> |
| 26 | |
| 27 | namespace aapt { |
| 28 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 29 | TEST(ResourceTableTest, FailToAddResourceWithBadName) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 30 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 31 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | EXPECT_FALSE(table.AddResource( |
| 33 | test::ParseNameOrDie("android:id/hey,there"), ConfigDescription{}, "", |
| 34 | test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(), |
| 35 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 36 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | EXPECT_FALSE(table.AddResource( |
| 38 | test::ParseNameOrDie("android:id/hey:there"), ConfigDescription{}, "", |
| 39 | test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(), |
| 40 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 43 | TEST(ResourceTableTest, AddResourceWithWeirdNameWhenAddingMangledResources) { |
| 44 | ResourceTable table; |
| 45 | |
| 46 | EXPECT_TRUE(table.AddResourceAllowMangled( |
| 47 | test::ParseNameOrDie("android:id/heythere "), ConfigDescription{}, "", |
| 48 | test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(), test::GetDiagnostics())); |
| 49 | } |
| 50 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 51 | TEST(ResourceTableTest, AddOneResource) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 53 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | EXPECT_TRUE(table.AddResource( |
| 55 | test::ParseNameOrDie("android:attr/id"), ConfigDescription{}, "", |
| 56 | test::ValueBuilder<Id>().SetSource("test/path/file.xml", 23u).Build(), |
| 57 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | ASSERT_NE(nullptr, test::GetValue<Id>(&table, "android:attr/id")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 62 | TEST(ResourceTableTest, AddMultipleResources) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 64 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | ConfigDescription config; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | ConfigDescription language_config; |
| 67 | memcpy(language_config.language, "pl", sizeof(language_config.language)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 68 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | EXPECT_TRUE(table.AddResource( |
| 70 | test::ParseNameOrDie("android:attr/layout_width"), config, "", |
| 71 | test::ValueBuilder<Id>().SetSource("test/path/file.xml", 10u).Build(), |
| 72 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 73 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | EXPECT_TRUE(table.AddResource( |
| 75 | test::ParseNameOrDie("android:attr/id"), config, "", |
| 76 | test::ValueBuilder<Id>().SetSource("test/path/file.xml", 12u).Build(), |
| 77 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | EXPECT_TRUE(table.AddResource( |
| 80 | test::ParseNameOrDie("android:string/ok"), config, "", |
| 81 | test::ValueBuilder<Id>().SetSource("test/path/file.xml", 14u).Build(), |
| 82 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 83 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | EXPECT_TRUE(table.AddResource( |
| 85 | test::ParseNameOrDie("android:string/ok"), language_config, "", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | test::ValueBuilder<BinaryPrimitive>(android::Res_value{}) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | .SetSource("test/path/file.xml", 20u) |
| 88 | .Build(), |
| 89 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 90 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | ASSERT_NE(nullptr, test::GetValue<Id>(&table, "android:attr/layout_width")); |
| 92 | ASSERT_NE(nullptr, test::GetValue<Id>(&table, "android:attr/id")); |
| 93 | ASSERT_NE(nullptr, test::GetValue<Id>(&table, "android:string/ok")); |
| 94 | ASSERT_NE(nullptr, test::GetValueForConfig<BinaryPrimitive>( |
| 95 | &table, "android:string/ok", language_config)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 98 | TEST(ResourceTableTest, OverrideWeakResourceValue) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 100 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | ASSERT_TRUE(table.AddResource( |
| 102 | test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "", |
| 103 | util::make_unique<Attribute>(true), test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 104 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 105 | Attribute* attr = test::GetValue<Attribute>(&table, "android:attr/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | ASSERT_NE(nullptr, attr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | EXPECT_TRUE(attr->IsWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 108 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | ASSERT_TRUE(table.AddResource( |
| 110 | test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "", |
| 111 | util::make_unique<Attribute>(false), test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 112 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 113 | attr = test::GetValue<Attribute>(&table, "android:attr/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 114 | ASSERT_NE(nullptr, attr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | EXPECT_FALSE(attr->IsWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 118 | TEST(ResourceTableTest, ProductVaryingValues) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | ResourceTable table; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 120 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"), |
| 122 | test::ParseConfigOrDie("land"), "tablet", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | util::make_unique<Id>(), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | test::GetDiagnostics())); |
| 125 | EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"), |
| 126 | test::ParseConfigOrDie("land"), "phone", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | util::make_unique<Id>(), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | test::GetDiagnostics())); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 129 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<Id>( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | &table, "android:string/foo", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | test::ParseConfigOrDie("land"), "tablet")); |
| 133 | EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<Id>( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | &table, "android:string/foo", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 135 | test::ParseConfigOrDie("land"), "phone")); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 136 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | Maybe<ResourceTable::SearchResult> sr = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | table.FindResource(test::ParseNameOrDie("android:string/foo")); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | AAPT_ASSERT_TRUE(sr); |
| 140 | std::vector<ResourceConfigValue*> values = |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 141 | sr.value().entry->FindAllValues(test::ParseConfigOrDie("land")); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | ASSERT_EQ(2u, values.size()); |
| 143 | EXPECT_EQ(std::string("phone"), values[0]->product); |
| 144 | EXPECT_EQ(std::string("tablet"), values[1]->product); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | } // namespace aapt |