File indexing completed on 2024-12-22 04:08:51

0001 /*
0002  *  SPDX-FileCopyrightText: 2023 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #include "TestKUndo2Stack.h"
0007 
0008 #include <QTime>
0009 
0010 #include <kundo2stack.h>
0011 #include <kundo2command.h>
0012 
0013 
0014 void TestKUndo2Stack::testExcludeFromMerge()
0015 {
0016     KUndo2Stack stack;
0017 
0018     stack.setUseCumulativeUndoRedo(true);
0019 
0020     KisCumulativeUndoData data;
0021     data.excludeFromMerge = 5;
0022     data.mergeTimeout = 1000;
0023     data.maxGroupSeparation = 1000;
0024     data.maxGroupDuration = 100000;
0025 
0026     stack.setCumulativeUndoData(data);
0027 
0028     int startTime = 0;
0029 
0030     for (int i = 0; i < 6; i++) {
0031         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0032 
0033         cmd->setTimedID(1);
0034         cmd->setTime(QTime(0, 0, startTime, 0));
0035         cmd->setEndTime(QTime(0, 0, startTime, 500));
0036 
0037         stack.push(cmd);
0038         startTime++;
0039     }
0040 
0041     QCOMPARE(stack.count(), 6);
0042     QCOMPARE(stack.index(), stack.count());
0043 
0044 //    qDebug() << "===";
0045 //    for (int j = 0; j < stack.count(); j++) {
0046 //        qDebug() << j << stack.command(j)->text();
0047 //    }
0048 //    qDebug() << "===";
0049 
0050     for (int i = 0; i < 50; i++) {
0051         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0052 
0053         cmd->setTimedID(1);
0054         cmd->setTime(QTime(0, 0, startTime, 0));
0055         cmd->setEndTime(QTime(0, 0, startTime, 500));
0056 
0057         stack.push(cmd);
0058         startTime++;
0059 
0060         QCOMPARE(stack.count(), 6);
0061         QCOMPARE(stack.index(), stack.count());
0062     }
0063 
0064     // verify that all the merged commands are merged into
0065     // command "50";
0066     for (int i = 0; i < stack.count(); i++) {
0067         QCOMPARE(stack.command(i)->text().toString(), QString::number(i + 50));
0068     }
0069 }
0070 
0071 void TestKUndo2Stack::testMergeTimeout()
0072 {
0073     KUndo2Stack stack;
0074 
0075     stack.setUseCumulativeUndoRedo(true);
0076 
0077     KisCumulativeUndoData data;
0078     data.excludeFromMerge = 1;
0079     data.mergeTimeout = 5000;
0080     data.maxGroupSeparation = 1000;
0081     data.maxGroupDuration = 50000;
0082 
0083     stack.setCumulativeUndoData(data);
0084 
0085     int startTime = 0;
0086 
0087     // group 1
0088     for (int i = 0; i < 7; i++) {
0089         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString("group 1: %1").arg(startTime)));
0090 
0091         cmd->setTimedID(1);
0092         cmd->setTime(QTime(0, 0, startTime, 0));
0093         cmd->setEndTime(QTime(0, 0, startTime, 500));
0094 
0095         stack.push(cmd);
0096         startTime++;
0097     }
0098 
0099     QCOMPARE(stack.count(), 7);
0100     QCOMPARE(stack.index(), stack.count());
0101 
0102     {
0103         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString("group 1: %1").arg(startTime)));
0104 
0105         cmd->setTimedID(1);
0106         cmd->setTime(QTime(0, 0, startTime, 0));
0107         cmd->setEndTime(QTime(0, 0, startTime, 500));
0108 
0109         stack.push(cmd);
0110         startTime++;
0111     }
0112 
0113     QCOMPARE(stack.count(), 7);
0114     QCOMPARE(stack.index(), stack.count());
0115 
0116     QCOMPARE(stack.command(0)->isMerged(), true);
0117     QCOMPARE(stack.command(1)->isMerged(), false);
0118     QCOMPARE(stack.command(2)->isMerged(), false);
0119     QCOMPARE(stack.command(3)->isMerged(), false);
0120     QCOMPARE(stack.command(4)->isMerged(), false);
0121     QCOMPARE(stack.command(5)->isMerged(), false);
0122     QCOMPARE(stack.command(6)->isMerged(), false);
0123 }
0124 
0125 void TestKUndo2Stack::testGroupSeparation()
0126 {
0127     KUndo2Stack stack;
0128 
0129     stack.setUseCumulativeUndoRedo(true);
0130 
0131     KisCumulativeUndoData data;
0132     data.excludeFromMerge = 1;
0133     data.mergeTimeout = 6000;
0134     data.maxGroupSeparation = 1000;
0135     data.maxGroupDuration = 50000;
0136 
0137     stack.setCumulativeUndoData(data);
0138 
0139     int startTime = 0;
0140 
0141     for (int i = 0; i < 3; i++) {
0142         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0143 
0144         cmd->setTimedID(1);
0145         cmd->setTime(QTime(0, 0, startTime, 0));
0146         cmd->setEndTime(QTime(0, 0, startTime, 500));
0147 
0148         stack.push(cmd);
0149         startTime++;
0150     }
0151 
0152     QCOMPARE(stack.count(), 3);
0153     QCOMPARE(stack.index(), stack.count());
0154 
0155     // add one second to split groups
0156     startTime += 1;
0157 
0158     for (int i = 0; i < 2; i++) {
0159         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0160 
0161         cmd->setTimedID(1);
0162         cmd->setTime(QTime(0, 0, startTime, 0));
0163         cmd->setEndTime(QTime(0, 0, startTime, 500));
0164 
0165         stack.push(cmd);
0166         startTime++;
0167     }
0168 
0169     QCOMPARE(stack.count(), 5);
0170     QCOMPARE(stack.index(), stack.count());
0171 
0172     // add five seconds to make all the previous commands outdated
0173     startTime += 6;
0174 
0175     {
0176         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0177 
0178         cmd->setTimedID(1);
0179         cmd->setTime(QTime(0, 0, startTime, 0));
0180         cmd->setEndTime(QTime(0, 0, startTime, 500));
0181 
0182         stack.push(cmd);
0183         startTime++;
0184     }
0185 
0186     // all the first commands were collapsed
0187     QCOMPARE(stack.count(), 3);
0188     QCOMPARE(stack.index(), stack.count());
0189 
0190     // two separate groups
0191     QCOMPARE(stack.command(0)->isMerged(), true);
0192     QCOMPARE(stack.command(1)->isMerged(), true);
0193     QCOMPARE(stack.command(2)->isMerged(), false);
0194 }
0195 
0196 void TestKUndo2Stack::testMaxGroupDuration()
0197 {
0198     KUndo2Stack stack;
0199 
0200     stack.setUseCumulativeUndoRedo(true);
0201 
0202     KisCumulativeUndoData data;
0203     data.excludeFromMerge = 1;
0204     data.mergeTimeout = 1000;
0205     data.maxGroupSeparation = 1000;
0206     data.maxGroupDuration = 3000;
0207 
0208     stack.setCumulativeUndoData(data);
0209 
0210     int startTime = 0;
0211 
0212     for (int i = 0; i < 3; i++) {
0213         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0214 
0215         cmd->setTimedID(1);
0216         cmd->setTime(QTime(0, 0, startTime, 0));
0217         cmd->setEndTime(QTime(0, 0, startTime, 500));
0218 
0219         stack.push(cmd);
0220         startTime++;
0221     }
0222 
0223     QCOMPARE(stack.count(), 3);
0224     QCOMPARE(stack.index(), stack.count());
0225 
0226     QCOMPARE(stack.command(0)->isMerged(), false);
0227     QCOMPARE(stack.command(1)->isMerged(), false);
0228     QCOMPARE(stack.command(2)->isMerged(), false);
0229 
0230     for (int i = 0; i < 3; i++) {
0231         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0232 
0233         cmd->setTimedID(1);
0234         cmd->setTime(QTime(0, 0, startTime, 0));
0235         cmd->setEndTime(QTime(0, 0, startTime, 500));
0236 
0237         stack.push(cmd);
0238         startTime++;
0239     }
0240 
0241     QCOMPARE(stack.count(), 4);
0242     QCOMPARE(stack.index(), stack.count());
0243 
0244     QCOMPARE(stack.command(0)->isMerged(), true);
0245     QCOMPARE(stack.command(1)->isMerged(), false);
0246     QCOMPARE(stack.command(2)->isMerged(), false);
0247     QCOMPARE(stack.command(3)->isMerged(), false);
0248 
0249     startTime += 5;
0250 
0251     {
0252         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0253 
0254         cmd->setTimedID(1);
0255         cmd->setTime(QTime(0, 0, startTime, 0));
0256         cmd->setEndTime(QTime(0, 0, startTime, 500));
0257 
0258         stack.push(cmd);
0259         startTime++;
0260     }
0261 
0262     QCOMPARE(stack.count(), 3);
0263     QCOMPARE(stack.index(), stack.count());
0264 
0265     // two separate groups, even though they have the same separation
0266     QCOMPARE(stack.command(0)->isMerged(), true);
0267     QCOMPARE(stack.command(1)->isMerged(), true);
0268     QCOMPARE(stack.command(2)->isMerged(), false);
0269 }
0270 
0271 void TestKUndo2Stack::testCleanIndexAfterMerge()
0272 {
0273     KUndo2Stack stack;
0274 
0275     stack.setUseCumulativeUndoRedo(true);
0276 
0277     KisCumulativeUndoData data;
0278     data.excludeFromMerge = 3;
0279     data.mergeTimeout = 5000;
0280     data.maxGroupSeparation = 1000;
0281     data.maxGroupDuration = 3000;
0282 
0283     stack.setCumulativeUndoData(data);
0284 
0285     int startTime = 0;
0286 
0287     for (int i = 0; i < 3; i++) {
0288         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0289 
0290         cmd->setTimedID(1);
0291         cmd->setTime(QTime(0, 0, startTime, 0));
0292         cmd->setEndTime(QTime(0, 0, startTime, 500));
0293 
0294         stack.push(cmd);
0295         startTime++;
0296     }
0297 
0298     QCOMPARE(stack.count(), 3);
0299     QCOMPARE(stack.index(), stack.count());
0300 
0301     QCOMPARE(stack.command(0)->isMerged(), false);
0302     QCOMPARE(stack.command(1)->isMerged(), false);
0303     QCOMPARE(stack.command(2)->isMerged(), false);
0304 
0305     for (int i = 0; i < 2; i++) {
0306         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0307 
0308         cmd->setTimedID(1);
0309         cmd->setTime(QTime(0, 0, startTime, 0));
0310         cmd->setEndTime(QTime(0, 0, startTime, 500));
0311 
0312         stack.push(cmd);
0313 
0314         if (i == 0) {
0315             stack.setClean();
0316         }
0317 
0318         startTime++;
0319     }
0320 
0321     QCOMPARE(stack.count(), 5);
0322     QCOMPARE(stack.index(), stack.count());
0323     QCOMPARE(stack.cleanIndex() - 1, 3);
0324 
0325     QCOMPARE(stack.command(0)->isMerged(), false);
0326     QCOMPARE(stack.command(1)->isMerged(), false);
0327     QCOMPARE(stack.command(2)->isMerged(), false);
0328     QCOMPARE(stack.command(3)->isMerged(), false);
0329     QCOMPARE(stack.command(4)->isMerged(), false);
0330 
0331     startTime += 10;
0332 
0333     {
0334         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0335 
0336         cmd->setTimedID(1);
0337         cmd->setTime(QTime(0, 0, startTime, 0));
0338         cmd->setEndTime(QTime(0, 0, startTime, 500));
0339 
0340         stack.push(cmd);
0341         startTime++;
0342     }
0343 
0344     QCOMPARE(stack.count(), 4);
0345     QCOMPARE(stack.index(), stack.count());
0346 
0347     // the clean index got adjusted!
0348     QCOMPARE(stack.cleanIndex() - 1, 1);
0349 
0350     QCOMPARE(stack.command(0)->isMerged(), true);
0351     QCOMPARE(stack.command(1)->isMerged(), false);
0352     QCOMPARE(stack.command(2)->isMerged(), false);
0353     QCOMPARE(stack.command(3)->isMerged(), false);
0354 }
0355 
0356 void TestKUndo2Stack::testCleanIndexBeforeMerge()
0357 {
0358     KUndo2Stack stack;
0359 
0360     stack.setUseCumulativeUndoRedo(true);
0361 
0362     KisCumulativeUndoData data;
0363     data.excludeFromMerge = 1;
0364     data.mergeTimeout = 5000;
0365     data.maxGroupSeparation = 1000;
0366     data.maxGroupDuration = 3000;
0367 
0368     stack.setCumulativeUndoData(data);
0369 
0370     int startTime = 0;
0371 
0372     {
0373         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0374 
0375         cmd->setTimedID(1);
0376         cmd->setTime(QTime(0, 0, startTime, 0));
0377         cmd->setEndTime(QTime(0, 0, startTime, 500));
0378 
0379         stack.push(cmd);
0380         startTime++;
0381     }
0382 
0383     // set the first command as clean
0384     stack.setClean();
0385 
0386     startTime += 3;
0387 
0388     for (int i = 0; i < 3; i++) {
0389         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0390 
0391         cmd->setTimedID(1);
0392         cmd->setTime(QTime(0, 0, startTime, 0));
0393         cmd->setEndTime(QTime(0, 0, startTime, 500));
0394 
0395         stack.push(cmd);
0396         startTime++;
0397     }
0398 
0399     QCOMPARE(stack.count(), 4);
0400     QCOMPARE(stack.index(), stack.count());
0401     QCOMPARE(stack.cleanIndex() - 1, 0);
0402 
0403     QCOMPARE(stack.command(0)->isMerged(), false);
0404     QCOMPARE(stack.command(1)->isMerged(), false);
0405     QCOMPARE(stack.command(2)->isMerged(), false);
0406     QCOMPARE(stack.command(3)->isMerged(), false);
0407 
0408     startTime += 10;
0409 
0410     {
0411         KUndo2Command *cmd = new KUndo2Command(kundo2_noi18n(QString::number(startTime)));
0412 
0413         cmd->setTimedID(1);
0414         cmd->setTime(QTime(0, 0, startTime, 0));
0415         cmd->setEndTime(QTime(0, 0, startTime, 500));
0416 
0417         stack.push(cmd);
0418         startTime++;
0419     }
0420 
0421     QCOMPARE(stack.count(), 3);
0422     QCOMPARE(stack.index(), stack.count());
0423 
0424     // the index is unchanged!
0425     QCOMPARE(stack.cleanIndex() - 1, 0);
0426 
0427     QCOMPARE(stack.command(0)->isMerged(), false);
0428     QCOMPARE(stack.command(1)->isMerged(), true);
0429     QCOMPARE(stack.command(2)->isMerged(), false);
0430 }
0431 
0432 SIMPLE_TEST_MAIN(TestKUndo2Stack)