| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "scene/resources/gradient.h" |
| 34 |
|
| 35 |
#include "thirdparty/doctest/doctest.h" |
| 36 |
|
| 37 |
namespace TestGradient { |
| 38 |
|
| 39 |
TEST_CASE("[Gradient] Default gradient") { |
| 40 |
|
| 41 |
Ref<Gradient> gradient = memnew(Gradient); |
| 42 |
|
| 43 |
CHECK_MESSAGE( |
| 44 |
gradient->get_point_count() == 2, |
| 45 |
"Default gradient should contain the expected number of points."); |
| 46 |
|
| 47 |
CHECK_MESSAGE( |
| 48 |
gradient->get_color_at_offset(0.0).is_equal_approx(Color(0, 0, 0)), |
| 49 |
"Default gradient should return the expected interpolated value at offset 0.0."); |
| 50 |
CHECK_MESSAGE( |
| 51 |
gradient->get_color_at_offset(0.4).is_equal_approx(Color(0.4, 0.4, 0.4)), |
| 52 |
"Default gradient should return the expected interpolated value at offset 0.4."); |
| 53 |
CHECK_MESSAGE( |
| 54 |
gradient->get_color_at_offset(0.8).is_equal_approx(Color(0.8, 0.8, 0.8)), |
| 55 |
"Default gradient should return the expected interpolated value at offset 0.8."); |
| 56 |
CHECK_MESSAGE( |
| 57 |
gradient->get_color_at_offset(1.0).is_equal_approx(Color(1, 1, 1)), |
| 58 |
"Default gradient should return the expected interpolated value at offset 1.0."); |
| 59 |
|
| 60 |
|
| 61 |
CHECK_MESSAGE( |
| 62 |
gradient->get_color_at_offset(-1.0).is_equal_approx(Color(0, 0, 0)), |
| 63 |
"Default gradient should return the expected interpolated value at offset -1.0."); |
| 64 |
CHECK_MESSAGE( |
| 65 |
gradient->get_color_at_offset(1234.0).is_equal_approx(Color(1, 1, 1)), |
| 66 |
"Default gradient should return the expected interpolated value at offset 1234.0."); |
| 67 |
} |
| 68 |
|
| 69 |
TEST_CASE("[Gradient] Custom gradient (points specified in order)") { |
| 70 |
|
| 71 |
Ref<Gradient> gradient = memnew(Gradient); |
| 72 |
Vector<float> offsets = { 0.0, 0.5, 1.0 }; |
| 73 |
Vector<Color> colors = { Color(1, 0, 0), Color(1, 1, 0), Color(0, 2, 0) }; |
| 74 |
|
| 75 |
gradient->set_offsets(offsets); |
| 76 |
gradient->set_colors(colors); |
| 77 |
|
| 78 |
CHECK_MESSAGE( |
| 79 |
gradient->get_point_count() == 3, |
| 80 |
"Custom gradient should contain the expected number of points."); |
| 81 |
|
| 82 |
CHECK_MESSAGE( |
| 83 |
gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 0, 0)), |
| 84 |
"Custom gradient should return the expected interpolated value at offset 0.0."); |
| 85 |
CHECK_MESSAGE( |
| 86 |
gradient->get_color_at_offset(0.25).is_equal_approx(Color(1, 0.5, 0)), |
| 87 |
"Custom gradient should return the expected interpolated value at offset 0.25."); |
| 88 |
CHECK_MESSAGE( |
| 89 |
gradient->get_color_at_offset(0.5).is_equal_approx(Color(1, 1, 0)), |
| 90 |
"Custom gradient should return the expected interpolated value at offset 0.5."); |
| 91 |
CHECK_MESSAGE( |
| 92 |
gradient->get_color_at_offset(0.75).is_equal_approx(Color(0.5, 1.5, 0)), |
| 93 |
"Custom gradient should return the expected interpolated value at offset 0.75."); |
| 94 |
CHECK_MESSAGE( |
| 95 |
gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 2, 0)), |
| 96 |
"Custom gradient should return the expected interpolated value at offset 1.0."); |
| 97 |
|
| 98 |
gradient->remove_point(1); |
| 99 |
CHECK_MESSAGE( |
| 100 |
gradient->get_point_count() == 2, |
| 101 |
"Custom gradient should contain the expected number of points after removing one point."); |
| 102 |
CHECK_MESSAGE( |
| 103 |
gradient->get_color_at_offset(0.5).is_equal_approx(Color(0.5, 1, 0)), |
| 104 |
"Custom gradient should return the expected interpolated value at offset 0.5 after removing point at index 1."); |
| 105 |
} |
| 106 |
|
| 107 |
TEST_CASE("[Gradient] Custom gradient (points specified out-of-order)") { |
| 108 |
|
| 109 |
|
| 110 |
Ref<Gradient> gradient = memnew(Gradient); |
| 111 |
LocalVector<Gradient::Point> points; |
| 112 |
Vector<float> offsets = { 0.2, 0.0, 0.8, 0.4, 1.0, 0.6 }; |
| 113 |
Vector<Color> colors = { Color(1, 0, 0), Color(1, 1, 0), Color(0, 1, 0), Color(0, 1, 1), Color(0, 0, 1), Color(1, 0, 1) }; |
| 114 |
|
| 115 |
gradient->set_offsets(offsets); |
| 116 |
gradient->set_colors(colors); |
| 117 |
|
| 118 |
CHECK_MESSAGE( |
| 119 |
gradient->get_point_count() == 6, |
| 120 |
"Custom out-of-order gradient should contain the expected number of points."); |
| 121 |
|
| 122 |
CHECK_MESSAGE( |
| 123 |
gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 1, 0)), |
| 124 |
"Custom out-of-order gradient should return the expected interpolated value at offset 0.0."); |
| 125 |
CHECK_MESSAGE( |
| 126 |
gradient->get_color_at_offset(0.3).is_equal_approx(Color(0.5, 0.5, 0.5)), |
| 127 |
"Custom out-of-order gradient should return the expected interpolated value at offset 0.3."); |
| 128 |
CHECK_MESSAGE( |
| 129 |
gradient->get_color_at_offset(0.6).is_equal_approx(Color(1, 0, 1)), |
| 130 |
"Custom out-of-order gradient should return the expected interpolated value at offset 0.6."); |
| 131 |
CHECK_MESSAGE( |
| 132 |
gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 0, 1)), |
| 133 |
"Custom out-of-order gradient should return the expected interpolated value at offset 1.0."); |
| 134 |
|
| 135 |
gradient->remove_point(0); |
| 136 |
CHECK_MESSAGE( |
| 137 |
gradient->get_point_count() == 5, |
| 138 |
"Custom out-of-order gradient should contain the expected number of points after removing one point."); |
| 139 |
|
| 140 |
CHECK_MESSAGE( |
| 141 |
gradient->get_color_at_offset(0.1).is_equal_approx(Color(1, 0, 0)), |
| 142 |
"Custom out-of-order gradient should return the expected interpolated value at offset 0.1 after removing point at index 0."); |
| 143 |
} |
| 144 |
} |