blob: 379135e2701cfaa7fca1b4024a6005f99c0bce87 [file] [log] [blame]
Vishnu Nair8c5b7002023-08-17 21:03:57 -07001/*
2 * Copyright 2023 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#undef LOG_TAG
18#define LOG_TAG "TransactionTraceWriterTest"
19
20#include <gmock/gmock.h>
21#include <gtest/gtest.h>
22#include <log/log.h>
23#include <filesystem>
24
25#include "TestableSurfaceFlinger.h"
26
27namespace android {
28
29class TransactionTraceWriterTest : public testing::Test {
30protected:
31 std::string mFilename = "/data/local/tmp/testfile_transaction_trace.winscope";
32
33 void SetUp() { mFlinger.initTransactionTraceWriter(); }
34 void TearDown() { std::filesystem::remove(mFilename); }
35
36 void verifyTraceFile() {
37 std::fstream file(mFilename, std::ios::in);
38 ASSERT_TRUE(file.is_open());
39 std::string line;
40 char magicNumber[8];
41 file.read(magicNumber, 8);
42 EXPECT_EQ("\tTNXTRAC", std::string(magicNumber, magicNumber + 8));
43 }
44
45 TestableSurfaceFlinger mFlinger;
46};
47
48TEST_F(TransactionTraceWriterTest, canWriteToFile) {
49 TransactionTraceWriter::getInstance().invokeForTest(mFilename, /* overwrite */ true);
50 EXPECT_EQ(access(mFilename.c_str(), F_OK), 0);
51 verifyTraceFile();
52}
53
54TEST_F(TransactionTraceWriterTest, canOverwriteFile) {
55 std::string testLine = "test";
56 {
57 std::ofstream file(mFilename, std::ios::out);
58 file << testLine;
59 }
60 TransactionTraceWriter::getInstance().invokeForTest(mFilename, /* overwrite */ true);
61 verifyTraceFile();
62}
63
64TEST_F(TransactionTraceWriterTest, doNotOverwriteFile) {
65 std::string testLine = "test";
66 {
67 std::ofstream file(mFilename, std::ios::out);
68 file << testLine;
69 }
70 TransactionTraceWriter::getInstance().invokeForTest(mFilename, /* overwrite */ false);
71 {
72 std::fstream file(mFilename, std::ios::in);
73 ASSERT_TRUE(file.is_open());
74 std::string line;
75 std::getline(file, line);
76 EXPECT_EQ(line, testLine);
77 }
78}
79} // namespace android