From ef403a4515aa93272f6e5155b2897ddd02fb128e Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Tue, 9 Dec 2014 13:01:44 +0100 Subject: GenericProjectManager: Tests: Clean up * Get rid of CppModelManagerHelper. Now we simply use CppModelManager::projectInfo(someProject).isValid() to test whether a project is loaded. * Copy project data to temporary dir before opening the projects. This avoids creating *.user files in the Qt Creator source tree and annoying pop ups on test start. Change-Id: I1a57441ca2099beb6bb96cf620390d669fb47601 Reviewed-by: Christian Stenger --- src/plugins/cpptools/cpptoolstestcase.cpp | 76 +++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'src/plugins/cpptools/cpptoolstestcase.cpp') diff --git a/src/plugins/cpptools/cpptoolstestcase.cpp b/src/plugins/cpptools/cpptoolstestcase.cpp index 00750ed0d9..0102384aa8 100644 --- a/src/plugins/cpptools/cpptoolstestcase.cpp +++ b/src/plugins/cpptools/cpptoolstestcase.cpp @@ -31,6 +31,8 @@ #include "cpptoolstestcase.h" #include +#include +#include #include #include #include @@ -40,6 +42,8 @@ #include +using namespace ProjectExplorer; + static bool closeEditorsWithoutGarbageCollectorInvocation(const QList &editors) { CppTools::CppModelManager::instance()->enableGarbageCollector(false); @@ -181,6 +185,24 @@ QList 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 +214,60 @@ bool TestCase::writeFile(const QString &filePath, const QByteArray &contents) return true; } +ProjectOpenerAndCloser::ProjectOpenerAndCloser() +{ + QVERIFY(!SessionManager::hasProjects()); +} + +ProjectOpenerAndCloser::~ProjectOpenerAndCloser() +{ + foreach (Project *project, m_openProjects) + ProjectExplorerPlugin::unloadProject(project); +} + +ProjectInfo ProjectOpenerAndCloser::open(const QString &projectFile) +{ + QString error; + Project *project = ProjectExplorerPlugin::openProject(projectFile, &error); + if (!error.isEmpty()) + qWarning() << error; + if (!project) + return ProjectInfo(); + m_openProjects.append(project); + + if (TestCase::waitUntilCppModelManagerIsAwareOf(project)) + return CppModelManager::instance()->projectInfo(project); + + return ProjectInfo(); +} + +TemporaryCopiedDir::TemporaryCopiedDir(const QString &sourceDirPath) + : m_temporaryDir(QDir::tempPath() + QLatin1String("/qtcreator-tests-XXXXXX")) + , m_isValid(m_temporaryDir.isValid()) +{ + 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) { -- cgit v1.2.1