blob: 76845ffec9669f9d4e18dcf44074f87268d10981 [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
19#include <string>
20#include <tuple>
21
22#include <android-base/unique_fd.h>
23
24namespace android {
25namespace gtest_extras {
26
27enum TestResult : uint8_t {
28 TEST_NONE = 0,
29 TEST_PASS,
30 TEST_XPASS,
31 TEST_FAIL,
32 TEST_XFAIL,
33 TEST_TIMEOUT,
Christopher Ferris849d24e2019-03-09 16:47:04 -080034 TEST_SKIPPED,
Christopher Ferris1a993562018-08-21 12:43:50 -070035};
36
37class 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
Christopher Ferris849d24e2019-03-09 16:47:04 -080053 void SetResultFromOutput();
54
Christopher Ferris1a993562018-08-21 12:43:50 -070055 void AppendOutput(std::string& output) { output_ += output; }
56 void AppendOutput(const char* output) { output_ += output; }
57
58 uint64_t RunTimeNs() const { return end_ns_ - start_ns_; }
59 uint64_t ElapsedNs(uint64_t cur_ns) const { return cur_ns - start_ns_; }
60
61 bool ExpectFail() const { return test_name_.find("xfail") == 0; }
62
Christopher Ferrisc2f85d62019-03-10 13:40:40 -070063 const std::string& suite_name() const { return suite_name_; }
Christopher Ferris1a993562018-08-21 12:43:50 -070064 const std::string& test_name() const { return test_name_; }
65 const std::string& name() const { return name_; }
66
67 size_t test_index() const { return test_index_; }
68 size_t run_index() const { return run_index_; }
69
70 int fd() const { return fd_; }
71
72 uint64_t start_ns() const { return start_ns_; }
73
74 uint64_t end_ns() const { return end_ns_; }
75 void set_end_ns(uint64_t end_ns) { end_ns_ = end_ns; }
76
77 TestResult result() const { return result_; }
78 void set_result(TestResult result) { result_ = result; }
79
80 void set_slow(bool slow) { slow_ = slow; }
81 bool slow() const { return slow_; }
82
83 const std::string& output() const { return output_; }
84
85 private:
Christopher Ferrisc2f85d62019-03-10 13:40:40 -070086 std::string suite_name_;
Christopher Ferris1a993562018-08-21 12:43:50 -070087 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