diff examples/image/main.cpp @ 74:78de82cc6bde

Tests: move graphical examples into examples, closes #606
author David Demelier <markand@malikania.fr>
date Thu, 22 Dec 2016 15:17:43 +0100
parents
children a162f380f02e
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/image/main.cpp	Thu Dec 22 15:17:43 2016 +0100
@@ -0,0 +1,111 @@
+/*
+ * main.cpp -- test Image
+ *
+ * Copyright (c) 2013-2016 Malikania Authors
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <chrono>
+#include <thread>
+
+#define BOOST_TEST_MODULE "Image"
+#include <boost/test/unit_test.hpp>
+
+#include <malikania/client_resources_loader.hpp>
+#include <malikania/image.hpp>
+#include <malikania/resources_locator.hpp>
+#include <malikania/window.hpp>
+
+using namespace std::chrono_literals;
+
+namespace {
+
+mlk::window window(400, 400);
+
+} // !namespace
+
+class test_image {
+protected:
+    mlk::directory_resources_locator m_locator;
+    mlk::client_resources_loader m_loader;
+
+public:
+    test_image()
+        : m_locator(SOURCE_DIRECTORY "/resources")
+        , m_loader(m_locator)
+    {
+    }
+};
+
+BOOST_FIXTURE_TEST_SUITE(test_image_suite, test_image)
+
+BOOST_AUTO_TEST_CASE(image100x10)
+{
+    try {
+        auto image = m_loader.load_image("images/100x10.png");
+
+        BOOST_REQUIRE_EQUAL(100U, image.size().width());
+        BOOST_REQUIRE_EQUAL(10U, image.size().height());
+    } catch (const std::exception &ex) {
+        BOOST_FAIL(ex.what());
+    }
+}
+
+BOOST_AUTO_TEST_CASE(image16x16)
+{
+    try {
+        auto image = m_loader.load_image("images/16x16.png");
+
+        BOOST_REQUIRE_EQUAL(16U, image.size().width());
+        BOOST_REQUIRE_EQUAL(16U, image.size().height());
+    } catch (const std::exception &ex) {
+        BOOST_FAIL(ex.what());
+    }
+}
+
+BOOST_AUTO_TEST_CASE(image10x100)
+{
+    try {
+        auto image = m_loader.load_image("images/10x100.png");
+
+        BOOST_REQUIRE_EQUAL(10U, image.size().width());
+        BOOST_REQUIRE_EQUAL(100U, image.size().height());
+    } catch (const std::exception &ex) {
+        BOOST_FAIL(ex.what());
+    }
+}
+
+BOOST_AUTO_TEST_CASE(notfound)
+{
+    BOOST_REQUIRE_THROW(m_loader.load_image("image-not-found"), std::exception);
+}
+
+BOOST_AUTO_TEST_CASE(draw)
+{
+    try {
+        auto image = m_loader.load_image("images/smiley.png");
+        auto x = (400 / 2) - (image.size().width() / 2);
+        auto y = (400 / 2) - (image.size().height() / 2);
+
+        window.clear();
+        image.draw(window, mlk::point(x, y));
+        window.present();
+
+        std::this_thread::sleep_for(3s);
+    } catch (const std::exception &ex) {
+        BOOST_FAIL(ex.what());
+    }
+}
+
+BOOST_AUTO_TEST_SUITE_END()