blob: 551f60f04471de5e53b92fafa4d2fb58bf220268 [file] [log] [blame]
Chris Mantona4020612020-01-14 16:34:08 -08001/*
2 * Copyright 2020 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
Chris Manton25cc8282020-02-18 07:56:15 -080017#pragma once
18
19#include <unordered_map>
20
21#include <unistd.h>
22
23#include "base/logging.h" // LOG() stdout and android log
24#include "test/headless/get_options.h"
25
Chris Mantona4020612020-01-14 16:34:08 -080026namespace bluetooth {
27namespace test {
28namespace headless {
29
Chris Manton25cc8282020-02-18 07:56:15 -080030namespace {
31
Chris Mantona4020612020-01-14 16:34:08 -080032template <typename T>
33using ExecutionUnit = std::function<T()>;
34
Chris Manton25cc8282020-02-18 07:56:15 -080035constexpr char kHeadlessStartSentinel[] =
36 " START HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS "
37 "HEADLESS";
38constexpr char kHeadlessStopSentinel[] =
39 " STOP HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS "
40 "HEADLESS";
Chris Mantona4020612020-01-14 16:34:08 -080041
Chris Manton25cc8282020-02-18 07:56:15 -080042} // namespace
43
44class HeadlessStack {
Chris Mantona4020612020-01-14 16:34:08 -080045 protected:
Chris Manton25cc8282020-02-18 07:56:15 -080046 HeadlessStack() = default;
47 virtual ~HeadlessStack() = default;
48
49 void SetUp();
50 void TearDown();
Chris Mantona4020612020-01-14 16:34:08 -080051};
52
Chris Manton25cc8282020-02-18 07:56:15 -080053class HeadlessRun : public HeadlessStack {
54 protected:
55 const bluetooth::test::headless::GetOpt& options_;
56 unsigned long loop_{0};
57
58 HeadlessRun(const bluetooth::test::headless::GetOpt& options)
59 : options_(options) {}
60
Chris Mantona4020612020-01-14 16:34:08 -080061 template <typename T>
Chris Manton25cc8282020-02-18 07:56:15 -080062 T RunOnHeadlessStack(ExecutionUnit<T> func) {
Chris Mantona4020612020-01-14 16:34:08 -080063 SetUp();
Chris Manton25cc8282020-02-18 07:56:15 -080064 LOG(INFO) << kHeadlessStartSentinel;
65
66 T rc;
67 for (loop_ = 0; loop_ < options_.loop_; loop_++) {
68 rc = func();
69 if (options_.msec_ != 0) {
70 usleep(options_.msec_ * 1000);
71 }
72 if (rc) {
73 break;
74 }
75 }
76 if (rc) {
77 LOG(ERROR) << "FAIL:" << rc << " loop/loops:" << loop_ << "/"
78 << options_.loop_;
79 } else {
80 LOG(INFO) << "PASS:" << rc << " loop/loops:" << loop_ << "/"
81 << options_.loop_;
82 }
83
84 LOG(INFO) << kHeadlessStopSentinel;
Chris Mantona4020612020-01-14 16:34:08 -080085 TearDown();
86 return rc;
87 }
Chris Manton25cc8282020-02-18 07:56:15 -080088 virtual ~HeadlessRun() = default;
89};
90
91template <typename T>
92class HeadlessTest : public HeadlessRun {
93 public:
94 virtual T Run() {
95 if (options_.non_options_.size() == 0) {
96 fprintf(stdout, "Must supply at least one subtest name\n");
97 return -1;
98 }
99
100 std::string subtest = options_.GetNextSubTest();
101 if (test_nodes_.find(subtest) == test_nodes_.end()) {
102 fprintf(stdout, "Unknown subtest module:%s\n", subtest.c_str());
103 return -1;
104 }
105 return test_nodes_.at(subtest)->Run();
106 }
107
108 virtual ~HeadlessTest() = default;
109
110 protected:
111 HeadlessTest(const bluetooth::test::headless::GetOpt& options)
112 : HeadlessRun(options) {}
113
114 std::unordered_map<std::string, std::unique_ptr<HeadlessTest<T>>> test_nodes_;
Chris Mantona4020612020-01-14 16:34:08 -0800115};
116
117} // namespace headless
118} // namespace test
119} // namespace bluetooth