diff --git a/src/presentation/applicationmodel.cpp b/src/presentation/applicationmodel.cpp --- a/src/presentation/applicationmodel.cpp +++ b/src/presentation/applicationmodel.cpp @@ -93,12 +93,15 @@ if (page == m_currentPage) return; - page->setParent(Q_NULLPTR); m_currentPage = QObjectPtr(page); - auto pageModel = m_currentPage.staticCast(); - Q_ASSERT(pageModel); - pageModel->setErrorHandler(errorHandler()); + if (m_currentPage) { + m_currentPage->setParent(Q_NULLPTR); + + auto pageModel = m_currentPage.staticCast(); + Q_ASSERT(pageModel); + pageModel->setErrorHandler(errorHandler()); + } emit currentPageChanged(page); } diff --git a/tests/units/presentation/applicationmodeltest.cpp b/tests/units/presentation/applicationmodeltest.cpp --- a/tests/units/presentation/applicationmodeltest.cpp +++ b/tests/units/presentation/applicationmodeltest.cpp @@ -148,6 +148,24 @@ QCOMPARE(spy.takeFirst().takeFirst().value(), page); } + void shouldSupportNullPage() + { + // GIVEN + Presentation::ApplicationModel app; + auto page = new FakePageModel(this); + app.setCurrentPage(page); + QCOMPARE(app.currentPage(), page); + QSignalSpy spy(&app, SIGNAL(currentPageChanged(QObject*))); + + // WHEN + app.setCurrentPage(Q_NULLPTR); + + // THEN + QVERIFY(!app.currentPage()); + QCOMPARE(spy.count(), 1); + QVERIFY(!spy.takeFirst().takeFirst().value()); + } + void shouldTakeOwnershipOfCurrentPage() { // GIVEN