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