updated copyright.
[platform/core/graphics/tizenvg.git] / test / testShape.cpp
index e32142f..a4a82c4 100644 (file)
-#include <thorvg.h>
-#include <Elementary.h>
+/*
+ * Copyright (c) 2021 - 2023 the ThorVG project. 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.
+ */
 
-using namespace std;
+#include <thorvg.h>
+#include "catch.hpp"
 
-#define WIDTH 800
-#define HEIGHT 800
+using namespace tvg;
 
-unique_ptr<tvg::Paint> tvgDrawCmds()
+TEST_CASE("Shape Creation", "[tvgShape]")
 {
-    //Prepare a Shape (Rectangle + Rectangle + Circle + Circle)
-    auto shape1 = tvg::Shape::gen();
-    shape1->appendRect(0, 0, 200, 200, 0);          //x, y, w, h, cornerRadius
-    shape1->appendRect(100, 100, 300, 300, 100);    //x, y, w, h, cornerRadius
-    shape1->appendCircle(400, 400, 100, 100);       //cx, cy, radiusW, radiusH
-    shape1->appendCircle(400, 500, 170, 100);       //cx, cy, radiusW, radiusH
-    shape1->fill(255, 255, 0, 255);                 //r, g, b, a
-
-    return move(shape1);
-}
+    auto shape = Shape::gen();
+    REQUIRE(shape);
 
-/************************************************************************/
-/* Sw Engine Test Code                                                  */
-/************************************************************************/
+    REQUIRE(shape->identifier() == Shape::identifier());
+    REQUIRE(shape->identifier() != Picture::identifier());
+    REQUIRE(shape->identifier() != Scene::identifier());
+}
 
-void tvgSwTest(uint32_t* buffer)
+TEST_CASE("Appending Commands", "[tvgShape]")
 {
-    //Initialize ThorVG Engine
-    tvg::Initializer::init(tvg::CanvasEngine::Sw);
-
-    //Create a Canvas
-    auto canvas = tvg::SwCanvas::gen();
-    canvas->target(buffer, WIDTH, WIDTH, HEIGHT);
-
-    /* Push the shape into the Canvas drawing list
-       When this shape is into the canvas list, the shape could update & prepare
-       internal data asynchronously for coming rendering.
-       Canvas keeps this shape node unless user call canvas->clear() */
-    canvas->push(tvgDrawCmds());
-    canvas->draw();
-    canvas->sync();
-
-    //Terminate ThorVG Engine
-    tvg::Initializer::term(tvg::CanvasEngine::Sw);
-}
+    auto shape = Shape::gen();
+    REQUIRE(shape);
 
-/************************************************************************/
-/* GL Engine Test Code                                                  */
-/************************************************************************/
+    REQUIRE(shape->close() == Result::Success);
 
-static unique_ptr<tvg::GlCanvas> canvas;
+    REQUIRE(shape->moveTo(100, 100) == Result::Success);
+    REQUIRE(shape->moveTo(99999999.0f, -99999999.0f) == Result::Success);
+    REQUIRE(shape->moveTo(0, 0) == Result::Success);
 
-void initGLview(Evas_Object *obj)
-{
-    static constexpr auto BPP = 4;
+    REQUIRE(shape->lineTo(120, 140) == Result::Success);
+    REQUIRE(shape->lineTo(99999999.0f, -99999999.0f) == Result::Success);
+    REQUIRE(shape->lineTo(0, 0) == Result::Success);
 
-    //Initialize ThorVG Engine
-    tvg::Initializer::init(tvg::CanvasEngine::Gl);
+    REQUIRE(shape->cubicTo(0, 0, 0, 0, 0, 0) == Result::Success);
+    REQUIRE(shape->cubicTo(0, 0, 99999999.0f, -99999999.0f, 0, 0) == Result::Success);
+    REQUIRE(shape->cubicTo(0, 0, 99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f) == Result::Success);
+    REQUIRE(shape->cubicTo(99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f) == Result::Success);
 
-    //Create a Canvas
-    canvas = tvg::GlCanvas::gen();
-    canvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
+    REQUIRE(shape->close() == Result::Success);
 
-    /* Push the shape into the Canvas drawing list
-       When this shape is into the canvas list, the shape could update & prepare
-       internal data asynchronously for coming rendering.
-       Canvas keeps this shape node unless user call canvas->clear() */
-    canvas->push(tvgDrawCmds());
+    REQUIRE(shape->reset() == Result::Success);
+    REQUIRE(shape->reset() == Result::Success);
 }
 
