Path: blob/master/tests/scene/test_image_texture.cpp
45991 views
/**************************************************************************/1/* test_image_texture.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_image_texture)3334#include "core/io/image.h"35#include "scene/resources/image_texture.h"36#include "tests/test_utils.h"3738namespace TestImageTexture {3940// [SceneTree] in a test case name enables initializing a mock render server,41// which ImageTexture is dependent on.42TEST_CASE("[SceneTree][ImageTexture] constructor") {43Ref<ImageTexture> image_texture = memnew(ImageTexture);44CHECK(image_texture->get_width() == 0);45CHECK(image_texture->get_height() == 0);46CHECK(image_texture->get_format() == 0);47CHECK(image_texture->has_alpha() == false);48CHECK(image_texture->get_image() == Ref<Image>());49}5051TEST_CASE("[SceneTree][ImageTexture] create_from_image") {52Ref<Image> image = memnew(Image(16, 8, true, Image::FORMAT_RGBA8));53Ref<ImageTexture> image_texture = ImageTexture::create_from_image(image);54CHECK(image_texture->get_width() == 16);55CHECK(image_texture->get_height() == 8);56CHECK(image_texture->get_format() == Image::FORMAT_RGBA8);57CHECK(image_texture->has_alpha() == true);58CHECK(image_texture->get_rid().is_valid() == true);59}6061TEST_CASE("[SceneTree][ImageTexture] set_image") {62Ref<ImageTexture> image_texture = memnew(ImageTexture);63Ref<Image> image = memnew(Image(8, 4, false, Image::FORMAT_RGB8));64image_texture->set_image(image);65CHECK(image_texture->get_width() == 8);66CHECK(image_texture->get_height() == 4);67CHECK(image_texture->get_format() == Image::FORMAT_RGB8);68CHECK(image_texture->has_alpha() == false);69CHECK(image_texture->get_width() == image_texture->get_image()->get_width());70CHECK(image_texture->get_height() == image_texture->get_image()->get_height());71CHECK(image_texture->get_format() == image_texture->get_image()->get_format());72}7374TEST_CASE("[SceneTree][ImageTexture] set_size_override") {75Ref<Image> image = memnew(Image(16, 8, false, Image::FORMAT_RGB8));76Ref<ImageTexture> image_texture = ImageTexture::create_from_image(image);77CHECK(image_texture->get_width() == 16);78CHECK(image_texture->get_height() == 8);79image_texture->set_size_override(Size2i(32, 16));80CHECK(image_texture->get_width() == 32);81CHECK(image_texture->get_height() == 16);82}8384TEST_CASE("[SceneTree][ImageTexture] is_pixel_opaque") {85Ref<Image> image = memnew(Image(8, 8, false, Image::FORMAT_RGBA8));86image->set_pixel(0, 0, Color(0.0, 0.0, 0.0, 0.0)); // not opaque87image->set_pixel(0, 1, Color(0.0, 0.0, 0.0, 0.1)); // not opaque88image->set_pixel(0, 2, Color(0.0, 0.0, 0.0, 0.5)); // opaque89image->set_pixel(0, 3, Color(0.0, 0.0, 0.0, 0.9)); // opaque90image->set_pixel(0, 4, Color(0.0, 0.0, 0.0, 1.0)); // opaque9192Ref<ImageTexture> image_texture = ImageTexture::create_from_image(image);93CHECK(image_texture->is_pixel_opaque(0, 0) == false);94CHECK(image_texture->is_pixel_opaque(0, 1) == false);95CHECK(image_texture->is_pixel_opaque(0, 2) == true);96CHECK(image_texture->is_pixel_opaque(0, 3) == true);97CHECK(image_texture->is_pixel_opaque(0, 4) == true);98}99100TEST_CASE("[SceneTree][ImageTexture] set_path") {101Ref<ImageTexture> image_texture = memnew(ImageTexture);102String path = TestUtils::get_data_path("images/icon.png");103image_texture->set_path(path, true);104CHECK(image_texture->get_path() == path);105}106107} // namespace TestImageTexture108109110