From 547920d3242b14cd48fdc07cc297932f58b97cb8 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Sun, 4 Jul 2021 01:23:18 +0900 Subject: [PATCH] test filling: add fill/linear/radial gradients. --- test/meson.build | 3 +- test/testFill.cpp | 165 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 test/testFill.cpp diff --git a/test/meson.build b/test/meson.build index 19cf6ff7..261bb4eb 100644 --- a/test/meson.build +++ b/test/meson.build @@ -1,6 +1,7 @@ test_file = [ - 'testMain.cpp', + 'testFill.cpp', 'testInitializer.cpp', + 'testMain.cpp', 'testPicture.cpp', 'testScene.cpp', 'testShape.cpp', diff --git a/test/testFill.cpp b/test/testFill.cpp new file mode 100644 index 00000000..c9bc189f --- /dev/null +++ b/test/testFill.cpp @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved. + + * 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 +#include +#include "catch.hpp" + +using namespace tvg; +using namespace std; + +TEST_CASE("Common Filling", "[tvgFill]") +{ + auto fill = LinearGradient::gen(); + REQUIRE(fill); + + //Options + REQUIRE(fill->spread() == FillSpread::Pad); + REQUIRE(fill->spread(FillSpread::Pad) == Result::Success); + REQUIRE(fill->spread(FillSpread::Reflect) == Result::Success); + REQUIRE(fill->spread(FillSpread::Repeat) == Result::Success); + REQUIRE(fill->spread() == FillSpread::Repeat); + + //ColorStops + const Fill::ColorStop* cs = nullptr; + REQUIRE(fill->colorStops(nullptr) == 0); + REQUIRE(fill->colorStops(&cs) == 0); + REQUIRE(cs == nullptr); + + Fill::ColorStop cs2[4] = { + {0.0f, 0, 0, 0, 0}, + {0.2f, 50, 25, 50, 25}, + {0.5f, 100, 100, 100, 125}, + {1.0f, 255, 255, 255, 255} + }; + + REQUIRE(fill->colorStops(nullptr, 4) == Result::InvalidArguments); + REQUIRE(fill->colorStops(cs2, 0) == Result::InvalidArguments); + REQUIRE(fill->colorStops(cs2, 4) == Result::Success); + REQUIRE(fill->colorStops(&cs) == 4); + + for (int i = 0; i < 4; ++i) { + REQUIRE(cs[i].offset == cs2[i].offset); + REQUIRE(cs[i].r == cs2[i].r); + REQUIRE(cs[i].g == cs2[i].g); + REQUIRE(cs[i].b == cs2[i].b); + }; + + //Reset ColorStop + REQUIRE(fill->colorStops(nullptr, 0) == Result::Success); + REQUIRE(fill->colorStops(&cs) == 0); + + //Set to Shape + auto shape = Shape::gen(); + REQUIRE(shape); + + auto pFill = fill.get(); + REQUIRE(shape->fill(move(fill)) == Result::Success); + REQUIRE(shape->fill() == pFill); +} + +TEST_CASE("Linear Filling", "[tvgFill]") +{ + auto fill = LinearGradient::gen(); + REQUIRE(fill); + + float x1, y1, x2, y2; + + REQUIRE(fill->linear(nullptr, nullptr, nullptr, nullptr) == Result::Success); + REQUIRE(fill->linear(0, 0, 0, 0) == Result::Success); + + REQUIRE(fill->linear(&x1, nullptr, &x2, nullptr) == Result::Success); + REQUIRE(x1 == 0.0f); + REQUIRE(x2 == 0.0f); + + REQUIRE(fill->linear(-1.0f, -1.0f, 100.0f, 100.0f) == Result::Success); + REQUIRE(fill->linear(&x1, &y1, &x2, &y2) == Result::Success); + REQUIRE(x1 == -1.0f); + REQUIRE(y1 == -1.0f); + REQUIRE(x2 == 100.0f); + REQUIRE(y2 == 100.0f); +} + +TEST_CASE("Radial Filling", "[tvgFill]") +{ + auto fill = RadialGradient::gen(); + REQUIRE(fill); + + float cx, cy, radius; + + REQUIRE(fill->radial(0, 0, -1) == Result::InvalidArguments); + REQUIRE(fill->radial(nullptr, nullptr, nullptr) == Result::Success); + REQUIRE(fill->radial(100, 120, 50) == Result::Success); + + REQUIRE(fill->radial(&cx, nullptr, &radius) == Result::Success); + REQUIRE(cx == 100.0f); + REQUIRE(radius == 50.0f); + + REQUIRE(fill->radial(nullptr, &cy, nullptr) == Result::Success); + REQUIRE(cy == 120); + + REQUIRE(fill->radial(0, 0, 0) == Result::Success); + REQUIRE(fill->radial(&cx, &cy, &radius) == Result::Success); + REQUIRE(cx == 0.0f); + REQUIRE(cy == 0.0f); + REQUIRE(radius == 0.0f); +} + +TEST_CASE("Filling Dupliction", "[tvgFill]") +{ + auto fill = RadialGradient::gen(); + REQUIRE(fill); + + //Setup + Fill::ColorStop cs[4] = { + {0.0f, 0, 0, 0, 0}, + {0.2f, 50, 25, 50, 25}, + {0.5f, 100, 100, 100, 125}, + {1.0f, 255, 255, 255, 255} + }; + + REQUIRE(fill->colorStops(cs, 4) == Result::Success); + REQUIRE(fill->spread(FillSpread::Reflect) == Result::Success); + REQUIRE(fill->radial(100.0f, 120.0f, 50.0f) == Result::Success); + + //Duplication + auto dup = unique_ptr(static_cast(fill->duplicate())); + REQUIRE(dup); + + REQUIRE(dup->spread() == FillSpread::Reflect); + + float cx, cy, radius; + REQUIRE(dup->radial(&cx, &cy, &radius) == Result::Success); + REQUIRE(cx == 100.0f); + REQUIRE(cy == 120.0f); + REQUIRE(radius == 50.0f); + + const Fill::ColorStop* cs2 = nullptr; + REQUIRE(fill->colorStops(&cs2) == 4); + + for (int i = 0; i < 4; ++i) { + REQUIRE(cs[i].offset == cs2[i].offset); + REQUIRE(cs[i].r == cs2[i].r); + REQUIRE(cs[i].g == cs2[i].g); + REQUIRE(cs[i].b == cs2[i].b); + }; +} \ No newline at end of file