|
| 1 | +return function() |
| 2 | + describe("Container Component", function() |
| 3 | + local mock_time |
| 4 | + local mock_input |
| 5 | + local druid_system |
| 6 | + local const |
| 7 | + |
| 8 | + local druid |
| 9 | + local context |
| 10 | + |
| 11 | + before(function() |
| 12 | + mock_time = require("deftest.mock.time") |
| 13 | + mock_input = require("test.helper.mock_input") |
| 14 | + druid_system = require("druid.druid") |
| 15 | + const = require("druid.const") |
| 16 | + |
| 17 | + mock_time.mock() |
| 18 | + mock_time.set(0) |
| 19 | + |
| 20 | + context = vmath.vector3() |
| 21 | + druid = druid_system.new(context) |
| 22 | + end) |
| 23 | + |
| 24 | + after(function() |
| 25 | + mock_time.unmock() |
| 26 | + druid:final() |
| 27 | + druid = nil |
| 28 | + end) |
| 29 | + |
| 30 | + |
| 31 | + it("Should initialize container with default settings", function() |
| 32 | + local container_node = gui.new_box_node(vmath.vector3(50, 50, 0), vmath.vector3(100, 100, 0)) |
| 33 | + local container = druid:new_container(container_node) |
| 34 | + |
| 35 | + assert(container ~= nil) |
| 36 | + assert(container.node == container_node) |
| 37 | + assert(container.origin_size.x == 100) |
| 38 | + assert(container.origin_size.y == 100) |
| 39 | + assert(container.size.x == 100) |
| 40 | + assert(container.size.y == 100) |
| 41 | + assert(container.mode == const.LAYOUT_MODE.FIT) |
| 42 | + assert(container.min_size_x == 0) |
| 43 | + assert(container.min_size_y == 0) |
| 44 | + assert(container._containers ~= nil) |
| 45 | + assert(#container._containers == 0) |
| 46 | + |
| 47 | + druid:remove(container) |
| 48 | + gui.delete_node(container_node) |
| 49 | + end) |
| 50 | + |
| 51 | + it("Should get and set size", function() |
| 52 | + local container_node = gui.new_box_node(vmath.vector3(50, 50, 0), vmath.vector3(100, 100, 0)) |
| 53 | + local container = druid:new_container(container_node) |
| 54 | + |
| 55 | + local size = container:get_size() |
| 56 | + assert(size.x == 100) |
| 57 | + assert(size.y == 100) |
| 58 | + |
| 59 | + container:set_size(200, 150) |
| 60 | + |
| 61 | + size = container:get_size() |
| 62 | + assert(size.x == 200) |
| 63 | + assert(size.y == 150) |
| 64 | + |
| 65 | + local node_size = gui.get_size(container_node) |
| 66 | + assert(node_size.x == 200) |
| 67 | + assert(node_size.y == 150) |
| 68 | + |
| 69 | + druid:remove(container) |
| 70 | + gui.delete_node(container_node) |
| 71 | + end) |
| 72 | + |
| 73 | + it("Should get and set position", function() |
| 74 | + local container_node = gui.new_box_node(vmath.vector3(50, 50, 0), vmath.vector3(100, 100, 0)) |
| 75 | + local container = druid:new_container(container_node) |
| 76 | + |
| 77 | + local position = container:get_position() |
| 78 | + assert(position.x == 50) |
| 79 | + assert(position.y == 50) |
| 80 | + |
| 81 | + container:set_position(100, 200) |
| 82 | + |
| 83 | + position = container:get_position() |
| 84 | + assert(position.x == 100) |
| 85 | + assert(position.y == 200) |
| 86 | + |
| 87 | + local node_position = gui.get_position(container_node) |
| 88 | + assert(node_position.x == 100) |
| 89 | + assert(node_position.y == 200) |
| 90 | + |
| 91 | + druid:remove(container) |
| 92 | + gui.delete_node(container_node) |
| 93 | + end) |
| 94 | + |
| 95 | + it("Should set pivot", function() |
| 96 | + local container_node = gui.new_box_node(vmath.vector3(50, 50, 0), vmath.vector3(100, 100, 0)) |
| 97 | + local container = druid:new_container(container_node) |
| 98 | + |
| 99 | + -- Default pivot is typically PIVOT_CENTER |
| 100 | + local initial_pivot = gui.get_pivot(container_node) |
| 101 | + |
| 102 | + container:set_pivot(gui.PIVOT_NW) |
| 103 | + assert(gui.get_pivot(container_node) == gui.PIVOT_NW) |
| 104 | + |
| 105 | + container:set_pivot(gui.PIVOT_SE) |
| 106 | + assert(gui.get_pivot(container_node) == gui.PIVOT_SE) |
| 107 | + |
| 108 | + druid:remove(container) |
| 109 | + gui.delete_node(container_node) |
| 110 | + end) |
| 111 | + |
| 112 | + it("Should set min size", function() |
| 113 | + local container_node = gui.new_box_node(vmath.vector3(50, 50, 0), vmath.vector3(100, 100, 0)) |
| 114 | + local container = druid:new_container(container_node) |
| 115 | + |
| 116 | + assert(container.min_size_x == 0) |
| 117 | + assert(container.min_size_y == 0) |
| 118 | + |
| 119 | + container:set_min_size(50, 75) |
| 120 | + |
| 121 | + assert(container.min_size_x == 50) |
| 122 | + assert(container.min_size_y == 75) |
| 123 | + |
| 124 | + -- Should respect min size when setting smaller size |
| 125 | + container:set_size(25, 25) |
| 126 | + local size = container:get_size() |
| 127 | + assert(size.x == 50) |
| 128 | + assert(size.y == 75) |
| 129 | + |
| 130 | + -- Should allow larger size |
| 131 | + container:set_size(200, 200) |
| 132 | + size = container:get_size() |
| 133 | + assert(size.x == 200) |
| 134 | + assert(size.y == 200) |
| 135 | + |
| 136 | + druid:remove(container) |
| 137 | + gui.delete_node(container_node) |
| 138 | + end) |
| 139 | + |
| 140 | + it("Should fire on_size_changed event", function() |
| 141 | + local container_node = gui.new_box_node(vmath.vector3(50, 50, 0), vmath.vector3(100, 100, 0)) |
| 142 | + local container = druid:new_container(container_node) |
| 143 | + |
| 144 | + local on_size_changed_calls = 0 |
| 145 | + local last_size = nil |
| 146 | + |
| 147 | + container.on_size_changed:subscribe(function(instance, new_size) |
| 148 | + on_size_changed_calls = on_size_changed_calls + 1 |
| 149 | + last_size = new_size |
| 150 | + end) |
| 151 | + |
| 152 | + container:set_size(200, 150) |
| 153 | + |
| 154 | + assert(on_size_changed_calls == 1) |
| 155 | + assert(last_size ~= nil) |
| 156 | + assert(last_size.x == 200) |
| 157 | + assert(last_size.y == 150) |
| 158 | + |
| 159 | + druid:remove(container) |
| 160 | + gui.delete_node(container_node) |
| 161 | + end) |
| 162 | + |
| 163 | + it("Should fit into custom size", function() |
| 164 | + local container_node = gui.new_box_node(vmath.vector3(50, 50, 0), vmath.vector3(100, 100, 0)) |
| 165 | + local container = druid:new_container(container_node, const.LAYOUT_MODE.STRETCH) |
| 166 | + |
| 167 | + local fit_size = vmath.vector3(200, 150, 0) |
| 168 | + container:fit_into_size(fit_size) |
| 169 | + |
| 170 | + assert(container.fit_size == fit_size) |
| 171 | + |
| 172 | + -- The exact result will depend on the implementation and screen aspect ratio, |
| 173 | + -- but we can at least verify it changes the size |
| 174 | + local size = container:get_size() |
| 175 | + assert(size.x > 100 or size.y > 100) |
| 176 | + |
| 177 | + druid:remove(container) |
| 178 | + gui.delete_node(container_node) |
| 179 | + end) |
| 180 | + |
| 181 | + it("Should create and manage parent-child container relationships", function() |
| 182 | + local parent_node = gui.new_box_node(vmath.vector3(400, 300, 0), vmath.vector3(800, 600, 0)) |
| 183 | + local child_node = gui.new_box_node(vmath.vector3(100, 100, 0), vmath.vector3(200, 200, 0)) |
| 184 | + |
| 185 | + local parent_container = druid:new_container(parent_node) |
| 186 | + |
| 187 | + -- Add a child container through the parent |
| 188 | + local child_container = parent_container:add_container(child_node) |
| 189 | + |
| 190 | + assert(child_container ~= nil) |
| 191 | + assert(child_container.node == child_node) |
| 192 | + assert(child_container._parent_container == parent_container) |
| 193 | + assert(#parent_container._containers == 1) |
| 194 | + assert(parent_container._containers[1] == child_container) |
| 195 | + |
| 196 | + -- Verify the child's node has been parented correctly |
| 197 | + assert(gui.get_parent(child_node) == parent_node) |
| 198 | + |
| 199 | + -- Remove the child container |
| 200 | + parent_container:remove_container_by_node(child_node) |
| 201 | + |
| 202 | + assert(#parent_container._containers == 0) |
| 203 | + |
| 204 | + druid:remove(parent_container) |
| 205 | + gui.delete_node(parent_node) |
| 206 | + gui.delete_node(child_node) |
| 207 | + end) |
| 208 | + |
| 209 | + it("Should handle different layout modes", function() |
| 210 | + local parent_node = gui.new_box_node(vmath.vector3(400, 300, 0), vmath.vector3(800, 600, 0)) |
| 211 | + local child_node = gui.new_box_node(vmath.vector3(100, 100, 0), vmath.vector3(200, 200, 0)) |
| 212 | + |
| 213 | + local parent_container = druid:new_container(parent_node) |
| 214 | + |
| 215 | + -- Test FIT mode |
| 216 | + local child_fit = parent_container:add_container(child_node, const.LAYOUT_MODE.FIT) |
| 217 | + assert(child_fit.mode == const.LAYOUT_MODE.FIT) |
| 218 | + |
| 219 | + local size_fit = child_fit:get_size() |
| 220 | + local original_size = vmath.vector3(200, 200, 0) |
| 221 | + |
| 222 | + -- Size should remain the same in FIT mode |
| 223 | + assert(math.abs(size_fit.x - original_size.x) < 0.001) |
| 224 | + assert(math.abs(size_fit.y - original_size.y) < 0.001) |
| 225 | + |
| 226 | + -- Set to STRETCH mode |
| 227 | + parent_container:remove_container_by_node(child_node) |
| 228 | + local child_stretch = parent_container:add_container(child_node, const.LAYOUT_MODE.STRETCH) |
| 229 | + assert(child_stretch.mode == const.LAYOUT_MODE.STRETCH) |
| 230 | + |
| 231 | + -- Set to STRETCH_X mode |
| 232 | + parent_container:remove_container_by_node(child_node) |
| 233 | + local child_stretch_x = parent_container:add_container(child_node, const.LAYOUT_MODE.STRETCH_X) |
| 234 | + assert(child_stretch_x.mode == const.LAYOUT_MODE.STRETCH_X) |
| 235 | + |
| 236 | + -- Set to STRETCH_Y mode |
| 237 | + parent_container:remove_container_by_node(child_node) |
| 238 | + local child_stretch_y = parent_container:add_container(child_node, const.LAYOUT_MODE.STRETCH_Y) |
| 239 | + assert(child_stretch_y.mode == const.LAYOUT_MODE.STRETCH_Y) |
| 240 | + |
| 241 | + druid:remove(parent_container) |
| 242 | + gui.delete_node(parent_node) |
| 243 | + gui.delete_node(child_node) |
| 244 | + end) |
| 245 | + |
| 246 | + it("Should create and clear draggable corners", function() |
| 247 | + local container_node = gui.new_box_node(vmath.vector3(50, 50, 0), vmath.vector3(100, 100, 0)) |
| 248 | + local container = druid:new_container(container_node) |
| 249 | + |
| 250 | + container:create_draggable_corners() |
| 251 | + |
| 252 | + assert(#container._draggable_corners > 0) |
| 253 | + |
| 254 | + container:clear_draggable_corners() |
| 255 | + |
| 256 | + assert(#container._draggable_corners == 0) |
| 257 | + |
| 258 | + druid:remove(container) |
| 259 | + gui.delete_node(container_node) |
| 260 | + end) |
| 261 | + |
| 262 | + it("Should set size and maintain anchor pivot position", function() |
| 263 | + local container_node = gui.new_box_node(vmath.vector3(50, 50, 0), vmath.vector3(100, 100, 0)) |
| 264 | + local container = druid:new_container(container_node) |
| 265 | + |
| 266 | + -- Set pivot to NW corner |
| 267 | + container:set_pivot(gui.PIVOT_NW) |
| 268 | + local initial_position = gui.get_position(container_node) |
| 269 | + |
| 270 | + -- Change size with anchor_pivot=PIVOT_NW - the NW corner should stay at the same position |
| 271 | + container:set_size(200, 150, gui.PIVOT_NW) |
| 272 | + |
| 273 | + local new_position = gui.get_position(container_node) |
| 274 | + assert(math.abs(new_position.x - initial_position.x) < 0.001) |
| 275 | + assert(math.abs(new_position.y - initial_position.y) < 0.001) |
| 276 | + |
| 277 | + -- Set pivot to SE corner |
| 278 | + container:set_pivot(gui.PIVOT_SE) |
| 279 | + initial_position = gui.get_position(container_node) |
| 280 | + |
| 281 | + -- Change size with anchor_pivot=PIVOT_SE - the SE corner should stay at the same position |
| 282 | + container:set_size(300, 250, gui.PIVOT_SE) |
| 283 | + |
| 284 | + new_position = gui.get_position(container_node) |
| 285 | + assert(math.abs(new_position.x - initial_position.x) < 0.001) |
| 286 | + assert(math.abs(new_position.y - initial_position.y) < 0.001) |
| 287 | + |
| 288 | + druid:remove(container) |
| 289 | + gui.delete_node(container_node) |
| 290 | + end) |
| 291 | + |
| 292 | + it("Should refresh origins", function() |
| 293 | + local container_node = gui.new_box_node(vmath.vector3(50, 50, 0), vmath.vector3(100, 100, 0)) |
| 294 | + local container = druid:new_container(container_node) |
| 295 | + |
| 296 | + -- Change the node size and position directly |
| 297 | + gui.set_size(container_node, vmath.vector3(200, 150, 0)) |
| 298 | + gui.set_position(container_node, vmath.vector3(75, 75, 0)) |
| 299 | + |
| 300 | + -- The container's internal origin values should not have updated yet |
| 301 | + assert(container.origin_size.x == 100) |
| 302 | + assert(container.origin_size.y == 100) |
| 303 | + assert(container.origin_position.x == 50) |
| 304 | + assert(container.origin_position.y == 50) |
| 305 | + |
| 306 | + -- Refresh origins |
| 307 | + container:refresh_origins() |
| 308 | + |
| 309 | + -- Now the origin values should match the node |
| 310 | + assert(container.origin_size.x == 200) |
| 311 | + assert(container.origin_size.y == 150) |
| 312 | + assert(container.origin_position.x == 75) |
| 313 | + assert(container.origin_position.y == 75) |
| 314 | + |
| 315 | + druid:remove(container) |
| 316 | + gui.delete_node(container_node) |
| 317 | + end) |
| 318 | + end) |
| 319 | +end |
0 commit comments