-void delGLview(Evas_Object *obj)
+TEST_CASE("Appending Shapes", "[tvgShape]")
 {
-    //Terminate ThorVG Engine
-    tvg::Initializer::term(tvg::CanvasEngine::Gl);
+    auto shape = Shape::gen();
+    REQUIRE(shape);
+
+    REQUIRE(shape->moveTo(100, 100) == Result::Success);
+    REQUIRE(shape->lineTo(120, 140) == Result::Success);
+
+    REQUIRE(shape->appendRect(0, 0, 0, 0, 0, 0) == Result::Success);
+    REQUIRE(shape->appendRect(0, 0,99999999.0f, -99999999.0f, 0, 0) == Result::Success);
+    REQUIRE(shape->appendRect(0, 0, 0, 0, -99999999.0f, 99999999.0f) == Result::Success);
+    REQUIRE(shape->appendRect(99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f) == Result::Success);
+
+    REQUIRE(shape->appendCircle(0, 0, 0, 0) == Result::Success);
+    REQUIRE(shape->appendCircle(-99999999.0f, 99999999.0f, 0, 0) == Result::Success);
+    REQUIRE(shape->appendCircle(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f) == Result::Success);
+
+    REQUIRE(shape->appendArc(0, 0, 0, 0, 0, false) == Result::Success);
+    REQUIRE(shape->appendArc(0, 0, 0, 0, 0, true) == Result::Success);
+    REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, 0, 0, 0, false) == Result::Success);
+    REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, 0, 0, 0, true) == Result::Success);
+    REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, 0, false) == Result::Success);
+    REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, 0, true) == Result::Success);
+    REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, -400, false) == Result::Success);
+    REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, 400, true) == Result::Success);
 }
 
-void drawGLview(Evas_Object *obj)
+TEST_CASE("Appending Pathes", "[tvgShape]")
 {
-    auto gl = elm_glview_gl_api_get(obj);
-    int w, h;
-    elm_glview_size_get(obj, &w, &h);
-    gl->glViewport(0, 0, w, h);
-    gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-    gl->glClear(GL_COLOR_BUFFER_BIT);
-    gl->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-    gl->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
-    gl->glEnable(GL_BLEND);
-
-    canvas->draw();
-    canvas->sync();
-}
+    auto shape = Shape::gen();
+    REQUIRE(shape);
+
+    //Negative cases
+    REQUIRE(shape->appendPath(nullptr, 0, nullptr, 0) == Result::InvalidArguments);
+    REQUIRE(shape->appendPath(nullptr, 100, nullptr, 0) == Result::InvalidArguments);
+    REQUIRE(shape->appendPath(nullptr, 0, nullptr, 100) == Result::InvalidArguments);
+
+    PathCommand cmds[5] = {
+        PathCommand::Close,
+        PathCommand::MoveTo,
+        PathCommand::LineTo,
+        PathCommand::CubicTo,
+        PathCommand::Close
+    };
+
+    Point pts[5] = {
+        {100, 100},
+        {200, 200},
+        {10, 10},
+        {20, 20},
+        {30, 30}
+    };
+
+    REQUIRE(shape->appendPath(cmds, 0, pts, 5) == Result::InvalidArguments);
+    REQUIRE(shape->appendPath(cmds, 5, pts, 0) == Result::InvalidArguments);
+    REQUIRE(shape->appendPath(cmds, 5, pts, 5) == Result::Success);
+
+    const PathCommand* cmds2;
+    REQUIRE(shape->pathCommands(&cmds2) == 5);
+
+    const Point* pts2;
+    REQUIRE(shape->pathCoords(&pts2) == 5);
+
+    for (int i = 0; i < 5; ++i) {
+        REQUIRE(cmds2[i] == cmds[i]);
+        REQUIRE(pts[i].x == pts2[i].x);
+        REQUIRE(pts[i].y == pts2[i].y);
+    }
 
-/************************************************************************/
-/* Common Infrastructure Code                                           */
-/************************************************************************/
+    shape->reset();
+    REQUIRE(shape->pathCommands(nullptr) == 0);
+    REQUIRE(shape->pathCoords(nullptr) == 0);
+}
 
