blob: 2a3c131f7b4bfdb0f6a9e2d5f4a8c40b9f8ac9f0 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinskicacb28f2016-10-19 12:18:14 -070018#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#include "ResourceValues.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070020#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "util/Util.h"
22
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <ostream>
25#include <string>
26
Adam Lesinskia45893a2017-05-30 15:19:02 -070027using ::testing::NotNull;
28
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029namespace aapt {
30
Adam Lesinskicc5609d2016-04-05 12:41:07 -070031TEST(ResourceTableTest, FailToAddResourceWithBadName) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 EXPECT_FALSE(table.AddResource(
35 test::ParseNameOrDie("android:id/hey,there"), ConfigDescription{}, "",
36 test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(),
37 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 EXPECT_FALSE(table.AddResource(
40 test::ParseNameOrDie("android:id/hey:there"), ConfigDescription{}, "",
41 test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(),
42 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043}
44
Adam Lesinskib1afa072017-03-29 13:52:38 -070045TEST(ResourceTableTest, AddResourceWithWeirdNameWhenAddingMangledResources) {
46 ResourceTable table;
47
48 EXPECT_TRUE(table.AddResourceAllowMangled(
49 test::ParseNameOrDie("android:id/heythere "), ConfigDescription{}, "",
50 test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(), test::GetDiagnostics()));
51}
52
Adam Lesinskicc5609d2016-04-05 12:41:07 -070053TEST(ResourceTableTest, AddOneResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 EXPECT_TRUE(table.AddResource(
57 test::ParseNameOrDie("android:attr/id"), ConfigDescription{}, "",
58 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 23u).Build(),
59 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060
Adam Lesinskia45893a2017-05-30 15:19:02 -070061 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062}
63
Adam Lesinskicc5609d2016-04-05 12:41:07 -070064TEST(ResourceTableTest, AddMultipleResources) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 ConfigDescription language_config;
69 memcpy(language_config.language, "pl", sizeof(language_config.language));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 EXPECT_TRUE(table.AddResource(
72 test::ParseNameOrDie("android:attr/layout_width"), config, "",
73 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 10u).Build(),
74 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 EXPECT_TRUE(table.AddResource(
77 test::ParseNameOrDie("android:attr/id"), config, "",
78 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 12u).Build(),
79 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 EXPECT_TRUE(table.AddResource(
82 test::ParseNameOrDie("android:string/ok"), config, "",
83 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 14u).Build(),
84 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 EXPECT_TRUE(table.AddResource(
87 test::ParseNameOrDie("android:string/ok"), language_config, "",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 .SetSource("test/path/file.xml", 20u)
90 .Build(),
91 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092
Adam Lesinskia45893a2017-05-30 15:19:02 -070093 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/layout_width"), NotNull());
94 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
95 EXPECT_THAT(test::GetValue<Id>(&table, "android:string/ok"), NotNull());
96 EXPECT_THAT(test::GetValueForConfig<BinaryPrimitive>(&table, "android:string/ok", language_config), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097}
98
Adam Lesinskicc5609d2016-04-05 12:41:07 -070099TEST(ResourceTableTest, OverrideWeakResourceValue) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 ASSERT_TRUE(table.AddResource(
103 test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "",
104 util::make_unique<Attribute>(true), test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 Attribute* attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700107 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 ASSERT_TRUE(table.AddResource(
111 test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "",
112 util::make_unique<Attribute>(false), test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700115 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 EXPECT_FALSE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800117}
118
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700119TEST(ResourceTableTest, ProductVaryingValues) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 ResourceTable table;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"),
123 test::ParseConfigOrDie("land"), "tablet",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 util::make_unique<Id>(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 test::GetDiagnostics()));
126 EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"),
127 test::ParseConfigOrDie("land"), "phone",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 util::make_unique<Id>(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 test::GetDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800130
Adam Lesinskia45893a2017-05-30 15:19:02 -0700131 EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "tablet"), NotNull());
132 EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "phone"), NotNull());
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800133
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 Maybe<ResourceTable::SearchResult> sr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 table.FindResource(test::ParseNameOrDie("android:string/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700136 ASSERT_TRUE(sr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 std::vector<ResourceConfigValue*> values =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700138 sr.value().entry->FindAllValues(test::ParseConfigOrDie("land"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 ASSERT_EQ(2u, values.size());
140 EXPECT_EQ(std::string("phone"), values[0]->product);
141 EXPECT_EQ(std::string("tablet"), values[1]->product);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800142}
143
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144} // namespace aapt