blob: 629300838bbe578dc16095801847da5980393041 [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"
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -070020#include "test/Common.h"
Jeremy Meyer16c83af2024-07-26 16:21:49 -070021#include "test/Test.h"
22#include "text/Printer.h"
23
24using ::aapt::io::StringOutputStream;
25using ::aapt::text::Printer;
26using testing::Eq;
27using testing::Ne;
28
29namespace aapt {
30
31using FlaggedResourcesTest = CommandTestFixture;
32
33static android::NoOpDiagnostics noop_diag;
34
35void DumpStringPoolToString(LoadedApk* loaded_apk, std::string* output) {
36 StringOutputStream output_stream(output);
37 Printer printer(&output_stream);
38
39 DumpStringsCommand command(&printer, &noop_diag);
40 ASSERT_EQ(command.Dump(loaded_apk), 0);
41 output_stream.Flush();
42}
43
Jeremy Meyer423c0f52024-08-02 12:06:54 -070044void DumpResourceTableToString(LoadedApk* loaded_apk, std::string* output) {
45 StringOutputStream output_stream(output);
46 Printer printer(&output_stream);
47
48 DumpTableCommand command(&printer, &noop_diag);
49 ASSERT_EQ(command.Dump(loaded_apk), 0);
50 output_stream.Flush();
51}
52
53void DumpChunksToString(LoadedApk* loaded_apk, std::string* output) {
54 StringOutputStream output_stream(output);
55 Printer printer(&output_stream);
56
57 DumpChunks command(&printer, &noop_diag);
58 ASSERT_EQ(command.Dump(loaded_apk), 0);
59 output_stream.Flush();
60}
61
62TEST_F(FlaggedResourcesTest, DisabledStringRemovedFromPool) {
Jeremy Meyer16c83af2024-07-26 16:21:49 -070063 auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
64 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
65
66 std::string output;
67 DumpStringPoolToString(loaded_apk.get(), &output);
68
69 std::string excluded = "DONTFIND";
70 ASSERT_EQ(output.find(excluded), std::string::npos);
71}
72
Jeremy Meyer423c0f52024-08-02 12:06:54 -070073TEST_F(FlaggedResourcesTest, DisabledResourcesRemovedFromTable) {
74 auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
75 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
76
77 std::string output;
78 DumpResourceTableToString(loaded_apk.get(), &output);
Jeremy Meyere08e0372024-09-17 12:45:26 -070079 ASSERT_EQ(output.find("bool4"), std::string::npos);
80 ASSERT_EQ(output.find("str1"), std::string::npos);
81 ASSERT_EQ(output.find("layout2"), std::string::npos);
82 ASSERT_EQ(output.find("removedpng"), std::string::npos);
Jeremy Meyer423c0f52024-08-02 12:06:54 -070083}
84
85TEST_F(FlaggedResourcesTest, DisabledResourcesRemovedFromTableChunks) {
86 auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
87 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
88
89 std::string output;
90 DumpChunksToString(loaded_apk.get(), &output);
91
Jeremy Meyer74684032024-08-20 14:10:59 -070092 ASSERT_EQ(output.find("bool4"), std::string::npos);
Jeremy Meyer423c0f52024-08-02 12:06:54 -070093 ASSERT_EQ(output.find("str1"), std::string::npos);
Jeremy Meyere08e0372024-09-17 12:45:26 -070094 ASSERT_EQ(output.find("layout2"), std::string::npos);
95 ASSERT_EQ(output.find("removedpng"), std::string::npos);
Jeremy Meyer423c0f52024-08-02 12:06:54 -070096}
97
98TEST_F(FlaggedResourcesTest, DisabledResourcesInRJava) {
99 auto r_path = file::BuildPath({android::base::GetExecutableDirectory(), "resource-flagging-java",
100 "com", "android", "intenal", "flaggedresources", "R.java"});
101 std::string r_contents;
102 ::android::base::ReadFileToString(r_path, &r_contents);
103
Jeremy Meyer74684032024-08-20 14:10:59 -0700104 ASSERT_NE(r_contents.find("public static final int bool4"), std::string::npos);
Jeremy Meyer423c0f52024-08-02 12:06:54 -0700105 ASSERT_NE(r_contents.find("public static final int str1"), std::string::npos);
106}
107
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700108TEST_F(FlaggedResourcesTest, TwoValuesSameDisabledFlag) {
109 test::TestDiagnosticsImpl diag;
110 const std::string compiled_files_dir = GetTestPath("compiled");
111 ASSERT_FALSE(CompileFile(
112 GetTestPath("res/values/values.xml"),
113 R"(<resources xmlns:android="http://schemas.android.com/apk/res/android">
114 <bool name="bool1" android:featureFlag="test.package.falseFlag">false</bool>
115 <bool name="bool1" android:featureFlag="test.package.falseFlag">true</bool>
116 </resources>)",
117 compiled_files_dir, &diag,
118 {"--feature-flags", "test.package.falseFlag:ro=false,test.package.trueFlag:ro=true"}));
119 ASSERT_TRUE(diag.GetLog().contains("duplicate value for resource 'bool/bool1'"));
120}
121
122TEST_F(FlaggedResourcesTest, TwoValuesSameDisabledFlagDifferentFiles) {
123 test::TestDiagnosticsImpl diag;
124 const std::string compiled_files_dir = GetTestPath("compiled");
125 ASSERT_TRUE(CompileFile(
126 GetTestPath("res/values/values1.xml"),
127 R"(<resources xmlns:android="http://schemas.android.com/apk/res/android">
128 <bool name="bool1" android:featureFlag="test.package.falseFlag">false</bool>
129 </resources>)",
130 compiled_files_dir, &diag,
131 {"--feature-flags", "test.package.falseFlag:ro=false,test.package.trueFlag:ro=true"}));
132 ASSERT_TRUE(CompileFile(
133 GetTestPath("res/values/values2.xml"),
134 R"(<resources xmlns:android="http://schemas.android.com/apk/res/android">
135 <bool name="bool1" android:featureFlag="test.package.falseFlag">true</bool>
136 </resources>)",
137 compiled_files_dir, &diag,
138 {"--feature-flags", "test.package.falseFlag:ro=false,test.package.trueFlag:ro=true"}));
139 const std::string out_apk = GetTestPath("out.apk");
140 std::vector<std::string> link_args = {
141 "--manifest",
142 GetDefaultManifest(),
143 "-o",
144 out_apk,
145 };
146
147 ASSERT_FALSE(Link(link_args, compiled_files_dir, &diag));
148 ASSERT_TRUE(diag.GetLog().contains("duplicate value for resource 'bool1'"));
149}
150
Jeremy Meyer16c83af2024-07-26 16:21:49 -0700151} // namespace aapt