Blender V5.3
asset_catalog_tree_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "AS_asset_catalog.hh"
7
8#include "BLI_path_utils.hh"
9
10#include "testing/testing.h"
11
13
15
17
18TEST_F(AssetCatalogTreeTest, insert_item_into_tree)
19{
20 {
22 std::unique_ptr<AssetCatalog> catalog_empty_path = AssetCatalog::from_path("");
23 tree.insert_item(*catalog_empty_path);
24
25 expect_tree_items(tree, {});
26 }
27
28 {
30
31 std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path("item");
32 tree.insert_item(*catalog);
33 expect_tree_items(tree, {"item"});
34
35 /* Insert child after parent already exists. */
36 std::unique_ptr<AssetCatalog> child_catalog = AssetCatalog::from_path("item/child");
37 tree.insert_item(*catalog);
38 expect_tree_items(tree, {"item", "item/child"});
39
40 std::vector<AssetCatalogPath> expected_paths;
41
42 /* Test inserting multi-component sub-path. */
43 std::unique_ptr<AssetCatalog> grandgrandchild_catalog = AssetCatalog::from_path(
44 "item/child/grandchild/grandgrandchild");
45 tree.insert_item(*catalog);
46 expected_paths = {
47 "item", "item/child", "item/child/grandchild", "item/child/grandchild/grandgrandchild"};
48 expect_tree_items(tree, expected_paths);
49
50 std::unique_ptr<AssetCatalog> root_level_catalog = AssetCatalog::from_path("root level");
51 tree.insert_item(*catalog);
52 expected_paths = {"item",
53 "item/child",
54 "item/child/grandchild",
55 "item/child/grandchild/grandgrandchild",
56 "root level"};
57 expect_tree_items(tree, expected_paths);
58 }
59
60 {
62
63 std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path("item/child");
64 tree.insert_item(*catalog);
65 expect_tree_items(tree, {"item", "item/child"});
66 }
67
68 {
70
71 std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path("white space");
72 tree.insert_item(*catalog);
73 expect_tree_items(tree, {"white space"});
74 }
75
76 {
78
79 std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path("/item/white space");
80 tree.insert_item(*catalog);
81 expect_tree_items(tree, {"item", "item/white space"});
82 }
83
84 {
86
87 std::unique_ptr<AssetCatalog> catalog_unicode_path = AssetCatalog::from_path("Ružena");
88 tree.insert_item(*catalog_unicode_path);
89 expect_tree_items(tree, {"Ružena"});
90
91 catalog_unicode_path = AssetCatalog::from_path("Ružena/Ružena");
92 tree.insert_item(*catalog_unicode_path);
93 expect_tree_items(tree, {"Ružena", "Ružena/Ružena"});
94 }
95}
96
97TEST_F(AssetCatalogTreeTest, load_single_file_into_tree)
98{
99 AssetCatalogService service(asset_library_root_);
100 service.load_from_disk(asset_library_root_ + SEP_STR + "blender_assets.cats.txt");
101
102 /* Contains not only paths from the CDF but also the missing parents (implicitly defined
103 * catalogs). */
104 std::vector<AssetCatalogPath> expected_paths{
105 "character",
106 "character/Ellie",
107 "character/Ellie/backslashes",
108 "character/Ellie/poselib",
109 "character/Ellie/poselib/tailslash",
110 "character/Ellie/poselib/white space",
111 "character/Ružena",
112 "character/Ružena/poselib",
113 "character/Ružena/poselib/face",
114 "character/Ružena/poselib/hand",
115 "path", /* Implicit. */
116 "path/without", /* Implicit. */
117 "path/without/simplename", /* From CDF. */
118 };
119
120 const std::shared_ptr<const AssetCatalogTree> tree = service.catalog_tree();
121 expect_tree_items(*tree, expected_paths);
122}
123
125{
126 {
127 AssetCatalogTree tree{};
128 const std::vector<AssetCatalogPath> no_catalogs{};
129
130 expect_tree_items(tree, no_catalogs);
131 expect_tree_root_items(tree, no_catalogs);
132 /* Need a root item to check child items. */
133 std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path("something");
134 tree.insert_item(*catalog);
135 tree.foreach_root_item([&no_catalogs](const AssetCatalogTreeItem &item) {
136 expect_tree_item_child_items(item, no_catalogs);
137 });
138 }
139
140 AssetCatalogService service(asset_library_root_);
141 service.load_from_disk(asset_library_root_ + SEP_STR + "blender_assets.cats.txt");
142
143 std::vector<AssetCatalogPath> expected_root_items{{"character", "path"}};
144 const std::shared_ptr<const AssetCatalogTree> tree = service.catalog_tree();
145 expect_tree_root_items(*tree, expected_root_items);
146
147 /* Test if the direct children of the root item are what's expected. */
148 std::vector<std::vector<AssetCatalogPath>> expected_root_child_items = {
149 /* Children of the "character" root item. */
150 {"character/Ellie", "character/Ružena"},
151 /* Children of the "path" root item. */
152 {"path/without"},
153 };
154 int i = 0;
155 tree->foreach_root_item([&expected_root_child_items, &i](const AssetCatalogTreeItem &item) {
156 expect_tree_item_child_items(item, expected_root_child_items[i]);
157 i++;
158 });
159}
160
161} // namespace blender::asset_system::tests
std::shared_ptr< const AssetCatalogTree > catalog_tree()
void insert_item(const AssetCatalog &catalog, std::optional< StringRef > skip_prefix=std::nullopt)
void foreach_root_item(ItemIterFn callback) const
static std::unique_ptr< AssetCatalog > from_path(const AssetCatalogPath &path)
TEST_F(AssetCatalogPathTest, construction)
#define SEP_STR
Definition unit.cc:41