1 /*
2 * Copyright 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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <filesystem>
20 #include <fstream>
21 #include <iostream>
22 #include <string>
23
24 #include <LayerTraceGenerator.h>
25 #include <Tracing/TransactionProtoParser.h>
26 #include <layerproto/LayerProtoHeader.h>
27 #include <log/log.h>
28
29 using namespace android::surfaceflinger;
30
31 namespace android {
32
33 class TransactionTraceTestSuite : public testing::Test,
34 public testing::WithParamInterface<std::filesystem::path> {
35 public:
36 static std::vector<std::filesystem::path> sTransactionTraces;
37 static constexpr std::string_view sTransactionTracePrefix = "transactions_trace_";
38 static constexpr std::string_view sLayersTracePrefix = "layers_trace_";
39 static constexpr std::string_view sTracePostfix = ".winscope";
40
41 proto::TransactionTraceFile mTransactionTrace;
42 LayersTraceFileProto mExpectedLayersTraceProto;
43 LayersTraceFileProto mActualLayersTraceProto;
44
45 protected:
SetUp()46 void SetUp() override {
47 std::filesystem::path transactionTracePath = GetParam();
48 parseTransactionTraceFromFile(transactionTracePath.c_str(), mTransactionTrace);
49
50 std::string expectedLayersFilename = std::string(sLayersTracePrefix) +
51 transactionTracePath.filename().string().substr(sTransactionTracePrefix.length());
52 std::string expectedLayersTracePath =
53 transactionTracePath.parent_path().string() + "/" + expectedLayersFilename;
54 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(expectedLayersTracePath)));
55 parseLayersTraceFromFile(expectedLayersTracePath.c_str(), mExpectedLayersTraceProto);
56 TemporaryDir temp_dir;
57 std::string actualLayersTracePath =
58 std::string(temp_dir.path) + "/" + expectedLayersFilename + "_actual";
59
60 EXPECT_TRUE(
61 LayerTraceGenerator().generate(mTransactionTrace, actualLayersTracePath.c_str()))
62 << "Failed to generate layers trace from " << transactionTracePath;
63 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(actualLayersTracePath)));
64 parseLayersTraceFromFile(actualLayersTracePath.c_str(), mActualLayersTraceProto);
65 }
66
parseTransactionTraceFromFile(const char * transactionTracePath,proto::TransactionTraceFile & outProto)67 void parseTransactionTraceFromFile(const char* transactionTracePath,
68 proto::TransactionTraceFile& outProto) {
69 ALOGD("Parsing file %s...", transactionTracePath);
70 std::fstream input(transactionTracePath, std::ios::in | std::ios::binary);
71 EXPECT_TRUE(input) << "Error could not open " << transactionTracePath;
72 EXPECT_TRUE(outProto.ParseFromIstream(&input))
73 << "Failed to parse " << transactionTracePath;
74 }
75
parseLayersTraceFromFile(const char * layersTracePath,LayersTraceFileProto & outProto)76 void parseLayersTraceFromFile(const char* layersTracePath, LayersTraceFileProto& outProto) {
77 ALOGD("Parsing file %s...", layersTracePath);
78 std::fstream input(layersTracePath, std::ios::in | std::ios::binary);
79 EXPECT_TRUE(input) << "Error could not open " << layersTracePath;
80 EXPECT_TRUE(outProto.ParseFromIstream(&input)) << "Failed to parse " << layersTracePath;
81 }
82 };
83
84 std::vector<std::filesystem::path> TransactionTraceTestSuite::sTransactionTraces{};
85
TEST_P(TransactionTraceTestSuite,validateEndState)86 TEST_P(TransactionTraceTestSuite, validateEndState) {
87 ASSERT_GT(mActualLayersTraceProto.entry_size(), 0);
88 ASSERT_GT(mExpectedLayersTraceProto.entry_size(), 0);
89
90 auto expectedLastEntry =
91 mExpectedLayersTraceProto.entry(mExpectedLayersTraceProto.entry_size() - 1);
92 auto actualLastEntry = mActualLayersTraceProto.entry(mActualLayersTraceProto.entry_size() - 1);
93
94 EXPECT_EQ(expectedLastEntry.layers().layers_size(), actualLastEntry.layers().layers_size());
95 for (int i = 0;
96 i < expectedLastEntry.layers().layers_size() && i < actualLastEntry.layers().layers_size();
97 i++) {
98 auto expectedLayer = expectedLastEntry.layers().layers(i);
99 auto actualLayer = actualLastEntry.layers().layers(i);
100 EXPECT_EQ(expectedLayer.id(), actualLayer.id());
101 EXPECT_EQ(expectedLayer.name(), actualLayer.name());
102 EXPECT_EQ(expectedLayer.parent(), actualLayer.parent());
103 EXPECT_EQ(expectedLayer.z(), actualLayer.z());
104 EXPECT_EQ(expectedLayer.curr_frame(), actualLayer.curr_frame());
105 ALOGV("Validating %s[%d] parent=%d z=%d frame=%" PRIu64, expectedLayer.name().c_str(),
106 expectedLayer.id(), expectedLayer.parent(), expectedLayer.z(),
107 expectedLayer.curr_frame());
108 }
109 }
110
PrintToStringParamName(const::testing::TestParamInfo<std::filesystem::path> & info)111 std::string PrintToStringParamName(const ::testing::TestParamInfo<std::filesystem::path>& info) {
112 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
113 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
114
115 const auto& filename = info.param.filename().string();
116 return filename.substr(prefix.length(), filename.length() - prefix.length() - postfix.length());
117 }
118
119 INSTANTIATE_TEST_CASE_P(TransactionTraceTestSuites, TransactionTraceTestSuite,
120 testing::ValuesIn(TransactionTraceTestSuite::sTransactionTraces),
121 PrintToStringParamName);
122
123 } // namespace android
124
main(int argc,char ** argv)125 int main(int argc, char** argv) {
126 for (const auto& entry : std::filesystem::directory_iterator(
127 android::base::GetExecutableDirectory() + "/testdata/")) {
128 if (!entry.is_regular_file()) {
129 continue;
130 }
131 const auto& filename = entry.path().filename().string();
132 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
133 if (filename.compare(0, prefix.length(), prefix)) {
134 continue;
135 }
136 const std::string& path = entry.path().string();
137 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
138 if (path.compare(path.length() - postfix.length(), postfix.length(), postfix)) {
139 continue;
140 }
141 android::TransactionTraceTestSuite::sTransactionTraces.push_back(path);
142 }
143 ::testing::InitGoogleTest(&argc, argv);
144 return RUN_ALL_TESTS();
145 }