test_image_texture.h 5.3 KB

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