From bc871637797ef0fa193a3c64b82389fff4ce3f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADcolas=20F=2E=20R=2E=20A=2E=20Prado?= Date: Fri, 2 Jul 2021 09:21:13 -0300 Subject: lc-compliance: Refactor using Googletest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor lc-compliance using Googletest as the test framework. Signed-off-by: Nícolas F. R. A. Prado Reviewed-by: Niklas Söderlund Reviewed-by: Jacopo Mondi Signed-off-by: Jacopo Mondi --- src/lc-compliance/main.cpp | 107 +++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 66 deletions(-) (limited to 'src/lc-compliance/main.cpp') diff --git a/src/lc-compliance/main.cpp b/src/lc-compliance/main.cpp index 54cee54a..ff14474c 100644 --- a/src/lc-compliance/main.cpp +++ b/src/lc-compliance/main.cpp @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2020, Google Inc. + * Copyright (C) 2021, Collabora Ltd. * * main.cpp - lc-compliance - The libcamera compliance tool */ @@ -9,10 +10,12 @@ #include #include +#include + #include +#include "environment.h" #include "../cam/options.h" -#include "tests.h" using namespace libcamera; @@ -21,97 +24,59 @@ enum { OptHelp = 'h', }; -class Harness +/* + * Make asserts act like exceptions, otherwise they only fail (or skip) the + * current function. From gtest documentation: + * https://google.github.io/googletest/advanced.html#asserting-on-subroutines-with-an-exception + */ +class ThrowListener : public testing::EmptyTestEventListener { -public: - Harness(const OptionsParser::Options &options); - ~Harness(); - - int exec(); - -private: - int init(); - void listCameras(); - - OptionsParser::Options options_; - std::unique_ptr cm_; - std::shared_ptr camera_; + void OnTestPartResult(const testing::TestPartResult &result) override + { + if (result.type() == testing::TestPartResult::kFatalFailure || + result.type() == testing::TestPartResult::kSkip) + throw testing::AssertionException(result); + } }; -Harness::Harness(const OptionsParser::Options &options) - : options_(options) -{ - cm_ = std::make_unique(); -} - -Harness::~Harness() +static void listCameras(CameraManager *cm) { - if (camera_) { - camera_->release(); - camera_.reset(); - } - - cm_->stop(); + for (const std::shared_ptr &cam : cm->cameras()) + std::cout << "- " << cam.get()->id() << std::endl; } -int Harness::exec() +static int initCamera(CameraManager *cm, OptionsParser::Options options) { - int ret = init(); - if (ret) - return ret; - - std::vector results; + std::shared_ptr camera; - results.push_back(testSingleStream(camera_)); - - for (const Results &result : results) { - ret = result.summary(); - if (ret) - return ret; - } - - return 0; -} - -int Harness::init() -{ - int ret = cm_->start(); + int ret = cm->start(); if (ret) { std::cout << "Failed to start camera manager: " << strerror(-ret) << std::endl; return ret; } - if (!options_.isSet(OptCamera)) { + if (!options.isSet(OptCamera)) { std::cout << "No camera specified, available cameras:" << std::endl; - listCameras(); + listCameras(cm); return -ENODEV; } - const std::string &cameraId = options_[OptCamera]; - camera_ = cm_->get(cameraId); - if (!camera_) { + const std::string &cameraId = options[OptCamera]; + camera = cm->get(cameraId); + if (!camera) { std::cout << "Camera " << cameraId << " not found, available cameras:" << std::endl; - listCameras(); + listCameras(cm); return -ENODEV; } - if (camera_->acquire()) { - std::cout << "Failed to acquire camera" << std::endl; - return -EINVAL; - } + Environment::get()->setup(cm, cameraId); std::cout << "Using camera " << cameraId << std::endl; return 0; } -void Harness::listCameras() -{ - for (const std::shared_ptr &cam : cm_->cameras()) - std::cout << "- " << cam.get()->id() << std::endl; -} - static int parseOptions(int argc, char **argv, OptionsParser::Options *options) { OptionsParser parser; @@ -142,7 +107,17 @@ int main(int argc, char **argv) if (ret < 0) return EXIT_FAILURE; - Harness harness(options); + std::unique_ptr cm = std::make_unique(); + + ret = initCamera(cm.get(), options); + if (ret) + return ret; + + testing::UnitTest::GetInstance()->listeners().Append(new ThrowListener); + + ret = RUN_ALL_TESTS(); + + cm->stop(); - return harness.exec() ? EXIT_FAILURE : EXIT_SUCCESS; + return ret; } -- cgit v1.2.1