Jeremy Meyer | 16c83af | 2024-07-26 16:21:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Meyer | 3d8d4a1 | 2024-08-23 17:29:03 -0700 | [diff] [blame] | 20 | #include "test/Common.h" |
Jeremy Meyer | 16c83af | 2024-07-26 16:21:49 -0700 | [diff] [blame] | 21 | #include "test/Test.h" |
| 22 | #include "text/Printer.h" |
| 23 | |
| 24 | using ::aapt::io::StringOutputStream; |
| 25 | using ::aapt::text::Printer; |
| 26 | using testing::Eq; |
| 27 | using testing::Ne; |
| 28 | |
| 29 | namespace aapt { |
| 30 | |
| 31 | using FlaggedResourcesTest = CommandTestFixture; |
| 32 | |
| 33 | static android::NoOpDiagnostics noop_diag; |
| 34 | |
| 35 | void 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 Meyer | 423c0f5 | 2024-08-02 12:06:54 -0700 | [diff] [blame] | 44 | void 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 | |
| 53 | void 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 | |
| 62 | TEST_F(FlaggedResourcesTest, DisabledStringRemovedFromPool) { |
Jeremy Meyer | 16c83af | 2024-07-26 16:21:49 -0700 | [diff] [blame] | 63 | 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 Meyer | 423c0f5 | 2024-08-02 12:06:54 -0700 | [diff] [blame] | 73 | TEST_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 Meyer | e08e037 | 2024-09-17 12:45:26 -0700 | [diff] [blame] | 79 | 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 Meyer | 423c0f5 | 2024-08-02 12:06:54 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST_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 Meyer | 7468403 | 2024-08-20 14:10:59 -0700 | [diff] [blame] | 92 | ASSERT_EQ(output.find("bool4"), std::string::npos); |
Jeremy Meyer | 423c0f5 | 2024-08-02 12:06:54 -0700 | [diff] [blame] | 93 | ASSERT_EQ(output.find("str1"), std::string::npos); |
Jeremy Meyer | e08e037 | 2024-09-17 12:45:26 -0700 | [diff] [blame] | 94 | ASSERT_EQ(output.find("layout2"), std::string::npos); |
| 95 | ASSERT_EQ(output.find("removedpng"), std::string::npos); |
Jeremy Meyer | 423c0f5 | 2024-08-02 12:06:54 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | TEST_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 Meyer | 7468403 | 2024-08-20 14:10:59 -0700 | [diff] [blame] | 104 | ASSERT_NE(r_contents.find("public static final int bool4"), std::string::npos); |
Jeremy Meyer | 423c0f5 | 2024-08-02 12:06:54 -0700 | [diff] [blame] | 105 | ASSERT_NE(r_contents.find("public static final int str1"), std::string::npos); |
| 106 | } |
| 107 | |
Jeremy Meyer | 3d8d4a1 | 2024-08-23 17:29:03 -0700 | [diff] [blame] | 108 | TEST_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 | |
| 122 | TEST_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 Meyer | 16c83af | 2024-07-26 16:21:49 -0700 | [diff] [blame] | 151 | } // namespace aapt |