| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2020 Saurabh Kumar <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "StoryboardTimelineSyncTest.h" |
| 8 |
#include "StoryboardModel.h" |
| 9 |
#include "StoryboardView.h" |
| 10 |
|
| 11 |
#include <kis_group_layer.h> |
| 12 |
|
| 13 |
#include <QSignalSpy> |
| 14 |
#include <simpletest.h> |
| 15 |
|
| 16 |
|
| 17 |
void StoryboardTimelineSyncTest::initTestCase() |
| 18 |
{ |
| 19 |
m_image = new KisImage(0, 100, 100, nullptr, "image"); |
| 20 |
|
| 21 |
m_storyboardModel = new StoryboardModel(this); |
| 22 |
m_storyboardView = new StoryboardView(); |
| 23 |
m_storyboardModel->setView(m_storyboardView); |
| 24 |
m_storyboardView->setModel(m_storyboardModel); |
| 25 |
m_storyboardModel->setImage(m_image); |
| 26 |
|
| 27 |
m_layer1 = new KisPaintLayer(m_image, "layer1", OPACITY_OPAQUE_U8); |
| 28 |
m_image->addNode(m_layer1, m_image->rootLayer()); |
| 29 |
|
| 30 |
m_layer1->enableAnimation(); |
| 31 |
m_channel1 = m_layer1->getKeyframeChannel(KisKeyframeChannel::Raster.id(), true); |
| 32 |
|
| 33 |
m_layer2 = new KisPaintLayer(m_image, "layer2", OPACITY_OPAQUE_U8); |
| 34 |
m_image->addNode(m_layer2, m_image->rootLayer()); |
| 35 |
|
| 36 |
m_layer2->enableAnimation(); |
| 37 |
m_channel2 = m_layer2->getKeyframeChannel(KisKeyframeChannel::Raster.id(), true); |
| 38 |
|
| 39 |
m_channel1->addKeyframe(0); |
| 40 |
m_channel2->addKeyframe(0); |
| 41 |
|
| 42 |
QCOMPARE(m_storyboardModel->rowCount(), 0); |
| 43 |
} |
| 44 |
|
| 45 |
void StoryboardTimelineSyncTest::cleanupTestCase() |
| 46 |
{ |
| 47 |
// Must give image time to finish before deallocating. |
| 48 |
m_image->waitForDone(); |
| 49 |
|
| 50 |
delete m_storyboardModel; |
| 51 |
delete m_storyboardView; |
| 52 |
} |
| 53 |
|
| 54 |
void StoryboardTimelineSyncTest::cleanup() |
| 55 |
{ |
| 56 |
const int priorChannel1Count = m_channel1->keyframeCount(); |
| 57 |
const int priorChannel2Count = m_channel2->keyframeCount(); |
| 58 |
|
| 59 |
m_storyboardModel->removeRows(0, m_storyboardModel->rowCount()); |
| 60 |
|
| 61 |
QCOMPARE(m_channel1->keyframeCount(), priorChannel1Count); |
| 62 |
QCOMPARE(m_channel2->keyframeCount(), priorChannel2Count); |
| 63 |
QCOMPARE(m_storyboardModel->rowCount(), 0); |
| 64 |
|
| 65 |
// Must give image time to finish before continuing on to the next test. |
| 66 |
m_image->waitForDone(); |
| 67 |
} |
| 68 |
|
| 69 |
void StoryboardTimelineSyncTest::testAddKeyframeExtendsDuration() |
| 70 |
{ |
| 71 |
m_storyboardModel->insertRow(0); |
| 72 |
m_storyboardModel->insertChildRows(0); |
| 73 |
QModelIndex item1Index = m_storyboardModel->index(0, 0); |
| 74 |
|
| 75 |
QVERIFY(item1Index.isValid()); |
| 76 |
QCOMPARE(m_storyboardModel->rowCount(), 1); |
| 77 |
|
| 78 |
//Adding a keyframe beyond the last scene (atm starting on frame 0) |
| 79 |
//should extend the length of the last scene to encompass new |
| 80 |
//keyframe entries. |
| 81 |
|
| 82 |
m_channel1->addKeyframe(5); |
| 83 |
|
| 84 |
{ |
| 85 |
// Scenes == [...] |
| 86 |
// [0 1 2 3 4 5] 6 7 8 ... |
| 87 |
//Scene duration should be 0s, 6f |
| 88 |
//Remember that first frame is included as 1 whole frame! |
| 89 |
QModelIndex frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, item1Index); |
| 90 |
QModelIndex secondDurationIndex = m_storyboardModel->index(StoryboardItem::DurationSecond, 0, item1Index); |
| 91 |
QCOMPARE(frameDurationIndex.data().toInt(), 6); |
| 92 |
QCOMPARE(secondDurationIndex.data().toInt(), 0); |
| 93 |
} |
| 94 |
|
| 95 |
m_channel1->moveKeyframe(5, 25); |
| 96 |
|
| 97 |
{ |
| 98 |
// [0 1 2 ... 23 24 25] 26 27 28 ... |
| 99 |
//Scene duration should be 1s, 1f |
| 100 |
QModelIndex frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, item1Index); |
| 101 |
QModelIndex secondDurationIndex = m_storyboardModel->index(StoryboardItem::DurationSecond, 0, item1Index); |
| 102 |
QCOMPARE(frameDurationIndex.data().toInt(), 2); |
| 103 |
QCOMPARE(secondDurationIndex.data().toInt(), 1); |
| 104 |
} |
| 105 |
|
| 106 |
m_storyboardModel->slotSetActiveNode(m_layer1); |
| 107 |
m_storyboardModel->insertItem(item1Index, true); |
| 108 |
QCOMPARE(m_storyboardModel->rowCount(), 2); |
| 109 |
QVERIFY(m_channel1->keyframeAt(26)); |
| 110 |
QVERIFY(m_channel2->keyframeAt(26)); |
| 111 |
QModelIndex item2Index = m_storyboardModel->index(1,0); |
| 112 |
|
| 113 |
{ |
| 114 |
// [0 1 2 ... 23 24 25] [26] 27 28 29 ... |
| 115 |
QModelIndex frameNumberIndex = m_storyboardModel->index(StoryboardItem::FrameNumber, 0, item2Index); |
| 116 |
QModelIndex frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, item2Index); |
| 117 |
QCOMPARE(frameNumberIndex.data().toInt(), 26); |
| 118 |
QCOMPARE(frameDurationIndex.data().toInt(), 1); |
| 119 |
} |
| 120 |
|
| 121 |
m_channel2->addKeyframe(28); |
| 122 |
|
| 123 |
{ |
| 124 |
// [0 1 2 ... 23 24 25] [26 27 28] 29 30 31 ... |
| 125 |
QModelIndex frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, item2Index); |
| 126 |
QModelIndex secondDurationIndex = m_storyboardModel->index(StoryboardItem::DurationSecond, 0, item2Index); |
| 127 |
QCOMPARE(frameDurationIndex.data().toInt(), 3); |
| 128 |
QCOMPARE(secondDurationIndex.data().toInt(), 0); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
m_channel2->removeKeyframe(28); |
| 133 |
m_channel2->removeKeyframe(26); |
| 134 |
m_channel1->removeKeyframe(26); |
| 135 |
m_channel1->removeKeyframe(25); |
| 136 |
|
| 137 |
QCOMPARE(m_channel2->keyframeCount(), 1); |
| 138 |
QCOMPARE(m_channel1->keyframeCount(), 1); |
| 139 |
|
| 140 |
m_storyboardModel->removeRows(0, 2); |
| 141 |
|
| 142 |
QVERIFY(m_storyboardModel->rowCount() == 0); |
| 143 |
} |
| 144 |
|
| 145 |
void StoryboardTimelineSyncTest::testStoryboardTimelineTimeSynchronization() |
| 146 |
{ |
| 147 |
// 0 1 2 3 4 5 6 7 8 9 |
| 148 |
// channel1 [| . |] [| .] . . . . . |
| 149 |
// channel2 [| . |] [| |] . . . . . |
| 150 |
|
| 151 |
{ //SETUP |
| 152 |
m_storyboardModel->insertItem(m_storyboardModel->index(0,0), true); |
| 153 |
QCOMPARE(m_storyboardModel->rowCount(), 1); |
| 154 |
m_channel1->addKeyframe(2); |
| 155 |
m_channel2->addKeyframe(2); |
| 156 |
|
| 157 |
m_storyboardModel->insertItem(m_storyboardModel->index(0,0), true); |
| 158 |
m_channel2->addKeyframe(4); |
| 159 |
QCOMPARE(m_storyboardModel->rowCount(), 2); |
| 160 |
|
| 161 |
QCOMPARE(m_channel1->keyframeCount(), 3); |
| 162 |
QCOMPARE(m_channel2->keyframeCount(), 4); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
QSignalSpy spyTimeChanged(m_image->animationInterface() , SIGNAL(sigUiTimeChanged(int))); |
| 167 |
QVERIFY(spyTimeChanged.isValid()); |
| 168 |
|
| 169 |
m_image->animationInterface()->switchCurrentTimeAsync(0); |
| 170 |
QCOMPARE(spyTimeChanged.count(), 1); |
| 171 |
|
| 172 |
QModelIndex parentIndex = m_storyboardView->currentIndex(); |
| 173 |
QVERIFY(parentIndex.isValid()); |
| 174 |
QCOMPARE(m_storyboardModel->index(StoryboardItem::FrameNumber, 0, parentIndex).data().toInt(), 0); |
| 175 |
|
| 176 |
m_image->animationInterface()->switchCurrentTimeAsync(2); |
| 177 |
parentIndex = m_storyboardView->selectionModel()->currentIndex(); |
| 178 |
QCOMPARE(m_storyboardModel->index(StoryboardItem::FrameNumber, 0, parentIndex).data().toInt(), 0); |
| 179 |
|
| 180 |
m_image->animationInterface()->switchCurrentTimeAsync(3); |
| 181 |
parentIndex = m_storyboardView->selectionModel()->currentIndex(); |
| 182 |
QCOMPARE(m_storyboardModel->index(StoryboardItem::FrameNumber, 0, parentIndex).data().toInt(), 3); |
| 183 |
|
| 184 |
{ // CLEANUP |
| 185 |
m_storyboardModel->removeRows(0, m_storyboardModel->rowCount()); |
| 186 |
QCOMPARE(m_storyboardModel->rowCount(), 0); |
| 187 |
|
| 188 |
m_channel2->removeKeyframe(4); |
| 189 |
m_channel2->removeKeyframe(3); |
| 190 |
m_channel2->removeKeyframe(2); |
| 191 |
m_channel1->removeKeyframe(3); |
| 192 |
m_channel1->removeKeyframe(2); |
| 193 |
QCOMPARE(m_channel1->keyframeCount(), 1); |
| 194 |
QCOMPARE(m_channel2->keyframeCount(), 1); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void StoryboardTimelineSyncTest::testDurationChange() |
| 199 |
{ |
| 200 |
const int fps = m_image->animationInterface()->framerate(); |
| 201 |
|
| 202 |
m_storyboardModel->insertRow(0); |
| 203 |
m_storyboardModel->insertChildRows(0); |
| 204 |
m_channel1->addKeyframe(3); |
| 205 |
m_storyboardModel->slotSetActiveNode(m_layer1); |
| 206 |
m_storyboardModel->insertItem(m_storyboardModel->index(0,0), true); |
| 207 |
QCOMPARE(m_channel1->keyframeCount(), 3); |
| 208 |
QVERIFY(m_channel1->keyframeAt(4)); |
| 209 |
QCOMPARE(m_storyboardModel->rowCount(), 2); |
| 210 |
|
| 211 |
{ |
| 212 |
// [0 1 2 3] [4] 5 6 7 .. |
| 213 |
// Verify the above ^ |
| 214 |
QModelIndex itemIndex = m_storyboardModel->index(0, 0); |
| 215 |
QModelIndex frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, itemIndex); |
| 216 |
QModelIndex secondDurationIndex = m_storyboardModel->index(StoryboardItem::DurationSecond, 0, itemIndex); |
| 217 |
QCOMPARE(frameDurationIndex.data().toInt(), 4); |
| 218 |
QCOMPARE(secondDurationIndex.data().toInt(), 0); |
| 219 |
|
| 220 |
itemIndex = m_storyboardModel->index(1, 0); |
| 221 |
frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, itemIndex); |
| 222 |
secondDurationIndex = m_storyboardModel->index(StoryboardItem::DurationSecond, 0, itemIndex); |
| 223 |
QCOMPARE(frameDurationIndex.data().toInt(), 1); |
| 224 |
QCOMPARE(secondDurationIndex.data().toInt(), 0); |
| 225 |
} |
| 226 |
|
| 227 |
{ // Try setting scene length too short. Scenes can never be shorter than their "internal" keyframe contents. |
| 228 |
// In this case, the scene length cannot be less than or equal to 3! |
| 229 |
QModelIndex parentIndex = m_storyboardModel->index(0,0); |
| 230 |
QModelIndex frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, parentIndex); |
| 231 |
m_storyboardModel->setData(frameDurationIndex, 3); |
| 232 |
QCOMPARE(frameDurationIndex.data().toInt(), 4); |
| 233 |
} |
| 234 |
|
| 235 |
{ // Try setting scene length 1 frame longer. |
| 236 |
// In this case, the next scene should start at frame 5 and keyframes should reflect this change! |
| 237 |
QModelIndex parentIndex = m_storyboardModel->index(0,0); |
| 238 |
QModelIndex nextParentIndex = m_storyboardModel->index(1,0); |
| 239 |
QModelIndex frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, parentIndex); |
| 240 |
QModelIndex nextFrameNumberIndex = m_storyboardModel->index(StoryboardItem::FrameNumber, 0, nextParentIndex); |
| 241 |
m_storyboardModel->setData(frameDurationIndex, 5); |
| 242 |
QCOMPARE(frameDurationIndex.data().toInt(), 5); |
| 243 |
QCOMPARE(nextFrameNumberIndex.data().toInt(), 5); |
| 244 |
QVERIFY(m_channel1->keyframeAt(5)); |
| 245 |
} |
| 246 |
|
| 247 |
{ // Try setting scene length 3 frames higher than fps count. |
| 248 |
// Should be 1s, 3f |
| 249 |
QModelIndex parentIndex = m_storyboardModel->index(0,0); |
| 250 |
QModelIndex frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, parentIndex); |
| 251 |
QModelIndex secondDurationIndex = m_storyboardModel->index(StoryboardItem::DurationSecond, 0, parentIndex); |
| 252 |
m_storyboardModel->setData(frameDurationIndex, fps + 3); |
| 253 |
QCOMPARE(frameDurationIndex.data().toInt(), 3); |
| 254 |
QCOMPARE(secondDurationIndex.data().toInt(), 1); |
| 255 |
QVERIFY(m_channel1->keyframeAt(fps + 3)); |
| 256 |
} |
| 257 |
|
| 258 |
{ // Try setting scene length to zero seconds. |
| 259 |
// Will try be 0s, 3f; However, it will be restricted to lowest possible length (0s, 4f) |
| 260 |
// as described above... |
| 261 |
QModelIndex parentIndex = m_storyboardModel->index(0,0); |
| 262 |
QModelIndex frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, parentIndex); |
| 263 |
QModelIndex secondDurationIndex = m_storyboardModel->index(StoryboardItem::DurationSecond, 0, parentIndex); |
| 264 |
m_storyboardModel->setData(secondDurationIndex, 0); |
| 265 |
QCOMPARE(frameDurationIndex.data().toInt(), 4); |
| 266 |
QCOMPARE(secondDurationIndex.data().toInt(), 0); |
| 267 |
QVERIFY(m_channel1->keyframeAt(4)); |
| 268 |
} |
| 269 |
|
| 270 |
{ // Cleanup time.. |
| 271 |
m_storyboardModel->removeRows(0, m_storyboardModel->rowCount()); |
| 272 |
|
| 273 |
Q_FOREACH( const int& keyframeTime, m_channel1->allKeyframeTimes()) { |
| 274 |
if (keyframeTime == 0) { |
| 275 |
continue; |
| 276 |
} |
| 277 |
|
| 278 |
m_channel1->removeKeyframe(keyframeTime); |
| 279 |
} |
| 280 |
|
| 281 |
Q_FOREACH( const int& keyframeTime, m_channel2->allKeyframeTimes()) { |
| 282 |
if (keyframeTime == 0) { |
| 283 |
continue; |
| 284 |
} |
| 285 |
|
| 286 |
m_channel2->removeKeyframe(keyframeTime); |
| 287 |
} |
| 288 |
|
| 289 |
QCOMPARE(m_storyboardModel->rowCount(), 0); |
| 290 |
QCOMPARE(m_channel1->keyframeCount(), 1); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
void StoryboardTimelineSyncTest::testFpsChanged() |
| 295 |
{ |
| 296 |
const int originalFPS = m_image->animationInterface()->framerate(); |
| 297 |
|
| 298 |
m_storyboardModel->insertRow(0); |
| 299 |
m_storyboardModel->insertChildRows(0); |
| 300 |
m_storyboardModel->slotSetActiveNode(m_layer1); |
| 301 |
m_storyboardModel->insertItem(m_storyboardModel->index(0,0), true); |
| 302 |
QCOMPARE(m_channel1->keyframeCount(), 2); |
| 303 |
QVERIFY(m_channel1->keyframeAt(1)); |
| 304 |
QCOMPARE(m_storyboardModel->rowCount(), 2); |
| 305 |
|
| 306 |
{ // Let's set the length of the first scene to 30 frames. Should be 1s, 6f |
| 307 |
QModelIndex parentIndex = m_storyboardModel->index(0,0); |
| 308 |
QModelIndex frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, parentIndex); |
| 309 |
QModelIndex secondDurationIndex = m_storyboardModel->index(StoryboardItem::DurationSecond, 0, parentIndex); |
| 310 |
m_storyboardModel->setData(frameDurationIndex, 30); |
| 311 |
QCOMPARE(frameDurationIndex.data().toInt(), 6); |
| 312 |
QCOMPARE(secondDurationIndex.data().toInt(), 1); |
| 313 |
QVERIFY(m_channel1->keyframeAt(30)); |
| 314 |
} |
| 315 |
|
| 316 |
for( int i = 0; i <= 30; i += 10) { |
| 317 |
const int newFPS = qMax(i, 1); |
| 318 |
m_image->animationInterface()->setFramerate(newFPS); |
| 319 |
|
| 320 |
{ // Let's check the length of the first scene to make sure it has been updated to match newFPS. |
| 321 |
QModelIndex parentIndex = m_storyboardModel->index(0,0); |
| 322 |
QModelIndex frameDurationIndex = m_storyboardModel->index(StoryboardItem::DurationFrame, 0, parentIndex); |
| 323 |
QModelIndex secondDurationIndex = m_storyboardModel->index(StoryboardItem::DurationSecond, 0, parentIndex); |
| 324 |
QCOMPARE(frameDurationIndex.data().toInt(), 30 % newFPS); |
| 325 |
QCOMPARE(secondDurationIndex.data().toInt(), 30 / newFPS); |
| 326 |
QVERIFY(m_channel1->keyframeAt(30)); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
{ // Cleanup time |
| 331 |
m_image->animationInterface()->setFramerate(originalFPS); |
| 332 |
|
| 333 |
m_storyboardModel->removeRows(0, m_storyboardModel->rowCount()); |
| 334 |
m_channel1->removeKeyframe(30); |
| 335 |
|
| 336 |
QCOMPARE( m_storyboardModel->rowCount(), 0); |
| 337 |
QCOMPARE( m_channel1->keyframeCount(), 1); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
SIMPLE_TEST_MAIN(StoryboardTimelineSyncTest) |