1 /* 2 * Copyright (C) 2017 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 #pragma once 18 19 #include <regex> 20 #include <string> 21 #include <tuple> 22 23 #include <android-base/unique_fd.h> 24 25 namespace android { 26 namespace gtest_extras { 27 28 enum TestResult : uint8_t { 29 TEST_NONE = 0, 30 TEST_PASS, 31 TEST_XPASS, 32 TEST_FAIL, 33 TEST_XFAIL, 34 TEST_TIMEOUT, 35 TEST_SKIPPED, 36 }; 37 38 class Test { 39 public: 40 Test(std::tuple<std::string, std::string>& test, size_t test_index, size_t run_index, int fd); 41 42 void Print(); 43 44 void Stop(); 45 46 bool Read(); 47 48 void ReadUntilClosed(); 49 50 void CloseFd(); 51 52 void SetResultFromOutput(); 53 AppendOutput(std::string & output)54 void AppendOutput(std::string& output) { output_ += output; } AppendOutput(const char * output)55 void AppendOutput(const char* output) { output_ += output; } 56 RunTimeNs()57 uint64_t RunTimeNs() const { return end_ns_ - start_ns_; } ElapsedNs(uint64_t cur_ns)58 uint64_t ElapsedNs(uint64_t cur_ns) const { return cur_ns - start_ns_; } 59 ExpectFail()60 bool ExpectFail() const { return test_name_.find("xfail") == 0; } 61 suite_name()62 const std::string& suite_name() const { return suite_name_; } test_name()63 const std::string& test_name() const { return test_name_; } name()64 const std::string& name() const { return name_; } 65 test_index()66 size_t test_index() const { return test_index_; } run_index()67 size_t run_index() const { return run_index_; } 68 fd()69 int fd() const { return fd_; } 70 start_ns()71 uint64_t start_ns() const { return start_ns_; } 72 end_ns()73 uint64_t end_ns() const { return end_ns_; } set_end_ns(uint64_t end_ns)74 void set_end_ns(uint64_t end_ns) { end_ns_ = end_ns; } 75 result()76 TestResult result() const { return result_; } set_result(TestResult result)77 void set_result(TestResult result) { result_ = result; } 78 set_slow(bool slow)79 void set_slow(bool slow) { slow_ = slow; } slow()80 bool slow() const { return slow_; } 81 output()82 const std::string& output() const { return output_; } 83 84 private: 85 std::string suite_name_; 86 std::string test_name_; 87 std::string name_; 88 size_t test_index_; // Index into test list. 89 size_t run_index_; // Index into running list. 90 android::base::unique_fd fd_; 91 92 uint64_t start_ns_; 93 uint64_t end_ns_ = 0; 94 bool slow_ = false; 95 96 TestResult result_ = TEST_NONE; 97 std::string output_; 98 99 static std::regex skipped_regex_; 100 }; 101 102 } // namespace gtest_extras 103 } // namespace android 104