|
| 1 | +/**************************************************************************/ |
| 2 | +/* test_tree.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 | +#ifndef TEST_TREE_H |
| 32 | +#define TEST_TREE_H |
| 33 | + |
| 34 | +#include "scene/gui/tree.h" |
| 35 | + |
| 36 | +#include "tests/test_macros.h" |
| 37 | + |
| 38 | +namespace TestTree { |
| 39 | + |
| 40 | +TEST_CASE("[SceneTree][Tree]") { |
| 41 | + SUBCASE("[Tree] Create and remove items.") { |
| 42 | + Tree *tree = memnew(Tree); |
| 43 | + TreeItem *root = tree->create_item(); |
| 44 | + |
| 45 | + TreeItem *child1 = tree->create_item(); |
| 46 | + CHECK_EQ(root->get_child_count(), 1); |
| 47 | + |
| 48 | + TreeItem *child2 = tree->create_item(root); |
| 49 | + CHECK_EQ(root->get_child_count(), 2); |
| 50 | + |
| 51 | + TreeItem *child3 = tree->create_item(root, 0); |
| 52 | + CHECK_EQ(root->get_child_count(), 3); |
| 53 | + |
| 54 | + CHECK_EQ(root->get_child(0), child3); |
| 55 | + CHECK_EQ(root->get_child(1), child1); |
| 56 | + CHECK_EQ(root->get_child(2), child2); |
| 57 | + |
| 58 | + root->remove_child(child3); |
| 59 | + CHECK_EQ(root->get_child_count(), 2); |
| 60 | + |
| 61 | + root->add_child(child3); |
| 62 | + CHECK_EQ(root->get_child_count(), 3); |
| 63 | + |
| 64 | + TreeItem *child4 = root->create_child(); |
| 65 | + CHECK_EQ(root->get_child_count(), 4); |
| 66 | + |
| 67 | + CHECK_EQ(root->get_child(0), child1); |
| 68 | + CHECK_EQ(root->get_child(1), child2); |
| 69 | + CHECK_EQ(root->get_child(2), child3); |
| 70 | + CHECK_EQ(root->get_child(3), child4); |
| 71 | + |
| 72 | + memdelete(tree); |
| 73 | + } |
| 74 | + |
| 75 | + SUBCASE("[Tree] Clear items.") { |
| 76 | + Tree *tree = memnew(Tree); |
| 77 | + TreeItem *root = tree->create_item(); |
| 78 | + |
| 79 | + for (int i = 0; i < 10; i++) { |
| 80 | + tree->create_item(); |
| 81 | + } |
| 82 | + CHECK_EQ(root->get_child_count(), 10); |
| 83 | + |
| 84 | + root->clear_children(); |
| 85 | + CHECK_EQ(root->get_child_count(), 0); |
| 86 | + |
| 87 | + memdelete(tree); |
| 88 | + } |
| 89 | + |
| 90 | + SUBCASE("[Tree] Get last item.") { |
| 91 | + Tree *tree = memnew(Tree); |
| 92 | + TreeItem *root = tree->create_item(); |
| 93 | + |
| 94 | + TreeItem *last; |
| 95 | + for (int i = 0; i < 10; i++) { |
| 96 | + last = tree->create_item(); |
| 97 | + } |
| 98 | + CHECK_EQ(root->get_child_count(), 10); |
| 99 | + CHECK_EQ(tree->get_last_item(), last); |
| 100 | + |
| 101 | + // Check nested. |
| 102 | + TreeItem *old_last = last; |
| 103 | + for (int i = 0; i < 10; i++) { |
| 104 | + last = tree->create_item(old_last); |
| 105 | + } |
| 106 | + CHECK_EQ(tree->get_last_item(), last); |
| 107 | + |
| 108 | + memdelete(tree); |
| 109 | + } |
| 110 | + |
| 111 | + SUBCASE("[Tree] Previous and Next items.") { |
| 112 | + Tree *tree = memnew(Tree); |
| 113 | + TreeItem *root = tree->create_item(); |
| 114 | + |
| 115 | + TreeItem *child1 = tree->create_item(); |
| 116 | + TreeItem *child2 = tree->create_item(); |
| 117 | + TreeItem *child3 = tree->create_item(); |
| 118 | + CHECK_EQ(child1->get_next(), child2); |
| 119 | + CHECK_EQ(child1->get_next_in_tree(), child2); |
| 120 | + CHECK_EQ(child2->get_next(), child3); |
| 121 | + CHECK_EQ(child2->get_next_in_tree(), child3); |
| 122 | + CHECK_EQ(child3->get_next(), nullptr); |
| 123 | + CHECK_EQ(child3->get_next_in_tree(), nullptr); |
| 124 | + |
| 125 | + CHECK_EQ(child1->get_prev(), nullptr); |
| 126 | + CHECK_EQ(child1->get_prev_in_tree(), root); |
| 127 | + CHECK_EQ(child2->get_prev(), child1); |
| 128 | + CHECK_EQ(child2->get_prev_in_tree(), child1); |
| 129 | + CHECK_EQ(child3->get_prev(), child2); |
| 130 | + CHECK_EQ(child3->get_prev_in_tree(), child2); |
| 131 | + |
| 132 | + TreeItem *nested1 = tree->create_item(child2); |
| 133 | + TreeItem *nested2 = tree->create_item(child2); |
| 134 | + TreeItem *nested3 = tree->create_item(child2); |
| 135 | + |
| 136 | + CHECK_EQ(child1->get_next(), child2); |
| 137 | + CHECK_EQ(child1->get_next_in_tree(), child2); |
| 138 | + CHECK_EQ(child2->get_next(), child3); |
| 139 | + CHECK_EQ(child2->get_next_in_tree(), nested1); |
| 140 | + CHECK_EQ(child3->get_prev(), child2); |
| 141 | + CHECK_EQ(child3->get_prev_in_tree(), nested3); |
| 142 | + CHECK_EQ(nested1->get_prev_in_tree(), child2); |
| 143 | + CHECK_EQ(nested1->get_next_in_tree(), nested2); |
| 144 | + |
| 145 | + memdelete(tree); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +} // namespace TestTree |
| 150 | + |
| 151 | +#endif // TEST_TREE_H |
0 commit comments