Skip to content

Commit

Permalink
Add camera imitation test
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanTyulyandin committed Nov 22, 2018
1 parent 1a6c3e4 commit 3b9d45b
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 6 deletions.
1 change: 1 addition & 0 deletions tests/test-system-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ equal to its class name.

<!-- A list of known devices. -->
<deviceClasses>
<camera type="file" src="/dev/video0" filters="*.jpg,*.png"/>
<servoMotor period="20000000" invert="false" controlMin="-90" controlMax="90" />
<pwmCapture />
<powerMotor period="4096" invert="false" measures="(0;0)(100;100)" />
Expand Down
2 changes: 2 additions & 0 deletions tests/tests.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ SUBDIRS = \
minimalCppApp \
selftest \
thirdparty \
trikCameraPhotoTests \
trikCommunicatorTests \
trikKernelTests \
trikScriptRunnerTests \
Expand All @@ -29,3 +30,4 @@ trikKernelTests.depends = thirdparty testUtils
trikScriptRunnerTests.depends = thirdparty testUtils
trikCommunicatorTests.depends = thirdparty testUtils
selftest.depends = thirdparty testUtils
trikCameraPhotoTests.depends = thirdparty testUtils
56 changes: 56 additions & 0 deletions tests/trikCameraPhotoTests/trikCameraPhotoImitationTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Copyright 2018 Ivan Tyulyandin and CyberTech Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */

#include "trikCameraPhotoImitationTest.h"

#include <trikControl/brickFactory.h>

using namespace tests;
using namespace trikControl;

void trikCameraPhotoImitationTest::SetUp() {
testBrick.reset(trikControl::BrickFactory::create("./test-system-config.xml"
, "./test-model-config.xml", "./media/"));
}

namespace {
QVector<uint8_t> qImageToQVector(const QImage &imgOrig) {
constexpr auto DESIRED_FORMAT = QImage::Format_RGB888;

constexpr auto SIZE_X = 320;
constexpr auto SIZE_Y = 240;
const QImage &img = imgOrig.format() == DESIRED_FORMAT ? imgOrig : imgOrig.convertToFormat(DESIRED_FORMAT);
const QImage &scaledImg = img.height() == SIZE_X && img.width() == SIZE_Y ? img : img.scaled(SIZE_X, SIZE_Y);
auto cb = scaledImg.constBits();
QVector<uint8_t> imageByteVector = QVector<uint8_t>(scaledImg.byteCount());
std::copy(cb, cb + scaledImg.byteCount(), imageByteVector.begin());

return imageByteVector;
}
}

TEST_F(trikCameraPhotoImitationTest, cameraImitationTest)
{
QVector<uint8_t> expectedPhoto = qImageToQVector(QImage("./media/trik_smile_normal.png"));
QVector<uint8_t> currentPhoto = testBrick->getStillImage();
ASSERT_EQ(expectedPhoto, currentPhoto);

currentPhoto = testBrick->getStillImage();
expectedPhoto = qImageToQVector(QImage("./media/trik_smile_sad.png"));
ASSERT_EQ(expectedPhoto, currentPhoto);

currentPhoto = testBrick->getStillImage();
expectedPhoto = qImageToQVector(QImage("./media/trik_smile_normal.png"));
ASSERT_EQ(expectedPhoto, currentPhoto);
}
35 changes: 35 additions & 0 deletions tests/trikCameraPhotoTests/trikCameraPhotoImitationTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright 2018 Ivan Tyulyandin and CyberTech Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */

#pragma once

#include <trikControl/brickInterface.h>

#include <gtest/gtest.h>

namespace tests {

/// Test suite for camera imitation
class trikCameraPhotoImitationTest : public testing::Test
{

protected:
void SetUp() override;
// void TearDown() override;

public:
QScopedPointer<trikControl::BrickInterface> testBrick;
};

}
24 changes: 24 additions & 0 deletions tests/trikCameraPhotoTests/trikCameraPhotoTests.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2018 Ivan Tylyandin and CyberTech Labs Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

include(../common.pri)

HEADERS += \
$$PWD/trikCameraPhotoImitationTest.h

SOURCES += \
$$PWD/trikCameraPhotoImitationTest.cpp

implementationIncludes(trikKernel trikControl tests/testUtils)
links(trikKernel trikControl trikHal testUtils)
12 changes: 6 additions & 6 deletions trikControl/src/imitationCameraImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include "imitationCameraImplementation.h"

#include <QtCore/QDir>
#include <QtCore/QFileInfoList>
#include <QtCore/QVector>
#include <QtGui/QImage>

#include <QsLog.h>
Expand All @@ -36,23 +34,25 @@ ImitationCameraImplementation::ImitationCameraImplementation(const QStringList &
if (filesList.size() > 1) {
QLOG_INFO() << "Few files matching regexps " << filters
<< " in file " << getTempDir();

if (filesList.size() < 10) {
for (auto file : filesList) {
for (auto &&file : filesList) {
QLOG_INFO() << file.fileName();
}
}
}
}


QVector<uint8_t> ImitationCameraImplementation::getPhoto() {
if ( ! filesList.isEmpty()) {
auto f = filesList[++cur%=filesList.size()];
QImage imgOrig(f.fileName());
QImage imgOrig(f.absoluteFilePath());

if (! imgOrig.isNull()) {
QLOG_INFO() << "Opening file " << f.fileName();
QLOG_INFO() << "Opening file " << f.absoluteFilePath();
} else {
QLOG_ERROR() << "Can not open file " << f.fileName();
QLOG_ERROR() << "Can not open file " << f.absoluteFilePath();
return QVector<uint8_t>();
}

Expand Down

0 comments on commit 3b9d45b

Please sign in to comment.