LoadedArsc_test.cpp revision 7ad1110ecd6a840fcd2895c62668828a1ca029c6
1/*
2 * Copyright (C) 2016 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/LoadedArsc.h"
18
19#include "android-base/file.h"
20#include "android-base/logging.h"
21#include "android-base/macros.h"
22
23#include "TestHelpers.h"
24#include "data/basic/R.h"
25#include "data/styles/R.h"
26
27namespace app = com::android::app;
28namespace basic = com::android::basic;
29
30namespace android {
31
32TEST(LoadedArscTest, LoadSinglePackageArsc) {
33  base::ScopedLogSeverity _log(base::LogSeverity::DEBUG);
34  std::string contents;
35  ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/styles/styles.apk", "resources.arsc",
36                                      &contents));
37
38  std::unique_ptr<LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(), contents.size());
39  ASSERT_NE(nullptr, loaded_arsc);
40
41  ResTable_config config;
42  memset(&config, 0, sizeof(config));
43  config.sdkVersion = 24;
44
45  LoadedArsc::Entry entry;
46  ResTable_config selected_config;
47  uint32_t flags;
48
49  ASSERT_TRUE(
50      loaded_arsc->FindEntry(app::R::string::string_one, config, &entry, &selected_config, &flags));
51  ASSERT_NE(nullptr, entry.entry);
52}
53
54TEST(LoadedArscTest, FindDefaultEntry) {
55  base::ScopedLogSeverity _log(base::LogSeverity::DEBUG);
56  std::string contents;
57  ASSERT_TRUE(
58      ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", "resources.arsc", &contents));
59
60  std::unique_ptr<LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(), contents.size());
61  ASSERT_NE(nullptr, loaded_arsc);
62
63  ResTable_config desired_config;
64  memset(&desired_config, 0, sizeof(desired_config));
65  desired_config.language[0] = 'd';
66  desired_config.language[1] = 'e';
67
68  LoadedArsc::Entry entry;
69  ResTable_config selected_config;
70  uint32_t flags;
71
72  ASSERT_TRUE(loaded_arsc->FindEntry(basic::R::string::test1, desired_config, &entry,
73                                     &selected_config, &flags));
74  ASSERT_NE(nullptr, entry.entry);
75}
76
77// structs with size fields (like Res_value, ResTable_entry) should be
78// backwards and forwards compatible (aka checking the size field against
79// sizeof(Res_value) might not be backwards compatible.
80TEST(LoadedArscTest, LoadingShouldBeForwardsAndBackwardsCompatible) { ASSERT_TRUE(false); }
81
82}  // namespace android
83