huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
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
/*
 * Copyright (C) 2017 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.
 */
 
#pragma once
 
#include <string>
#include <tuple>
 
#include <android-base/unique_fd.h>
 
namespace android {
namespace gtest_extras {
 
enum TestResult : uint8_t {
  TEST_NONE = 0,
  TEST_PASS,
  TEST_XPASS,
  TEST_FAIL,
  TEST_XFAIL,
  TEST_TIMEOUT,
  TEST_SKIPPED,
};
 
class Test {
 public:
  Test(std::tuple<std::string, std::string>& test, size_t test_index, size_t run_index, int fd);
 
  void PrintGtestFormat();
 
  void Print(bool gtest_format);
 
  void Stop();
 
  bool Read();
 
  void ReadUntilClosed();
 
  void CloseFd();
 
  void SetResultFromOutput();
 
  void AppendOutput(std::string& output) { output_ += output; }
  void AppendOutput(const char* output) { output_ += output; }
 
  uint64_t RunTimeNs() const { return end_ns_ - start_ns_; }
  uint64_t ElapsedNs(uint64_t cur_ns) const { return cur_ns - start_ns_; }
 
  bool ExpectFail() const { return test_name_.find("xfail") == 0; }
 
  const std::string& suite_name() const { return suite_name_; }
  const std::string& test_name() const { return test_name_; }
  const std::string& name() const { return name_; }
 
  size_t test_index() const { return test_index_; }
  size_t run_index() const { return run_index_; }
 
  int fd() const { return fd_; }
 
  uint64_t start_ns() const { return start_ns_; }
 
  uint64_t end_ns() const { return end_ns_; }
  void set_end_ns(uint64_t end_ns) { end_ns_ = end_ns; }
 
  TestResult result() const { return result_; }
  void set_result(TestResult result) { result_ = result; }
 
  void set_slow(bool slow) { slow_ = slow; }
  bool slow() const { return slow_; }
 
  const std::string& output() const { return output_; }
 
 private:
  std::string suite_name_;
  std::string test_name_;
  std::string name_;
  size_t test_index_;  // Index into test list.
  size_t run_index_;   // Index into running list.
  android::base::unique_fd fd_;
 
  uint64_t start_ns_;
  uint64_t end_ns_ = 0;
  bool slow_ = false;
 
  TestResult result_ = TEST_NONE;
  std::string output_;
};
 
}  // namespace gtest_extras
}  // namespace android