blob: b1c69cd9a7b7280f672eee7f575f51281dc6d64b [file] [log] [blame]
Iurii Makhnodcead622022-04-13 18:00:03 +00001/*
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 "Dump.h"
18
19#include "LoadedApk.h"
20#include "io/StringStream.h"
21#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 DumpTest = CommandTestFixture;
32
Jeremy Meyer56f36e82022-05-20 20:35:42 +000033static android::NoOpDiagnostics noop_diag;
Iurii Makhnodcead622022-04-13 18:00:03 +000034
35void DumpBadgingToString(LoadedApk* loaded_apk, std::string* output, bool include_meta_data = false,
36 bool only_permissions = false) {
37 StringOutputStream output_stream(output);
38 Printer printer(&output_stream);
39
40 DumpBadgingCommand command(&printer, &noop_diag);
41 command.SetIncludeMetaData(include_meta_data);
42 command.SetOnlyPermissions(only_permissions);
43 ASSERT_EQ(command.Dump(loaded_apk), 0);
44 output_stream.Flush();
45}
46
47TEST_F(DumpTest, DumpBadging) {
48 auto apk_path = file::BuildPath(
49 {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "minimal.apk"});
50 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
51
52 std::string output;
53 DumpBadgingToString(loaded_apk.get(), &output);
54
55 std::string expected;
56 auto expected_path = file::BuildPath({android::base::GetExecutableDirectory(),
57 "integration-tests", "DumpTest", "minimal_expected.txt"});
58 ::android::base::ReadFileToString(expected_path, &expected);
59 ASSERT_EQ(output, expected);
60}
61
Iurii Makhno05fd9292022-08-24 18:56:12 +000062TEST_F(DumpTest, DumpBadgingMultipleUsesSdkTakesLatest) {
63 auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests",
64 "DumpTest", "multiple_uses_sdk.apk"});
65 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
66
67 std::string output;
68 DumpBadgingToString(loaded_apk.get(), &output);
69
70 std::string expected;
71 auto expected_path =
72 file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest",
73 "multiple_uses_sdk_expected.txt"});
74 ::android::base::ReadFileToString(expected_path, &expected);
75 ASSERT_EQ(output, expected);
76}
77
Iurii Makhnodcead622022-04-13 18:00:03 +000078TEST_F(DumpTest, DumpBadgingAllComponents) {
79 auto apk_path = file::BuildPath(
80 {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "components.apk"});
81 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
82
83 std::string output;
84 DumpBadgingToString(loaded_apk.get(), &output, /* include_meta_data= */ true);
85
86 std::string expected;
87 auto expected_path =
88 file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest",
89 "components_expected.txt"});
90 ::android::base::ReadFileToString(expected_path, &expected);
91 ASSERT_EQ(output, expected);
92}
93
94TEST_F(DumpTest, DumpBadgingPermissionsOnly) {
95 auto apk_path = file::BuildPath(
96 {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "components.apk"});
97 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
98
99 std::string output;
100 DumpBadgingToString(loaded_apk.get(), &output, /* include_meta_data= */ false,
101 /* only_permissions= */ true);
102
103 std::string expected;
104 auto expected_path =
105 file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest",
106 "components_permissions_expected.txt"});
107 ::android::base::ReadFileToString(expected_path, &expected);
108 ASSERT_EQ(output, expected);
109}
110
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000111} // namespace aapt