blob: b4a1182f2362f39e7d771219c010de7bc3e8ce47 [file] [log] [blame]
Christopher Ferris1a993562018-08-21 12:43:50 -07001/*
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
Christopher Ferris7cfd24e2019-11-14 19:27:19 -080019#include <regex>
Christopher Ferris1a993562018-08-21 12:43:50 -070020#include <string>
21#include <tuple>
22
23#include <android-base/unique_fd.h>
24
25namespace android {
26namespace gtest_extras {
27
28enum TestResult : uint8_t {
29 TEST_NONE = 0,
30 TEST_PASS,
31 TEST_XPASS,
32 TEST_FAIL,
33 TEST_XFAIL,
34 TEST_TIMEOUT,
Christopher Ferris849d24e2019-03-09 16:47:04 -080035 TEST_SKIPPED,
Christopher Ferris1a993562018-08-21 12:43:50 -070036};
37
38class Test {
39 public:
40 Test(std::tuple<std::string, std::string>& test, size_t test_index, size_t run_index, int fd);
41
Christopher Ferrisa3827162019-06-12 19:01:56 -070042 void Print();
Christopher Ferris1a993562018-08-21 12:43:50 -070043
44 void Stop();
45
46 bool Read();
47
48 void ReadUntilClosed();
49
50 void CloseFd();
51
Christopher Ferris849d24e2019-03-09 16:47:04 -080052 void SetResultFromOutput();
53
Christopher Ferris1a993562018-08-21 12:43:50 -070054 void AppendOutput(std::string& output) { output_ += output; }
55 void AppendOutput(const char* output) { output_ += output; }
56
57 uint64_t RunTimeNs() const { return end_ns_ - start_ns_; }
58 uint64_t ElapsedNs(uint64_t cur_ns) const { return cur_ns - start_ns_; }
59
60 bool ExpectFail() const { return test_name_.find("xfail") == 0; }
61
Christopher Ferrisc2f85d62019-03-10 13:40:40 -070062 const std::string& suite_name() const { return suite_name_; }
Christopher Ferris1a993562018-08-21 12:43:50 -070063 const std::string& test_name() const { return test_name_; }
64 const std::string& name() const { return name_; }
65
66 size_t test_index() const { return test_index_; }
67 size_t run_index() const { return run_index_; }
68
69 int fd() const { return fd_; }
70
71 uint64_t start_ns() const { return start_ns_; }
72
73 uint64_t end_ns() const { return end_ns_; }
74 void set_end_ns(uint64_t end_ns) { end_ns_ = end_ns; }
75
76 TestResult result() const { return result_; }
77 void set_result(TestResult result) { result_ = result; }
78
79 void set_slow(bool slow) { slow_ = slow; }
80 bool slow() const { return slow_; }
81
82 const std::string& output() const { return output_; }
83
84 private:
Christopher Ferrisc2f85d62019-03-10 13:40:40 -070085 std::string suite_name_;
Christopher Ferris1a993562018-08-21 12:43:50 -070086 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_;
Christopher Ferris7cfd24e2019-11-14 19:27:19 -080098
99 static std::regex skipped_regex_;
Christopher Ferris1a993562018-08-21 12:43:50 -0700100};
101
102} // namespace gtest_extras
103} // namespace android