diff --git a/src/plugins/coreplugin/testdatadir.cpp b/src/plugins/coreplugin/testdatadir.cpp index b582d6ea68f..8e67db098aa 100644 --- a/src/plugins/coreplugin/testdatadir.cpp +++ b/src/plugins/coreplugin/testdatadir.cpp @@ -59,6 +59,11 @@ QString TestDataDir::file(const QString &fileName) const return directory() + fileName; } +QString TestDataDir::path() const +{ + return m_directory; +} + QString TestDataDir::directory(const QString &subdir, bool clean) const { QString path = m_directory; diff --git a/src/plugins/coreplugin/testdatadir.h b/src/plugins/coreplugin/testdatadir.h index 232b128ad05..54f6a10933b 100644 --- a/src/plugins/coreplugin/testdatadir.h +++ b/src/plugins/coreplugin/testdatadir.h @@ -51,9 +51,12 @@ class CORE_EXPORT TestDataDir { public: TestDataDir(const QString &directory); + QString file(const QString &fileName) const; QString directory(const QString &subdir = QString(), bool clean = true) const; + QString path() const; + private: QString m_directory; }; diff --git a/src/plugins/cpptools/cppmodelmanager_test.cpp b/src/plugins/cpptools/cppmodelmanager_test.cpp index b160251875e..2f176dd4337 100644 --- a/src/plugins/cpptools/cppmodelmanager_test.cpp +++ b/src/plugins/cpptools/cppmodelmanager_test.cpp @@ -49,9 +49,10 @@ #include #include -using namespace CppTools::Internal; -using namespace ProjectExplorer; using namespace CppTools; +using namespace CppTools::Internal; +using namespace CppTools::Tests; +using namespace ProjectExplorer; typedef CPlusPlus::Document Document; @@ -112,46 +113,6 @@ public: QStringList projectFiles; }; -/// Open and configure given project as example project and remove -/// generated *.user file on destruction. -/// -/// Requirement: No *.user file exists for the project. -class ExampleProjectConfigurator -{ -public: - ExampleProjectConfigurator(const QString &projectFile) - { - const QString projectUserFile = projectFile + _(".user"); - QVERIFY(!QFileInfo::exists(projectUserFile)); - - // Open project - QString errorOpeningProject; - m_project = ProjectExplorerPlugin::openProject(projectFile, &errorOpeningProject); - QVERIFY(m_project); - QVERIFY(errorOpeningProject.isEmpty()); - - // Configure project - m_project->configureAsExampleProject(QStringList()); - - m_fileToRemove = projectUserFile; - } - - ~ExampleProjectConfigurator() - { - QVERIFY(!m_fileToRemove.isEmpty()); - QVERIFY(QFile::remove(m_fileToRemove)); - } - - Project *project() const - { - return m_project; - } - -private: - Project *m_project; - QString m_fileToRemove; -}; - /// Changes a file on the disk and restores its original contents on destruction class FileChangerAndRestorer { @@ -657,16 +618,19 @@ void CppToolsPlugin::test_modelmanager_snapshot_after_two_projects() /// is added for the ui_* file. /// Check: (2) The CppSourceProcessor can successfully resolve the ui_* file /// though it might not be actually generated in the build dir. +/// + void CppToolsPlugin::test_modelmanager_extraeditorsupport_uiFiles() { - ModelManagerTestHelper helper; + VerifyCleanCppModelManager verify; - MyTestDataDir testDataDirectory(_("testdata_guiproject1")); - const QString projectFile = testDataDirectory.file(_("testdata_guiproject1.pro")); + TemporaryCopiedDir temporaryDir(MyTestDataDir(QLatin1String("testdata_guiproject1")).path()); + QVERIFY(temporaryDir.isValid()); + const QString projectFile = temporaryDir.absolutePath("testdata_guiproject1.pro"); - // Open project with *.ui file - ExampleProjectConfigurator exampleProjectConfigurator(projectFile); - Project *project = exampleProjectConfigurator.project(); + ProjectOpenerAndCloser projects(/*waitForFinishedGcOnDestruction=*/ true); + ProjectInfo projectInfo = projects.open(projectFile, /*configureAsExampleProject=*/ true); + QVERIFY(projectInfo.isValid()); // Check working copy. // An AbstractEditorSupport object should have been added for the ui_* file. @@ -688,7 +652,7 @@ void CppToolsPlugin::test_modelmanager_extraeditorsupport_uiFiles() // Check CppSourceProcessor / includes. // The CppSourceProcessor is expected to find the ui_* file in the working copy. - const QString fileIncludingTheUiFile = testDataDirectory.file(_("mainwindow.cpp")); + const QString fileIncludingTheUiFile = temporaryDir.absolutePath("mainwindow.cpp"); while (!mm->snapshot().document(fileIncludingTheUiFile)) QCoreApplication::processEvents(); @@ -699,10 +663,6 @@ void CppToolsPlugin::test_modelmanager_extraeditorsupport_uiFiles() QCOMPARE(includedFiles.size(), 2); QCOMPARE(QFileInfo(includedFiles.at(0)).fileName(), _("mainwindow.h")); QCOMPARE(QFileInfo(includedFiles.at(1)).fileName(), _("ui_mainwindow.h")); - - // Close Project - SessionManager::removeProject(project); - helper.waitForFinishedGc(); } /// QTCREATORBUG-9828: Locator shows symbols of closed files diff --git a/src/plugins/cpptools/cpptoolstestcase.cpp b/src/plugins/cpptools/cpptoolstestcase.cpp index 0102384aa8e..271410bf4cb 100644 --- a/src/plugins/cpptools/cpptoolstestcase.cpp +++ b/src/plugins/cpptools/cpptoolstestcase.cpp @@ -30,6 +30,8 @@ #include "cpptoolstestcase.h" +#include "cppworkingcopy.h" + #include #include #include @@ -214,18 +216,30 @@ bool TestCase::writeFile(const QString &filePath, const QByteArray &contents) return true; } -ProjectOpenerAndCloser::ProjectOpenerAndCloser() +ProjectOpenerAndCloser::ProjectOpenerAndCloser(bool waitForFinishedGcOnDestruction) + : m_waitForFinishedGcOnDestruction(waitForFinishedGcOnDestruction) + , m_gcFinished(false) { QVERIFY(!SessionManager::hasProjects()); + if (m_waitForFinishedGcOnDestruction) { + CppModelManager *mm = CppModelManager::instance(); + connect(mm, &CppModelManager::gcFinished, this, &ProjectOpenerAndCloser::onGcFinished); + } } ProjectOpenerAndCloser::~ProjectOpenerAndCloser() { foreach (Project *project, m_openProjects) ProjectExplorerPlugin::unloadProject(project); + + if (m_waitForFinishedGcOnDestruction) { + m_gcFinished = false; + while (!m_gcFinished) + QCoreApplication::processEvents(); + } } -ProjectInfo ProjectOpenerAndCloser::open(const QString &projectFile) +ProjectInfo ProjectOpenerAndCloser::open(const QString &projectFile, bool configureAsExampleProject) { QString error; Project *project = ProjectExplorerPlugin::openProject(projectFile, &error); @@ -235,12 +249,20 @@ ProjectInfo ProjectOpenerAndCloser::open(const QString &projectFile) return ProjectInfo(); m_openProjects.append(project); + if (configureAsExampleProject) + project->configureAsExampleProject(QStringList()); + if (TestCase::waitUntilCppModelManagerIsAwareOf(project)) return CppModelManager::instance()->projectInfo(project); return ProjectInfo(); } +void ProjectOpenerAndCloser::onGcFinished() +{ + m_gcFinished = true; +} + TemporaryCopiedDir::TemporaryCopiedDir(const QString &sourceDirPath) : m_temporaryDir(QDir::tempPath() + QLatin1String("/qtcreator-tests-XXXXXX")) , m_isValid(m_temporaryDir.isValid()) @@ -301,5 +323,18 @@ IAssistProposalScopedPointer::~IAssistProposalScopedPointer() delete d->model(); } +void VerifyCleanCppModelManager::verify() +{ + CppModelManager *mm = CppModelManager::instance(); + QVERIFY(mm); + QVERIFY(mm->projectInfos().isEmpty()); + QVERIFY(mm->headerPaths().isEmpty()); + QVERIFY(mm->definedMacros().isEmpty()); + QVERIFY(mm->projectFiles().isEmpty()); + QVERIFY(mm->snapshot().isEmpty()); + QCOMPARE(mm->workingCopy().size(), 1); + QVERIFY(mm->workingCopy().contains(mm->configurationFileName())); +} + } // namespace Tests } // namespace CppTools diff --git a/src/plugins/cpptools/cpptoolstestcase.h b/src/plugins/cpptools/cpptoolstestcase.h index d20950ebb41..458e62b5cf2 100644 --- a/src/plugins/cpptools/cpptoolstestcase.h +++ b/src/plugins/cpptools/cpptoolstestcase.h @@ -106,15 +106,21 @@ private: bool m_runGarbageCollector; }; -class CPPTOOLS_EXPORT ProjectOpenerAndCloser +class CPPTOOLS_EXPORT ProjectOpenerAndCloser : public QObject { + Q_OBJECT + public: - ProjectOpenerAndCloser(); + ProjectOpenerAndCloser(bool waitForFinishedGcOnDestruction = false); ~ProjectOpenerAndCloser(); // Closes opened projects - ProjectInfo open(const QString &projectFile); + ProjectInfo open(const QString &projectFile, bool configureAsExampleProject = false); private: + void onGcFinished(); + + bool m_waitForFinishedGcOnDestruction; + bool m_gcFinished; QList m_openProjects; }; @@ -132,6 +138,14 @@ private: bool m_isValid; }; +class VerifyCleanCppModelManager +{ +public: + VerifyCleanCppModelManager() { verify(); } + ~VerifyCleanCppModelManager() { verify(); } + static void verify(); +}; + class FileWriterAndRemover { public: diff --git a/src/plugins/cpptools/modelmanagertesthelper.cpp b/src/plugins/cpptools/modelmanagertesthelper.cpp index a47741e22d5..6571aabed71 100644 --- a/src/plugins/cpptools/modelmanagertesthelper.cpp +++ b/src/plugins/cpptools/modelmanagertesthelper.cpp @@ -30,6 +30,7 @@ #include "modelmanagertesthelper.h" +#include "cpptoolstestcase.h" #include "cppworkingcopy.h" #include @@ -68,13 +69,13 @@ ModelManagerTestHelper::ModelManagerTestHelper(QObject *parent) : connect(mm, SIGNAL(gcFinished()), this, SLOT(gcFinished())); cleanup(); - verifyClean(); + Tests::VerifyCleanCppModelManager::verify(); } ModelManagerTestHelper::~ModelManagerTestHelper() { cleanup(); - verifyClean(); + Tests::VerifyCleanCppModelManager::verify(); } void ModelManagerTestHelper::cleanup() @@ -90,20 +91,6 @@ void ModelManagerTestHelper::cleanup() waitForFinishedGc(); } -void ModelManagerTestHelper::verifyClean() -{ - CppModelManager *mm = CppModelManager::instance(); - assert(mm); - - QVERIFY(mm->projectInfos().isEmpty()); - QVERIFY(mm->headerPaths().isEmpty()); - QVERIFY(mm->definedMacros().isEmpty()); - QVERIFY(mm->projectFiles().isEmpty()); - QVERIFY(mm->snapshot().isEmpty()); - QCOMPARE(mm->workingCopy().size(), 1); - QVERIFY(mm->workingCopy().contains(mm->configurationFileName())); -} - ModelManagerTestHelper::Project *ModelManagerTestHelper::createProject(const QString &name) { TestProject *tp = new TestProject(name, this); diff --git a/src/plugins/cpptools/modelmanagertesthelper.h b/src/plugins/cpptools/modelmanagertesthelper.h index fc6744fbab1..82c5dfec3a7 100644 --- a/src/plugins/cpptools/modelmanagertesthelper.h +++ b/src/plugins/cpptools/modelmanagertesthelper.h @@ -76,7 +76,6 @@ public: ~ModelManagerTestHelper(); void cleanup(); - static void verifyClean(); Project *createProject(const QString &name);