From 0cac83af8b3fe4421bd974f73bbb807b5aa7f7b7 Mon Sep 17 00:00:00 2001
From: Erik Verbruggen <erik.verbruggen@digia.com>
Date: Thu, 20 Dec 2012 14:07:27 +0100
Subject: [PATCH] C++: add unittest for modelmanager path cleaning.

Change-Id: I30815b191654cba5eedf92a9afd2ed1b48af8b87
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com>
---
 src/plugins/cpptools/ModelManagerInterface.h  |  3 +
 src/plugins/cpptools/cppmodelmanager_test.cpp | 92 +++++++++++++++++
 src/plugins/cpptools/cpptools.pro             |  9 +-
 src/plugins/cpptools/cpptoolsplugin.h         |  2 +
 .../cpptools/modelmanagertesthelper.cpp       | 99 +++++++++++++++++++
 src/plugins/cpptools/modelmanagertesthelper.h | 94 ++++++++++++++++++
 6 files changed, 298 insertions(+), 1 deletion(-)
 create mode 100644 src/plugins/cpptools/cppmodelmanager_test.cpp
 create mode 100644 src/plugins/cpptools/modelmanagertesthelper.cpp
 create mode 100644 src/plugins/cpptools/modelmanagertesthelper.h

diff --git a/src/plugins/cpptools/ModelManagerInterface.h b/src/plugins/cpptools/ModelManagerInterface.h
index ed805545bd6..8477b032231 100644
--- a/src/plugins/cpptools/ModelManagerInterface.h
+++ b/src/plugins/cpptools/ModelManagerInterface.h
@@ -168,6 +168,9 @@ public:
         QHashIterator<QString, QPair<QString, unsigned> > iterator() const
         { return QHashIterator<QString, QPair<QString, unsigned> >(_elements); }
 
+        int size() const
+        { return _elements.size(); }
+
     private:
         typedef QHash<QString, QPair<QString, unsigned> > Table;
         Table _elements;
diff --git a/src/plugins/cpptools/cppmodelmanager_test.cpp b/src/plugins/cpptools/cppmodelmanager_test.cpp
new file mode 100644
index 00000000000..51cc82f5ed6
--- /dev/null
+++ b/src/plugins/cpptools/cppmodelmanager_test.cpp
@@ -0,0 +1,92 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of Qt Creator.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#include "cpptoolsplugin.h"
+#include "cppmodelmanager.h"
+#include "modelmanagertesthelper.h"
+
+#include <QtTest>
+#include <QDebug>
+
+using namespace CppTools::Internal;
+
+typedef CPlusPlus::CppModelManagerInterface::ProjectInfo ProjectInfo;
+typedef CPlusPlus::CppModelManagerInterface::ProjectPart ProjectPart;
+typedef ProjectExplorer::Project Project;
+
+namespace {
+QString testDataDir(const QString& subdir, bool cleaned = true)
+{
+    QString path = QLatin1String(SRCDIR "/../../../tests/cppmodelmanager/testdata");
+    if (!subdir.isEmpty())
+        path += QLatin1String("/") + subdir;
+    if (cleaned)
+        return CppPreprocessor::cleanPath(path);
+    else
+        return path;
+}
+
+QString testIncludeDir(bool cleaned = true)
+{
+    return testDataDir(QLatin1String("include"), cleaned);
+}
+
+QString testFrameworksDir(bool cleaned = true)
+{
+    return testDataDir(QLatin1String("frameworks"), cleaned);
+}
+} // anonymous namespace
+
+void CppToolsPlugin::test_modelmanager_paths()
+{
+    ModelManagerTestHelper helper;
+    CppModelManager *mm = CppModelManager::instance();
+
+    Project *project = helper.createProject(QLatin1String("test_modelmanager_paths"));
+    ProjectInfo pi = mm->projectInfo(project);
+    QCOMPARE(pi.project().data(), project);
+
+    ProjectPart::Ptr part(new ProjectPart);
+    pi.appendProjectPart(part);
+    part->language = ProjectPart::CXX;
+    part->qtVersion = ProjectPart::Qt5;
+    part->defines = QByteArray("#define OH_BEHAVE -1\n");
+    part->includePaths = QStringList() << testIncludeDir(false);
+    part->frameworkPaths = QStringList() << testFrameworksDir(false);
+
+    mm->updateProjectInfo(pi);
+
+    QStringList includePaths = mm->includePaths();
+    QCOMPARE(includePaths.size(), 1);
+    QVERIFY(includePaths.contains(testIncludeDir()));
+
+    QStringList frameworkPaths = mm->frameworkPaths();
+    QCOMPARE(frameworkPaths.size(), 1);
+    QVERIFY(frameworkPaths.contains(testFrameworksDir()));
+}
diff --git a/src/plugins/cpptools/cpptools.pro b/src/plugins/cpptools/cpptools.pro
index 5cb5f217491..7198cf28a8c 100644
--- a/src/plugins/cpptools/cpptools.pro
+++ b/src/plugins/cpptools/cpptools.pro
@@ -98,5 +98,12 @@ FORMS += completionsettingspage.ui \
 equals(TEST, 1) {
     SOURCES += \
         cppcodegen_test.cpp \
-        cppcompletion_test.cpp
+        cppcompletion_test.cpp \
+        cppmodelmanager_test.cpp \
+        modelmanagertesthelper.cpp
+
+    HEADERS += \
+        modelmanagertesthelper.h
+
+    DEFINES += SRCDIR=\\\"$$PWD\\\"
 }
