kcompositejobtest.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. This file is part of the KDE project
  3. SPDX-FileCopyrightText: 2013 Kevin Funk <kevin@kfunk.org>
  4. SPDX-License-Identifier: LGPL-2.0-only
  5. */
  6. #include "kcompositejobtest.h"
  7. #include <QSignalSpy>
  8. #include <QTest>
  9. #include <QTimer>
  10. TestJob::TestJob(QObject *parent)
  11. : KJob(parent)
  12. {
  13. }
  14. void TestJob::start()
  15. {
  16. QTimer::singleShot(1000, this, &TestJob::doEmit);
  17. }
  18. void TestJob::doEmit()
  19. {
  20. emitResult();
  21. }
  22. void CompositeJob::start()
  23. {
  24. if (hasSubjobs()) {
  25. subjobs().first()->start();
  26. } else {
  27. emitResult();
  28. }
  29. }
  30. bool CompositeJob::addSubjob(KJob *job)
  31. {
  32. return KCompositeJob::addSubjob(job);
  33. }
  34. void CompositeJob::slotResult(KJob *job)
  35. {
  36. KCompositeJob::slotResult(job);
  37. if (!error() && hasSubjobs()) {
  38. // start next
  39. subjobs().first()->start();
  40. } else {
  41. setError(job->error());
  42. setErrorText(job->errorText());
  43. emitResult();
  44. }
  45. }
  46. KCompositeJobTest::KCompositeJobTest()
  47. : loop(this)
  48. {
  49. }
  50. /**
  51. * In case a composite job is deleted during execution
  52. * we still want to assure that we don't crash
  53. *
  54. * see bug: https://bugs.kde.org/show_bug.cgi?id=230692
  55. */
  56. void KCompositeJobTest::testDeletionDuringExecution()
  57. {
  58. QObject *someParent = new QObject;
  59. KJob *job = new TestJob(someParent);
  60. CompositeJob *compositeJob = new CompositeJob;
  61. compositeJob->setAutoDelete(false);
  62. QVERIFY(compositeJob->addSubjob(job));
  63. QCOMPARE(job->parent(), compositeJob);
  64. QSignalSpy destroyed_spy(job, &QObject::destroyed);
  65. // check if job got reparented properly
  66. delete someParent;
  67. someParent = nullptr;
  68. // the job should still exist, because it is a child of KCompositeJob now
  69. QCOMPARE(destroyed_spy.size(), 0);
  70. // start async, the subjob takes 1 second to finish
  71. compositeJob->start();
  72. // delete the job during the execution
  73. delete compositeJob;
  74. compositeJob = nullptr;
  75. // at this point, the subjob should be deleted, too
  76. QCOMPARE(destroyed_spy.size(), 1);
  77. }
  78. QTEST_GUILESS_MAIN(KCompositeJobTest)
  79. #include "moc_kcompositejobtest.cpp"