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