diff options
Diffstat (limited to 'src/plugins/cpptools/cpptoolstestcase.cpp')
-rw-r--r-- | src/plugins/cpptools/cpptoolstestcase.cpp | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/plugins/cpptools/cpptoolstestcase.cpp b/src/plugins/cpptools/cpptoolstestcase.cpp index c56c2ceeda..45960a01d8 100644 --- a/src/plugins/cpptools/cpptoolstestcase.cpp +++ b/src/plugins/cpptools/cpptoolstestcase.cpp @@ -30,7 +30,11 @@ #include "cpptoolstestcase.h" +#include "cppworkingcopy.h" + #include <coreplugin/editormanager/editormanager.h> +#include <projectexplorer/projectexplorer.h> +#include <projectexplorer/session.h> #include <texteditor/texteditor.h> #include <texteditor/codeassist/iassistproposal.h> #include <texteditor/codeassist/iassistproposalmodel.h> @@ -40,6 +44,8 @@ #include <QtTest> +using namespace ProjectExplorer; + static bool closeEditorsWithoutGarbageCollectorInvocation(const QList<Core::IEditor *> &editors) { CppTools::CppModelManager::instance()->enableGarbageCollector(false); @@ -71,8 +77,12 @@ TestDocument::TestDocument(const QByteArray &fileName, const QByteArray &source, QString TestDocument::filePath() const { + if (!m_baseDirectory.isEmpty()) + return QDir::cleanPath(m_baseDirectory + QLatin1Char('/') + m_fileName); + if (!QFileInfo(m_fileName).isAbsolute()) return QDir::tempPath() + QLatin1Char('/') + m_fileName; + return m_fileName; } @@ -181,6 +191,24 @@ QList<CPlusPlus::Document::Ptr> TestCase::waitForFilesInGlobalSnapshot( return result; } +bool TestCase::waitUntilCppModelManagerIsAwareOf(Project *project, int timeOut) +{ + if (!project) + return false; + + QTime t; + t.start(); + + CppModelManager *modelManager = CppModelManager::instance(); + forever { + if (modelManager->projectInfo(project).isValid()) + return true; + if (t.elapsed() > timeOut) + return false; + QCoreApplication::processEvents(); + } +} + bool TestCase::writeFile(const QString &filePath, const QByteArray &contents) { Utils::FileSaver saver(filePath); @@ -192,6 +220,97 @@ bool TestCase::writeFile(const QString &filePath, const QByteArray &contents) return true; } +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, bool configureAsExampleProject) +{ + QString error; + Project *project = ProjectExplorerPlugin::openProject(projectFile, &error); + if (!error.isEmpty()) + qWarning() << error; + if (!project) + 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; +} + +TemporaryDir::TemporaryDir() + : m_temporaryDir(QFileInfo(QDir::tempPath()).canonicalFilePath() + + QLatin1String("/qtcreator-tests-XXXXXX")) + , m_isValid(m_temporaryDir.isValid()) +{ +} + +QString TemporaryDir::createFile(const QByteArray &relativePath, const QByteArray &contents) +{ + const QString relativePathString = QString::fromUtf8(relativePath); + if (relativePathString.isEmpty() || QFileInfo(relativePathString).isAbsolute()) + return QString(); + + const QString filePath = m_temporaryDir.path() + QLatin1Char('/') + relativePathString; + if (!TestCase::writeFile(filePath, contents)) + return QString(); + return filePath; +} + +TemporaryCopiedDir::TemporaryCopiedDir(const QString &sourceDirPath) +{ + if (!m_isValid) + return; + + if (!sourceDirPath.isEmpty()) { + QFileInfo fi(sourceDirPath); + if (!fi.exists() || !fi.isReadable()) { + m_isValid = false; + return; + } + + if (!Utils::FileUtils::copyRecursively(Utils::FileName::fromString(sourceDirPath), + Utils::FileName::fromString(path()))) { + m_isValid = false; + return; + } + } +} + +QString TemporaryCopiedDir::absolutePath(const QByteArray &relativePath) const +{ + return m_temporaryDir.path() + QLatin1Char('/') + QString::fromUtf8(relativePath); +} + FileWriterAndRemover::FileWriterAndRemover(const QString &filePath, const QByteArray &contents) : m_filePath(filePath) { @@ -225,5 +344,32 @@ IAssistProposalScopedPointer::~IAssistProposalScopedPointer() delete d->model(); } +VerifyCleanCppModelManager::VerifyCleanCppModelManager() +{ + QVERIFY(isClean()); +} + +VerifyCleanCppModelManager::~VerifyCleanCppModelManager() { + QVERIFY(isClean()); +} + +#define RETURN_FALSE_IF_NOT(check) if (!(check)) return false; + +bool VerifyCleanCppModelManager::isClean() +{ + CppModelManager *mm = CppModelManager::instance(); + RETURN_FALSE_IF_NOT(mm); + RETURN_FALSE_IF_NOT(mm->projectInfos().isEmpty()); + RETURN_FALSE_IF_NOT(mm->headerPaths().isEmpty()); + RETURN_FALSE_IF_NOT(mm->definedMacros().isEmpty()); + RETURN_FALSE_IF_NOT(mm->projectFiles().isEmpty()); + RETURN_FALSE_IF_NOT(mm->snapshot().isEmpty()); + RETURN_FALSE_IF_NOT(mm->workingCopy().size() == 1); + RETURN_FALSE_IF_NOT(mm->workingCopy().contains(mm->configurationFileName())); + return true; +} + +#undef RETURN_FALSE_IF_NOT + } // namespace Tests } // namespace CppTools |