mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
5.5 KiB
141 lines
5.5 KiB
|
4 years ago
|
/*
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
|
||
|
|
2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
|
|
||
|
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||
|
|
copy of this software and associated documentation files (the "Software"),
|
||
|
|
to deal in the Software without restriction, including without limitation
|
||
|
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||
|
|
and/or sell copies of the Software, and to permit persons to whom the
|
||
|
|
Software is furnished to do so, subject to the following conditions:
|
||
|
|
|
||
|
|
The above copyright notice and this permission notice shall be included
|
||
|
|
in all copies or substantial portions of the Software.
|
||
|
|
|
||
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||
|
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||
|
|
DEALINGS IN THE SOFTWARE.
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include <cstdlib>
|
||
|
|
#include <Corrade/Containers/Array.h>
|
||
|
|
#include <Corrade/Containers/Optional.h>
|
||
|
|
#include <Corrade/Containers/Pair.h>
|
||
|
|
#include <Corrade/Containers/String.h>
|
||
|
|
#include <Corrade/TestSuite/Tester.h>
|
||
|
|
#include <Corrade/TestSuite/Compare/StringToFile.h>
|
||
|
|
#include <Corrade/Utility/Format.h>
|
||
|
|
#include <Corrade/Utility/Path.h>
|
||
|
|
|
||
|
|
#include "Magnum/Trade/AbstractImporter.h"
|
||
|
|
|
||
|
|
#include "configure.h"
|
||
|
|
|
||
|
|
namespace Magnum { namespace Trade { namespace Test { namespace {
|
||
|
|
|
||
|
|
struct ImageConverterTest: TestSuite::Tester {
|
||
|
|
explicit ImageConverterTest();
|
||
|
|
|
||
|
|
void info();
|
||
|
|
};
|
||
|
|
|
||
|
|
using namespace Containers::Literals;
|
||
|
|
|
||
|
|
const struct {
|
||
|
|
const char* name;
|
||
|
|
Containers::Array<Containers::String> args;
|
||
|
|
const char* requiresImporter;
|
||
|
|
const char* expected;
|
||
|
|
} InfoData[]{
|
||
|
|
{"data", Containers::array<Containers::String>({
|
||
|
|
"-I", "TgaImporter", "--info", Utility::Path::join(TRADE_TEST_DIR, "ImageConverterTestFiles/file.tga")}),
|
||
|
|
"TgaImporter",
|
||
|
|
"info-data.txt"},
|
||
|
|
{"data, map", Containers::array<Containers::String>({
|
||
|
|
"--map", "-I", "TgaImporter", "--info", Utility::Path::join(TRADE_TEST_DIR, "ImageConverterTestFiles/file.tga")}),
|
||
|
|
"TgaImporter",
|
||
|
|
/** @todo change to something else once we have a plugin that can
|
||
|
|
zero-copy pass the imported data */
|
||
|
|
"info-data.txt"},
|
||
|
|
{"data, ignored output file", Containers::array<Containers::String>({
|
||
|
|
"-I", "TgaImporter", "--info", Utility::Path::join(TRADE_TEST_DIR, "ImageConverterTestFiles/file.tga"), "whatever.png"}),
|
||
|
|
"TgaImporter",
|
||
|
|
"info-data-ignored-output.txt"}
|
||
|
|
};
|
||
|
|
|
||
|
|
ImageConverterTest::ImageConverterTest() {
|
||
|
|
#ifndef CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT
|
||
|
|
addInstancedTests({&ImageConverterTest::info},
|
||
|
|
Containers::arraySize(InfoData));
|
||
|
|
#endif
|
||
|
|
|
||
|
|
/* Create output dir, if doesn't already exist */
|
||
|
|
Utility::Path::make(Utility::Path::join(TRADE_TEST_OUTPUT_DIR, "ImageConverterTestFiles"));
|
||
|
|
}
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
#ifdef IMAGECONVERTER_EXECUTABLE_FILENAME
|
||
|
|
/** @todo take a StringIterable once it exists */
|
||
|
|
Containers::Pair<bool, Containers::String> call(Containers::ArrayView<const Containers::String> arguments) {
|
||
|
|
/* Create a string view array for the arguments, implicitly pass the
|
||
|
|
application name and plugin directory override */
|
||
|
|
/** @todo drop once StringIterable exists */
|
||
|
|
Containers::Array<Containers::StringView> argumentViews{ValueInit, arguments.size() + 3};
|
||
|
|
argumentViews[0] = ""_s;
|
||
|
|
argumentViews[1] = "--plugin-dir"_s;
|
||
|
|
argumentViews[2] = MAGNUM_PLUGINS_INSTALL_DIR;
|
||
|
|
for(std::size_t i = 0; i != arguments.size(); ++i)
|
||
|
|
argumentViews[i + 3] = arguments[i];
|
||
|
|
|
||
|
|
const Containers::String outputFilename = Utility::Path::join(TRADE_TEST_OUTPUT_DIR, "ImageConverterTestFiles/output.txt");
|
||
|
|
/** @todo clean up once Utility::System::execute() with output redirection
|
||
|
|
exists */
|
||
|
|
const bool success = std::system(Utility::format("{} {} > {} 2>&1",
|
||
|
|
IMAGECONVERTER_EXECUTABLE_FILENAME,
|
||
|
|
" "_s.join(argumentViews), /** @todo handle space escaping here? */
|
||
|
|
outputFilename
|
||
|
|
).data()) == 0;
|
||
|
|
|
||
|
|
const Containers::Optional<Containers::String> output = Utility::Path::readString(outputFilename);
|
||
|
|
CORRADE_VERIFY(output);
|
||
|
|
|
||
|
|
return {success, std::move(*output)};
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void ImageConverterTest::info() {
|
||
|
|
auto&& data = InfoData[testCaseInstanceId()];
|
||
|
|
setTestCaseDescription(data.name);
|
||
|
|
|
||
|
|
#ifndef IMAGECONVERTER_EXECUTABLE_FILENAME
|
||
|
|
CORRADE_SKIP("magnum-imageconverter not built, can't test");
|
||
|
|
#else
|
||
|
|
/* Check if required plugins can be loaded. Catches also ABI and interface
|
||
|
|
mismatch errors. */
|
||
|
|
PluginManager::Manager<Trade::AbstractImporter> importerManager{MAGNUM_PLUGINS_IMPORTER_INSTALL_DIR};
|
||
|
|
if(data.requiresImporter && !(importerManager.load(data.requiresImporter) & PluginManager::LoadState::Loaded))
|
||
|
|
CORRADE_SKIP(data.requiresImporter << "plugin can't be loaded.");
|
||
|
|
|
||
|
|
CORRADE_VERIFY(true); /* capture correct function name */
|
||
|
|
|
||
|
|
Containers::Pair<bool, Containers::String> output = call(data.args);
|
||
|
|
CORRADE_COMPARE_AS(output.second(),
|
||
|
|
Utility::Path::join({TRADE_TEST_DIR, "ImageConverterTestFiles", data.expected}),
|
||
|
|
TestSuite::Compare::StringToFile);
|
||
|
|
CORRADE_VERIFY(output.first());
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
}}}}
|
||
|
|
|
||
|
|
CORRADE_TEST_MAIN(Magnum::Trade::Test::ImageConverterTest)
|