blob: 42508fe154b8db6d27f8a991081bb46591071d02 [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
Adam Lesinski1ab598f2015-08-14 14:26:04 -070017#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018#include "ResourceTable.h"
19#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include "util/Util.h"
21
Adam Lesinskie78fd612015-10-22 12:48:43 -070022#include "test/Builders.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023
24#include <algorithm>
25#include <gtest/gtest.h>
26#include <ostream>
27#include <string>
28
29namespace aapt {
30
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031struct ResourceTableTest : public ::testing::Test {
32 struct EmptyDiagnostics : public IDiagnostics {
33 void error(const DiagMessage& msg) override {}
34 void warn(const DiagMessage& msg) override {}
35 void note(const DiagMessage& msg) override {}
36 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038 EmptyDiagnostics mDiagnostics;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039};
40
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041TEST_F(ResourceTableTest, FailToAddResourceWithBadName) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
44 EXPECT_FALSE(table.addResource(
Adam Lesinskie78fd612015-10-22 12:48:43 -070045 ResourceNameRef(u"android", ResourceType::kId, u"hey,there"),
46 ConfigDescription{},
47 test::ValueBuilder<Id>().setSource("test.xml", 21u).build(),
48 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
50 EXPECT_FALSE(table.addResource(
Adam Lesinskie78fd612015-10-22 12:48:43 -070051 ResourceNameRef(u"android", ResourceType::kId, u"hey:there"),
52 ConfigDescription{},
53 test::ValueBuilder<Id>().setSource("test.xml", 21u).build(),
54 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055}
56
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057TEST_F(ResourceTableTest, AddOneResource) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
Adam Lesinskie78fd612015-10-22 12:48:43 -070060 EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/id"),
61 ConfigDescription{},
62 test::ValueBuilder<Id>()
63 .setSource("test/path/file.xml", 23u).build(),
64 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067}
68
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069TEST_F(ResourceTableTest, AddMultipleResources) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
72 ConfigDescription config;
73 ConfigDescription languageConfig;
74 memcpy(languageConfig.language, "pl", sizeof(languageConfig.language));
75
76 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077 test::parseNameOrDie(u"@android:attr/layout_width"),
Adam Lesinskie78fd612015-10-22 12:48:43 -070078 config,
79 test::ValueBuilder<Id>().setSource("test/path/file.xml", 10u).build(),
80 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
82 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083 test::parseNameOrDie(u"@android:attr/id"),
Adam Lesinskie78fd612015-10-22 12:48:43 -070084 config,
85 test::ValueBuilder<Id>().setSource("test/path/file.xml", 12u).build(),
86 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
88 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089 test::parseNameOrDie(u"@android:string/ok"),
Adam Lesinskie78fd612015-10-22 12:48:43 -070090 config,
91 test::ValueBuilder<Id>().setSource("test/path/file.xml", 14u).build(),
92 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
94 EXPECT_TRUE(table.addResource(
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095 test::parseNameOrDie(u"@android:string/ok"),
Adam Lesinskie78fd612015-10-22 12:48:43 -070096 languageConfig,
97 test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
98 .setSource("test/path/file.xml", 20u)
99 .build(),
100 &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/layout_width"));
103 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id"));
104 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:string/ok"));
105 ASSERT_NE(nullptr, test::getValueForConfig<BinaryPrimitive>(&table, u"@android:string/ok",
106 languageConfig));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800107}
108
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109TEST_F(ResourceTableTest, OverrideWeakResourceValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800110 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111
Adam Lesinskie78fd612015-10-22 12:48:43 -0700112 ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{},
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113 util::make_unique<Attribute>(true), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700115 Attribute* attr = test::getValue<Attribute>(&table, u"@android:attr/foo");
116 ASSERT_NE(nullptr, attr);
117 EXPECT_TRUE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118
Adam Lesinskie78fd612015-10-22 12:48:43 -0700119 ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{},
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120 util::make_unique<Attribute>(false), &mDiagnostics));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122 attr = test::getValue<Attribute>(&table, u"@android:attr/foo");
123 ASSERT_NE(nullptr, attr);
124 EXPECT_FALSE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125}
126
127} // namespace aapt