blob: 3db37c2fa6f8398824341f85b3e6c2288fd2c3c5 [file] [log] [blame]
Jeremy Meyer16c83af2024-07-26 16:21:49 -07001/*
2 * Copyright (C) 2022 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 "LoadedApk.h"
18#include "cmd/Dump.h"
19#include "io/StringStream.h"
20#include "test/Test.h"
21#include "text/Printer.h"
22
23using ::aapt::io::StringOutputStream;
24using ::aapt::text::Printer;
25using testing::Eq;
26using testing::Ne;
27
28namespace aapt {
29
30using FlaggedResourcesTest = CommandTestFixture;
31
32static android::NoOpDiagnostics noop_diag;
33
34void DumpStringPoolToString(LoadedApk* loaded_apk, std::string* output) {
35 StringOutputStream output_stream(output);
36 Printer printer(&output_stream);
37
38 DumpStringsCommand command(&printer, &noop_diag);
39 ASSERT_EQ(command.Dump(loaded_apk), 0);
40 output_stream.Flush();
41}
42
Jeremy Meyer423c0f52024-08-02 12:06:54 -070043void DumpResourceTableToString(LoadedApk* loaded_apk, std::string* output) {
44 StringOutputStream output_stream(output);
45 Printer printer(&output_stream);
46
47 DumpTableCommand command(&printer, &noop_diag);
48 ASSERT_EQ(command.Dump(loaded_apk), 0);
49 output_stream.Flush();
50}
51
52void DumpChunksToString(LoadedApk* loaded_apk, std::string* output) {
53 StringOutputStream output_stream(output);
54 Printer printer(&output_stream);
55
56 DumpChunks command(&printer, &noop_diag);
57 ASSERT_EQ(command.Dump(loaded_apk), 0);
58 output_stream.Flush();
59}
60
61TEST_F(FlaggedResourcesTest, DisabledStringRemovedFromPool) {
Jeremy Meyer16c83af2024-07-26 16:21:49 -070062 auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
63 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
64
65 std::string output;
66 DumpStringPoolToString(loaded_apk.get(), &output);
67
68 std::string excluded = "DONTFIND";
69 ASSERT_EQ(output.find(excluded), std::string::npos);
70}
71
Jeremy Meyer423c0f52024-08-02 12:06:54 -070072TEST_F(FlaggedResourcesTest, DisabledResourcesRemovedFromTable) {
73 auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
74 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
75
76 std::string output;
77 DumpResourceTableToString(loaded_apk.get(), &output);
78}
79
80TEST_F(FlaggedResourcesTest, DisabledResourcesRemovedFromTableChunks) {
81 auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
82 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
83
84 std::string output;
85 DumpChunksToString(loaded_apk.get(), &output);
86
Jeremy Meyer74684032024-08-20 14:10:59 -070087 ASSERT_EQ(output.find("bool4"), std::string::npos);
Jeremy Meyer423c0f52024-08-02 12:06:54 -070088 ASSERT_EQ(output.find("str1"), std::string::npos);
89}
90
91TEST_F(FlaggedResourcesTest, DisabledResourcesInRJava) {
92 auto r_path = file::BuildPath({android::base::GetExecutableDirectory(), "resource-flagging-java",
93 "com", "android", "intenal", "flaggedresources", "R.java"});
94 std::string r_contents;
95 ::android::base::ReadFileToString(r_path, &r_contents);
96
Jeremy Meyer74684032024-08-20 14:10:59 -070097 ASSERT_NE(r_contents.find("public static final int bool4"), std::string::npos);
Jeremy Meyer423c0f52024-08-02 12:06:54 -070098 ASSERT_NE(r_contents.find("public static final int str1"), std::string::npos);
99}
100
Jeremy Meyer16c83af2024-07-26 16:21:49 -0700101} // namespace aapt