1 /*
2 * Copyright (C) 2014 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 <androidfw/ResourceTypes.h>
18
19 #include <utils/String8.h>
20 #include <utils/String16.h>
21 #include "TestHelpers.h"
22 #include "data/basic/R.h"
23
24 #include <gtest/gtest.h>
25
26 using namespace android;
27
28 namespace {
29
30 /**
31 * Include a binary resource table.
32 *
33 * Package: com.android.test.basic
34 */
35 #include "data/basic/basic_arsc.h"
36
37 /**
38 * Include a binary resource table.
39 * This table is an overlay.
40 *
41 * Package: com.android.test.basic
42 */
43 #include "data/overlay/overlay_arsc.h"
44
45 enum { MAY_NOT_BE_BAG = false };
46
47 class IdmapTest : public ::testing::Test {
48 protected:
SetUp()49 virtual void SetUp() {
50 ASSERT_EQ(NO_ERROR, mTargetTable.add(basic_arsc, basic_arsc_len));
51 ASSERT_EQ(NO_ERROR, mOverlayTable.add(overlay_arsc, overlay_arsc_len));
52 char targetName[256] = "com.android.test.basic";
53 ASSERT_EQ(NO_ERROR, mTargetTable.createIdmap(mOverlayTable, 0, 0,
54 targetName, targetName, &mData, &mDataSize));
55 }
56
TearDown()57 virtual void TearDown() {
58 free(mData);
59 }
60
61 ResTable mTargetTable;
62 ResTable mOverlayTable;
63 void* mData;
64 size_t mDataSize;
65 };
66
TEST_F(IdmapTest,canLoadIdmap)67 TEST_F(IdmapTest, canLoadIdmap) {
68 ASSERT_EQ(NO_ERROR, mTargetTable.add(overlay_arsc, overlay_arsc_len, mData, mDataSize));
69 }
70
TEST_F(IdmapTest,overlayOverridesResourceValue)71 TEST_F(IdmapTest, overlayOverridesResourceValue) {
72 Res_value val;
73 ssize_t block = mTargetTable.getResource(base::R::string::test2, &val, false);
74 ASSERT_GE(block, 0);
75 ASSERT_EQ(Res_value::TYPE_STRING, val.dataType);
76 const ResStringPool* pool = mTargetTable.getTableStringBlock(block);
77 ASSERT_TRUE(pool != NULL);
78 ASSERT_LT(val.data, pool->size());
79
80 size_t strLen;
81 const char16_t* targetStr16 = pool->stringAt(val.data, &strLen);
82 ASSERT_TRUE(targetStr16 != NULL);
83 ASSERT_EQ(String16("test2"), String16(targetStr16, strLen));
84
85 ASSERT_EQ(NO_ERROR, mTargetTable.add(overlay_arsc, overlay_arsc_len, mData, mDataSize));
86
87 ssize_t newBlock = mTargetTable.getResource(base::R::string::test2, &val, false);
88 ASSERT_GE(newBlock, 0);
89 ASSERT_NE(block, newBlock);
90 ASSERT_EQ(Res_value::TYPE_STRING, val.dataType);
91 pool = mTargetTable.getTableStringBlock(newBlock);
92 ASSERT_TRUE(pool != NULL);
93 ASSERT_LT(val.data, pool->size());
94
95 targetStr16 = pool->stringAt(val.data, &strLen);
96 ASSERT_TRUE(targetStr16 != NULL);
97 ASSERT_EQ(String16("test2-overlay"), String16(targetStr16, strLen));
98 }
99
TEST_F(IdmapTest,overlaidResourceHasSameName)100 TEST_F(IdmapTest, overlaidResourceHasSameName) {
101 ASSERT_EQ(NO_ERROR, mTargetTable.add(overlay_arsc, overlay_arsc_len, mData, mDataSize));
102
103 ResTable::resource_name resName;
104 ASSERT_TRUE(mTargetTable.getResourceName(base::R::array::integerArray1, false, &resName));
105
106 ASSERT_TRUE(resName.package != NULL);
107 ASSERT_TRUE(resName.type != NULL);
108 ASSERT_TRUE(resName.name != NULL);
109
110 EXPECT_EQ(String16("com.android.test.basic"), String16(resName.package, resName.packageLen));
111 EXPECT_EQ(String16("array"), String16(resName.type, resName.typeLen));
112 EXPECT_EQ(String16("integerArray1"), String16(resName.name, resName.nameLen));
113 }
114
115 } // namespace
116