summaryrefslogtreecommitdiff
path: root/services/surfaceflinger/tests/unittests/TransactionTraceWriterTest.cpp
blob: 4a83d445fc0b4b775a3b3c7ef35e601ebe1f946d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#undef LOG_TAG
#define LOG_TAG "TransactionTraceWriterTest"

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <log/log.h>
#include <filesystem>

#include "TestableSurfaceFlinger.h"

namespace android {

class TransactionTraceWriterTest : public testing::Test {
protected:
    std::string mFilename = "/data/local/tmp/testfile_transaction_trace.winscope";

    void SetUp() { mFlinger.initTransactionTraceWriter(); }
    void TearDown() { std::filesystem::remove(mFilename); }

    void verifyTraceFile() {
        std::fstream file(mFilename, std::ios::in);
        ASSERT_TRUE(file.is_open());
        std::string line;
        char magicNumber[8];
        file.read(magicNumber, 8);
        EXPECT_EQ("\tTNXTRAC", std::string(magicNumber, magicNumber + 8));
    }

    TestableSurfaceFlinger mFlinger;
};

// Check that a new file is written if overwrite=true and no file exists.
TEST_F(TransactionTraceWriterTest, canWriteToFile_overwriteTrue) {
    TransactionTraceWriter::getInstance().invokeForTest(mFilename, /* overwrite */ true);
    EXPECT_EQ(access(mFilename.c_str(), F_OK), 0);
    verifyTraceFile();
}

// Check that a new file is written if overwrite=false and no file exists.
TEST_F(TransactionTraceWriterTest, canWriteToFile_overwriteFalse) {
    TransactionTraceWriter::getInstance().invokeForTest(mFilename, /* overwrite */ false);
    EXPECT_EQ(access(mFilename.c_str(), F_OK), 0);
    verifyTraceFile();
}

// Check that an existing file is overwritten when overwrite=true.
TEST_F(TransactionTraceWriterTest, canOverwriteFile) {
    std::string testLine = "test";
    {
        std::ofstream file(mFilename, std::ios::out);
        file << testLine;
    }
    TransactionTraceWriter::getInstance().invokeForTest(mFilename, /* overwrite */ true);
    verifyTraceFile();
}

// Check that an existing file isn't overwritten when it is new and overwrite=false.
TEST_F(TransactionTraceWriterTest, doNotOverwriteFile) {
    std::string testLine = "test";
    {
        std::ofstream file(mFilename, std::ios::out);
        file << testLine;
    }
    TransactionTraceWriter::getInstance().invokeForTest(mFilename, /* overwrite */ false);
    {
        std::fstream file(mFilename, std::ios::in);
        ASSERT_TRUE(file.is_open());
        std::string line;
        std::getline(file, line);
        EXPECT_EQ(line, testLine);
    }
}

// Check that an existing file is overwritten when it is old and overwrite=false.
TEST_F(TransactionTraceWriterTest, overwriteOldFile) {
    std::string testLine = "test";
    {
        std::ofstream file(mFilename, std::ios::out);
        file << testLine;
    }

    // Update file modification time to 15 minutes ago.
    using Clock = std::filesystem::file_time_type::clock;
    std::error_code error;
    std::filesystem::last_write_time(mFilename, Clock::now() - std::chrono::minutes{15}, error);
    ASSERT_EQ(error.value(), 0);

    TransactionTraceWriter::getInstance().invokeForTest(mFilename, /* overwrite */ false);
    verifyTraceFile();
}

} // namespace android