diff --git a/autotests/client/test_plasma_window_model.cpp b/autotests/client/test_plasma_window_model.cpp --- a/autotests/client/test_plasma_window_model.cpp +++ b/autotests/client/test_plasma_window_model.cpp @@ -73,6 +73,7 @@ void testRequests(); // TODO: minimized geometry // TODO: model reset + void testCreateWithUnmappedWindow(); private: bool testBooleanData(PlasmaWindowModel::AdditionalRoles role, void (PlasmaWindowInterface::*function)(bool)); @@ -681,5 +682,38 @@ QCOMPARE(shadeRequestedSpy.last().first().toBool(), false); } +void PlasmaWindowModelTest::testCreateWithUnmappedWindow() +{ + // this test verifies that creating the model just when an unmapped window exists doesn't cause problems + // that is the unmapped window should be added (as expected), but also be removed again + + // create a window in "normal way" + QSignalSpy windowCreatedSpy(m_pw, &PlasmaWindowManagement::windowCreated); + QVERIFY(windowCreatedSpy.isValid()); + auto w = m_pwInterface->createWindow(m_pwInterface); + QVERIFY(w); + QVERIFY(windowCreatedSpy.wait()); + PlasmaWindow *window = windowCreatedSpy.first().first().value(); + QVERIFY(window); + QSignalSpy unmappedSpy(window, &PlasmaWindow::unmapped); + QVERIFY(unmappedSpy.isValid()); + QSignalSpy destroyedSpy(window, &PlasmaWindow::destroyed); + QVERIFY(destroyedSpy.isValid()); + // unmap should be triggered, but not yet the destroyed + w->unmap(); + QVERIFY(unmappedSpy.wait()); + QVERIFY(destroyedSpy.isEmpty()); + + auto model = m_pw->createWindowModel(); + QVERIFY(model); + QCOMPARE(model->rowCount(), 1); + QSignalSpy rowRemovedSpy(model, &PlasmaWindowModel::rowsRemoved); + QVERIFY(rowRemovedSpy.isValid()); + QVERIFY(rowRemovedSpy.wait()); + QCOMPARE(rowRemovedSpy.count(), 1); + QCOMPARE(model->rowCount(), 0); + QCOMPARE(destroyedSpy.count(), 1); +} + QTEST_GUILESS_MAIN(PlasmaWindowModelTest) #include "test_plasma_window_model.moc" diff --git a/src/client/plasmawindowmodel.cpp b/src/client/plasmawindowmodel.cpp --- a/src/client/plasmawindowmodel.cpp +++ b/src/client/plasmawindowmodel.cpp @@ -57,16 +57,17 @@ windows.append(window); q->endInsertRows(); - QObject::connect(window, &PlasmaWindow::unmapped, - [window, this] { - const int row = windows.indexOf(window); - if (row != -1) { - q->beginRemoveRows(QModelIndex(), row, row); - windows.removeAt(row); - q->endRemoveRows(); - } + auto removeWindow = [window, this] { + const int row = windows.indexOf(window); + if (row != -1) { + q->beginRemoveRows(QModelIndex(), row, row); + windows.removeAt(row); + q->endRemoveRows(); } - ); + }; + + QObject::connect(window, &PlasmaWindow::unmapped, q, removeWindow); + QObject::connect(window, &QObject::destroyed, q, removeWindow); QObject::connect(window, &PlasmaWindow::titleChanged, [window, this] { this->dataChanged(window, Qt::DisplayRole); }