blob: 67d0c41f6b6fdd9ab878b761b417a83b718cbeed [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);
79}
80
81TEST_F(FlaggedResourcesTest, DisabledResourcesRemovedFromTableChunks) {
82 auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
83 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
84
85 std::string output;
86 DumpChunksToString(loaded_apk.get(), &output);
87
Jeremy Meyer74684032024-08-20 14:10:59 -070088 ASSERT_EQ(output.find("bool4"), std::string::npos);
Jeremy Meyer423c0f52024-08-02 12:06:54 -070089 ASSERT_EQ(output.find("str1"), std::string::npos);
90}
91
92TEST_F(FlaggedResourcesTest, DisabledResourcesInRJava) {
93 auto r_path = file::BuildPath({android::base::GetExecutableDirectory(), "resource-flagging-java",
94 "com", "android", "intenal", "flaggedresources", "R.java"});
95 std::string r_contents;
96 ::android::base::ReadFileToString(r_path, &r_contents);
97
Jeremy Meyer74684032024-08-20 14:10:59 -070098 ASSERT_NE(r_contents.find("public static final int bool4"), std::string::npos);
Jeremy Meyer423c0f52024-08-02 12:06:54 -070099 ASSERT_NE(r_contents.find("public static final int str1"), std::string::npos);
100}
101
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700102TEST_F(FlaggedResourcesTest, TwoValuesSameDisabledFlag) {
103 test::TestDiagnosticsImpl diag;
104 const std::string compiled_files_dir = GetTestPath("compiled");
105 ASSERT_FALSE(CompileFile(
106 GetTestPath("res/values/values.xml"),
107 R"(<resources xmlns:android="http://schemas.android.com/apk/res/android">
108 <bool name="bool1" android:featureFlag="test.package.falseFlag">false</bool>
109 <bool name="bool1" android:featureFlag="test.package.falseFlag">true</bool>
110 </resources>)",
111 compiled_files_dir, &diag,
112 {"--feature-flags", "test.package.falseFlag:ro=false,test.package.trueFlag:ro=true"}));
113 ASSERT_TRUE(diag.GetLog().contains("duplicate value for resource 'bool/bool1'"));
114}
115
116TEST_F(FlaggedResourcesTest, TwoValuesSameDisabledFlagDifferentFiles) {
117 test::TestDiagnosticsImpl diag;
118 const std::string compiled_files_dir = GetTestPath("compiled");
119 ASSERT_TRUE(CompileFile(
120 GetTestPath("res/values/values1.xml"),
121 R"(<resources xmlns:android="http://schemas.android.com/apk/res/android">
122 <bool name="bool1" android:featureFlag="test.package.falseFlag">false</bool>
123 </resources>)",
124 compiled_files_dir, &diag,
125 {"--feature-flags", "test.package.falseFlag:ro=false,test.package.trueFlag:ro=true"}));
126 ASSERT_TRUE(CompileFile(
127 GetTestPath("res/values/values2.xml"),
128 R"(<resources xmlns:android="http://schemas.android.com/apk/res/android">
129 <bool name="bool1" android:featureFlag="test.package.falseFlag">true</bool>
130 </resources>)",
131 compiled_files_dir, &diag,
132 {"--feature-flags", "test.package.falseFlag:ro=false,test.package.trueFlag:ro=true"}));
133 const std::string out_apk = GetTestPath("out.apk");
134 std::vector<std::string> link_args = {
135 "--manifest",
136 GetDefaultManifest(),
137 "-o",
138 out_apk,
139 };
140
141 ASSERT_FALSE(Link(link_args, compiled_files_dir, &diag));
142 ASSERT_TRUE(diag.GetLog().contains("duplicate value for resource 'bool1'"));
143}
144
Jeremy Meyer16c83af2024-07-26 16:21:49 -0700145} // namespace aapt