blob: d018882fad9cf33364077b76458eab8e1061ad95 [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
33class NoopDiagnostics : public IDiagnostics {
34 public:
35 void Log(Level level, DiagMessageActual& actualMsg) override {
36 }
37};
38static NoopDiagnostics noop_diag;
39
40void DumpBadgingToString(LoadedApk* loaded_apk, std::string* output, bool include_meta_data = false,
41 bool only_permissions = false) {
42 StringOutputStream output_stream(output);
43 Printer printer(&output_stream);
44
45 DumpBadgingCommand command(&printer, &noop_diag);
46 command.SetIncludeMetaData(include_meta_data);
47 command.SetOnlyPermissions(only_permissions);
48 ASSERT_EQ(command.Dump(loaded_apk), 0);
49 output_stream.Flush();
50}
51
52TEST_F(DumpTest, DumpBadging) {
53 auto apk_path = file::BuildPath(
54 {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "minimal.apk"});
55 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
56
57 std::string output;
58 DumpBadgingToString(loaded_apk.get(), &output);
59
60 std::string expected;
61 auto expected_path = file::BuildPath({android::base::GetExecutableDirectory(),
62 "integration-tests", "DumpTest", "minimal_expected.txt"});
63 ::android::base::ReadFileToString(expected_path, &expected);
64 ASSERT_EQ(output, expected);
65}
66
67TEST_F(DumpTest, DumpBadgingAllComponents) {
68 auto apk_path = file::BuildPath(
69 {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "components.apk"});
70 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
71
72 std::string output;
73 DumpBadgingToString(loaded_apk.get(), &output, /* include_meta_data= */ true);
74
75 std::string expected;
76 auto expected_path =
77 file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest",
78 "components_expected.txt"});
79 ::android::base::ReadFileToString(expected_path, &expected);
80 ASSERT_EQ(output, expected);
81}
82
83TEST_F(DumpTest, DumpBadgingPermissionsOnly) {
84 auto apk_path = file::BuildPath(
85 {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "components.apk"});
86 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
87
88 std::string output;
89 DumpBadgingToString(loaded_apk.get(), &output, /* include_meta_data= */ false,
90 /* only_permissions= */ true);
91
92 std::string expected;
93 auto expected_path =
94 file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest",
95 "components_permissions_expected.txt"});
96 ::android::base::ReadFileToString(expected_path, &expected);
97 ASSERT_EQ(output, expected);
98}
99
100} // namespace aapt