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" |
| 20 | #include "test/Test.h" |
| 21 | #include "text/Printer.h" |
| 22 | |
| 23 | using ::aapt::io::StringOutputStream; |
| 24 | using ::aapt::text::Printer; |
| 25 | using testing::Eq; |
| 26 | using testing::Ne; |
| 27 | |
| 28 | namespace aapt { |
| 29 | |
| 30 | using FlaggedResourcesTest = CommandTestFixture; |
| 31 | |
| 32 | static android::NoOpDiagnostics noop_diag; |
| 33 | |
| 34 | void 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 Meyer | 423c0f5 | 2024-08-02 12:06:54 -0700 | [diff] [blame] | 43 | void 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 | |
| 52 | void 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 | |
| 61 | TEST_F(FlaggedResourcesTest, DisabledStringRemovedFromPool) { |
Jeremy Meyer | 16c83af | 2024-07-26 16:21:49 -0700 | [diff] [blame] | 62 | 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 Meyer | 423c0f5 | 2024-08-02 12:06:54 -0700 | [diff] [blame] | 72 | TEST_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 | |
| 80 | TEST_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 Meyer | 7468403 | 2024-08-20 14:10:59 -0700 | [diff] [blame] | 87 | ASSERT_EQ(output.find("bool4"), std::string::npos); |
Jeremy Meyer | 423c0f5 | 2024-08-02 12:06:54 -0700 | [diff] [blame] | 88 | ASSERT_EQ(output.find("str1"), std::string::npos); |
| 89 | } |
| 90 | |
| 91 | TEST_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 Meyer | 7468403 | 2024-08-20 14:10:59 -0700 | [diff] [blame] | 97 | 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] | 98 | ASSERT_NE(r_contents.find("public static final int str1"), std::string::npos); |
| 99 | } |
| 100 | |
Jeremy Meyer | 16c83af | 2024-07-26 16:21:49 -0700 | [diff] [blame] | 101 | } // namespace aapt |