ResTable_test.cpp revision 833f3ccbc8f4dd1ec8abb9121988b99ff34ec4c1
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/R.h" 23 24#include <gtest/gtest.h> 25 26using namespace android; 27 28namespace { 29 30/** 31 * Include a binary resource table. 32 * 33 * Package: com.android.test.basic 34 */ 35#include "data/basic/basic_arsc.h" 36 37enum { MAY_NOT_BE_BAG = false }; 38 39TEST(ResTableTest, shouldLoadSuccessfully) { 40 ResTable table; 41 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); 42} 43 44TEST(ResTableTest, simpleTypeIsRetrievedCorrectly) { 45 ResTable table; 46 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); 47 48 Res_value val; 49 ssize_t block = table.getResource(R::string::test1, &val, MAY_NOT_BE_BAG); 50 51 ASSERT_GE(block, 0); 52 ASSERT_EQ(Res_value::TYPE_STRING, val.dataType); 53 54 const ResStringPool* pool = table.getTableStringBlock(block); 55 ASSERT_TRUE(NULL != pool); 56 ASSERT_EQ(String8("test1"), pool->string8ObjectAt(val.data)); 57} 58 59TEST(ResTableTest, resourceNameIsResolved) { 60 ResTable table; 61 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); 62 63 String16 defPackage("com.android.test.basic"); 64 String16 testName("@string/test1"); 65 uint32_t resID = table.identifierForName(testName.string(), testName.size(), 66 0, 0, 67 defPackage.string(), defPackage.size()); 68 ASSERT_NE(uint32_t(0x00000000), resID); 69 ASSERT_EQ(R::string::test1, resID); 70} 71 72TEST(ResTableTest, noParentThemeIsAppliedCorrectly) { 73 ResTable table; 74 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); 75 76 ResTable::Theme theme(table); 77 ASSERT_EQ(NO_ERROR, theme.applyStyle(R::style::Theme1)); 78 79 Res_value val; 80 uint32_t specFlags = 0; 81 ssize_t index = theme.getAttribute(R::attr::attr1, &val, &specFlags); 82 ASSERT_GE(index, 0); 83 ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); 84 ASSERT_EQ(uint32_t(100), val.data); 85 86 index = theme.getAttribute(R::attr::attr2, &val, &specFlags); 87 ASSERT_GE(index, 0); 88 ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); 89 ASSERT_EQ(R::integer::number1, val.data); 90} 91 92TEST(ResTableTest, parentThemeIsAppliedCorrectly) { 93 ResTable table; 94 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); 95 96 ResTable::Theme theme(table); 97 ASSERT_EQ(NO_ERROR, theme.applyStyle(R::style::Theme2)); 98 99 Res_value val; 100 uint32_t specFlags = 0; 101 ssize_t index = theme.getAttribute(R::attr::attr1, &val, &specFlags); 102 ASSERT_GE(index, 0); 103 ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); 104 ASSERT_EQ(uint32_t(300), val.data); 105 106 index = theme.getAttribute(R::attr::attr2, &val, &specFlags); 107 ASSERT_GE(index, 0); 108 ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); 109 ASSERT_EQ(R::integer::number1, val.data); 110} 111 112TEST(ResTableTest, referenceToBagIsNotResolved) { 113 ResTable table; 114 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); 115 116 Res_value val; 117 ssize_t block = table.getResource(R::integer::number2, &val, MAY_NOT_BE_BAG); 118 ASSERT_GE(block, 0); 119 ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); 120 ASSERT_EQ(R::array::integerArray1, val.data); 121 122 ssize_t newBlock = table.resolveReference(&val, block); 123 EXPECT_EQ(block, newBlock); 124 EXPECT_EQ(Res_value::TYPE_REFERENCE, val.dataType); 125 EXPECT_EQ(R::array::integerArray1, val.data); 126} 127 128TEST(ResTableTest, resourcesStillAccessibleAfterParameterChange) { 129 ResTable table; 130 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); 131 132 Res_value val; 133 ssize_t block = table.getResource(R::integer::number1, &val, MAY_NOT_BE_BAG); 134 ASSERT_GE(block, 0); 135 ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); 136 137 const ResTable::bag_entry* entry; 138 ssize_t count = table.lockBag(R::array::integerArray1, &entry); 139 ASSERT_GE(count, 0); 140 table.unlockBag(entry); 141 142 ResTable_config param; 143 memset(¶m, 0, sizeof(param)); 144 param.density = 320; 145 table.setParameters(¶m); 146 147 block = table.getResource(R::integer::number1, &val, MAY_NOT_BE_BAG); 148 ASSERT_GE(block, 0); 149 ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); 150 151 count = table.lockBag(R::array::integerArray1, &entry); 152 ASSERT_GE(count, 0); 153 table.unlockBag(entry); 154} 155 156TEST(ResTableTest, resourceIsOverridenWithBetterConfig) { 157 ResTable table; 158 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); 159 160 Res_value val; 161 ssize_t block = table.getResource(R::integer::number1, &val, MAY_NOT_BE_BAG); 162 ASSERT_GE(block, 0); 163 ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); 164 ASSERT_EQ(uint32_t(200), val.data); 165 166 ResTable_config param; 167 memset(¶m, 0, sizeof(param)); 168 param.language[0] = 's'; 169 param.language[1] = 'v'; 170 param.country[0] = 'S'; 171 param.country[1] = 'E'; 172 table.setParameters(¶m); 173 174 block = table.getResource(R::integer::number1, &val, MAY_NOT_BE_BAG); 175 ASSERT_GE(block, 0); 176 ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); 177 ASSERT_EQ(uint32_t(400), val.data); 178} 179 180} 181