-void win_del(void *data, Evas_Object *o, void *ev)
+TEST_CASE("Stroking", "[tvgShape]")
 {
-   elm_exit();
+    auto shape = Shape::gen();
+    REQUIRE(shape);
+
+    //Stroke Width
+    REQUIRE(shape->stroke(0) == Result::Success);
+    REQUIRE(shape->strokeWidth() == 0);
+    REQUIRE(shape->stroke(300) == Result::Success);
+    REQUIRE(shape->strokeWidth() == 300);
+
+    //Stroke Color
+    uint8_t r, g, b, a;
+    REQUIRE(shape->stroke(0, 50, 100, 200) == Result::Success);
+    REQUIRE(shape->strokeColor(nullptr, nullptr, &b, nullptr) == Result::Success);
+    REQUIRE(b == 100);
+    REQUIRE(shape->strokeColor(&r, &g, &b, &a) == Result::Success);
+    REQUIRE(r == 0);
+    REQUIRE(g == 50);
+    REQUIRE(b == 100);
+    REQUIRE(a == 200);
+    REQUIRE(shape->strokeColor(nullptr, nullptr, nullptr, nullptr) == Result::Success);
+
+    //Stroke Dash
+    float dashPattern[3] = {0, 1.5f, 2.22f};
+    REQUIRE(shape->stroke(dashPattern, 3) == Result::InvalidArguments);
+
+    float dashPattern2[3] = {1.0f, 1.5f, 2.22f};
+    REQUIRE(shape->stroke(dashPattern2, 3) == Result::Success);
+
+    const float* dashPattern3;
+    REQUIRE(shape->strokeDash(nullptr) == 3);
+    REQUIRE(shape->strokeDash(&dashPattern3) == 3);
+    REQUIRE(dashPattern3[0] == 1.0f);
+    REQUIRE(dashPattern3[1] == 1.5f);
+    REQUIRE(dashPattern3[2] == 2.22f);
+
+    REQUIRE(shape->stroke(nullptr, 0) == Result::Success);
+
+    //Stroke Cap
+    REQUIRE(shape->strokeCap() == StrokeCap::Square);
+    REQUIRE(shape->stroke(StrokeCap::Round) == Result::Success);
+    REQUIRE(shape->stroke(StrokeCap::Butt) == Result::Success);
+    REQUIRE(shape->strokeCap() == StrokeCap::Butt);
+
+    //Stroke Join
+    REQUIRE(shape->strokeJoin() == StrokeJoin::Bevel);
+    REQUIRE(shape->stroke(StrokeJoin::Miter) == Result::Success);
+    REQUIRE(shape->stroke(StrokeJoin::Round) == Result::Success);
+    REQUIRE(shape->strokeJoin() == StrokeJoin::Round);
 }
 
-int main(int argc, char **argv)
+TEST_CASE("Shape Filling", "[tvgShape]")
 {
-    bool swEngine = true;
-
-    if (argc > 1) {
-        if (!strcmp(argv[1], "gl")) swEngine = false;
-    }
-
-    elm_init(argc, argv);
-
-    //Show the result using EFL...
-    elm_config_accel_preference_set("gl");
-
-    Eo* win = elm_win_util_standard_add(NULL, "ThorVG Test");
-    evas_object_smart_callback_add(win, "delete,request", win_del, 0);
-
-    Eo* viewer;
-
-    if (swEngine) {
-        static uint32_t buffer[WIDTH * HEIGHT];
-        viewer = evas_object_image_filled_add(evas_object_evas_get(win));
-        evas_object_image_size_set(viewer, WIDTH, HEIGHT);
-        evas_object_image_data_set(viewer, buffer);
-        evas_object_size_hint_weight_set(viewer, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
-        evas_object_show(viewer);
-        tvgSwTest(buffer);
-    //GlEngine
-    } else {
-        viewer = elm_glview_add(win);
-        evas_object_size_hint_weight_set(viewer, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
-        elm_glview_mode_set(viewer, ELM_GLVIEW_ALPHA);
-        elm_glview_resize_policy_set(viewer, ELM_GLVIEW_RESIZE_POLICY_RECREATE);
-        elm_glview_render_policy_set(viewer, ELM_GLVIEW_RENDER_POLICY_ON_DEMAND);
-        elm_glview_init_func_set(viewer, initGLview);
-        elm_glview_del_func_set(viewer, delGLview);
-        elm_glview_render_func_set(viewer, drawGLview);
-        evas_object_show(viewer);
-    }
-
-    elm_win_resize_object_add(win, viewer);
-    evas_object_geometry_set(win, 0, 0, WIDTH, HEIGHT);
-    evas_object_show(win);
-
-    elm_run();
-    elm_shutdown();
+    auto shape = Shape::gen();
+    REQUIRE(shape);
+
+    //Fill Color
+    uint8_t r, g, b, a;
+    REQUIRE(shape->fill(255, 100, 50, 5) == Result::Success);
+    REQUIRE(shape->fillColor(&r, nullptr, &b, nullptr) == Result::Success);
+    REQUIRE(r == 255);
+    REQUIRE(b == 50);
+    REQUIRE(shape->fillColor(&r, &g, &b, &a) == Result::Success);
+    REQUIRE(g == 100);
+    REQUIRE(a == 5);
+
+    //Fill Rule
+    REQUIRE(shape->fillRule() == FillRule::Winding);
+    REQUIRE(shape->fill(FillRule::EvenOdd) == Result::Success);
+    REQUIRE(shape->fillRule() == FillRule::EvenOdd);
 }