first commit

This commit is contained in:
Your Name
2022-10-20 20:29:11 +08:00
commit 4d531f8044
3238 changed files with 1387862 additions and 0 deletions
@@ -0,0 +1,12 @@
set(TESTSUITE_SOURCES
${TESTSUITE_SOURCES}
${CMAKE_CURRENT_SOURCE_DIR}/TestSuite.cxx
${CMAKE_CURRENT_SOURCE_DIR}/testCanvas.cxx
PARENT_SCOPE
)
set(TESTSUITE_HEADERS
${TESTSUITE_HEADERS}
${CMAKE_CURRENT_SOURCE_DIR}/testCanvas.hxx
PARENT_SCOPE
)
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2016 Edward d'Auvergne
*
* This file is part of the program FlightGear.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "testCanvas.hxx"
// Set up the tests.
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CanvasTests, "Simgear unit tests");
@@ -0,0 +1,123 @@
#include "testCanvas.hxx"
#include <simgear/canvas/Canvas.hxx>
#include <simgear/canvas/elements/CanvasImage.hxx>
#include "test_suite/FGTestApi/testGlobals.hxx"
#include <Main/globals.hxx>
#include <Main/fg_commands.hxx>
#include <Canvas/canvas_mgr.hxx>
#include <Canvas/FGCanvasSystemAdapter.hxx>
extern bool global_nasalMinimalInit;
void verifyPixel(osg::Image* img, int x, int y, const uint8_t* pixel)
{
auto rawPixel = img->data(x, y);
CPPUNIT_ASSERT(memcmp(rawPixel, pixel, 4) == 0);
}
void CanvasTests::setUp()
{
global_nasalMinimalInit = false;
FGTestApi::setUp::initTestGlobals("canvas");
// Canvas needs loadxml command
fgInitCommands();
simgear::canvas::Canvas::setSystemAdapter(
simgear::canvas::SystemAdapterPtr(new canvas::FGCanvasSystemAdapter)
);
globals->add_subsystem("Canvas", new CanvasMgr, SGSubsystemMgr::DISPLAY);
globals->get_subsystem_mgr()->bind();
globals->get_subsystem_mgr()->init();
FGTestApi::setUp::initStandardNasal(true /* withCanvas */);
globals->get_subsystem_mgr()->postinit();
}
// Clean up after each test.
void CanvasTests::tearDown()
{
FGTestApi::tearDown::shutdownTestGlobals();
}
void CanvasTests::testCanvasBasic()
{
bool ok = FGTestApi::executeNasal(R"(
var my_canvas = canvas.new({
"name": "PFD-Test",
"size": [512, 512],
"view": [768, 1024],
"mipmapping": 1
});
globals.pfd = my_canvas;
)");
CPPUNIT_ASSERT(ok);
auto canvasPtr = globals->get_subsystem<CanvasMgr>()->getCanvas("PFD-Test");
CPPUNIT_ASSERT(canvasPtr.valid());
ok = FGTestApi::executeNasal(R"(
globals.pfd.createGroup("wibble");
)");
CPPUNIT_ASSERT(ok);
auto wibbleGroupPtr = canvasPtr->getGroup("wibble");
CPPUNIT_ASSERT(wibbleGroupPtr.valid());
}
void CanvasTests::testImagePixelOps()
{
bool ok = FGTestApi::executeNasal(R"(
var my_canvas = canvas.new({
"name": "ImagePixelOpsCanvas",
"size": [512, 512],
"view": [768, 1024],
"mipmapping": 1
});
var g = my_canvas.createGroup("root");
var img = g.createChild('image', 'ops-image');
# deliberate non-power-of-two sizes here
img.setSize(150, 170);
globals.pixelOps = my_canvas;
)");
CPPUNIT_ASSERT(ok);
auto canvasPtr = globals->get_subsystem<CanvasMgr>()->getCanvas("ImagePixelOpsCanvas");
CPPUNIT_ASSERT(canvasPtr.valid());
auto imagePtr = canvasPtr->getRootGroup()->getElementById("ops-image");
CPPUNIT_ASSERT(imagePtr.valid());
auto image = dynamic_cast<simgear::canvas::Image*>(imagePtr.get());
image->setFill(osg::Vec4(1.0, 0.0, 0.5, 1.0));
image->fillRect(SGRecti(20, 40, 70, 55), "#7f3f1f");
ok = FGTestApi::executeNasal(R"(
var canvas = globals.pixelOps;
var img = canvas.getGroup('root').getElementById('ops-image');
print('Image is:', img);
img.fillRect([5, 8, 180, 37], '#aabbcc');
)");
CPPUNIT_ASSERT(ok);
auto osgImage = image->getImage();
const uint8_t testColor1[] = {0xff, 0xcc, 0xbb, 0xaa}; // little endian
verifyPixel(osgImage.get(), 5, 8, testColor1);
verifyPixel(osgImage.get(), 149, 44, testColor1);
// test pixles should not have been touched by the second fillRect
const uint8_t testColor2[] = {0xff, 0x1f, 0x3f, 0x7f}; // little endian
verifyPixel(osgImage.get(), 20, 45, testColor2);
}
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2016 Edward d'Auvergne
*
* This file is part of the program FlightGear.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestFixture.h>
// The unit tests.
class CanvasTests : public CppUnit::TestFixture
{
// Set up the test suite.
CPPUNIT_TEST_SUITE(CanvasTests);
CPPUNIT_TEST(testCanvasBasic);
CPPUNIT_TEST(testImagePixelOps);
CPPUNIT_TEST_SUITE_END();
public:
// Set up function for each test.
void setUp();
// Clean up after each test.
void tearDown();
// The tests.
void testCanvasBasic();
void testImagePixelOps();
};