File: TransactionTraceWriterTest.cpp

package info (click to toggle)
android-platform-tools 35.0.2-1~exp6
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,716 kB
  • sloc: cpp: 995,749; java: 290,495; ansic: 145,647; xml: 58,531; python: 39,608; sh: 14,500; javascript: 5,198; asm: 4,866; makefile: 3,115; yacc: 769; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (120 lines) | stat: -rw-r--r-- 4,193 bytes parent folder | download
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
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * 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();
}

// Check we cannot write to file if the trace write is disabled.
TEST_F(TransactionTraceWriterTest, canDisableTraceWriter) {
    TransactionTraceWriter::getInstance().disable();
    TransactionTraceWriter::getInstance().invokeForTest(mFilename, /* overwrite */ true);
    EXPECT_NE(access(mFilename.c_str(), F_OK), 0);

    TransactionTraceWriter::getInstance().enable();
    TransactionTraceWriter::getInstance().invokeForTest(mFilename, /* overwrite */ true);
    EXPECT_EQ(access(mFilename.c_str(), F_OK), 0);
    verifyTraceFile();
}

} // namespace android