lc-compliance: Refactor using Googletest

Refactor lc-compliance using Googletest as the test framework.

Signed-off-by: Nícolas F. R. A. Prado <nfraprado@collabora.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
This commit is contained in:
Nícolas F. R. A. Prado
2021-07-02 09:21:13 -03:00
committed by Jacopo Mondi
parent e5c51e1fcf
commit bc87163779
9 changed files with 209 additions and 355 deletions
+41 -66
View File
@@ -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 <iostream>
#include <string.h>
#include <gtest/gtest.h>
#include <libcamera/libcamera.h>
#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<CameraManager> cm_;
std::shared_ptr<Camera> 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)
static void listCameras(CameraManager *cm)
{
cm_ = std::make_unique<CameraManager>();
for (const std::shared_ptr<Camera> &cam : cm->cameras())
std::cout << "- " << cam.get()->id() << std::endl;
}
Harness::~Harness()
static int initCamera(CameraManager *cm, OptionsParser::Options options)
{
if (camera_) {
camera_->release();
camera_.reset();
}
std::shared_ptr<Camera> camera;
cm_->stop();
}
int Harness::exec()
{
int ret = init();
if (ret)
return ret;
std::vector<Results> results;
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<Camera> &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<CameraManager> cm = std::make_unique<CameraManager>();
return harness.exec() ? EXIT_FAILURE : EXIT_SUCCESS;
ret = initCamera(cm.get(), options);
if (ret)
return ret;
testing::UnitTest::GetInstance()->listeners().Append(new ThrowListener);
ret = RUN_ALL_TESTS();
cm->stop();
return ret;
}