Blender V5.3
action_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "ANIM_action.hh"
6
7#include "BKE_action.hh"
8#include "BKE_anim_data.hh"
9#include "BKE_fcurve.hh"
10#include "BKE_gtest_base.hh"
11#include "BKE_idtype.hh"
12#include "BKE_lib_id.hh"
13#include "BKE_main.hh"
14#include "BKE_object.hh"
15
16#include "DNA_anim_types.h"
17#include "DNA_object_types.h"
18
19#include "RNA_access.hh"
20
21#include "BLI_string.hh"
22#include "BLI_string_utf8.hh"
23
25
26#include <limits>
27
28#include "testing/testing.h"
29
31
33
34TEST_F(ActionTest, low_level_initialisation)
35{
36 bAction *action = BKE_id_new_nomain<bAction>("NewAction");
37
38 EXPECT_NE(action->last_slot_handle, 0)
39 << "bAction::last_slot_handle should not be initialised to 0";
40
41 BKE_id_free(nullptr, action);
42}
43
45 public:
51
52 void SetUp() override
53 {
55 action = BKE_id_new<Action>(bmain, "ACÄnimåtië");
59 }
60
61 void TearDown() override
62 {
64 }
65};
66
68{
69 Layer &layer = action->layer_add("layer name");
70
71 EXPECT_EQ(action->layer(0), &layer);
72 EXPECT_EQ("layer name", std::string(layer.name));
73 EXPECT_EQ(1.0f, layer.influence) << "Expected DNA defaults to be used.";
74 EXPECT_EQ(0, action->layer_active_index)
75 << "Expected newly added layer to become the active layer.";
76 ASSERT_EQ(0, layer.strips().size()) << "Expected newly added layer to have no strip.";
77}
78
79TEST_F(ActionLayersTest, add_layer__reset_idroot)
80{
81 /* An empty Action is a valid legacy Action, and thus can have its idroot set to a non-zero
82 * value. If such an Action gets a layer, it no longer is a valid legacy Action, and thus its
83 * idtype should be reset to zero. */
84 action->idroot = ID_CA; /* Fake that this was assigned to a camera data-block. */
85 ASSERT_NE(0, action->idroot) << "action->idroot should not be zero at the start of this test.";
86
87 action->layer_add("layer name");
88
89 EXPECT_EQ(0, action->idroot)
90 << "action->idroot should get reset when the Action becomes layered.";
91}
92
94{
95 Layer &layer0 = action->layer_add("Test Læür nul");
96 Layer &layer1 = action->layer_add("Test Læür één");
97 Layer &layer2 = action->layer_add("Test Læür twee");
98
99 /* Add some strips to check that they are freed correctly too (implicitly by the
100 * memory leak checker). */
101 layer0.strip_add(*action, Strip::Type::Keyframe);
102 layer1.strip_add(*action, Strip::Type::Keyframe);
103 layer2.strip_add(*action, Strip::Type::Keyframe);
104
105 { /* Test removing a layer that is not owned. */
106 Action *other_anim = BKE_id_new<Action>(bmain, "ACOtherAnim");
107 Layer &other_layer = other_anim->layer_add("Another Layer");
108 EXPECT_FALSE(action->layer_remove(other_layer))
109 << "Removing a layer not owned by the Action should be gracefully rejected";
110 BKE_id_free(bmain, &other_anim->id);
111 }
112
113 EXPECT_TRUE(action->layer_remove(layer1));
114 EXPECT_EQ(2, action->layers().size());
115 EXPECT_STREQ(layer0.name, action->layer(0)->name);
116 EXPECT_STREQ(layer2.name, action->layer(1)->name);
117
118 EXPECT_TRUE(action->layer_remove(layer2));
119 EXPECT_EQ(1, action->layers().size());
120 EXPECT_STREQ(layer0.name, action->layer(0)->name);
121
122 EXPECT_TRUE(action->layer_remove(layer0));
123 EXPECT_EQ(0, action->layers().size());
124}
125
127{
128 Layer &layer = action->layer_add("Test Læür");
129
130 Strip &strip = layer.strip_add(*action, Strip::Type::Keyframe);
131 ASSERT_EQ(1, layer.strips().size());
132 EXPECT_EQ(&strip, layer.strip(0));
133
134 constexpr float inf = std::numeric_limits<float>::infinity();
135 EXPECT_EQ(-inf, strip.frame_start) << "Expected strip to be infinite.";
136 EXPECT_EQ(inf, strip.frame_end) << "Expected strip to be infinite.";
137 EXPECT_EQ(0, strip.frame_offset) << "Expected infinite strip to have no offset.";
138
139 Strip &another_strip = layer.strip_add(*action, Strip::Type::Keyframe);
140 ASSERT_EQ(2, layer.strips().size());
141 EXPECT_EQ(&another_strip, layer.strip(1));
142
143 EXPECT_EQ(-inf, another_strip.frame_start) << "Expected strip to be infinite.";
144 EXPECT_EQ(inf, another_strip.frame_end) << "Expected strip to be infinite.";
145 EXPECT_EQ(0, another_strip.frame_offset) << "Expected infinite strip to have no offset.";
146
147 /* Add some keys to check that also the strip data is freed correctly. */
148 const KeyframeSettings settings = get_keyframe_settings(false);
149 Slot &slot = action->slot_add();
150 strip.data<StripKeyframeData>(*action).keyframe_insert(
151 bmain, slot, {"location", 0}, {1.0f, 47.0f}, settings);
152 another_strip.data<StripKeyframeData>(*action).keyframe_insert(
153 bmain, slot, {"location", 0}, {1.0f, 47.0f}, settings);
154}
155
157{
158 Layer &layer = action->layer_add("Test Læür");
159 Strip &strip0 = layer.strip_add(*action, Strip::Type::Keyframe);
160 Strip &strip1 = layer.strip_add(*action, Strip::Type::Keyframe);
161 Strip &strip2 = layer.strip_add(*action, Strip::Type::Keyframe);
162 Strip &strip3 = layer.strip_add(*action, Strip::Type::Keyframe);
163 StripKeyframeData &strip_data0 = strip0.data<StripKeyframeData>(*action);
164 StripKeyframeData &strip_data1 = strip1.data<StripKeyframeData>(*action);
165 StripKeyframeData &strip_data2 = strip2.data<StripKeyframeData>(*action);
166 StripKeyframeData &strip_data3 = strip3.data<StripKeyframeData>(*action);
167
168 /* Add some keys to check that also the strip data is freed correctly. */
169 const KeyframeSettings settings = get_keyframe_settings(false);
170 Slot &slot = action->slot_add();
171 strip_data0.keyframe_insert(bmain, slot, {"location", 0}, {1.0f, 47.0f}, settings);
172 strip_data1.keyframe_insert(bmain, slot, {"location", 0}, {1.0f, 48.0f}, settings);
173 strip_data2.keyframe_insert(bmain, slot, {"location", 0}, {1.0f, 49.0f}, settings);
174 strip_data3.keyframe_insert(bmain, slot, {"location", 0}, {1.0f, 50.0f}, settings);
175
176 EXPECT_EQ(4, action->strip_keyframe_data().size());
177 EXPECT_EQ(0, strip0.data_index);
178 EXPECT_EQ(1, strip1.data_index);
179 EXPECT_EQ(2, strip2.data_index);
180 EXPECT_EQ(3, strip3.data_index);
181
182 EXPECT_TRUE(layer.strip_remove(*action, strip1));
183 EXPECT_EQ(3, action->strip_keyframe_data().size());
184 EXPECT_EQ(3, layer.strips().size());
185 EXPECT_EQ(&strip0, layer.strip(0));
186 EXPECT_EQ(&strip2, layer.strip(1));
187 EXPECT_EQ(&strip3, layer.strip(2));
188 EXPECT_EQ(0, strip0.data_index);
189 EXPECT_EQ(2, strip2.data_index);
190 EXPECT_EQ(1, strip3.data_index); /* Swapped in when removing strip 1's data. */
191 EXPECT_EQ(&strip_data0, &strip0.data<StripKeyframeData>(*action));
192 EXPECT_EQ(&strip_data2, &strip2.data<StripKeyframeData>(*action));
193 EXPECT_EQ(&strip_data3, &strip3.data<StripKeyframeData>(*action));
194
195 EXPECT_TRUE(layer.strip_remove(*action, strip2));
196 EXPECT_EQ(2, action->strip_keyframe_data().size());
197 EXPECT_EQ(2, layer.strips().size());
198 EXPECT_EQ(&strip0, layer.strip(0));
199 EXPECT_EQ(&strip3, layer.strip(1));
200 EXPECT_EQ(0, strip0.data_index);
201 EXPECT_EQ(1, strip3.data_index);
202 EXPECT_EQ(&strip_data0, &strip0.data<StripKeyframeData>(*action));
203 EXPECT_EQ(&strip_data3, &strip3.data<StripKeyframeData>(*action));
204
205 EXPECT_TRUE(layer.strip_remove(*action, strip3));
206 EXPECT_EQ(1, action->strip_keyframe_data().size());
207 EXPECT_EQ(1, layer.strips().size());
208 EXPECT_EQ(&strip0, layer.strip(0));
209 EXPECT_EQ(0, strip0.data_index);
210 EXPECT_EQ(&strip_data0, &strip0.data<StripKeyframeData>(*action));
211
212 EXPECT_TRUE(layer.strip_remove(*action, strip0));
213 EXPECT_EQ(0, action->strip_keyframe_data().size());
214 EXPECT_EQ(0, layer.strips().size());
215
216 { /* Test removing a strip that is not owned. */
217 Layer &other_layer = action->layer_add("Another Layer");
218 Strip &other_strip = other_layer.strip_add(*action, Strip::Type::Keyframe);
219
220 EXPECT_FALSE(layer.strip_remove(*action, other_strip))
221 << "Removing a strip not owned by the layer should be gracefully rejected";
222 }
223}
224
225/* NOTE: this test creates strip instances in a bespoke way for the purpose of
226 * exercising the strip removal code, because at the time of writing we don't
227 * have a proper API for creating strip instances. When such an API is added,
228 * this test should be updated to use it. */
229TEST_F(ActionLayersTest, remove_strip_instances)
230{
231 Layer &layer = action->layer_add("Test Læür");
232 Strip &strip0 = layer.strip_add(*action, Strip::Type::Keyframe);
233 Strip &strip1 = layer.strip_add(*action, Strip::Type::Keyframe);
234 Strip &strip2 = layer.strip_add(*action, Strip::Type::Keyframe);
235
236 /* Make on of the strips an instance of another. */
237 strip0.data_index = strip1.data_index;
238
239 StripKeyframeData &strip_data_0_1 = strip0.data<StripKeyframeData>(*action);
240 StripKeyframeData &strip_data_2 = strip2.data<StripKeyframeData>(*action);
241
242 /* Add some keys to check that also the strip data is freed correctly. */
243 const KeyframeSettings settings = get_keyframe_settings(false);
244 Slot &slot = action->slot_add();
245 strip_data_0_1.keyframe_insert(bmain, slot, {"location", 0}, {1.0f, 47.0f}, settings);
246 strip_data_2.keyframe_insert(bmain, slot, {"location", 0}, {1.0f, 48.0f}, settings);
247
248 EXPECT_EQ(3, action->strip_keyframe_data().size());
249 EXPECT_EQ(1, strip0.data_index);
250 EXPECT_EQ(1, strip1.data_index);
251 EXPECT_EQ(2, strip2.data_index);
252
253 /* Removing an instance should not delete the underlying data as long as there
254 * is still another strip using it. */
255 EXPECT_TRUE(layer.strip_remove(*action, strip1));
256 EXPECT_EQ(3, action->strip_keyframe_data().size());
257 EXPECT_EQ(2, layer.strips().size());
258 EXPECT_EQ(&strip0, layer.strip(0));
259 EXPECT_EQ(&strip2, layer.strip(1));
260 EXPECT_EQ(1, strip0.data_index);
261 EXPECT_EQ(2, strip2.data_index);
262 EXPECT_EQ(&strip_data_0_1, &strip0.data<StripKeyframeData>(*action));
263 EXPECT_EQ(&strip_data_2, &strip2.data<StripKeyframeData>(*action));
264
265 /* Removing the last user of strip data should also delete the data. */
266 EXPECT_TRUE(layer.strip_remove(*action, strip0));
267 EXPECT_EQ(2, action->strip_keyframe_data().size());
268 EXPECT_EQ(1, layer.strips().size());
269 EXPECT_EQ(&strip2, layer.strip(0));
270 EXPECT_EQ(1, strip2.data_index);
271 EXPECT_EQ(&strip_data_2, &strip2.data<StripKeyframeData>(*action));
272}
273
275{
276 { /* Creating an 'unused' Slot should just be called 'Slot'. */
277 Slot &slot = action->slot_add();
278 EXPECT_EQ(DNA_DEFAULT_ACTION_LAST_SLOT_HANDLE + 1, action->last_slot_handle);
280
281 EXPECT_STREQ("XXSlot", slot.identifier);
282 EXPECT_EQ(0, slot.idtype);
283 }
284
285 { /* Creating a Slot for a specific ID should name it after the ID. */
286 Slot &slot = action->slot_add_for_id(cube->id);
287 EXPECT_EQ(DNA_DEFAULT_ACTION_LAST_SLOT_HANDLE + 2, action->last_slot_handle);
289
290 EXPECT_STREQ(cube->id.name, slot.identifier);
291 EXPECT_EQ(ID_OB, slot.idtype);
292 }
293
294 { /* Creating a Slot for a specific ID that already had a slot assigned before should name it
295 * after that previous slot. This should also ensure that the first two characters are actually
296 * correct for the ID type. */
297 AnimData *adt = BKE_animdata_ensure_id(&cube->id);
298 STRNCPY_UTF8(adt->last_slot_identifier, "$$Kübuš 😹");
299 Slot &slot = action->slot_add_for_id(cube->id);
300 EXPECT_EQ(DNA_DEFAULT_ACTION_LAST_SLOT_HANDLE + 3, action->last_slot_handle);
302
303 EXPECT_STREQ("Kübuš 😹", slot.identifier + 2)
304 << "The last-assigned slot name should be reused";
305 EXPECT_STREQ("OBKübuš 😹", slot.identifier)
306 << "The ID type encoded in the slot identifier should be correct";
307 EXPECT_EQ(ID_OB, slot.idtype);
308 }
309}
310
311TEST_F(ActionLayersTest, add_slot__reset_idroot)
312{
313 /* An empty Action is a valid legacy Action, and thus can have its idroot set
314 * to a non-zero value. If such an Action gets a slot, it no longer is a
315 * valid legacy Action, and thus its idtype should be reset to zero. */
316 action->idroot = ID_CA; /* Fake that this was assigned to a camera data-block. */
317 ASSERT_NE(0, action->idroot) << "action->idroot should not be zero at the start of this test.";
318
319 action->slot_add();
320
321 EXPECT_EQ(0, action->idroot)
322 << "action->idroot should get reset when the Action becomes layered.";
323}
324
325TEST_F(ActionLayersTest, add_slot_multiple)
326{
327 Slot &slot_cube = action->slot_add();
328 Slot &slot_suzanne = action->slot_add();
329 EXPECT_TRUE(assign_action(action, cube->id));
331 EXPECT_TRUE(assign_action(action, suzanne->id));
332 EXPECT_EQ(assign_action_slot(&slot_suzanne, suzanne->id), ActionSlotAssignmentResult::OK);
333
334 EXPECT_EQ(DNA_DEFAULT_ACTION_LAST_SLOT_HANDLE + 2, action->last_slot_handle);
337}
338
340{
341 { /* Canary test: removing a just-created slot on an otherwise empty Action should work. */
342 Slot &slot = action->slot_add();
344 EXPECT_EQ(DNA_DEFAULT_ACTION_LAST_SLOT_HANDLE + 1, action->last_slot_handle);
345
346 EXPECT_TRUE(action->slot_remove(slot));
347 EXPECT_EQ(DNA_DEFAULT_ACTION_LAST_SLOT_HANDLE + 1, action->last_slot_handle)
348 << "Removing a slot should not change the last-used slot handle.";
349 EXPECT_EQ(0, action->slot_array_num);
350 }
351
352 { /* Removing a non-existing slot should return false. */
353 Slot slot;
354 EXPECT_FALSE(action->slot_remove(slot));
355 }
356
357 { /* Removing a slot should remove its Channelbag. */
358 Slot &slot = action->slot_add();
359 const slot_handle_t slot_handle = slot.handle;
361 EXPECT_EQ(DNA_DEFAULT_ACTION_LAST_SLOT_HANDLE + 2, action->last_slot_handle);
362
363 /* Create an F-Curve in a Channelbag for the slot. */
364 action->layer_keystrip_ensure();
365 StripKeyframeData &strip_data = action->layer(0)->strip(0)->data<StripKeyframeData>(*action);
366 Channelbag &channelbag = strip_data.channelbag_for_slot_ensure(slot);
367 channelbag.fcurve_create_unique(bmain, {"location", 1});
368
369 /* Remove the slot. */
370 EXPECT_TRUE(action->slot_remove(slot));
371 EXPECT_EQ(DNA_DEFAULT_ACTION_LAST_SLOT_HANDLE + 2, action->last_slot_handle)
372 << "Removing a slot should not change the last-used slot handle.";
373 EXPECT_EQ(0, action->slot_array_num);
374
375 /* Check that its channelbag is gone. */
376 Channelbag *found_cbag = strip_data.channelbag_for_slot(slot_handle);
377 EXPECT_EQ(found_cbag, nullptr);
378 }
379
380 { /* Removing one slot should leave the other two in place. */
381 Slot &slot1 = action->slot_add();
382 Slot &slot2 = action->slot_add();
383 Slot &slot3 = action->slot_add();
387 EXPECT_EQ(DNA_DEFAULT_ACTION_LAST_SLOT_HANDLE + 5, action->last_slot_handle);
388
389 /* For referencing the slot handle after the slot is removed. */
390 const slot_handle_t slot2_handle = slot2.handle;
391
392 /* Create a Channel-bag for each slot. */
393 action->layer_keystrip_ensure();
394 StripKeyframeData &strip_data = action->layer(0)->strip(0)->data<StripKeyframeData>(*action);
395 strip_data.channelbag_for_slot_ensure(slot1);
396 strip_data.channelbag_for_slot_ensure(slot2);
397 strip_data.channelbag_for_slot_ensure(slot3);
398
399 /* Remove the slot. */
400 EXPECT_TRUE(action->slot_remove(slot2));
401 EXPECT_EQ(DNA_DEFAULT_ACTION_LAST_SLOT_HANDLE + 5, action->last_slot_handle);
402
403 /* Check the correct slot + channel-bag are removed. */
404 EXPECT_EQ(action->slot_for_handle(slot1.handle), &slot1);
405 EXPECT_EQ(action->slot_for_handle(slot2_handle), nullptr);
406 EXPECT_EQ(action->slot_for_handle(slot3.handle), &slot3);
407
408 EXPECT_NE(strip_data.channelbag_for_slot(slot1.handle), nullptr);
409 EXPECT_EQ(strip_data.channelbag_for_slot(slot2_handle), nullptr);
410 EXPECT_NE(strip_data.channelbag_for_slot(slot3.handle), nullptr);
411 }
412
413 { /* Removing an in-use slot doesn't un-assign it from its users.
414 * This is not that important, but it covers the current behavior. */
415 Slot &slot = action->slot_add_for_id(cube->id);
416 ASSERT_EQ(assign_action_and_slot(action, &slot, cube->id), ActionSlotAssignmentResult::OK);
417
418 ASSERT_TRUE(slot.runtime_users().contains(&cube->id));
419 ASSERT_EQ(cube->adt->slot_handle, slot.handle);
420
421 const slot_handle_t removed_slot_handle = slot.handle;
422 ASSERT_TRUE(action->slot_remove(slot));
423 EXPECT_EQ(cube->adt->slot_handle, removed_slot_handle);
424 }
425
426 { /* Creating a slot after removing one should not reuse its handle. */
427 action->last_slot_handle = 3; /* To create independence between sub-tests. */
428 Slot &slot1 = action->slot_add();
429 ASSERT_EQ(4, slot1.handle);
430 ASSERT_EQ(4, action->last_slot_handle);
431 ASSERT_TRUE(action->slot_remove(slot1));
432
433 Slot &slot2 = action->slot_add();
434 EXPECT_EQ(5, slot2.handle);
435 EXPECT_EQ(5, action->last_slot_handle);
436 }
437}
438
439TEST_F(ActionLayersTest, slot_move_to_index)
440{
441 Slot &slot_a = action->slot_add_for_id_type(ID_ME);
442 Slot &slot_b = action->slot_add_for_id_type(ID_CA);
443 Slot &slot_cube = action->slot_add_for_id(cube->id);
444 Slot &slot_suzanne = action->slot_add_for_id(suzanne->id);
445
446 assign_action_and_slot(action, &slot_cube, cube->id);
447 assign_action_and_slot(action, &slot_suzanne, suzanne->id);
448
449 const slot_handle_t handle_a = slot_a.handle;
450 const slot_handle_t handle_b = slot_b.handle;
451 const slot_handle_t handle_cube = slot_cube.handle;
452 const slot_handle_t handle_suzanne = slot_suzanne.handle;
453
454 ASSERT_EQ(action->slot(0)->handle, handle_a);
455 ASSERT_EQ(action->slot(0)->idtype_string(), "ME");
456 ASSERT_EQ(action->slot(1)->handle, handle_b);
457 ASSERT_EQ(action->slot(1)->idtype_string(), "CA");
458 ASSERT_EQ(action->slot(2)->handle, handle_cube);
459 ASSERT_EQ(action->slot(2)->idtype_string(), "OB");
460 ASSERT_EQ(action->slot(2)->users(*bmain)[0], &cube->id);
461 ASSERT_EQ(action->slot(3)->handle, handle_suzanne);
462 ASSERT_EQ(action->slot(3)->idtype_string(), "OB");
463 ASSERT_EQ(action->slot(3)->users(*bmain)[0], &suzanne->id);
464
465 /* First "move" a slot to its own location, which should do nothing. */
466 action->slot_move_to_index(slot_b, 1);
467 EXPECT_EQ(action->slot(0)->handle, handle_a);
468 EXPECT_EQ(action->slot(0)->idtype_string(), "ME");
469 EXPECT_EQ(action->slot(1)->handle, handle_b);
470 EXPECT_EQ(action->slot(1)->idtype_string(), "CA");
471 EXPECT_EQ(action->slot(2)->handle, handle_cube);
472 EXPECT_EQ(action->slot(2)->idtype_string(), "OB");
473 EXPECT_EQ(action->slot(2)->users(*bmain)[0], &cube->id);
474 EXPECT_EQ(action->slot(3)->handle, handle_suzanne);
475 EXPECT_EQ(action->slot(3)->idtype_string(), "OB");
476 EXPECT_EQ(action->slot(3)->users(*bmain)[0], &suzanne->id);
477
478 /* Then move slots around in various ways. */
479
480 action->slot_move_to_index(slot_a, 2);
481 EXPECT_EQ(action->slot(0)->handle, handle_b);
482 EXPECT_EQ(action->slot(0)->idtype_string(), "CA");
483 EXPECT_EQ(action->slot(1)->handle, handle_cube);
484 EXPECT_EQ(action->slot(1)->idtype_string(), "OB");
485 EXPECT_EQ(action->slot(1)->users(*bmain)[0], &cube->id);
486 EXPECT_EQ(action->slot(2)->handle, handle_a);
487 EXPECT_EQ(action->slot(2)->idtype_string(), "ME");
488 EXPECT_EQ(action->slot(3)->handle, handle_suzanne);
489 EXPECT_EQ(action->slot(3)->idtype_string(), "OB");
490 EXPECT_EQ(action->slot(3)->users(*bmain)[0], &suzanne->id);
491
492 action->slot_move_to_index(slot_suzanne, 1);
493 EXPECT_EQ(action->slot(0)->handle, handle_b);
494 EXPECT_EQ(action->slot(0)->idtype_string(), "CA");
495 EXPECT_EQ(action->slot(1)->handle, handle_suzanne);
496 EXPECT_EQ(action->slot(1)->idtype_string(), "OB");
497 EXPECT_EQ(action->slot(1)->users(*bmain)[0], &suzanne->id);
498 EXPECT_EQ(action->slot(2)->handle, handle_cube);
499 EXPECT_EQ(action->slot(2)->idtype_string(), "OB");
500 EXPECT_EQ(action->slot(2)->users(*bmain)[0], &cube->id);
501 EXPECT_EQ(action->slot(3)->handle, handle_a);
502 EXPECT_EQ(action->slot(3)->idtype_string(), "ME");
503
504 action->slot_move_to_index(slot_cube, 3);
505 EXPECT_EQ(action->slot(0)->handle, handle_b);
506 EXPECT_EQ(action->slot(0)->idtype_string(), "CA");
507 EXPECT_EQ(action->slot(1)->handle, handle_suzanne);
508 EXPECT_EQ(action->slot(1)->idtype_string(), "OB");
509 EXPECT_EQ(action->slot(1)->users(*bmain)[0], &suzanne->id);
510 EXPECT_EQ(action->slot(2)->handle, handle_a);
511 EXPECT_EQ(action->slot(2)->idtype_string(), "ME");
512 EXPECT_EQ(action->slot(3)->handle, handle_cube);
513 EXPECT_EQ(action->slot(3)->idtype_string(), "OB");
514 EXPECT_EQ(action->slot(3)->users(*bmain)[0], &cube->id);
515
516 action->slot_move_to_index(slot_suzanne, 0);
517 EXPECT_EQ(action->slot(0)->handle, handle_suzanne);
518 EXPECT_EQ(action->slot(0)->idtype_string(), "OB");
519 EXPECT_EQ(action->slot(0)->users(*bmain)[0], &suzanne->id);
520 EXPECT_EQ(action->slot(1)->handle, handle_b);
521 EXPECT_EQ(action->slot(1)->idtype_string(), "CA");
522 EXPECT_EQ(action->slot(2)->handle, handle_a);
523 EXPECT_EQ(action->slot(2)->idtype_string(), "ME");
524 EXPECT_EQ(action->slot(3)->handle, handle_cube);
525 EXPECT_EQ(action->slot(3)->idtype_string(), "OB");
526 EXPECT_EQ(action->slot(3)->users(*bmain)[0], &cube->id);
527}
528
529TEST_F(ActionLayersTest, action_assign_id)
530{
531 /* Assign to the only, 'virgin' Slot, should always work. */
532 Slot &slot_cube = action->slot_add();
533 ASSERT_NE(nullptr, slot_cube.runtime);
534 ASSERT_STREQ(slot_cube.identifier, "XXSlot");
535 ASSERT_EQ(assign_action_and_slot(action, &slot_cube, cube->id), ActionSlotAssignmentResult::OK);
536
537 EXPECT_EQ(slot_cube.handle, cube->adt->slot_handle);
538 EXPECT_STREQ(slot_cube.identifier, "OBSlot");
539 EXPECT_STREQ(slot_cube.identifier, cube->adt->last_slot_identifier)
540 << "The slot identifier should be copied to the adt";
541
542 EXPECT_TRUE(slot_cube.users(*bmain).contains(&cube->id))
543 << "Expecting Cube to be registered as animated by its slot.";
544
545 /* Assign another ID to the same Slot. */
546 ASSERT_EQ(assign_action_and_slot(action, &slot_cube, suzanne->id),
548 EXPECT_STREQ(slot_cube.identifier, "OBSlot");
549 EXPECT_STREQ(slot_cube.identifier, cube->adt->last_slot_identifier)
550 << "The slot identifier should be copied to the adt";
551
552 EXPECT_TRUE(slot_cube.users(*bmain).contains(&cube->id))
553 << "Expecting Suzanne to be registered as animated by the Cube slot.";
554
555 { /* Assign Cube to another action+slot without unassigning first. */
556 Action *another_anim = BKE_id_new<Action>(bmain, "ACOtherAnim");
557 Slot &another_slot = another_anim->slot_add();
558 ASSERT_EQ(assign_action_and_slot(another_anim, &another_slot, cube->id),
560 EXPECT_FALSE(slot_cube.users(*bmain).contains(&cube->id))
561 << "Expecting Cube to no longer be registered as user of its old slot.";
562 EXPECT_TRUE(another_slot.users(*bmain).contains(&cube->id))
563 << "Expecting Cube to be registered as user of its new slot.";
564 }
565
566 { /* Assign Cube to another slot of the same Action, this should work. */
567 ASSERT_EQ(assign_action_and_slot(action, &slot_cube, cube->id),
569 const int user_count_pre = action->id.us;
570 Slot &slot_cube_2 = action->slot_add();
571 ASSERT_EQ(assign_action_and_slot(action, &slot_cube_2, cube->id),
573 ASSERT_EQ(action->id.us, user_count_pre)
574 << "Assigning to a different slot of the same Action should _not_ change the user "
575 "count of that Action";
576 EXPECT_FALSE(slot_cube.users(*bmain).contains(&cube->id))
577 << "Expecting Cube to no longer be registered as animated by the Cube slot.";
578 EXPECT_TRUE(slot_cube_2.users(*bmain).contains(&cube->id))
579 << "Expecting Cube to be registered as animated by the 'cube_2' slot.";
580 }
581
582 { /* Unassign the Action. */
583 const int user_count_pre = action->id.us;
584 EXPECT_TRUE(unassign_action(cube->id));
585 ASSERT_EQ(action->id.us, user_count_pre - 1)
586 << "Unassigning an Action should lower its user count";
587
588 ASSERT_EQ(2, action->slots().size()) << "Expecting the Action to have two Slots";
589 EXPECT_FALSE(action->slot(0)->users(*bmain).contains(&cube->id))
590 << "Expecting Cube to no longer be registered as animated by any slot.";
591 EXPECT_FALSE(action->slot(1)->users(*bmain).contains(&cube->id))
592 << "Expecting Cube to no longer be registered as animated by any slot.";
593 }
594
595 /* Assign Cube to another 'virgin' slot. This should not cause a name
596 * collision between the Slots. */
597 Slot &another_slot_cube = action->slot_add();
598 ASSERT_EQ(assign_action_and_slot(action, &another_slot_cube, cube->id),
600 EXPECT_EQ(another_slot_cube.handle, cube->adt->slot_handle);
601 EXPECT_STREQ("OBSlot.002", another_slot_cube.identifier) << "The slot should be uniquely named";
602 EXPECT_STREQ("OBSlot.002", cube->adt->last_slot_identifier)
603 << "The slot identifier should be copied to the adt";
604 EXPECT_TRUE(another_slot_cube.users(*bmain).contains(&cube->id))
605 << "Expecting Cube to be registered as animated by the 'another_slot_cube' slot.";
606
607 /* Create an ID of another type. This should not be assignable to this slot. */
608 ID *mesh = static_cast<ID *>(BKE_id_new_nomain(ID_ME, "Mesh"));
609 ASSERT_TRUE(assign_action(action, *mesh));
611 << "Mesh should not be animatable by an Object slot";
612 EXPECT_FALSE(another_slot_cube.users(*bmain).contains(mesh))
613 << "Expecting Mesh to not be registered as animated by the 'slot_cube' slot.";
614 BKE_id_free(nullptr, mesh);
615}
616
618{
619 Slot &slot_cube = action->slot_add();
620 ASSERT_EQ(assign_action_and_slot(action, &slot_cube, cube->id), ActionSlotAssignmentResult::OK);
621 EXPECT_EQ(slot_cube.handle, cube->adt->slot_handle);
622 EXPECT_STREQ("OBSlot", slot_cube.identifier);
623 EXPECT_STREQ(slot_cube.identifier, cube->adt->last_slot_identifier)
624 << "The slot identifier should be copied to the adt";
625
626 action->slot_identifier_define(slot_cube, "OBNew Slot Name");
627 EXPECT_STREQ("OBNew Slot Name", slot_cube.identifier);
628 /* At this point the slot identifier will not have been copied to the cube
629 * AnimData. However, I don't want to test for that here, as it's not exactly
630 * desirable behavior, but more of a side-effect of the current
631 * implementation. */
632
633 action->slot_identifier_propagate(*bmain, slot_cube);
634 EXPECT_STREQ("OBNew Slot Name", cube->adt->last_slot_identifier);
635
636 /* Rename via the display name, which should propagate to the ADT. */
637 action->slot_display_name_set(*bmain, slot_cube, "Slot's New Display Name");
638 EXPECT_STREQ("OBSlot's New Display Name", slot_cube.identifier);
639 EXPECT_STREQ("OBSlot's New Display Name", cube->adt->last_slot_identifier);
640
641 /* Finally, do another rename, do NOT call the propagate function, then
642 * unassign. This should still result in the correct slot name being stored
643 * on the ADT. */
644 action->slot_identifier_define(slot_cube, "OBEven Newer Name");
645 EXPECT_TRUE(unassign_action(cube->id));
646 EXPECT_STREQ("OBEven Newer Name", cube->adt->last_slot_identifier);
647}
648
649TEST_F(ActionLayersTest, slot_identifier_ensure_prefix)
650{
651 class AccessibleSlot : public Slot {
652 public:
653 void identifier_ensure_prefix()
654 {
656 }
657 };
658
659 Slot &raw_slot = action->slot_add();
660 AccessibleSlot &slot = static_cast<AccessibleSlot &>(raw_slot);
661 ASSERT_STREQ("XXSlot", slot.identifier);
662 ASSERT_EQ(0, slot.idtype);
663
664 /* Check defaults, idtype zeroed. */
665 slot.identifier_ensure_prefix();
666 EXPECT_STREQ("XXSlot", slot.identifier);
667
668 /* idtype CA, default name. */
669 slot.idtype = ID_CA;
670 slot.identifier_ensure_prefix();
671 EXPECT_STREQ("CASlot", slot.identifier);
672
673 /* idtype ME, explicit name of other idtype. */
674 action->slot_identifier_define(slot, "CANewName");
675 slot.idtype = ID_ME;
676 slot.identifier_ensure_prefix();
677 EXPECT_STREQ("MENewName", slot.identifier);
678
679 /* Zeroing out idtype. */
680 slot.idtype = 0;
681 slot.identifier_ensure_prefix();
682 EXPECT_STREQ("XXNewName", slot.identifier);
683}
684
685TEST_F(ActionLayersTest, slot_identifier_prefix)
686{
687 Slot &slot = action->slot_add();
688 EXPECT_EQ("XX", slot.idtype_string());
689 EXPECT_EQ("XX", slot.identifier_prefix());
690
691 slot.idtype = ID_CA;
692 EXPECT_EQ("CA", slot.idtype_string());
693 EXPECT_EQ("XX", slot.identifier_prefix());
694
696 EXPECT_EQ("CA", slot.idtype_string());
697 EXPECT_EQ("CA", slot.identifier_prefix());
698}
699
700TEST_F(ActionLayersTest, rename_slot_identifier_collision)
701{
702 Slot &slot1 = action->slot_add();
703 Slot &slot2 = action->slot_add();
704
705 action->slot_identifier_define(slot1, "New Slot Name");
706 action->slot_identifier_define(slot2, "New Slot Name");
707 EXPECT_STREQ("New Slot Name", slot1.identifier);
708 EXPECT_STREQ("New Slot Name.001", slot2.identifier);
709}
710
712{
713 /* ===
714 * Empty case, no slots exist yet and the ID doesn't even have an AnimData. */
715 EXPECT_EQ(nullptr, generic_slot_for_autoassign(cube->id, *this->action, ""));
716
717 /* ===
718 * Slot exists with the same name & type as the ID, but the ID doesn't have any AnimData yet.
719 * These should nevertheless be matched up. */
720 Slot &slot = action->slot_add();
721 slot.handle = 327;
722 STRNCPY_UTF8(slot.identifier, "OBKüüübus");
723 slot.idtype = GS(cube->id.name);
724 EXPECT_EQ(&slot, generic_slot_for_autoassign(cube->id, *this->action, ""));
725
726 /* ===
727 * Slot exists with the same name & type as the ID, and the ID has an AnimData with the same
728 * slot identifier, but a different slot_handle. Since the Action has not yet been
729 * assigned to this ID, the slot_handle should be ignored, and the slot identifier used for
730 * matching. */
731
732 /* Create a slot with a handle that should be ignored. */
733 Slot &other_slot = action->slot_add();
734 other_slot.handle = 47;
735
736 AnimData *adt = BKE_animdata_ensure_id(&cube->id);
737 adt->action = nullptr;
738 /* Configure adt to use the handle of one slot, and the identifier of the other. */
739 adt->slot_handle = other_slot.handle;
741 EXPECT_EQ(&slot,
742 generic_slot_for_autoassign(cube->id, *this->action, cube->adt->last_slot_identifier));
743
744 /* ===
745 * Assigned slot info exists, but doesn't match anything in the action data of the cube. This
746 * should fall back to using the ID name. */
747 adt->slot_handle = 161;
748 STRNCPY_UTF8(adt->last_slot_identifier, "¿¿What's this??");
749 EXPECT_EQ(&slot,
750 generic_slot_for_autoassign(cube->id, *this->action, cube->adt->last_slot_identifier));
751}
752
753TEST_F(ActionLayersTest, generic_slot_for_autoassign_untyped_wildcarding)
754{
755 /* Test the untyped slot "wildcard" behavior, where OBSlot should be chosen when the last slot
756 * identifier was "XXSlot", and vice versa. */
757
758 /* ===
759 * Action has OBSlot, last-used slot is XXSlot. Should pick OBSlot. */
760 AnimData *adt = BKE_animdata_ensure_id(&cube->id);
761 STRNCPY_UTF8(adt->last_slot_identifier, "XXSlot");
762 Slot &ob_slot = action->slot_add_for_id_type(ID_OB);
763 action->slot_identifier_define(ob_slot, "OBSlot");
764
765 EXPECT_EQ(&ob_slot,
766 generic_slot_for_autoassign(cube->id, *this->action, adt->last_slot_identifier));
767
768 /* ===
769 * Action has OBSlot and XXSlot, last-used slot is XXSlot. Should pick OBSlot. */
770 Slot &xx_slot = action->slot_add();
771 action->slot_identifier_define(xx_slot, "XXSlot");
772 ASSERT_FALSE(xx_slot.has_idtype());
773 ASSERT_STREQ("XXSlot", xx_slot.identifier);
774 ASSERT_STREQ("XXSlot", adt->last_slot_identifier);
775 EXPECT_EQ(&ob_slot,
776 generic_slot_for_autoassign(cube->id, *this->action, adt->last_slot_identifier));
777
778 /* ===
779 * Action has OBSlot and XXSlot, last-used slot is OBSlot. Should pick OBSlot. */
780 STRNCPY_UTF8(adt->last_slot_identifier, "OBSlot");
781 EXPECT_EQ(&ob_slot,
782 generic_slot_for_autoassign(cube->id, *this->action, adt->last_slot_identifier));
783
784 /* ===
785 * Action has XXSlot, last-used slot is OBSlot. Should pick XXSlot. */
786 action->slot_remove(ob_slot);
787 ASSERT_STREQ("OBSlot", adt->last_slot_identifier);
788 EXPECT_EQ(&xx_slot,
789 generic_slot_for_autoassign(cube->id, *this->action, adt->last_slot_identifier));
790}
791
793{
794 { /* Empty case, no slots exist yet. */
795 EXPECT_EQ(nullptr, action->slot_active_get());
796
797 action->slot_active_set(Slot::unassigned);
798 EXPECT_EQ(nullptr, action->slot_active_get());
799 }
800
801 { /* Single slot case. */
802 Slot &slot_cube = *assign_action_ensure_slot_for_keying(*action, cube->id);
803 EXPECT_EQ(nullptr, action->slot_active_get())
804 << "Adding the first slot should not change what is the active slot.";
805
806 action->slot_active_set(slot_cube.handle);
807 EXPECT_EQ(&slot_cube, action->slot_active_get())
808 << "It should be possible to activate the only available slot";
809 EXPECT_TRUE(slot_cube.is_active());
810
811 action->slot_active_set(Slot::unassigned);
812 EXPECT_EQ(nullptr, action->slot_active_get())
813 << "It should be possible to de-activate the only available slot";
814 EXPECT_FALSE(slot_cube.is_active());
815 }
816
817 {
818 /* Multiple slots case. */
819 Slot &slot_cube = *action->slot(0);
820 action->slot_active_set(slot_cube.handle);
821
822 Slot &slot_suz = *assign_action_ensure_slot_for_keying(*action, suzanne->id);
823 Slot &slot_bob = *assign_action_ensure_slot_for_keying(*action, bob->id);
824 EXPECT_EQ(&slot_cube, action->slot_active_get())
825 << "Adding a subsequent slot should not change what is the active slot.";
826 EXPECT_TRUE(slot_cube.is_active());
827
828 action->slot_active_set(slot_suz.handle);
829 EXPECT_EQ(&slot_suz, action->slot_active_get());
830 EXPECT_FALSE(slot_cube.is_active());
831 EXPECT_TRUE(slot_suz.is_active());
832 EXPECT_FALSE(slot_bob.is_active());
833
834 action->slot_active_set(slot_bob.handle);
835 EXPECT_EQ(&slot_bob, action->slot_active_get());
836 EXPECT_FALSE(slot_cube.is_active());
837 EXPECT_FALSE(slot_suz.is_active());
838 EXPECT_TRUE(slot_bob.is_active());
839
840 action->slot_active_set(Slot::unassigned);
841 EXPECT_EQ(nullptr, action->slot_active_get());
842 EXPECT_FALSE(slot_cube.is_active());
843 EXPECT_FALSE(slot_suz.is_active());
844 EXPECT_FALSE(slot_bob.is_active());
845 }
846}
847
849{
850 { /* Slotless Action, should create a typed slot. */
851 Action &action = action_add(*this->bmain, "ACEmpty");
852 EXPECT_EQ(action.id.us, 0);
853 Slot *chosen_slot = assign_action_ensure_slot_for_keying(action, cube->id);
854 EXPECT_EQ(action.id.us, 1);
855 ASSERT_NE(nullptr, chosen_slot);
856 EXPECT_EQ(ID_OB, chosen_slot->idtype);
857 EXPECT_STREQ("OBKüüübus", chosen_slot->identifier);
858 }
859
860 { /* Single slot with same name as ID, Action not yet assigned.
861 * Should assign the Action and the slot. */
862 Action &action = action_add(*this->bmain, "ACAction");
863 const Slot &slot_for_id = action.slot_add_for_id(cube->id);
864 Slot *chosen_slot = assign_action_ensure_slot_for_keying(action, cube->id);
865 ASSERT_NE(nullptr, chosen_slot);
866 EXPECT_EQ(&slot_for_id, chosen_slot) << "The expected slot should be chosen";
867 EXPECT_EQ(cube->adt->action, &action) << "The Action should be assigned";
868 EXPECT_EQ(cube->adt->slot_handle, chosen_slot->handle) << "The chosen slot should be assigned";
869 }
870
871 { /* Single slot with same name as ID, Action already assigned but not the slot.
872 * Should create new slot. */
873 Action &action = action_add(*this->bmain, "ACAction");
874 const Slot &slot_for_id = action.slot_add_for_id(cube->id);
875 ASSERT_EQ(ActionSlotAssignmentResult::OK, assign_action_and_slot(&action, nullptr, cube->id));
876
877 Slot *chosen_slot = assign_action_ensure_slot_for_keying(action, cube->id);
878 ASSERT_NE(nullptr, chosen_slot);
879 EXPECT_NE(&slot_for_id, chosen_slot) << "A new slot should be chosen";
880 EXPECT_STREQ("OBKüüübus.001", chosen_slot->identifier);
881 EXPECT_EQ(cube->adt->action, &action) << "The Action should be assigned";
882 EXPECT_EQ(cube->adt->slot_handle, chosen_slot->handle) << "The chosen slot should be assigned";
883 }
884
885 { /* Single untyped slot, Action already assigned but not the slot. Should assign the untyped
886 * slot. */
887 Action &action = action_add(*this->bmain, "ACAction");
888
889 /* Assign the Action before adding the untyped slot, otherwise the slot gets assigned & thus
890 * typed. */
891 ASSERT_EQ(ActionSlotAssignmentResult::OK, assign_action_and_slot(&action, nullptr, cube->id));
892
893 Slot &untyped_slot = action.slot_add();
894 action.slot_identifier_define(untyped_slot, "XXJust A Slot");
895
896 Slot *chosen_slot = assign_action_ensure_slot_for_keying(action, cube->id);
897
898 ASSERT_NE(nullptr, chosen_slot);
899 EXPECT_EQ(&untyped_slot, chosen_slot) << "The untyped slot should be chosen";
900 EXPECT_TRUE(untyped_slot.has_idtype()) << "Slot should have gotten an ID type";
901 EXPECT_STREQ("OBJust A Slot", untyped_slot.identifier);
902 EXPECT_EQ(cube->adt->action, &action) << "The Action should be assigned";
903 EXPECT_EQ(cube->adt->slot_handle, chosen_slot->handle) << "The chosen slot should be assigned";
904 }
905}
906
908{
909 constexpr float inf = std::numeric_limits<float>::infinity();
910 Layer &layer0 = action->layer_add("Test Læür nul");
911 Strip &strip = layer0.strip_add(*action, Strip::Type::Keyframe);
912
913 strip.resize(-inf, inf);
914 EXPECT_TRUE(strip.contains_frame(0.0f));
915 EXPECT_TRUE(strip.contains_frame(-100000.0f));
916 EXPECT_TRUE(strip.contains_frame(100000.0f));
917 EXPECT_TRUE(strip.is_last_frame(inf));
918
919 strip.resize(1.0f, 2.0f);
920 EXPECT_FALSE(strip.contains_frame(0.0f))
921 << "Strip should not contain frames before its first frame";
922 EXPECT_TRUE(strip.contains_frame(1.0f)) << "Strip should contain its first frame.";
923 EXPECT_TRUE(strip.contains_frame(2.0f)) << "Strip should contain its last frame.";
924 EXPECT_FALSE(strip.contains_frame(2.0001f))
925 << "Strip should not contain frames after its last frame";
926
927 EXPECT_FALSE(strip.is_last_frame(1.0f));
928 EXPECT_FALSE(strip.is_last_frame(1.5f));
929 EXPECT_FALSE(strip.is_last_frame(1.9999f));
930 EXPECT_TRUE(strip.is_last_frame(2.0f));
931 EXPECT_FALSE(strip.is_last_frame(2.0001f));
932
933 /* Same test as above, but with much larger end frame number. This is 2 hours at 24 FPS. */
934 strip.resize(1.0f, 172800.0f);
935 EXPECT_TRUE(strip.contains_frame(172800.0f)) << "Strip should contain its last frame.";
936 EXPECT_FALSE(strip.contains_frame(172800.1f))
937 << "Strip should not contain frames after its last frame";
938
939 /* You can't get much closer to the end frame before it's considered equal. */
940 EXPECT_FALSE(strip.is_last_frame(172799.925f));
941 EXPECT_TRUE(strip.is_last_frame(172800.0f));
942 EXPECT_FALSE(strip.is_last_frame(172800.075f));
943}
944
945TEST_F(ActionLayersTest, KeyframeStrip__keyframe_insert)
946{
947 Slot &slot = action->slot_add();
948 ASSERT_EQ(assign_action_and_slot(action, &slot, cube->id), ActionSlotAssignmentResult::OK);
949 Layer &layer = action->layer_add("Kübus layer");
950
951 Strip &strip = layer.strip_add(*action, Strip::Type::Keyframe);
952 StripKeyframeData &strip_data = strip.data<StripKeyframeData>(*action);
953
954 const KeyframeSettings settings = get_keyframe_settings(false);
955 SingleKeyingResult result_loc_a = strip_data.keyframe_insert(
956 bmain, slot, {"location", 0}, {1.0f, 47.0f}, settings);
957 ASSERT_EQ(SingleKeyingResult::SUCCESS, result_loc_a)
958 << "Expected keyframe insertion to be successful";
959
960 /* Check the strip was created correctly, with the channels for the slot. */
961 ASSERT_EQ(1, strip_data.channelbags().size());
962 Channelbag *channels = strip_data.channelbag(0);
963 EXPECT_EQ(slot.handle, channels->slot_handle);
964
965 /* Insert a second key, should insert into the same FCurve as before. */
966 SingleKeyingResult result_loc_b = strip_data.keyframe_insert(
967 bmain, slot, {"location", 0}, {5.0f, 47.1f}, settings);
969 ASSERT_EQ(1, channels->fcurves().size()) << "Expect insertion with the same (slot/rna "
970 "path/array index) tuple to go into the same FCurve";
971 EXPECT_EQ(2, channels->fcurves()[0]->totvert)
972 << "Expect insertion with the same (slot/rna path/array index) tuple to go into the same "
973 "FCurve";
974
975 EXPECT_EQ(47.0f, evaluate_fcurve(channels->fcurves()[0], 1.0f));
976 EXPECT_EQ(47.1f, evaluate_fcurve(channels->fcurves()[0], 5.0f));
977
978 /* Insert another key for another property, should create another FCurve. */
979 SingleKeyingResult result_rot = strip_data.keyframe_insert(
980 bmain, slot, {"rotation_quaternion", 0}, {1.0f, 0.25f}, settings);
982 ASSERT_EQ(2, channels->fcurves().size()) << "Expected a second FCurve to be created.";
983 EXPECT_EQ(2, channels->fcurves()[0]->totvert);
984 EXPECT_EQ(1, channels->fcurves()[1]->totvert);
985}
986
987TEST_F(ActionLayersTest, action_slot_get_id_for_keying__empty_action)
988{
989 EXPECT_TRUE(assign_action(action, cube->id));
990
991 /* Double-check that the action is considered empty for the test. */
992 EXPECT_TRUE(action->is_empty());
993
994 /* None should return an ID, since there are no slots yet which could have this ID assigned.
995 * Assignment of the Action itself (cube) shouldn't matter. */
996 EXPECT_EQ(nullptr, action_slot_get_id_for_keying(*bmain, *action, 0, &cube->id));
997 EXPECT_EQ(nullptr, action_slot_get_id_for_keying(*bmain, *action, 0, nullptr));
998 EXPECT_EQ(nullptr, action_slot_get_id_for_keying(*bmain, *action, 0, &suzanne->id));
999}
1000
1001TEST_F(ActionLayersTest, action_slot_get_id_for_keying__layered_action)
1002{
1003 Slot &slot = action->slot_add();
1004
1005 /* A slot with no users should never return a user. */
1006 EXPECT_EQ(nullptr, action_slot_get_id_for_keying(*bmain, *action, slot.handle, nullptr));
1007 EXPECT_EQ(nullptr, action_slot_get_id_for_keying(*bmain, *action, slot.handle, &cube->id));
1008
1009 /* A slot with precisely one user should always return that user. */
1010 ASSERT_EQ(assign_action_and_slot(action, &slot, cube->id), ActionSlotAssignmentResult::OK);
1011 EXPECT_EQ(&cube->id, action_slot_get_id_for_keying(*bmain, *action, slot.handle, nullptr));
1012 EXPECT_EQ(&cube->id, action_slot_get_id_for_keying(*bmain, *action, slot.handle, &cube->id));
1013 EXPECT_EQ(&cube->id, action_slot_get_id_for_keying(*bmain, *action, slot.handle, &suzanne->id));
1014
1015 /* A slot with more than one user should return the passed `primary_id` if it
1016 * is among its users, and nullptr otherwise. */
1017 ASSERT_EQ(assign_action_and_slot(action, &slot, suzanne->id), ActionSlotAssignmentResult::OK);
1018 EXPECT_EQ(&cube->id, action_slot_get_id_for_keying(*bmain, *action, slot.handle, &cube->id));
1019 EXPECT_EQ(&suzanne->id,
1020 action_slot_get_id_for_keying(*bmain, *action, slot.handle, &suzanne->id));
1021 EXPECT_EQ(nullptr, action_slot_get_id_for_keying(*bmain, *action, slot.handle, nullptr));
1022 EXPECT_EQ(nullptr, action_slot_get_id_for_keying(*bmain, *action, slot.handle, &bob->id));
1023}
1024
1025TEST_F(ActionLayersTest, action_move_slot)
1026{
1027 Action *action_2 = BKE_id_new<Action>(bmain, "Action 2");
1028 EXPECT_TRUE(action->is_empty());
1029
1030 Slot &slot_cube = action->slot_add();
1031 Slot &slot_suzanne = action_2->slot_add();
1032 EXPECT_EQ(assign_action_and_slot(action, &slot_cube, cube->id), ActionSlotAssignmentResult::OK);
1033 EXPECT_EQ(assign_action_and_slot(action_2, &slot_suzanne, suzanne->id),
1035
1036 PointerRNA cube_rna_pointer = RNA_id_pointer_create(&cube->id);
1037 PointerRNA suzanne_rna_pointer = RNA_id_pointer_create(&suzanne->id);
1038
1039 action_fcurve_ensure_ex(bmain, action, &cube_rna_pointer, {"location", 0});
1040 action_fcurve_ensure_ex(bmain, action, &cube_rna_pointer, {"rotation_euler", 1});
1041
1042 action_fcurve_ensure_ex(bmain, action_2, &suzanne_rna_pointer, {"location", 0});
1043 action_fcurve_ensure_ex(bmain, action_2, &suzanne_rna_pointer, {"rotation_euler", 1});
1044
1045 ASSERT_EQ(action->layer_array_num, 1);
1046 ASSERT_EQ(action_2->layer_array_num, 1);
1047
1048 Layer *layer_1 = action->layer(0);
1049 Layer *layer_2 = action_2->layer(0);
1050
1051 ASSERT_EQ(layer_1->strip_array_num, 1);
1052 ASSERT_EQ(layer_2->strip_array_num, 1);
1053
1054 StripKeyframeData &strip_data_1 = layer_1->strip(0)->data<StripKeyframeData>(*action);
1055 StripKeyframeData &strip_data_2 = layer_2->strip(0)->data<StripKeyframeData>(*action_2);
1056
1057 ASSERT_EQ(strip_data_1.channelbag_array_num, 1);
1058 ASSERT_EQ(strip_data_2.channelbag_array_num, 1);
1059
1060 Channelbag *bag_1 = strip_data_1.channelbag(0);
1061 Channelbag *bag_2 = strip_data_2.channelbag(0);
1062
1063 ASSERT_EQ(bag_1->fcurve_array_num, 2);
1064 ASSERT_EQ(bag_2->fcurve_array_num, 2);
1065
1066 move_slot(*bmain, slot_suzanne, *action_2, *action);
1067
1068 ASSERT_EQ(strip_data_1.channelbag_array_num, 2);
1069 ASSERT_EQ(strip_data_2.channelbag_array_num, 0);
1070
1071 ASSERT_EQ(action->slot_array_num, 2);
1072 ASSERT_EQ(action_2->slot_array_num, 0);
1073
1074 /* Action should have been reassigned. */
1075 ASSERT_EQ(action, cube->adt->action);
1076 ASSERT_EQ(action, suzanne->adt->action);
1077}
1078
1079TEST_F(ActionLayersTest, action_move_slot_without_channelbag)
1080{
1081 Action *action_2 = BKE_id_new<Action>(bmain, "Action 2");
1082 EXPECT_TRUE(action->is_empty());
1083
1084 Slot &slot_cube = action->slot_add();
1085 Slot &slot_suzanne = action_2->slot_add();
1086 EXPECT_EQ(assign_action_and_slot(action, &slot_cube, cube->id), ActionSlotAssignmentResult::OK);
1087 EXPECT_EQ(assign_action_and_slot(action_2, &slot_suzanne, suzanne->id),
1089
1090 PointerRNA cube_rna_pointer = RNA_id_pointer_create(&cube->id);
1091 PointerRNA suzanne_rna_pointer = RNA_id_pointer_create(&suzanne->id);
1092
1093 action_fcurve_ensure_ex(bmain, action, &cube_rna_pointer, {"location", 0});
1094 action_fcurve_ensure_ex(bmain, action, &cube_rna_pointer, {"rotation_euler", 1});
1095
1096 /* Make sure action_2 has a keyframe strip, but without a channelbag. */
1097 action_2->layer_add("Bagless").strip_add(*action_2, Strip::Type::Keyframe);
1098
1099 ASSERT_EQ(action->layer_array_num, 1);
1100 ASSERT_EQ(action_2->layer_array_num, 1);
1101
1102 Layer *layer_1 = action->layer(0);
1103 Layer *layer_2 = action_2->layer(0);
1104
1105 ASSERT_EQ(layer_1->strip_array_num, 1);
1106 ASSERT_EQ(layer_2->strip_array_num, 1);
1107
1108 StripKeyframeData &strip_data_1 = layer_1->strip(0)->data<StripKeyframeData>(*action);
1109 StripKeyframeData &strip_data_2 = layer_2->strip(0)->data<StripKeyframeData>(*action_2);
1110
1111 ASSERT_EQ(strip_data_1.channelbag_array_num, 1);
1112 ASSERT_EQ(strip_data_2.channelbag_array_num, 0)
1113 << "the keyframe strip of action_2 should NOT have a channelbag in this test";
1114
1115 Channelbag *bag_1 = strip_data_1.channelbag(0);
1116 ASSERT_EQ(bag_1->fcurve_array_num, 2);
1117
1118 move_slot(*bmain, slot_suzanne, *action_2, *action);
1119
1120 ASSERT_EQ(strip_data_1.channelbag_array_num, 1);
1121 ASSERT_EQ(strip_data_2.channelbag_array_num, 0);
1122
1123 ASSERT_EQ(action->slot_array_num, 2);
1124 ASSERT_EQ(action_2->slot_array_num, 0);
1125
1126 /* Action should have been reassigned. */
1127 ASSERT_EQ(action, cube->adt->action);
1128 ASSERT_EQ(action, suzanne->adt->action);
1129}
1130
1131TEST_F(ActionLayersTest, action_duplicate_slot)
1132{
1133 ASSERT_TRUE(action->is_empty());
1134
1135 Slot &slot_cube = action->slot_add();
1136 ASSERT_EQ(assign_action_and_slot(action, &slot_cube, cube->id), ActionSlotAssignmentResult::OK);
1137
1138 PointerRNA cube_rna_pointer = RNA_id_pointer_create(&cube->id);
1139
1140 action_fcurve_ensure_ex(bmain, action, &cube_rna_pointer, {"location", 0});
1141 action_fcurve_ensure_ex(bmain, action, &cube_rna_pointer, {"rotation_euler", 1});
1142
1143 ASSERT_EQ(action->layer_array_num, 1);
1144 Layer *layer = action->layer(0);
1145
1146 ASSERT_EQ(layer->strip_array_num, 1);
1147 StripKeyframeData &strip_data = layer->strip(0)->data<StripKeyframeData>(*action);
1148
1149 ASSERT_EQ(strip_data.channelbag_array_num, 1);
1150 Channelbag *bag = strip_data.channelbag(0);
1151 ASSERT_EQ(bag->fcurve_array_num, 2);
1152
1153 /* Duplicate the slot and check it for uniqueness within the Action. */
1154 Slot &dupli_slot = duplicate_slot(*action, slot_cube);
1155 EXPECT_NE(dupli_slot.identifier, slot_cube.identifier);
1156 EXPECT_NE(dupli_slot.handle, slot_cube.handle);
1157 ASSERT_EQ(action->slot_array_num, 2);
1158 EXPECT_EQ(&dupli_slot, action->slot(1));
1159
1160 /* Check the channelbag has been duplicated correctly. */
1161 ASSERT_EQ(strip_data.channelbag_array_num, 2);
1162 Channelbag *dupli_bag = strip_data.channelbag(1);
1163 EXPECT_EQ(dupli_bag->slot_handle, dupli_slot.handle);
1164 EXPECT_EQ(dupli_bag->fcurve_array_num, 2);
1165
1166 /* Check the original channelbag is untouched. */
1167 EXPECT_EQ(bag->slot_handle, slot_cube.handle);
1168 EXPECT_EQ(bag->fcurve_array_num, 2);
1169
1170 /* The slot should NOT have been reassigned. */
1171 EXPECT_EQ(action, cube->adt->action);
1172 EXPECT_EQ(slot_cube.handle, cube->adt->slot_handle);
1173}
1174
1175TEST_F(ActionLayersTest, action_duplicate_slot_without_channelbag)
1176{
1177 ASSERT_TRUE(action->is_empty());
1178
1179 Slot &slot_cube = action->slot_add();
1180 ASSERT_EQ(assign_action_and_slot(action, &slot_cube, cube->id), ActionSlotAssignmentResult::OK);
1181
1182 /* Create a keyframe strip, but without any channelbags. */
1183 action->layer_keystrip_ensure();
1184
1185 ASSERT_EQ(action->layer_array_num, 1);
1186 Layer *layer = action->layer(0);
1187
1188 ASSERT_EQ(layer->strip_array_num, 1);
1189 StripKeyframeData &strip_data = layer->strip(0)->data<StripKeyframeData>(*action);
1190
1191 ASSERT_EQ(strip_data.channelbag_array_num, 0);
1192
1193 /* Duplicate the slot and check it for uniqueness within the Action. */
1194 Slot &dupli_slot = duplicate_slot(*action, slot_cube);
1195 EXPECT_NE(dupli_slot.identifier, slot_cube.identifier);
1196 EXPECT_NE(dupli_slot.handle, slot_cube.handle);
1197 ASSERT_EQ(action->slot_array_num, 2);
1198 EXPECT_EQ(&dupli_slot, action->slot(1));
1199
1200 /* Check there are still no channelbags. */
1201 EXPECT_EQ(strip_data.channelbag_array_num, 0);
1202
1203 /* The slot should NOT have been reassigned. */
1204 EXPECT_EQ(action, cube->adt->action);
1205 EXPECT_EQ(slot_cube.handle, cube->adt->slot_handle);
1206}
1207
1209{
1210 Slot &slot1 = action->slot_add();
1211 Slot &slot2 = action->slot_add();
1212
1213 action->layer_keystrip_ensure();
1214 StripKeyframeData &key_data = action->layer(0)->strip(0)->data<StripKeyframeData>(*action);
1215
1216 FCurve &fcurve1 = key_data.channelbag_for_slot_ensure(slot1).fcurve_ensure(bmain,
1217 {"location", 1});
1218 FCurve &fcurve2 = key_data.channelbag_for_slot_ensure(slot2).fcurve_ensure(bmain, {"scale", 2});
1219
1220 Vector<FCurve *> fcurve1_expect = {&fcurve1};
1221 Vector<FCurve *> fcurve2_expect = {&fcurve2};
1222 EXPECT_EQ(fcurve1_expect.as_span(), fcurves_for_action_slot(*action, slot1.handle));
1223 EXPECT_EQ(fcurve2_expect.as_span(), fcurves_for_action_slot(*action, slot2.handle));
1224}
1225
1226/*-----------------------------------------------------------*/
1227
1228/* Allocate fcu->bezt, and also return a unique_ptr to it for easily freeing the memory. */
1229static void allocate_keyframes(FCurve &fcu, const size_t num_keyframes)
1230{
1231 fcu.bezt = MEM_new_array_zeroed<BezTriple>(num_keyframes, __func__);
1232}
1233
1234/* Append keyframe, assumes that fcu->bezt is allocated and has enough space. */
1235static void add_keyframe(FCurve &fcu, float x, float y)
1236{
1237 /* The insert_keyframe functions are in the editors, so we cannot link to those here. */
1238 BezTriple the_keyframe = {};
1239
1240 /* Copied from insert_vert_fcurve() in `keyframing.cc`. */
1241 the_keyframe.vec[0][0] = x - 1.0f;
1242 the_keyframe.vec[0][1] = y;
1243 the_keyframe.vec[1][0] = x;
1244 the_keyframe.vec[1][1] = y;
1245 the_keyframe.vec[2][0] = x + 1.0f;
1246 the_keyframe.vec[2][1] = y;
1247
1248 memcpy(&fcu.bezt[fcu.totvert], &the_keyframe, sizeof(the_keyframe));
1249 fcu.totvert++;
1250}
1251
1252static void add_fcurve_to_action(Action &action, FCurve &fcu)
1253{
1254 Slot &slot = action.slot_array_num > 0 ? *action.slot(0) : action.slot_add();
1255 action.layer_keystrip_ensure();
1256 StripKeyframeData &strip_data = action.layer(0)->strip(0)->data<StripKeyframeData>(action);
1257 Channelbag &cbag = strip_data.channelbag_for_slot_ensure(slot);
1258 cbag.fcurve_append(fcu);
1259}
1260
1262 public:
1264
1265 void SetUp() override
1266 {
1267 bmain = BKE_main_new();
1268 }
1269
1270 void TearDown() override
1271 {
1273 }
1274
1276 {
1277 return *BKE_id_new<Action>(bmain, "ACÄnimåtië");
1278 }
1279};
1280
1281TEST_F(ActionQueryTest, BKE_action_frame_range_calc)
1282{
1283 /* No FCurves. */
1284 {
1285 const Action &empty = action_new();
1286 EXPECT_EQ((float2{0.0f, 0.0f}), empty.get_frame_range_of_keys(false));
1287 }
1288
1289 /* One curve with one key. */
1290 {
1291 FCurve &fcu = *BKE_fcurve_create();
1292 allocate_keyframes(fcu, 1);
1293 add_keyframe(fcu, 1.0f, 2.0f);
1294
1295 Action &action = action_new();
1296 add_fcurve_to_action(action, fcu);
1297
1298 const float2 frame_range = action.get_frame_range_of_keys(false);
1299 EXPECT_FLOAT_EQ(frame_range[0], 1.0f);
1300 EXPECT_FLOAT_EQ(frame_range[1], 1.0f);
1301 }
1302
1303 /* Two curves with one key each on different frames. */
1304 {
1305 FCurve &fcu1 = *BKE_fcurve_create();
1306 FCurve &fcu2 = *BKE_fcurve_create();
1307 allocate_keyframes(fcu1, 1);
1308 allocate_keyframes(fcu2, 1);
1309 add_keyframe(fcu1, 1.0f, 2.0f);
1310 add_keyframe(fcu2, 1.5f, 2.0f);
1311
1312 Action &action = action_new();
1313 add_fcurve_to_action(action, fcu1);
1314 add_fcurve_to_action(action, fcu2);
1315
1316 const float2 frame_range = action.get_frame_range_of_keys(false);
1317 EXPECT_FLOAT_EQ(frame_range[0], 1.0f);
1318 EXPECT_FLOAT_EQ(frame_range[1], 1.5f);
1319 }
1320
1321 /* One curve with two keys. */
1322 {
1323 FCurve &fcu = *BKE_fcurve_create();
1324 allocate_keyframes(fcu, 2);
1325 add_keyframe(fcu, 1.0f, 2.0f);
1326 add_keyframe(fcu, 1.5f, 2.0f);
1327
1328 Action &action = action_new();
1329 add_fcurve_to_action(action, fcu);
1330
1331 const float2 frame_range = action.get_frame_range_of_keys(false);
1332 EXPECT_FLOAT_EQ(frame_range[0], 1.0f);
1333 EXPECT_FLOAT_EQ(frame_range[1], 1.5f);
1334 }
1335
1336 /* TODO: action with fcurve modifiers. */
1337}
1338
1339TEST_F(ActionQueryTest, action_has_single_frame)
1340{
1341 /* No FCurves. */
1342 {
1343 Action &action = action_new();
1345 << "Action without FCurves cannot have a single frame.";
1346 }
1347
1348 /* One curve with one key. */
1349 {
1350 FCurve &fcu = *BKE_fcurve_create();
1351 allocate_keyframes(fcu, 1);
1352 add_keyframe(fcu, 1.0f, 2.0f);
1353
1354 Action &action = action_new();
1355 add_fcurve_to_action(action, fcu);
1356
1358 << "Action with one FCurve and one key should have single frame.";
1359 }
1360 return;
1361
1362 /* Two curves with one key each. */
1363 {
1364 FCurve &fcu1 = *BKE_fcurve_create();
1365 FCurve &fcu2 = *BKE_fcurve_create();
1366 allocate_keyframes(fcu1, 1);
1367 allocate_keyframes(fcu2, 1);
1368 add_keyframe(fcu1, 1.0f, 327.0f);
1369 add_keyframe(fcu2, 1.0f, 47.0f); /* Same X-coordinate as the other one. */
1370
1371 Action &action = action_new();
1372 add_fcurve_to_action(action, fcu1);
1373 add_fcurve_to_action(action, fcu2);
1374
1375 EXPECT_TRUE(action.wrap().has_single_frame())
1376 << "Two FCurves with keys on the same frame should have single frame.";
1377
1378 /* Modify the 2nd curve so it's keyed on a different frame. */
1379 fcu2.bezt[0].vec[1][0] = 2.0f;
1381 << "Two FCurves with keys on different frames should have animation.";
1382 }
1383
1384 /* One curve with two keys. */
1385 {
1386 FCurve &fcu = *BKE_fcurve_create();
1387 allocate_keyframes(fcu, 2);
1388 add_keyframe(fcu, 1.0f, 2.0f);
1389 add_keyframe(fcu, 2.0f, 2.5f);
1390
1391 Action &action = action_new();
1392 add_fcurve_to_action(action, fcu);
1393
1395 << "Action with one FCurve and two keys must have animation.";
1396 }
1397}
1398
1399/*-----------------------------------------------------------*/
1400
1402 public:
1404
1405 void SetUp() override
1406 {
1407 channelbag = new Channelbag();
1408 }
1409
1410 void TearDown() override
1411 {
1412 delete channelbag;
1413 }
1414};
1415
1416TEST_F(ChannelbagTest, fcurve_create_many)
1417{
1418 FCurve &existing1 = channelbag->fcurve_ensure(nullptr, {"fcu0", 0, {}, {}, "group0"});
1419 FCurve &existing2 = channelbag->fcurve_ensure(nullptr, {"fcu0", 1, {}, {}, "group0"});
1420 FCurve &existing3 = channelbag->fcurve_ensure(nullptr, {"fcu1", 1, {}, {}, "group1"});
1421 FCurve &existing4 = channelbag->fcurve_ensure(nullptr, {"fcu_", 0});
1422 ASSERT_EQ(2, channelbag->channel_groups().size());
1423 ASSERT_EQ(4, channelbag->fcurves().size());
1424
1425 FCurveDescriptor desc[] = {
1426 /* New group. */
1427 {"fcu2", 0, {}, {}, "group2"},
1428 {"fcu2", 1, {}, {}, "group2"},
1429 {"fcu2", 2, {}, {}, "group2"},
1430 /* Existing groups. */
1431 {"fcu3", 0, {}, {}, "group1"},
1432 {"fcu4", 0, {}, {}, "group0"},
1433 {"fcu5", 0, {}, {}, "group1"},
1434 {"fcu6", 0, {}, {}, "group0"},
1435 {"fcu7", 0, {}, {}, "group2"},
1436 /* No group. */
1437 {"fcu8", 0},
1438 {"fcu8", 1},
1439 /* Empty rna path, should return null. */
1440 {"", 0, {}, {}, "irrelevant"},
1441 /* Should return null since such curves already exist. */
1442 {"fcu0", 1, {}, {}, "irrelevant"},
1443 {"fcu5", 0, {}, {}, "also unused"},
1444 {"fcu2", 0, {}, {}, "group2"},
1445 {"fcu6", 0},
1446 };
1447 Vector<FCurve *> fcurves = channelbag->fcurve_create_many(nullptr, {desc, ARRAY_SIZE(desc)});
1448 ASSERT_EQ(15, fcurves.size());
1449
1450 EXPECT_STREQ("group2", fcurves[0]->grp->name);
1451 EXPECT_STREQ("group2", fcurves[1]->grp->name);
1452 EXPECT_STREQ("group2", fcurves[2]->grp->name);
1453 EXPECT_STREQ("group1", fcurves[3]->grp->name);
1454 EXPECT_STREQ("group0", fcurves[4]->grp->name);
1455 EXPECT_STREQ("group1", fcurves[5]->grp->name);
1456 EXPECT_STREQ("group0", fcurves[6]->grp->name);
1457 EXPECT_STREQ("group2", fcurves[7]->grp->name);
1458 EXPECT_EQ(nullptr, fcurves[8]->grp);
1459 EXPECT_EQ(nullptr, fcurves[9]->grp);
1460 EXPECT_EQ(nullptr, fcurves[10]);
1461 EXPECT_EQ(nullptr, fcurves[11]);
1462 EXPECT_EQ(nullptr, fcurves[12]);
1463 EXPECT_EQ(nullptr, fcurves[13]);
1464 EXPECT_EQ(nullptr, fcurves[14]);
1465
1466 EXPECT_EQ(3, channelbag->channel_groups().size());
1467 EXPECT_EQ(14, channelbag->fcurves().size());
1468
1469 EXPECT_STREQ("group0", existing1.grp->name);
1470 EXPECT_STREQ("group0", existing2.grp->name);
1471 EXPECT_STREQ("group1", existing3.grp->name);
1472 EXPECT_EQ(nullptr, existing4.grp);
1473}
1474
1475TEST_F(ChannelbagTest, fcurve_move_to_index)
1476{
1477 FCurve &fcu0 = channelbag->fcurve_ensure(nullptr, {"fcu0", 0, {}, {}, "group0"});
1478 FCurve &fcu1 = channelbag->fcurve_ensure(nullptr, {"fcu1", 0, {}, {}, "group0"});
1479 FCurve &fcu2 = channelbag->fcurve_ensure(nullptr, {"fcu2", 0, {}, {}, "group1"});
1480 FCurve &fcu3 = channelbag->fcurve_ensure(nullptr, {"fcu3", 0, {}, {}, "group1"});
1481 FCurve &fcu4 = channelbag->fcurve_ensure(nullptr, {"fcu4", 0});
1482
1483 ASSERT_EQ(5, channelbag->fcurves().size());
1484 ASSERT_EQ(2, channelbag->channel_groups().size());
1485
1486 bActionGroup &group0 = *channelbag->channel_group(0);
1487 bActionGroup &group1 = *channelbag->channel_group(1);
1488
1489 /* Moving an fcurve to where it already is should be fine. */
1490 channelbag->fcurve_move_to_index(fcu0, 0);
1491 EXPECT_EQ(&fcu0, channelbag->fcurve(0));
1492 EXPECT_EQ(&fcu1, channelbag->fcurve(1));
1493 EXPECT_EQ(&fcu2, channelbag->fcurve(2));
1494 EXPECT_EQ(&fcu3, channelbag->fcurve(3));
1495 EXPECT_EQ(&fcu4, channelbag->fcurve(4));
1496 EXPECT_EQ(&group0, fcu0.grp);
1497 EXPECT_EQ(&group0, fcu1.grp);
1498 EXPECT_EQ(&group1, fcu2.grp);
1499 EXPECT_EQ(&group1, fcu3.grp);
1500 EXPECT_EQ(nullptr, fcu4.grp);
1501
1502 /* Move to first. */
1503 channelbag->fcurve_move_to_index(fcu4, 0);
1504 EXPECT_EQ(0, group0.fcurve_range_start);
1505 EXPECT_EQ(2, group0.fcurve_range_length);
1506 EXPECT_EQ(2, group1.fcurve_range_start);
1507 EXPECT_EQ(2, group1.fcurve_range_length);
1508 EXPECT_EQ(&fcu4, channelbag->fcurve(0));
1509 EXPECT_EQ(&fcu0, channelbag->fcurve(1));
1510 EXPECT_EQ(&fcu1, channelbag->fcurve(2));
1511 EXPECT_EQ(&fcu2, channelbag->fcurve(3));
1512 EXPECT_EQ(&fcu3, channelbag->fcurve(4));
1513 EXPECT_EQ(&group0, fcu4.grp);
1514 EXPECT_EQ(&group0, fcu0.grp);
1515 EXPECT_EQ(&group1, fcu1.grp);
1516 EXPECT_EQ(&group1, fcu2.grp);
1517 EXPECT_EQ(nullptr, fcu3.grp);
1518
1519 /* Move to last. */
1520 channelbag->fcurve_move_to_index(fcu1, 4);
1521 EXPECT_EQ(0, group0.fcurve_range_start);
1522 EXPECT_EQ(2, group0.fcurve_range_length);
1523 EXPECT_EQ(2, group1.fcurve_range_start);
1524 EXPECT_EQ(2, group1.fcurve_range_length);
1525 EXPECT_EQ(&fcu4, channelbag->fcurve(0));
1526 EXPECT_EQ(&fcu0, channelbag->fcurve(1));
1527 EXPECT_EQ(&fcu2, channelbag->fcurve(2));
1528 EXPECT_EQ(&fcu3, channelbag->fcurve(3));
1529 EXPECT_EQ(&fcu1, channelbag->fcurve(4));
1530 EXPECT_EQ(&group0, fcu4.grp);
1531 EXPECT_EQ(&group0, fcu0.grp);
1532 EXPECT_EQ(&group1, fcu2.grp);
1533 EXPECT_EQ(&group1, fcu3.grp);
1534 EXPECT_EQ(nullptr, fcu1.grp);
1535
1536 /* Move to middle. */
1537 channelbag->fcurve_move_to_index(fcu4, 2);
1538 EXPECT_EQ(0, group0.fcurve_range_start);
1539 EXPECT_EQ(2, group0.fcurve_range_length);
1540 EXPECT_EQ(2, group1.fcurve_range_start);
1541 EXPECT_EQ(2, group1.fcurve_range_length);
1542 EXPECT_EQ(&fcu0, channelbag->fcurve(0));
1543 EXPECT_EQ(&fcu2, channelbag->fcurve(1));
1544 EXPECT_EQ(&fcu4, channelbag->fcurve(2));
1545 EXPECT_EQ(&fcu3, channelbag->fcurve(3));
1546 EXPECT_EQ(&fcu1, channelbag->fcurve(4));
1547 EXPECT_EQ(&group0, fcu0.grp);
1548 EXPECT_EQ(&group0, fcu2.grp);
1549 EXPECT_EQ(&group1, fcu4.grp);
1550 EXPECT_EQ(&group1, fcu3.grp);
1551 EXPECT_EQ(nullptr, fcu1.grp);
1552}
1553
1554TEST_F(ChannelbagTest, channel_group_create)
1555{
1556 ASSERT_TRUE(channelbag->channel_groups().is_empty());
1557
1558 bActionGroup &group0 = channelbag->channel_group_create("Foo");
1559 ASSERT_EQ(channelbag->channel_groups().size(), 1);
1560 EXPECT_EQ(StringRef{group0.name}, StringRef{"Foo"});
1561 EXPECT_EQ(group0.fcurve_range_start, 0);
1562 EXPECT_EQ(group0.fcurve_range_length, 0);
1563 EXPECT_EQ(&group0, channelbag->channel_group(0));
1564
1565 /* Set for testing purposes. Does not reflect actual fcurves in this test. */
1566 group0.fcurve_range_length = 2;
1567
1568 bActionGroup &group1 = channelbag->channel_group_create("Bar");
1569 ASSERT_EQ(channelbag->channel_groups().size(), 2);
1570 EXPECT_EQ(StringRef{group1.name}, StringRef{"Bar"});
1571 EXPECT_EQ(group1.fcurve_range_start, 2);
1572 EXPECT_EQ(group1.fcurve_range_length, 0);
1573 EXPECT_EQ(&group0, channelbag->channel_group(0));
1574 EXPECT_EQ(&group1, channelbag->channel_group(1));
1575
1576 /* Set for testing purposes. Does not reflect actual fcurves in this test. */
1577 group1.fcurve_range_length = 1;
1578
1579 bActionGroup &group2 = channelbag->channel_group_create("Yar");
1580 ASSERT_EQ(channelbag->channel_groups().size(), 3);
1581 EXPECT_EQ(StringRef{group2.name}, StringRef{"Yar"});
1582 EXPECT_EQ(group2.fcurve_range_start, 3);
1583 EXPECT_EQ(group2.fcurve_range_length, 0);
1584 EXPECT_EQ(&group0, channelbag->channel_group(0));
1585 EXPECT_EQ(&group1, channelbag->channel_group(1));
1586 EXPECT_EQ(&group2, channelbag->channel_group(2));
1587}
1588
1589TEST_F(ChannelbagTest, channel_group_remove)
1590{
1591 bActionGroup &group0 = channelbag->channel_group_create("Group0");
1592 bActionGroup &group1 = channelbag->channel_group_create("Group1");
1593 bActionGroup &group2 = channelbag->channel_group_create("Group2");
1594
1595 FCurve &fcu0 = channelbag->fcurve_ensure(nullptr, {"fcu0", 0, {}, {}, "Group0"});
1596 FCurve &fcu1 = channelbag->fcurve_ensure(nullptr, {"fcu1", 0, {}, {}, "Group0"});
1597 FCurve &fcu2 = channelbag->fcurve_ensure(nullptr, {"fcu2", 0, {}, {}, "Group2"});
1598 FCurve &fcu3 = channelbag->fcurve_ensure(nullptr, {"fcu3", 0, {}, {}, "Group2"});
1599 FCurve &fcu4 = channelbag->fcurve_ensure(nullptr, {"fcu4", 0});
1600
1601 ASSERT_EQ(3, channelbag->channel_groups().size());
1602 ASSERT_EQ(5, channelbag->fcurves().size());
1603
1604 /* Attempt to remove a group that's not in the channel bag. Shouldn't do
1605 * anything. */
1606 bActionGroup bogus;
1607 EXPECT_EQ(false, channelbag->channel_group_remove(bogus));
1608 ASSERT_EQ(3, channelbag->channel_groups().size());
1609 ASSERT_EQ(5, channelbag->fcurves().size());
1610 EXPECT_EQ(&group0, channelbag->channel_group(0));
1611 EXPECT_EQ(&group1, channelbag->channel_group(1));
1612 EXPECT_EQ(&group2, channelbag->channel_group(2));
1613 EXPECT_EQ(&fcu0, channelbag->fcurve(0));
1614 EXPECT_EQ(&fcu1, channelbag->fcurve(1));
1615 EXPECT_EQ(&fcu2, channelbag->fcurve(2));
1616 EXPECT_EQ(&fcu3, channelbag->fcurve(3));
1617 EXPECT_EQ(&fcu4, channelbag->fcurve(4));
1618 EXPECT_EQ(&group0, fcu0.grp);
1619 EXPECT_EQ(&group0, fcu1.grp);
1620 EXPECT_EQ(&group2, fcu2.grp);
1621 EXPECT_EQ(&group2, fcu3.grp);
1622 EXPECT_EQ(nullptr, fcu4.grp);
1623
1624 /* Removing an empty group shouldn't affect the fcurves at all. */
1625 EXPECT_EQ(true, channelbag->channel_group_remove(group1));
1626 ASSERT_EQ(2, channelbag->channel_groups().size());
1627 ASSERT_EQ(5, channelbag->fcurves().size());
1628 EXPECT_EQ(&group0, channelbag->channel_group(0));
1629 EXPECT_EQ(&group2, channelbag->channel_group(1));
1630 EXPECT_EQ(&fcu0, channelbag->fcurve(0));
1631 EXPECT_EQ(&fcu1, channelbag->fcurve(1));
1632 EXPECT_EQ(&fcu2, channelbag->fcurve(2));
1633 EXPECT_EQ(&fcu3, channelbag->fcurve(3));
1634 EXPECT_EQ(&fcu4, channelbag->fcurve(4));
1635 EXPECT_EQ(&group0, fcu0.grp);
1636 EXPECT_EQ(&group0, fcu1.grp);
1637 EXPECT_EQ(&group2, fcu2.grp);
1638 EXPECT_EQ(&group2, fcu3.grp);
1639 EXPECT_EQ(nullptr, fcu4.grp);
1640
1641 /* Removing a group that's not at the end of the group array should move its
1642 * fcurves to be just after the grouped fcurves. */
1643 EXPECT_EQ(true, channelbag->channel_group_remove(group0));
1644 ASSERT_EQ(1, channelbag->channel_groups().size());
1645 ASSERT_EQ(5, channelbag->fcurves().size());
1646 EXPECT_EQ(&group2, channelbag->channel_group(0));
1647 EXPECT_EQ(&fcu2, channelbag->fcurve(0));
1648 EXPECT_EQ(&fcu3, channelbag->fcurve(1));
1649 EXPECT_EQ(&fcu0, channelbag->fcurve(2));
1650 EXPECT_EQ(&fcu1, channelbag->fcurve(3));
1651 EXPECT_EQ(&fcu4, channelbag->fcurve(4));
1652 EXPECT_EQ(nullptr, fcu0.grp);
1653 EXPECT_EQ(nullptr, fcu1.grp);
1654 EXPECT_EQ(&group2, fcu2.grp);
1655 EXPECT_EQ(&group2, fcu3.grp);
1656 EXPECT_EQ(nullptr, fcu4.grp);
1657
1658 /* Removing a group at the end of the group array shouldn't move its
1659 * fcurves. */
1660 EXPECT_EQ(true, channelbag->channel_group_remove(group2));
1661 ASSERT_EQ(0, channelbag->channel_groups().size());
1662 ASSERT_EQ(5, channelbag->fcurves().size());
1663 EXPECT_EQ(&fcu2, channelbag->fcurve(0));
1664 EXPECT_EQ(&fcu3, channelbag->fcurve(1));
1665 EXPECT_EQ(&fcu0, channelbag->fcurve(2));
1666 EXPECT_EQ(&fcu1, channelbag->fcurve(3));
1667 EXPECT_EQ(&fcu4, channelbag->fcurve(4));
1668 EXPECT_EQ(nullptr, fcu0.grp);
1669 EXPECT_EQ(nullptr, fcu1.grp);
1670 EXPECT_EQ(nullptr, fcu2.grp);
1671 EXPECT_EQ(nullptr, fcu3.grp);
1672 EXPECT_EQ(nullptr, fcu4.grp);
1673}
1674
1675TEST_F(ChannelbagTest, channel_group_find)
1676{
1677 bActionGroup &group0a = channelbag->channel_group_create("Foo");
1678 bActionGroup &group1a = channelbag->channel_group_create("Bar");
1679 bActionGroup &group2a = channelbag->channel_group_create("Yar");
1680
1681 bActionGroup *group0b = channelbag->channel_group_find("Foo");
1682 bActionGroup *group1b = channelbag->channel_group_find("Bar");
1683 bActionGroup *group2b = channelbag->channel_group_find("Yar");
1684
1685 EXPECT_EQ(&group0a, group0b);
1686 EXPECT_EQ(&group1a, group1b);
1687 EXPECT_EQ(&group2a, group2b);
1688
1689 EXPECT_EQ(nullptr, channelbag->channel_group_find("Wat"));
1690}
1691
1692TEST_F(ChannelbagTest, channel_group_ensure)
1693{
1694 bActionGroup &group0 = channelbag->channel_group_create("Foo");
1695 bActionGroup &group1 = channelbag->channel_group_create("Bar");
1696 EXPECT_EQ(channelbag->channel_groups().size(), 2);
1697
1698 EXPECT_EQ(&group0, &channelbag->channel_group_ensure("Foo"));
1699 EXPECT_EQ(channelbag->channel_groups().size(), 2);
1700
1701 EXPECT_EQ(&group1, &channelbag->channel_group_ensure("Bar"));
1702 EXPECT_EQ(channelbag->channel_groups().size(), 2);
1703
1704 bActionGroup &group2 = channelbag->channel_group_ensure("Yar");
1705 ASSERT_EQ(channelbag->channel_groups().size(), 3);
1706 EXPECT_EQ(&group2, channelbag->channel_group(2));
1707}
1708
1709TEST_F(ChannelbagTest, channel_group_fcurve_creation)
1710{
1711 FCurve &fcu0 = channelbag->fcurve_ensure(nullptr, {"fcu0", 0});
1712 EXPECT_EQ(1, channelbag->fcurves().size());
1713 EXPECT_TRUE(channelbag->channel_groups().is_empty());
1714
1715 /* If an fcurve already exists, then ensuring it with a channel group in the
1716 * fcurve descriptor should NOT add it that group, nor should the group be
1717 * created if it doesn't already exist. */
1718 channelbag->fcurve_ensure(nullptr, {"fcu0", 0, {}, {}, "group0"});
1719 EXPECT_EQ(1, channelbag->fcurves().size());
1720 EXPECT_EQ(nullptr, fcu0.grp);
1721 EXPECT_TRUE(channelbag->channel_groups().is_empty());
1722
1723 /* Creating a new fcurve with a channel group in the fcurve descriptor should
1724 * create the group and put the fcurve in it. This also implies that the
1725 * fcurve will be added before any non-grouped fcurves in the array. */
1726 FCurve &fcu1 = channelbag->fcurve_ensure(nullptr, {"fcu1", 0, {}, {}, "group0"});
1727 ASSERT_EQ(2, channelbag->fcurves().size());
1728 ASSERT_EQ(1, channelbag->channel_groups().size());
1729 bActionGroup &group0 = *channelbag->channel_group(0);
1730 EXPECT_EQ(&fcu1, channelbag->fcurve(0));
1731 EXPECT_EQ(&fcu0, channelbag->fcurve(1));
1732 EXPECT_EQ(&group0, fcu1.grp);
1733 EXPECT_EQ(nullptr, fcu0.grp);
1734 EXPECT_EQ(0, group0.fcurve_range_start);
1735 EXPECT_EQ(1, group0.fcurve_range_length);
1736
1737 /* Creating a new fcurve with a second channel group in the fcurve descriptor
1738 * should create the group and put the fcurve in it. This also implies that
1739 * the fcurve will be added before non-grouped fcurves, but after other
1740 * grouped ones. */
1741 FCurve &fcu2 = channelbag->fcurve_ensure(nullptr, {"fcu2", 0, {}, {}, "group1"});
1742 ASSERT_EQ(3, channelbag->fcurves().size());
1743 ASSERT_EQ(2, channelbag->channel_groups().size());
1744 EXPECT_EQ(&group0, channelbag->channel_group(0));
1745 bActionGroup &group1 = *channelbag->channel_group(1);
1746 EXPECT_EQ(&fcu1, channelbag->fcurve(0));
1747 EXPECT_EQ(&fcu2, channelbag->fcurve(1));
1748 EXPECT_EQ(&fcu0, channelbag->fcurve(2));
1749 EXPECT_EQ(&group0, fcu1.grp);
1750 EXPECT_EQ(&group1, fcu2.grp);
1751 EXPECT_EQ(nullptr, fcu0.grp);
1752 EXPECT_EQ(0, group0.fcurve_range_start);
1753 EXPECT_EQ(1, group0.fcurve_range_length);
1754 EXPECT_EQ(1, group1.fcurve_range_start);
1755 EXPECT_EQ(1, group1.fcurve_range_length);
1756
1757 /* Creating a new fcurve with the first channel group again should put it at
1758 * the end of that group. */
1759 FCurve &fcu3 = channelbag->fcurve_ensure(nullptr, {"fcu3", 0, {}, {}, "group0"});
1760 ASSERT_EQ(4, channelbag->fcurves().size());
1761 ASSERT_EQ(2, channelbag->channel_groups().size());
1762 EXPECT_EQ(&group0, channelbag->channel_group(0));
1763 EXPECT_EQ(&group1, channelbag->channel_group(1));
1764 EXPECT_EQ(&fcu1, channelbag->fcurve(0));
1765 EXPECT_EQ(&fcu3, channelbag->fcurve(1));
1766 EXPECT_EQ(&fcu2, channelbag->fcurve(2));
1767 EXPECT_EQ(&fcu0, channelbag->fcurve(3));
1768 EXPECT_EQ(&group0, fcu1.grp);
1769 EXPECT_EQ(&group0, fcu3.grp);
1770 EXPECT_EQ(&group1, fcu2.grp);
1771 EXPECT_EQ(nullptr, fcu0.grp);
1772 EXPECT_EQ(0, group0.fcurve_range_start);
1773 EXPECT_EQ(2, group0.fcurve_range_length);
1774 EXPECT_EQ(2, group1.fcurve_range_start);
1775 EXPECT_EQ(1, group1.fcurve_range_length);
1776
1777 /* Finally, creating a new fcurve with the second channel group again should
1778 * also put it at the end of that group. */
1779 FCurve &fcu4 = channelbag->fcurve_ensure(nullptr, {"fcu4", 0, {}, {}, "group1"});
1780 ASSERT_EQ(5, channelbag->fcurves().size());
1781 ASSERT_EQ(2, channelbag->channel_groups().size());
1782 EXPECT_EQ(&group0, channelbag->channel_group(0));
1783 EXPECT_EQ(&group1, channelbag->channel_group(1));
1784 EXPECT_EQ(&fcu1, channelbag->fcurve(0));
1785 EXPECT_EQ(&fcu3, channelbag->fcurve(1));
1786 EXPECT_EQ(&fcu2, channelbag->fcurve(2));
1787 EXPECT_EQ(&fcu4, channelbag->fcurve(3));
1788 EXPECT_EQ(&fcu0, channelbag->fcurve(4));
1789 EXPECT_EQ(&group0, fcu1.grp);
1790 EXPECT_EQ(&group0, fcu3.grp);
1791 EXPECT_EQ(&group1, fcu2.grp);
1792 EXPECT_EQ(&group1, fcu4.grp);
1793 EXPECT_EQ(nullptr, fcu0.grp);
1794 EXPECT_EQ(0, group0.fcurve_range_start);
1795 EXPECT_EQ(2, group0.fcurve_range_length);
1796 EXPECT_EQ(2, group1.fcurve_range_start);
1797 EXPECT_EQ(2, group1.fcurve_range_length);
1798}
1799
1800TEST_F(ChannelbagTest, channel_group_fcurve_removal)
1801{
1802 FCurve &fcu0 = channelbag->fcurve_ensure(nullptr, {"fcu0", 0, {}, {}, "group0"});
1803 FCurve &fcu1 = channelbag->fcurve_ensure(nullptr, {"fcu1", 0, {}, {}, "group0"});
1804 FCurve &fcu2 = channelbag->fcurve_ensure(nullptr, {"fcu2", 0, {}, {}, "group1"});
1805 FCurve &fcu3 = channelbag->fcurve_ensure(nullptr, {"fcu3", 0, {}, {}, "group1"});
1806 FCurve &fcu4 = channelbag->fcurve_ensure(nullptr, {"fcu4", 0});
1807
1808 ASSERT_EQ(5, channelbag->fcurves().size());
1809 ASSERT_EQ(2, channelbag->channel_groups().size());
1810
1811 bActionGroup &group0 = *channelbag->channel_group(0);
1812 bActionGroup &group1 = *channelbag->channel_group(1);
1813
1814 EXPECT_EQ(0, group0.fcurve_range_start);
1815 EXPECT_EQ(2, group0.fcurve_range_length);
1816 EXPECT_EQ(2, group1.fcurve_range_start);
1817 EXPECT_EQ(2, group1.fcurve_range_length);
1818 EXPECT_EQ(&group0, fcu0.grp);
1819 EXPECT_EQ(&group0, fcu1.grp);
1820 EXPECT_EQ(&group1, fcu2.grp);
1821 EXPECT_EQ(&group1, fcu3.grp);
1822 EXPECT_EQ(nullptr, fcu4.grp);
1823
1824 channelbag->fcurve_remove(fcu3);
1825 ASSERT_EQ(4, channelbag->fcurves().size());
1826 ASSERT_EQ(2, channelbag->channel_groups().size());
1827 EXPECT_EQ(&group0, channelbag->channel_group(0));
1828 EXPECT_EQ(&group1, channelbag->channel_group(1));
1829 EXPECT_EQ(0, group0.fcurve_range_start);
1830 EXPECT_EQ(2, group0.fcurve_range_length);
1831 EXPECT_EQ(2, group1.fcurve_range_start);
1832 EXPECT_EQ(1, group1.fcurve_range_length);
1833 EXPECT_EQ(&group0, fcu0.grp);
1834 EXPECT_EQ(&group0, fcu1.grp);
1835 EXPECT_EQ(&group1, fcu2.grp);
1836 EXPECT_EQ(nullptr, fcu4.grp);
1837
1838 channelbag->fcurve_remove(fcu0);
1839 ASSERT_EQ(3, channelbag->fcurves().size());
1840 ASSERT_EQ(2, channelbag->channel_groups().size());
1841 EXPECT_EQ(&group0, channelbag->channel_group(0));
1842 EXPECT_EQ(&group1, channelbag->channel_group(1));
1843 EXPECT_EQ(0, group0.fcurve_range_start);
1844 EXPECT_EQ(1, group0.fcurve_range_length);
1845 EXPECT_EQ(1, group1.fcurve_range_start);
1846 EXPECT_EQ(1, group1.fcurve_range_length);
1847 EXPECT_EQ(&group0, fcu1.grp);
1848 EXPECT_EQ(&group1, fcu2.grp);
1849 EXPECT_EQ(nullptr, fcu4.grp);
1850
1851 channelbag->fcurve_remove(fcu1);
1852 ASSERT_EQ(2, channelbag->fcurves().size());
1853 ASSERT_EQ(1, channelbag->channel_groups().size());
1854 EXPECT_EQ(&group1, channelbag->channel_group(0));
1855 EXPECT_EQ(0, group1.fcurve_range_start);
1856 EXPECT_EQ(1, group1.fcurve_range_length);
1857 EXPECT_EQ(&group1, fcu2.grp);
1858 EXPECT_EQ(nullptr, fcu4.grp);
1859
1860 channelbag->fcurve_remove(fcu4);
1861 ASSERT_EQ(1, channelbag->fcurves().size());
1862 ASSERT_EQ(1, channelbag->channel_groups().size());
1863 EXPECT_EQ(&group1, channelbag->channel_group(0));
1864 EXPECT_EQ(0, group1.fcurve_range_start);
1865 EXPECT_EQ(1, group1.fcurve_range_length);
1866 EXPECT_EQ(&group1, fcu2.grp);
1867
1868 channelbag->fcurve_remove(fcu2);
1869 ASSERT_EQ(0, channelbag->fcurves().size());
1870 ASSERT_EQ(0, channelbag->channel_groups().size());
1871}
1872
1873TEST_F(ChannelbagTest, channel_group_move_to_index)
1874{
1875 FCurve &fcu0 = channelbag->fcurve_ensure(nullptr, {"fcu0", 0, {}, {}, "group0"});
1876 FCurve &fcu1 = channelbag->fcurve_ensure(nullptr, {"fcu1", 0, {}, {}, "group1"});
1877 FCurve &fcu2 = channelbag->fcurve_ensure(nullptr, {"fcu2", 0, {}, {}, "group1"});
1878 FCurve &fcu3 = channelbag->fcurve_ensure(nullptr, {"fcu3", 0, {}, {}, "group2"});
1879 FCurve &fcu4 = channelbag->fcurve_ensure(nullptr, {"fcu4", 0});
1880
1881 ASSERT_EQ(5, channelbag->fcurves().size());
1882 ASSERT_EQ(3, channelbag->channel_groups().size());
1883
1884 bActionGroup &group0 = *channelbag->channel_group(0);
1885 bActionGroup &group1 = *channelbag->channel_group(1);
1886 bActionGroup &group2 = *channelbag->channel_group(2);
1887
1888 channelbag->channel_group_move_to_index(group0, 2);
1889 EXPECT_EQ(&group1, channelbag->channel_group(0));
1890 EXPECT_EQ(&group2, channelbag->channel_group(1));
1891 EXPECT_EQ(&group0, channelbag->channel_group(2));
1892 EXPECT_EQ(0, group1.fcurve_range_start);
1893 EXPECT_EQ(2, group1.fcurve_range_length);
1894 EXPECT_EQ(2, group2.fcurve_range_start);
1895 EXPECT_EQ(1, group2.fcurve_range_length);
1896 EXPECT_EQ(3, group0.fcurve_range_start);
1897 EXPECT_EQ(1, group0.fcurve_range_length);
1898 EXPECT_EQ(&fcu1, channelbag->fcurve(0));
1899 EXPECT_EQ(&fcu2, channelbag->fcurve(1));
1900 EXPECT_EQ(&fcu3, channelbag->fcurve(2));
1901 EXPECT_EQ(&fcu0, channelbag->fcurve(3));
1902 EXPECT_EQ(&fcu4, channelbag->fcurve(4));
1903 EXPECT_EQ(&group1, fcu1.grp);
1904 EXPECT_EQ(&group1, fcu2.grp);
1905 EXPECT_EQ(&group2, fcu3.grp);
1906 EXPECT_EQ(&group0, fcu0.grp);
1907 EXPECT_EQ(nullptr, fcu4.grp);
1908
1909 channelbag->channel_group_move_to_index(group1, 1);
1910 EXPECT_EQ(&group2, channelbag->channel_group(0));
1911 EXPECT_EQ(&group1, channelbag->channel_group(1));
1912 EXPECT_EQ(&group0, channelbag->channel_group(2));
1913 EXPECT_EQ(0, group2.fcurve_range_start);
1914 EXPECT_EQ(1, group2.fcurve_range_length);
1915 EXPECT_EQ(1, group1.fcurve_range_start);
1916 EXPECT_EQ(2, group1.fcurve_range_length);
1917 EXPECT_EQ(3, group0.fcurve_range_start);
1918 EXPECT_EQ(1, group0.fcurve_range_length);
1919 EXPECT_EQ(&fcu3, channelbag->fcurve(0));
1920 EXPECT_EQ(&fcu1, channelbag->fcurve(1));
1921 EXPECT_EQ(&fcu2, channelbag->fcurve(2));
1922 EXPECT_EQ(&fcu0, channelbag->fcurve(3));
1923 EXPECT_EQ(&fcu4, channelbag->fcurve(4));
1924 EXPECT_EQ(&group2, fcu3.grp);
1925 EXPECT_EQ(&group1, fcu1.grp);
1926 EXPECT_EQ(&group1, fcu2.grp);
1927 EXPECT_EQ(&group0, fcu0.grp);
1928 EXPECT_EQ(nullptr, fcu4.grp);
1929
1930 channelbag->channel_group_move_to_index(group0, 0);
1931 EXPECT_EQ(&group0, channelbag->channel_group(0));
1932 EXPECT_EQ(&group2, channelbag->channel_group(1));
1933 EXPECT_EQ(&group1, channelbag->channel_group(2));
1934 EXPECT_EQ(0, group0.fcurve_range_start);
1935 EXPECT_EQ(1, group0.fcurve_range_length);
1936 EXPECT_EQ(1, group2.fcurve_range_start);
1937 EXPECT_EQ(1, group2.fcurve_range_length);
1938 EXPECT_EQ(2, group1.fcurve_range_start);
1939 EXPECT_EQ(2, group1.fcurve_range_length);
1940 EXPECT_EQ(&fcu0, channelbag->fcurve(0));
1941 EXPECT_EQ(&fcu3, channelbag->fcurve(1));
1942 EXPECT_EQ(&fcu1, channelbag->fcurve(2));
1943 EXPECT_EQ(&fcu2, channelbag->fcurve(3));
1944 EXPECT_EQ(&fcu4, channelbag->fcurve(4));
1945 EXPECT_EQ(&group0, fcu0.grp);
1946 EXPECT_EQ(&group2, fcu3.grp);
1947 EXPECT_EQ(&group1, fcu1.grp);
1948 EXPECT_EQ(&group1, fcu2.grp);
1949 EXPECT_EQ(nullptr, fcu4.grp);
1950}
1951
1952TEST_F(ChannelbagTest, channel_group_move_fcurve_into)
1953{
1954 FCurve &fcu0 = channelbag->fcurve_ensure(nullptr, {"fcu0", 0});
1955 FCurve &fcu1 = channelbag->fcurve_ensure(nullptr, {"fcu1", 0});
1956 FCurve &fcu2 = channelbag->fcurve_ensure(nullptr, {"fcu2", 0});
1957 bActionGroup &group0 = channelbag->channel_group_create("group0");
1958 bActionGroup &group1 = channelbag->channel_group_create("group1");
1959
1960 ASSERT_EQ(3, channelbag->fcurves().size());
1961 EXPECT_EQ(&fcu0, channelbag->fcurve(0));
1962 EXPECT_EQ(&fcu1, channelbag->fcurve(1));
1963 EXPECT_EQ(&fcu2, channelbag->fcurve(2));
1964 ASSERT_EQ(2, channelbag->channel_groups().size());
1965 EXPECT_EQ(&group0, channelbag->channel_group(0));
1966 EXPECT_EQ(&group1, channelbag->channel_group(1));
1967 EXPECT_EQ(0, group0.fcurve_range_start);
1968 EXPECT_EQ(0, group0.fcurve_range_length);
1969 EXPECT_EQ(0, group1.fcurve_range_start);
1970 EXPECT_EQ(0, group1.fcurve_range_length);
1971
1972 channelbag->fcurve_assign_to_channel_group(fcu2, group1);
1973 EXPECT_EQ(&fcu2, channelbag->fcurve(0));
1974 EXPECT_EQ(&fcu0, channelbag->fcurve(1));
1975 EXPECT_EQ(&fcu1, channelbag->fcurve(2));
1976 EXPECT_EQ(0, group0.fcurve_range_start);
1977 EXPECT_EQ(0, group0.fcurve_range_length);
1978 EXPECT_EQ(0, group1.fcurve_range_start);
1979 EXPECT_EQ(1, group1.fcurve_range_length);
1980
1981 channelbag->fcurve_assign_to_channel_group(fcu1, group0);
1982 EXPECT_EQ(&fcu1, channelbag->fcurve(0));
1983 EXPECT_EQ(&fcu2, channelbag->fcurve(1));
1984 EXPECT_EQ(&fcu0, channelbag->fcurve(2));
1985 EXPECT_EQ(0, group0.fcurve_range_start);
1986 EXPECT_EQ(1, group0.fcurve_range_length);
1987 EXPECT_EQ(1, group1.fcurve_range_start);
1988 EXPECT_EQ(1, group1.fcurve_range_length);
1989
1990 channelbag->fcurve_assign_to_channel_group(fcu0, group1);
1991 EXPECT_EQ(&fcu1, channelbag->fcurve(0));
1992 EXPECT_EQ(&fcu2, channelbag->fcurve(1));
1993 EXPECT_EQ(&fcu0, channelbag->fcurve(2));
1994 EXPECT_EQ(0, group0.fcurve_range_start);
1995 EXPECT_EQ(1, group0.fcurve_range_length);
1996 EXPECT_EQ(1, group1.fcurve_range_start);
1997 EXPECT_EQ(2, group1.fcurve_range_length);
1998
1999 channelbag->fcurve_assign_to_channel_group(fcu0, group0);
2000 EXPECT_EQ(&fcu1, channelbag->fcurve(0));
2001 EXPECT_EQ(&fcu0, channelbag->fcurve(1));
2002 EXPECT_EQ(&fcu2, channelbag->fcurve(2));
2003 EXPECT_EQ(0, group0.fcurve_range_start);
2004 EXPECT_EQ(2, group0.fcurve_range_length);
2005 EXPECT_EQ(2, group1.fcurve_range_start);
2006 EXPECT_EQ(1, group1.fcurve_range_length);
2007
2008 channelbag->fcurve_assign_to_channel_group(fcu1, group1);
2009 EXPECT_EQ(&fcu0, channelbag->fcurve(0));
2010 EXPECT_EQ(&fcu2, channelbag->fcurve(1));
2011 EXPECT_EQ(&fcu1, channelbag->fcurve(2));
2012 EXPECT_EQ(0, group0.fcurve_range_start);
2013 EXPECT_EQ(1, group0.fcurve_range_length);
2014 EXPECT_EQ(1, group1.fcurve_range_start);
2015 EXPECT_EQ(2, group1.fcurve_range_length);
2016}
2017
2018TEST_F(ChannelbagTest, channel_group_fcurve_ungroup)
2019{
2020 FCurve &fcu0 = channelbag->fcurve_ensure(nullptr, {"fcu0", 0, {}, {}, "group0"});
2021 FCurve &fcu1 = channelbag->fcurve_ensure(nullptr, {"fcu1", 0, {}, {}, "group0"});
2022 FCurve &fcu2 = channelbag->fcurve_ensure(nullptr, {"fcu2", 0, {}, {}, "group1"});
2023 FCurve &fcu3 = channelbag->fcurve_ensure(nullptr, {"fcu3", 0, {}, {}, "group1"});
2024 FCurve &fcu4 = channelbag->fcurve_ensure(nullptr, {"fcu4", 0});
2025
2026 ASSERT_EQ(5, channelbag->fcurves().size());
2027 ASSERT_EQ(2, channelbag->channel_groups().size());
2028
2029 bActionGroup &group0 = *channelbag->channel_group(0);
2030 bActionGroup &group1 = *channelbag->channel_group(1);
2031
2032 /* Attempting to ungroup an fcurve that's not in the channel bag should fail. */
2033 FCurve bogus = {};
2034 EXPECT_FALSE(channelbag->fcurve_ungroup(bogus));
2035
2036 /* Attempting to ungroup an fcurve that's already ungrouped is fine. */
2037 EXPECT_TRUE(channelbag->fcurve_ungroup(fcu4));
2038
2039 /* Ungroup each fcurve until all are ungrouped. */
2040
2041 EXPECT_TRUE(channelbag->fcurve_ungroup(fcu0));
2042 EXPECT_EQ(0, group0.fcurve_range_start);
2043 EXPECT_EQ(1, group0.fcurve_range_length);
2044 EXPECT_EQ(1, group1.fcurve_range_start);
2045 EXPECT_EQ(2, group1.fcurve_range_length);
2046 EXPECT_EQ(&fcu1, channelbag->fcurve(0));
2047 EXPECT_EQ(&fcu2, channelbag->fcurve(1));
2048 EXPECT_EQ(&fcu3, channelbag->fcurve(2));
2049 EXPECT_EQ(&fcu4, channelbag->fcurve(3));
2050 EXPECT_EQ(&fcu0, channelbag->fcurve(4));
2051 EXPECT_EQ(&group0, fcu1.grp);
2052 EXPECT_EQ(&group1, fcu2.grp);
2053 EXPECT_EQ(&group1, fcu3.grp);
2054 EXPECT_EQ(nullptr, fcu4.grp);
2055 EXPECT_EQ(nullptr, fcu0.grp);
2056
2057 EXPECT_TRUE(channelbag->fcurve_ungroup(fcu3));
2058 EXPECT_EQ(0, group0.fcurve_range_start);
2059 EXPECT_EQ(1, group0.fcurve_range_length);
2060 EXPECT_EQ(1, group1.fcurve_range_start);
2061 EXPECT_EQ(1, group1.fcurve_range_length);
2062 EXPECT_EQ(&fcu1, channelbag->fcurve(0));
2063 EXPECT_EQ(&fcu2, channelbag->fcurve(1));
2064 EXPECT_EQ(&fcu4, channelbag->fcurve(2));
2065 EXPECT_EQ(&fcu0, channelbag->fcurve(3));
2066 EXPECT_EQ(&fcu3, channelbag->fcurve(4));
2067 EXPECT_EQ(&group0, fcu1.grp);
2068 EXPECT_EQ(&group1, fcu2.grp);
2069 EXPECT_EQ(nullptr, fcu4.grp);
2070 EXPECT_EQ(nullptr, fcu0.grp);
2071 EXPECT_EQ(nullptr, fcu3.grp);
2072
2073 EXPECT_TRUE(channelbag->fcurve_ungroup(fcu1));
2074 EXPECT_EQ(1, channelbag->channel_groups().size());
2075 EXPECT_EQ(&group1, channelbag->channel_group(0));
2076 EXPECT_EQ(0, group1.fcurve_range_start);
2077 EXPECT_EQ(1, group1.fcurve_range_length);
2078 EXPECT_EQ(&fcu2, channelbag->fcurve(0));
2079 EXPECT_EQ(&fcu4, channelbag->fcurve(1));
2080 EXPECT_EQ(&fcu0, channelbag->fcurve(2));
2081 EXPECT_EQ(&fcu3, channelbag->fcurve(3));
2082 EXPECT_EQ(&fcu1, channelbag->fcurve(4));
2083 EXPECT_EQ(&group1, fcu2.grp);
2084 EXPECT_EQ(nullptr, fcu4.grp);
2085 EXPECT_EQ(nullptr, fcu0.grp);
2086 EXPECT_EQ(nullptr, fcu3.grp);
2087 EXPECT_EQ(nullptr, fcu1.grp);
2088
2089 EXPECT_TRUE(channelbag->fcurve_ungroup(fcu2));
2090 EXPECT_EQ(0, channelbag->channel_groups().size());
2091 EXPECT_EQ(&fcu4, channelbag->fcurve(0));
2092 EXPECT_EQ(&fcu0, channelbag->fcurve(1));
2093 EXPECT_EQ(&fcu3, channelbag->fcurve(2));
2094 EXPECT_EQ(&fcu1, channelbag->fcurve(3));
2095 EXPECT_EQ(&fcu2, channelbag->fcurve(4));
2096 EXPECT_EQ(nullptr, fcu4.grp);
2097 EXPECT_EQ(nullptr, fcu0.grp);
2098 EXPECT_EQ(nullptr, fcu3.grp);
2099 EXPECT_EQ(nullptr, fcu1.grp);
2100 EXPECT_EQ(nullptr, fcu2.grp);
2101}
2102
2103/*-----------------------------------------------------------*/
2104
2106 public:
2108
2109 void SetUp() override
2110 {
2111 bmain = BKE_main_new();
2112 }
2113
2114 void TearDown() override
2115 {
2117 }
2118};
2119
2120TEST_F(ActionFCurveMoveTest, test_fcurve_move_layered)
2121{
2122 Action &action_src = action_add(*this->bmain, "SourceAction");
2123 Action &action_dst = action_add(*this->bmain, "DestinationAction");
2124
2125 /* Add F-Curves to source Action. */
2126 Slot &slot_src = action_src.slot_add();
2127 action_src.layer_keystrip_ensure();
2128 StripKeyframeData &strip_data_src = action_src.layer(0)->strip(0)->data<StripKeyframeData>(
2129 action_src);
2130 Channelbag &cbag_src = strip_data_src.channelbag_for_slot_ensure(slot_src);
2131
2132 cbag_src.fcurve_ensure(this->bmain, {"source_prop", 0});
2133 FCurve &fcurve_to_move = cbag_src.fcurve_ensure(this->bmain, {"source_prop", 2});
2134 bActionGroup &group_src = cbag_src.channel_group_create("Gröpje");
2135 cbag_src.fcurve_assign_to_channel_group(fcurve_to_move, group_src);
2136
2137 /* Add F-Curves to destination Action. */
2138 Slot &slot_dst = action_dst.slot_add();
2139 action_dst.layer_keystrip_ensure();
2140 StripKeyframeData &strip_data_dst = action_dst.layer(0)->strip(0)->data<StripKeyframeData>(
2141 action_dst);
2142 Channelbag &cbag_dst = strip_data_dst.channelbag_for_slot_ensure(slot_dst);
2143
2144 cbag_dst.fcurve_ensure(this->bmain, {"dest_prop", 0});
2145
2146 action_fcurve_move(action_dst, slot_dst.handle, action_src, fcurve_to_move);
2147
2148 EXPECT_EQ(nullptr, cbag_src.fcurve_find({fcurve_to_move.rna_path(), fcurve_to_move.array_index}))
2149 << "F-Curve should no longer exist in source Action";
2150 EXPECT_EQ(&fcurve_to_move,
2151 cbag_dst.fcurve_find({fcurve_to_move.rna_path(), fcurve_to_move.array_index}))
2152 << "F-Curve should exist in destination Action";
2153
2154 EXPECT_EQ(1, cbag_src.fcurves().size()) << "Source Action should still have the other F-Curve";
2155 EXPECT_EQ(2, cbag_dst.fcurves().size())
2156 << "Destination Action should have its original and the moved F-Curve";
2157
2158 bActionGroup *group_dst = cbag_dst.channel_group_find("Gröpje");
2159 ASSERT_NE(nullptr, group_dst) << "Expected channel group to be created";
2160 ASSERT_EQ(group_dst, fcurve_to_move.grp) << "Expected group membership to move as well";
2161}
2162
2163} // namespace blender::animrig::tests
Functions and classes to work with Actions.
Blender kernel action and pose functionality.
General operations, lookup, etc. for blender objects.
#define STRNCPY_UTF8(dst, src)
#define DNA_DEFAULT_ACTION_LAST_SLOT_HANDLE
Object is a sort of wrapper for general info.
int64_t size() const
Span< T > as_span() const
Slot & slot_add_for_id(const ID &animated_id)
const Layer * layer(int64_t index) const
const Slot * slot(int64_t index) const
float2 get_frame_range_of_keys(bool include_modifiers) const ATTR_WARN_UNUSED_RESULT
void slot_identifier_define(Slot &slot, StringRefNull new_identifier)
bool has_single_frame() const ATTR_WARN_UNUSED_RESULT
Layer & layer_add(std::optional< StringRefNull > name)
Span< const FCurve * > fcurves() const
FCurve & fcurve_ensure(Main *bmain, const FCurveDescriptor &fcurve_descriptor)
FCurve * fcurve_create_unique(Main *bmain, const FCurveDescriptor &fcurve_descriptor)
const Strip * strip(int64_t index) const
Strip & strip_add(Action &owning_action, Strip::Type strip_type)
std::string idtype_string() const
Span< ID * > users(Main &bmain) const
static constexpr slot_handle_t unassigned
const Channelbag * channelbag_for_slot(const Slot &slot) const
Span< const Channelbag * > channelbags() const
SingleKeyingResult keyframe_insert(Main *bmain, const Slot &slot, const FCurveDescriptor &fcurve_descriptor, float2 time_value, const KeyframeSettings &settings, eInsertKeyFlags insert_key_flags=INSERTKEY_NOFLAGS, std::optional< float2 > cycle_range=std::nullopt)
Channelbag & channelbag_for_slot_ensure(const Slot &slot)
const Channelbag * channelbag(int64_t index) const
bool contains_frame(float frame_time) const
void resize(float frame_start, float frame_end)
bool is_last_frame(float frame_time) const
const T & data(const Action &owning_action) const
#define layer
#define EXPECT_FALSE(result)
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
static void add_fcurve_to_action(Action &action, FCurve &fcu)
static void allocate_keyframes(FCurve &fcu, const size_t num_keyframes)
TEST_F(ActionIteratorsTest, iterate_all_fcurves_of_slot)
static void add_keyframe(FCurve &fcu, float x, float y)
decltype(blender::ActionSlot::handle) slot_handle_t
KeyframeSettings get_keyframe_settings(bool from_userprefs)
Slot * assign_action_ensure_slot_for_keying(Action &action, ID &animated_id)
FCurve * action_fcurve_ensure_ex(Main *bmain, bAction *act, PointerRNA *ptr, const FCurveDescriptor &fcurve_descriptor)
void action_fcurve_move(Action &action_dst, slot_handle_t action_slot_dst, Action &action_src, FCurve &fcurve)
Action & action_add(Main &bmain, StringRefNull name)
Slot & duplicate_slot(Action &action, const Slot &slot)
ActionSlotAssignmentResult assign_action_and_slot(Action *action, Slot *slot_to_assign, ID &animated_id)
Span< FCurve * > fcurves_for_action_slot(Action &action, slot_handle_t slot_handle)
ID * action_slot_get_id_for_keying(Main &bmain, Action &action, slot_handle_t slot_handle, ID *primary_id)
bool unassign_action(ID &animated_id)
bool assign_action(bAction *action, ID &animated_id)
ActionSlotAssignmentResult assign_action_slot(Slot *slot_to_assign, ID &animated_id)
void move_slot(Main &bmain, Slot &slot, Action &from_action, Action &to_action)
Slot * generic_slot_for_autoassign(const ID &animated_id, Action &action, StringRefNull last_slot_identifier)
PointerRNA RNA_id_pointer_create(ID *id)
Main * BKE_main_new()
Definition main.cc:93
AnimData * BKE_animdata_ensure_id(ID *id)
Definition anim_data.cc:107
VecBase< float, 2 > float2
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
void BKE_id_free(Main *bmain, void *idv)
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
ID_Type GS(const char *name)
Definition DNA_ID.h:410
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
FCurve * BKE_fcurve_create()
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
float evaluate_fcurve(const FCurve *fcu, float evaltime)
animrig::SlotRuntime * runtime
char last_slot_identifier[258]
bActionGroup * grp
unsigned int totvert