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