test_image_texture.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef TEST_IMAGE_TEXTURE_H
  31. #define TEST_IMAGE_TEXTURE_H
  32. #include "core/io/image.h"
  33. #include "scene/resources/image_texture.h"
  34. #include "tests/test_macros.h"
  35. #include "tests/test_utils.h"
  36. namespace TestImageTexture {
  37. // [SceneTree] in a test case name enables initializing a mock render server,
  38. // which ImageTexture is dependent on.
  39. TEST_CASE("[SceneTree][ImageTexture] constructor") {
  40. Ref<ImageTexture> image_texture = memnew(ImageTexture);
  41. CHECK(image_texture->get_width() == 0);
  42. CHECK(image_texture->get_height() == 0);
  43. CHECK(image_texture->get_format() == 0);
  44. CHECK(image_texture->has_alpha() == false);
  45. CHECK(image_texture->get_image() == Ref<Image>());
  46. }
  47. TEST_CASE("[SceneTree][ImageTexture] create_from_image") {
  48. Ref<Image> image = memnew(Image(16, 8, true, Image::FORMAT_RGBA8));
  49. Ref<ImageTexture> image_texture = ImageTexture::create_from_image(image);
  50. CHECK(image_texture->get_width() == 16);
  51. CHECK(image_texture->get_height() == 8);
  52. CHECK(image_texture->get_format() == Image::FORMAT_RGBA8);
  53. CHECK(image_texture->has_alpha() == true);
  54. CHECK(image_texture->get_rid().is_valid() == true);
  55. }
  56. TEST_CASE("[SceneTree][ImageTexture] set_image") {
  57. Ref<ImageTexture> image_texture = memnew(ImageTexture);
  58. Ref<Image> image = memnew(Image(8, 4, false, Image::FORMAT_RGB8));
  59. image_texture->set_image(image);
  60. CHECK(image_texture->get_width() == 8);
  61. CHECK(image_texture->get_height() == 4);
  62. CHECK(image_texture->get_format() == Image::FORMAT_RGB8);
  63. CHECK(image_texture->has_alpha() == false);
  64. CHECK(image_texture->get_width() == image_texture->get_image()->get_width());
  65. CHECK(image_texture->get_height() == image_texture->get_image()->get_height());
  66. CHECK(image_texture->get_format() == image_texture->get_image()->get_format());
  67. }
  68. TEST_CASE("[SceneTree][ImageTexture] set_size_override") {
  69. Ref<Image> image = memnew(Image(16, 8, false, Image::FORMAT_RGB8));
  70. Ref<ImageTexture> image_texture = ImageTexture::create_from_image(image);
  71. CHECK(image_texture->get_width() == 16);
  72. CHECK(image_texture->get_height() == 8);
  73. image_texture->set_size_override(Size2i(32, 16));
  74. CHECK(image_texture->get_width() == 32);
  75. CHECK(image_texture->get_height() == 16);
  76. }
  77. TEST_CASE("[SceneTree][ImageTexture] is_pixel_opaque") {
  78. Ref<Image> image = memnew(Image(8, 8, false, Image::FORMAT_RGBA8));
  79. image->set_pixel(0, 0, Color(0.0, 0.0, 0.0, 0.0)); // not opaque
  80. image->set_pixel(0, 1, Color(0.0, 0.0, 0.0, 0.1)); // not opaque
  81. image->set_pixel(0, 2, Color(0.0, 0.0, 0.0, 0.5)); // opaque
  82. image->set_pixel(0, 3, Color(0.0, 0.0, 0.0, 0.9)); // opaque
  83. image->set_pixel(0, 4, Color(0.0, 0.0, 0.0, 1.0)); // opaque
  84. Ref<ImageTexture> image_texture = ImageTexture::create_from_image(image);
  85. CHECK(image_texture->is_pixel_opaque(0, 0) == false);
  86. CHECK(image_texture->is_pixel_opaque(0, 1) == false);
  87. CHECK(image_texture->is_pixel_opaque(0, 2) == true);
  88. CHECK(image_texture->is_pixel_opaque(0, 3) == true);
  89. CHECK(image_texture->is_pixel_opaque(0, 4) == true);
  90. }
  91. TEST_CASE("[SceneTree][ImageTexture] set_path") {
  92. Ref<ImageTexture> image_texture = memnew(ImageTexture);
  93. String path = TestUtils::get_data_path("images/icon.png");
  94. image_texture->set_path(path, true);
  95. CHECK(image_texture->get_path() == path);
  96. }
  97. } //namespace TestImageTexture
  98. #endif // TEST_IMAGE_TEXTURE_H