diff --git a/src/plugins/cpptools/cpptoolsplugin.h b/src/plugins/cpptools/cpptoolsplugin.h
index 29f119db76a..e6ad68cfb1d 100644
--- a/src/plugins/cpptools/cpptoolsplugin.h
+++ b/src/plugins/cpptools/cpptoolsplugin.h
@@ -113,6 +113,8 @@ private slots:
     void test_completion_instantiate_nested_class_when_enclosing_is_template();
     void test_completion_instantiate_nested_of_nested_class_when_enclosing_is_template();
 
+    void test_modelmanager_paths();
+
 private:
     void test_completion();
 #endif
diff --git a/src/plugins/cpptools/modelmanagertesthelper.cpp b/src/plugins/cpptools/modelmanagertesthelper.cpp
new file mode 100644
index 00000000000..a9b34090306
--- /dev/null
+++ b/src/plugins/cpptools/modelmanagertesthelper.cpp
@@ -0,0 +1,99 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of Qt Creator.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#include "modelmanagertesthelper.h"
+
+#include <QtTest>
+
+#include <cassert>
+
+using namespace CppTools::Internal;
+
+TestProject::TestProject(const QString &name, QObject *parent)
+    : m_name (name)
+{
+    setParent(parent);
+}
+
+TestProject::~TestProject()
+{
+}
+
+ModelManagerTestHelper::ModelManagerTestHelper(QObject *parent) :
+    QObject(parent)
+
+{
+    CppModelManager *mm = CppModelManager::instance();
+    assert(mm);
+
+    connect(this, SIGNAL(aboutToRemoveProject(ProjectExplorer::Project*)), mm, SLOT(onAboutToRemoveProject(ProjectExplorer::Project*)));
+    connect(this, SIGNAL(projectAdded(ProjectExplorer::Project*)), mm, SLOT(onProjectAdded(ProjectExplorer::Project*)));
+
+    cleanup();
+    verifyClean();
+}
+
+ModelManagerTestHelper::~ModelManagerTestHelper()
+{
+    cleanup();
+    verifyClean();
+}
+
+void ModelManagerTestHelper::cleanup()
+{
+    CppModelManager *mm = CppModelManager::instance();
+    assert(mm);
+
+    QList<ProjectInfo> pies = mm->projectInfos();
+    foreach (const ProjectInfo &pie, pies)
+        emit aboutToRemoveProject(pie.project().data());
+}
+
+void ModelManagerTestHelper::verifyClean()
+{
+    CppModelManager *mm = CppModelManager::instance();
+    assert(mm);
+
+    QVERIFY(mm->projectInfos().isEmpty());
+    QVERIFY(mm->includePaths().isEmpty());
+    QVERIFY(mm->frameworkPaths().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);
+    emit projectAdded(tp);
+    return tp;
+}
+
diff --git a/src/plugins/cpptools/modelmanagertesthelper.h b/src/plugins/cpptools/modelmanagertesthelper.h
new file mode 100644
index 00000000000..5de107888d8
--- /dev/null
+++ b/src/plugins/cpptools/modelmanagertesthelper.h
@@ -0,0 +1,94 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of Qt Creator.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#ifndef CPPTOOLS_INTERNAL_MODELMANAGERTESTHELPER_H
+#define CPPTOOLS_INTERNAL_MODELMANAGERTESTHELPER_H
+
+#include "cppmodelmanager.h"
+
+#include <QObject>
+
+namespace CppTools {
+namespace Internal {
+
+class TestProject: public ProjectExplorer::Project
+{
+    Q_OBJECT
+
+public:
+    TestProject(const QString &name, QObject *parent);
+    virtual ~TestProject();
+
+    virtual QString displayName() const
+    { return m_name; }
+
+    virtual Core::Id id() const
+    { return Core::Id(m_name); }
+
+    virtual Core::IDocument *document() const
+    { return 0; }
+
+    virtual ProjectExplorer::IProjectManager *projectManager() const
+    { return 0; }
+
+    virtual ProjectExplorer::ProjectNode *rootProjectNode() const
+    { return 0; }
+
+    virtual QStringList files(FilesMode fileMode) const
+    { Q_UNUSED(fileMode); return QStringList(); }
+
+private:
+    QString m_name;
+};
+
+class ModelManagerTestHelper: public QObject
+{
+    Q_OBJECT
+
+public:
+    typedef CPlusPlus::CppModelManagerInterface::ProjectInfo ProjectInfo;
+    typedef ProjectExplorer::Project Project;
+
+    explicit ModelManagerTestHelper(QObject *parent = 0);
+    ~ModelManagerTestHelper();
+
+    void cleanup();
+    void verifyClean();
+
+    Project *createProject(const QString &name);
+
+signals:
+    void aboutToRemoveProject(ProjectExplorer::Project *project);
+    void projectAdded(ProjectExplorer::Project*);
+};
+
+} // namespace Internal
+} // namespace CppTools
+
+#endif // CPPTOOLS_INTERNAL_MODELMANAGERTESTHELPER_H
-- 
GitLab