view tests/libclient/image/main.cpp @ 45:719b5457ea92

Misc: add .txt extension to Duktape
author David Demelier <markand@malikania.fr>
date Wed, 30 Nov 2016 21:16:34 +0100
parents f30c84b4b9ed
children fe7e3524e571
line wrap: on
line source

/*
 * 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);
}

/*
 * Draw
 * ------------------------------------------------------------------
 */

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()