1f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski/*
2f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * Copyright (C) 2014 The Android Open Source Project
3f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski *
4f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
5f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * you may not use this file except in compliance with the License.
6f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * You may obtain a copy of the License at
7f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski *
8f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
9f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski *
10f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * Unless required by applicable law or agreed to in writing, software
11f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
12f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * See the License for the specific language governing permissions and
14f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * limitations under the License.
15f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski */
16f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
17f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski#include <androidfw/ResourceTypes.h>
18f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
19f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski#include <utils/String8.h>
20f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski#include <utils/String16.h>
21f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski#include "TestHelpers.h"
22ccf25c7bf69eb8c04246e3f79da31b52c2922a80Adam Lesinski#include "data/basic/R.h"
23f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
24f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski#include <gtest/gtest.h>
25f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
26f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinskiusing namespace android;
27f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
28f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinskinamespace {
29f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
30f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski/**
31f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * Include a binary resource table.
32f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski *
33f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * Package: com.android.test.basic
34f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski */
35f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski#include "data/basic/basic_arsc.h"
36f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
37f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski/**
38f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * Include a binary resource table.
39f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * This table is an overlay.
40f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski *
41f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski * Package: com.android.test.basic
42f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski */
43f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski#include "data/overlay/overlay_arsc.h"
44f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
45f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinskienum { MAY_NOT_BE_BAG = false };
46f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
47f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinskiclass IdmapTest : public ::testing::Test {
48f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinskiprotected:
49f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    virtual void SetUp() {
50f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski        ASSERT_EQ(NO_ERROR, mTargetTable.add(basic_arsc, basic_arsc_len));
51f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski        ASSERT_EQ(NO_ERROR, mOverlayTable.add(overlay_arsc, overlay_arsc_len));
52f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski        char targetName[256] = "com.android.test.basic";
53f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski        ASSERT_EQ(NO_ERROR, mTargetTable.createIdmap(mOverlayTable, 0, 0,
54f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski                    targetName, targetName, &mData, &mDataSize));
55f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    }
56f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
57f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    virtual void TearDown() {
58f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski        free(mData);
59f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    }
60f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
61f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ResTable mTargetTable;
62f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ResTable mOverlayTable;
63f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    void* mData;
64f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    size_t mDataSize;
65f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski};
66f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
67f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam LesinskiTEST_F(IdmapTest, canLoadIdmap) {
68f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_EQ(NO_ERROR, mTargetTable.add(overlay_arsc, overlay_arsc_len, mData, mDataSize));
69f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski}
70f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
71f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam LesinskiTEST_F(IdmapTest, overlayOverridesResourceValue) {
72f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    Res_value val;
73ccf25c7bf69eb8c04246e3f79da31b52c2922a80Adam Lesinski    ssize_t block = mTargetTable.getResource(base::R::string::test2, &val, false);
74f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_GE(block, 0);
75f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_EQ(Res_value::TYPE_STRING, val.dataType);
76f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    const ResStringPool* pool = mTargetTable.getTableStringBlock(block);
77f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_TRUE(pool != NULL);
78f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_LT(val.data, pool->size());
79f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
80f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    size_t strLen;
81f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    const char16_t* targetStr16 = pool->stringAt(val.data, &strLen);
82f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_TRUE(targetStr16 != NULL);
83f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_EQ(String16("test2"), String16(targetStr16, strLen));
84f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
85f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_EQ(NO_ERROR, mTargetTable.add(overlay_arsc, overlay_arsc_len, mData, mDataSize));
86f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
87ccf25c7bf69eb8c04246e3f79da31b52c2922a80Adam Lesinski    ssize_t newBlock = mTargetTable.getResource(base::R::string::test2, &val, false);
88f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_GE(newBlock, 0);
89f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_NE(block, newBlock);
90f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_EQ(Res_value::TYPE_STRING, val.dataType);
91f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    pool = mTargetTable.getTableStringBlock(newBlock);
92f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_TRUE(pool != NULL);
93f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_LT(val.data, pool->size());
94f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
95f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    targetStr16 = pool->stringAt(val.data, &strLen);
96f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_TRUE(targetStr16 != NULL);
97f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_EQ(String16("test2-overlay"), String16(targetStr16, strLen));
98f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski}
99f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
100f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam LesinskiTEST_F(IdmapTest, overlaidResourceHasSameName) {
101f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_EQ(NO_ERROR, mTargetTable.add(overlay_arsc, overlay_arsc_len, mData, mDataSize));
102f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
103f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ResTable::resource_name resName;
104ccf25c7bf69eb8c04246e3f79da31b52c2922a80Adam Lesinski    ASSERT_TRUE(mTargetTable.getResourceName(base::R::array::integerArray1, false, &resName));
105f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
106f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_TRUE(resName.package != NULL);
107f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_TRUE(resName.type != NULL);
108f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    ASSERT_TRUE(resName.name != NULL);
109f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
110f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    EXPECT_EQ(String16("com.android.test.basic"), String16(resName.package, resName.packageLen));
111f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    EXPECT_EQ(String16("array"), String16(resName.type, resName.typeLen));
112f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski    EXPECT_EQ(String16("integerArray1"), String16(resName.name, resName.nameLen));
113f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski}
114f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski
115f90f2f8dc36e7243b85e0b6a7fd5a590893c827eAdam Lesinski} // namespace
116