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