Split_test.cpp revision f90f2f8dc36e7243b85e0b6a7fd5a590893c827e
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
23#include <gtest/gtest.h>
24
25/**
26 * Include a binary resource table. This table
27 * is a base table for an APK split.
28 *
29 * Package: com.android.example.split
30 *
31 * layout/main          0x7f020000 {default, fr-sw600dp-v13}
32 *
33 * string/app_title     0x7f030000 {default}
34 * string/test          0x7f030001 {default}
35 * string/boom          0x7f030002 {default}
36 * string/blah          0x7f030003 {default}
37 *
38 * array/lotsofstrings  0x7f040000 {default}
39 * array/numList        0x7f040001 {default}
40 * array/ary            0x7f040002 {default}
41 *
42 */
43#include "data/split_base_arsc.h"
44
45/**
46 * Include a binary resource table. This table
47 * is a configuration split table for an APK split.
48 *
49 * Package: com.android.example.split
50 *
51 * string/app_title     0x7f030000 {fr}
52 * string/test          0x7f030001 {de,fr}
53 * string/blah          0x7f030003 {fr}
54 *
55 * array/lotsofstrings  0x7f040000 {fr}
56 *
57 */
58#include "data/split_de_fr_arsc.h"
59
60
61using namespace android;
62
63enum { MAY_NOT_BE_BAG = false };
64
65void makeConfigFrench(ResTable_config* config) {
66    memset(config, 0, sizeof(*config));
67    config->language[0] = 'f';
68    config->language[1] = 'r';
69}
70
71TEST(SplitTest, TestLoadBase) {
72    ResTable table;
73    ASSERT_EQ(NO_ERROR, table.add(split_base_arsc, split_base_arsc_len));
74}
75
76TEST(SplitTest, TestGetResourceFromBase) {
77    ResTable_config frenchConfig;
78    makeConfigFrench(&frenchConfig);
79
80    ResTable table;
81    table.setParameters(&frenchConfig);
82
83    ASSERT_EQ(NO_ERROR, table.add(split_base_arsc, split_base_arsc_len));
84
85    ResTable_config expectedConfig;
86    memset(&expectedConfig, 0, sizeof(expectedConfig));
87
88    Res_value val;
89    ResTable_config config;
90    ssize_t block = table.getResource(0x7f030000, &val, MAY_NOT_BE_BAG, 0, NULL, &config);
91
92    // The returned block should tell us which string pool to get the value, if it is a string.
93    EXPECT_GE(block, 0);
94
95    // We expect the default resource to be selected since it is the only resource configuration.
96    EXPECT_EQ(0, expectedConfig.compare(config));
97
98    EXPECT_EQ(Res_value::TYPE_STRING, val.dataType);
99}
100
101TEST(SplitTest, TestGetResourceFromSplit) {
102    ResTable_config expectedConfig;
103    makeConfigFrench(&expectedConfig);
104
105    ResTable table;
106    table.setParameters(&expectedConfig);
107
108    ASSERT_EQ(NO_ERROR, table.add(split_base_arsc, split_base_arsc_len));
109    ASSERT_EQ(NO_ERROR, table.add(split_de_fr_arsc, split_de_fr_arsc_len));
110
111    Res_value val;
112    ResTable_config config;
113    ssize_t block = table.getResource(0x7f030000, &val, MAY_NOT_BE_BAG, 0, NULL, &config);
114
115    EXPECT_GE(block, 0);
116
117    EXPECT_EQ(0, expectedConfig.compare(config));
118
119    EXPECT_EQ(Res_value::TYPE_STRING, val.dataType);
120}
121
122TEST(SplitTest, ResourcesFromBaseAndSplitHaveSameNames) {
123    ResTable_config expectedConfig;
124    makeConfigFrench(&expectedConfig);
125
126    ResTable table;
127    table.setParameters(&expectedConfig);
128
129    ASSERT_EQ(NO_ERROR, table.add(split_base_arsc, split_base_arsc_len));
130
131    ResTable::resource_name baseName;
132    EXPECT_TRUE(table.getResourceName(0x7f030003, false, &baseName));
133
134    ASSERT_EQ(NO_ERROR, table.add(split_de_fr_arsc, split_de_fr_arsc_len));
135
136    ResTable::resource_name frName;
137    EXPECT_TRUE(table.getResourceName(0x7f030003, false, &frName));
138
139    EXPECT_EQ(
140            String16(baseName.package, baseName.packageLen),
141            String16(frName.package, frName.packageLen));
142
143    EXPECT_EQ(
144            String16(baseName.type, baseName.typeLen),
145            String16(frName.type, frName.typeLen));
146
147    EXPECT_EQ(
148            String16(baseName.name, baseName.nameLen),
149            String16(frName.name, frName.nameLen));
150}
151
152TEST(SplitTest, TypeEntrySpecFlagsAreUpdated) {
153    ResTable_config defaultConfig;
154    memset(&defaultConfig, 0, sizeof(defaultConfig));
155
156    ResTable table;
157    ASSERT_EQ(NO_ERROR, table.add(split_base_arsc, split_base_arsc_len));
158
159    Res_value val;
160    uint32_t specFlags = 0;
161    ssize_t block = table.getResource(0x7f030000, &val, MAY_NOT_BE_BAG, 0, &specFlags, NULL);
162    EXPECT_GE(block, 0);
163
164    EXPECT_EQ(static_cast<uint32_t>(0), specFlags);
165
166    ASSERT_EQ(NO_ERROR, table.add(split_de_fr_arsc, split_de_fr_arsc_len));
167
168    uint32_t frSpecFlags = 0;
169    block = table.getResource(0x7f030000, &val, MAY_NOT_BE_BAG, 0, &frSpecFlags, NULL);
170    EXPECT_GE(block, 0);
171
172    EXPECT_EQ(ResTable_config::CONFIG_LOCALE, frSpecFlags);
173}
174