Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 27 | namespace android { |
| 28 | |
| 29 | class TransactionTraceWriterTest : public testing::Test { |
| 30 | protected: |
| 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 | |
| 48 | TEST_F(TransactionTraceWriterTest, canWriteToFile) { |
| 49 | TransactionTraceWriter::getInstance().invokeForTest(mFilename, /* overwrite */ true); |
| 50 | EXPECT_EQ(access(mFilename.c_str(), F_OK), 0); |
| 51 | verifyTraceFile(); |
| 52 | } |
| 53 | |
| 54 | TEST_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 | |
| 64 | TEST_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 |