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