cppmodelmanager_test.cpp 44.2 KB
Newer Older
1 2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5 6 7 8 9 10 11
**
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27 28 29 30
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

31
#include "baseeditordocumentprocessor.h"
32
#include "builtineditordocumentparser.h"
33
#include "cppsourceprocessor.h"
34 35
#include "cpptoolsplugin.h"
#include "cpptoolstestcase.h"
36
#include "editordocumenthandle.h"
37 38
#include "modelmanagertesthelper.h"

39
#include <coreplugin/editormanager/editormanager.h>
40
#include <coreplugin/fileutils.h>
41
#include <coreplugin/testdatadir.h>
42 43
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
44 45

#include <cplusplus/LookupContext.h>
46
#include <utils/hostosinfo.h>
47

48
#include <QDebug>
49
#include <QtTest>
50

51 52 53 54
#define VERIFY_DOCUMENT_REVISION(document, expectedRevision) \
    QVERIFY(document); \
    QCOMPARE(document->revision(), expectedRevision);

55
using namespace CppTools;
56
using namespace CppTools::Internal;
57
using namespace CppTools::Tests;
hjk's avatar
hjk committed
58
using namespace ProjectExplorer;
59

60
typedef CPlusPlus::Document Document;
61

Orgad Shaneh's avatar
Orgad Shaneh committed
62
Q_DECLARE_METATYPE(QList<ProjectFile>)
63

64 65
namespace {

66 67
inline QString _(const QByteArray &ba) { return QString::fromLatin1(ba, ba.size()); }

68
class MyTestDataDir : public Core::Tests::TestDataDir
69
{
70
public:
71
    MyTestDataDir(const QString &dir)
72
        : TestDataDir(_(SRCDIR "/../../../tests/cppmodelmanager/") + dir)
73
    {}
74

75
    QString includeDir(bool cleaned = true) const
76
    { return directory(_("include"), cleaned); }
77 78

    QString frameworksDir(bool cleaned = true) const
79
    { return directory(_("frameworks"), cleaned); }
80 81

    QString fileFromSourcesDir(const QString &fileName) const
82
    { return directory(_("sources")) + QLatin1Char('/') + fileName; }
83 84
};

85 86 87 88 89 90 91 92 93
QStringList toAbsolutePaths(const QStringList &relativePathList,
                            const Tests::TemporaryCopiedDir &temporaryDir)
{
    QStringList result;
    foreach (const QString &file, relativePathList)
        result << temporaryDir.absolutePath(file.toUtf8());
    return result;
}

94 95 96 97 98 99 100 101 102
// TODO: When possible, use this helper class in all tests
class ProjectCreator
{
public:
    ProjectCreator(ModelManagerTestHelper *modelManagerTestHelper)
        : modelManagerTestHelper(modelManagerTestHelper)
    {}

    /// 'files' is expected to be a list of file names that reside in 'dir'.
103
    void create(const QString &name, const QString &dir, const QStringList &files)
104
    {
105
        const MyTestDataDir projectDir(dir);
106 107 108 109
        foreach (const QString &file, files)
            projectFiles << projectDir.file(file);

        Project *project = modelManagerTestHelper->createProject(name);
110
        projectInfo = ProjectInfo(project);
111 112

        ProjectPart::Ptr part(new ProjectPart);
113
        part->languageVersion = ProjectPart::CXX14;
114 115 116 117 118
        part->qtVersion = ProjectPart::Qt5;
        foreach (const QString &file, projectFiles) {
            ProjectFile projectFile(file, ProjectFile::classify(file));
            part->files.append(projectFile);
        }
119
        projectInfo.appendProjectPart(part);
120
        projectInfo.finish();
121 122 123 124 125 126 127
    }

    ModelManagerTestHelper *modelManagerTestHelper;
    ProjectInfo projectInfo;
    QStringList projectFiles;
};

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/// Changes a file on the disk and restores its original contents on destruction
class FileChangerAndRestorer
{
public:
    FileChangerAndRestorer(const QString &filePath)
        : m_filePath(filePath)
    {
    }

    ~FileChangerAndRestorer()
    {
        restoreContents();
    }

    /// Saves the contents also internally so it can be restored on destruction
    bool readContents(QByteArray *contents)
    {
        Utils::FileReader fileReader;
        const bool isFetchOk = fileReader.fetch(m_filePath);
        if (isFetchOk) {
            m_originalFileContents = fileReader.data();
            if (contents)
                *contents = m_originalFileContents;
        }
        return isFetchOk;
    }

155
    bool writeContents(const QByteArray &contents) const
156
    {
157
        return TestCase::writeFile(m_filePath, contents);
158 159 160 161 162
    }

private:
    void restoreContents() const
    {
163
        TestCase::writeFile(m_filePath, m_originalFileContents);
164 165 166 167 168 169
    }

    QByteArray m_originalFileContents;
    const QString &m_filePath;
};

170
ProjectPart::Ptr projectPartOfEditorDocument(const QString &filePath)
171
{
172 173 174
    auto *editorDocument = CppModelManager::instance()->cppEditorDocument(filePath);
    QTC_ASSERT(editorDocument, return ProjectPart::Ptr());
    return editorDocument->processor()->parser()->projectPart();
175 176
}

177 178
} // anonymous namespace

179 180
/// Check: The preprocessor cleans include and framework paths.
void CppToolsPlugin::test_modelmanager_paths_are_clean()
181 182 183 184
{
    ModelManagerTestHelper helper;
    CppModelManager *mm = CppModelManager::instance();

185
    const MyTestDataDir testDataDir(_("testdata"));
186

187
    Project *project = helper.createProject(_("test_modelmanager_paths_are_clean"));
188
    ProjectInfo pi = ProjectInfo(project);
189

190 191
    typedef ProjectPart::HeaderPath HeaderPath;

192
    ProjectPart::Ptr part(new ProjectPart);
193
    part->languageVersion = ProjectPart::CXX14;
194
    part->qtVersion = ProjectPart::Qt5;
195
    part->projectDefines = QByteArray("#define OH_BEHAVE -1\n");
196 197 198
    part->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDir.includeDir(false), HeaderPath::IncludePath)
            << HeaderPath(testDataDir.frameworksDir(false), HeaderPath::FrameworkPath);
199
    pi.appendProjectPart(part);
200
    pi.finish();
201 202 203

    mm->updateProjectInfo(pi);

204 205 206 207 208
    QList<HeaderPath> headerPaths = mm->headerPaths();
    QCOMPARE(headerPaths.size(), 2);
    QVERIFY(headerPaths.contains(HeaderPath(testDataDir.includeDir(), HeaderPath::IncludePath)));
    QVERIFY(headerPaths.contains(HeaderPath(testDataDir.frameworksDir(),
                                            HeaderPath::FrameworkPath)));
209
}
210

211
/// Check: Frameworks headers are resolved.
212 213
void CppToolsPlugin::test_modelmanager_framework_headers()
{
214
    if (Utils::HostOsInfo::isWindowsHost())
215
        QSKIP("Can't resolve framework soft links on Windows.");
216

217 218 219
    ModelManagerTestHelper helper;
    CppModelManager *mm = CppModelManager::instance();

220
    const MyTestDataDir testDataDir(_("testdata"));
221

222
    Project *project = helper.createProject(_("test_modelmanager_framework_headers"));
223
    ProjectInfo pi = ProjectInfo(project);
224

225 226
    typedef ProjectPart::HeaderPath HeaderPath;

227
    ProjectPart::Ptr part(new ProjectPart);
228
    part->languageVersion = ProjectPart::CXX14;
229
    part->qtVersion = ProjectPart::Qt5;
230
    part->projectDefines = QByteArray("#define OH_BEHAVE -1\n");
231 232 233
    part->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDir.includeDir(false), HeaderPath::IncludePath)
            << HeaderPath(testDataDir.frameworksDir(false), HeaderPath::FrameworkPath);
234
    const QString &source = testDataDir.fileFromSourcesDir(
235
        _("test_modelmanager_framework_headers.cpp"));
236
    part->files << ProjectFile(source, ProjectFile::CXXSource);
237
    pi.appendProjectPart(part);
238
    pi.finish();
239

240
    mm->updateProjectInfo(pi).waitForFinished();
241 242
    QCoreApplication::processEvents();

243
    QVERIFY(mm->snapshot().contains(source));
244
    Document::Ptr doc = mm->document(source);
245 246 247 248 249 250 251 252 253 254 255 256 257 258
    QVERIFY(!doc.isNull());
    CPlusPlus::Namespace *ns = doc->globalNamespace();
    QVERIFY(ns);
    QVERIFY(ns->memberCount() > 0);
    for (unsigned i = 0, ei = ns->memberCount(); i < ei; ++i) {
        CPlusPlus::Symbol *s = ns->memberAt(i);
        QVERIFY(s);
        QVERIFY(s->name());
        const CPlusPlus::Identifier *id = s->name()->asNameId();
        QVERIFY(id);
        QByteArray chars = id->chars();
        QVERIFY(chars.startsWith("success"));
    }
}
259 260

/// QTCREATORBUG-9056
261 262 263
/// Check: If the project configuration changes, all project files and their
///        includes have to be reparsed.
void CppToolsPlugin::test_modelmanager_refresh_also_includes_of_project_files()
264 265 266 267
{
    ModelManagerTestHelper helper;
    CppModelManager *mm = CppModelManager::instance();

268
    const MyTestDataDir testDataDir(_("testdata"));
269

270 271
    const QString testCpp(testDataDir.fileFromSourcesDir(_("test_modelmanager_refresh.cpp")));
    const QString testHeader(testDataDir.fileFromSourcesDir( _("test_modelmanager_refresh.h")));
272

273
    Project *project = helper.createProject(
274
                _("test_modelmanager_refresh_also_includes_of_project_files"));
275
    ProjectInfo pi = ProjectInfo(project);
276

277 278
    typedef ProjectPart::HeaderPath HeaderPath;

279
    ProjectPart::Ptr part(new ProjectPart);
280
    part->languageVersion = ProjectPart::CXX14;
281
    part->qtVersion = ProjectPart::Qt5;
282
    part->projectDefines = QByteArray("#define OH_BEHAVE -1\n");
283 284
    part->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDir.includeDir(false), HeaderPath::IncludePath);
285
    part->files.append(ProjectFile(testCpp, ProjectFile::CXXSource));
286
    pi.appendProjectPart(part);
287
    pi.finish();
288

289
    QSet<QString> refreshedFiles = helper.updateProjectInfo(pi);
290 291 292 293 294 295
    QCOMPARE(refreshedFiles.size(), 1);
    QVERIFY(refreshedFiles.contains(testCpp));
    CPlusPlus::Snapshot snapshot = mm->snapshot();
    QVERIFY(snapshot.contains(testHeader));
    QVERIFY(snapshot.contains(testCpp));

296 297 298 299
    Document::Ptr headerDocumentBefore = snapshot.document(testHeader);
    const QList<CPlusPlus::Macro> macrosInHeaderBefore = headerDocumentBefore->definedMacros();
    QCOMPARE(macrosInHeaderBefore.size(), 1);
    QVERIFY(macrosInHeaderBefore.first().name() == "test_modelmanager_refresh_h");
300

301
    // Introduce a define that will enable another define once the document is reparsed.
302
    part->projectDefines = QByteArray("#define TEST_DEFINE 1\n");
303
    pi = ProjectInfo(project);
304
    pi.appendProjectPart(part);
305
    pi.finish();
306

307
    refreshedFiles = helper.updateProjectInfo(pi);
308 309 310 311 312 313

    QCOMPARE(refreshedFiles.size(), 1);
    QVERIFY(refreshedFiles.contains(testCpp));
    snapshot = mm->snapshot();
    QVERIFY(snapshot.contains(testHeader));
    QVERIFY(snapshot.contains(testCpp));
314 315 316 317 318 319

    Document::Ptr headerDocumentAfter = snapshot.document(testHeader);
    const QList<CPlusPlus::Macro> macrosInHeaderAfter = headerDocumentAfter->definedMacros();
    QCOMPARE(macrosInHeaderAfter.size(), 2);
    QVERIFY(macrosInHeaderAfter.at(0).name() == "test_modelmanager_refresh_h");
    QVERIFY(macrosInHeaderAfter.at(1).name() == "TEST_DEFINE_DEFINED");
320
}
321 322

/// QTCREATORBUG-9205
323
/// Check: When reparsing the same files again, no errors occur
324
///        (The CppSourceProcessor's already seen files are properly cleared!).
325
void CppToolsPlugin::test_modelmanager_refresh_several_times()
326 327 328 329
{
    ModelManagerTestHelper helper;
    CppModelManager *mm = CppModelManager::instance();

330
    const MyTestDataDir testDataDir(_("testdata_refresh"));
331

332 333 334
    const QString testHeader1(testDataDir.file(_("defines.h")));
    const QString testHeader2(testDataDir.file(_("header.h")));
    const QString testCpp(testDataDir.file(_("source.cpp")));
335

336
    Project *project = helper.createProject(_("test_modelmanager_refresh_several_times"));
337
    ProjectInfo pi = ProjectInfo(project);
338 339

    ProjectPart::Ptr part(new ProjectPart);
340
    part->languageVersion = ProjectPart::CXX14;
341 342 343 344
    part->qtVersion = ProjectPart::Qt5;
    part->files.append(ProjectFile(testHeader1, ProjectFile::CXXHeader));
    part->files.append(ProjectFile(testHeader2, ProjectFile::CXXHeader));
    part->files.append(ProjectFile(testCpp, ProjectFile::CXXSource));
345
    pi.appendProjectPart(part);
346
    pi.finish();
347 348 349
    mm->updateProjectInfo(pi);

    CPlusPlus::Snapshot snapshot;
350
    QSet<QString> refreshedFiles;
351 352
    CPlusPlus::Document::Ptr document;

353
    QByteArray defines = "#define FIRST_DEFINE";
354
    for (int i = 0; i < 2; ++i) {
355
        pi = ProjectInfo(project);
356 357 358
        ProjectPart::Ptr part(new ProjectPart);
        // Simulate project configuration change by having different defines each time.
        defines += "\n#define ANOTHER_DEFINE";
359
        part->projectDefines = defines;
360
        part->languageVersion = ProjectPart::CXX14;
361 362 363 364 365
        part->qtVersion = ProjectPart::Qt5;
        part->files.append(ProjectFile(testHeader1, ProjectFile::CXXHeader));
        part->files.append(ProjectFile(testHeader2, ProjectFile::CXXHeader));
        part->files.append(ProjectFile(testCpp, ProjectFile::CXXSource));
        pi.appendProjectPart(part);
366
        pi.finish();
367

368
        refreshedFiles = helper.updateProjectInfo(pi);
369
        QCOMPARE(refreshedFiles.size(), 3);
370

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
        QVERIFY(refreshedFiles.contains(testHeader1));
        QVERIFY(refreshedFiles.contains(testHeader2));
        QVERIFY(refreshedFiles.contains(testCpp));

        snapshot = mm->snapshot();
        QVERIFY(snapshot.contains(testHeader1));
        QVERIFY(snapshot.contains(testHeader2));
        QVERIFY(snapshot.contains(testCpp));

        // No diagnostic messages expected
        document = snapshot.document(testHeader1);
        QVERIFY(document->diagnosticMessages().isEmpty());

        document = snapshot.document(testHeader2);
        QVERIFY(document->diagnosticMessages().isEmpty());

        document = snapshot.document(testCpp);
        QVERIFY(document->diagnosticMessages().isEmpty());
    }
}
391

392 393 394 395 396 397 398
/// QTCREATORBUG-9581
/// Check: If nothing has changes, nothing should be reindexed.
void CppToolsPlugin::test_modelmanager_refresh_test_for_changes()
{
    ModelManagerTestHelper helper;
    CppModelManager *mm = CppModelManager::instance();

399 400
    const MyTestDataDir testDataDir(_("testdata_refresh"));
    const QString testCpp(testDataDir.file(_("source.cpp")));
401

402
    Project *project = helper.createProject(_("test_modelmanager_refresh_2"));
403
    ProjectInfo pi = ProjectInfo(project);
404 405

    ProjectPart::Ptr part(new ProjectPart);
406
    part->languageVersion = ProjectPart::CXX14;
407 408 409
    part->qtVersion = ProjectPart::Qt5;
    part->files.append(ProjectFile(testCpp, ProjectFile::CXXSource));
    pi.appendProjectPart(part);
410
    pi.finish();
411 412

    // Reindexing triggers a reparsing thread
413
    helper.resetRefreshedSourceFiles();
414 415
    QFuture<void> firstFuture = mm->updateProjectInfo(pi);
    QVERIFY(firstFuture.isStarted() || firstFuture.isRunning());
416
    firstFuture.waitForFinished();
417
    const QSet<QString> refreshedFiles = helper.waitForRefreshedSourceFiles();
418 419 420 421 422 423 424 425
    QCOMPARE(refreshedFiles.size(), 1);
    QVERIFY(refreshedFiles.contains(testCpp));

    // No reindexing since nothing has changed
    QFuture<void> subsequentFuture = mm->updateProjectInfo(pi);
    QVERIFY(subsequentFuture.isCanceled() && subsequentFuture.isFinished());
}

426 427 428 429 430 431 432
/// Check: (1) Added project files are recognized and parsed.
/// Check: (2) Removed project files are recognized and purged from the snapshot.
void CppToolsPlugin::test_modelmanager_refresh_added_and_purge_removed()
{
    ModelManagerTestHelper helper;
    CppModelManager *mm = CppModelManager::instance();

433
    const MyTestDataDir testDataDir(_("testdata_refresh"));
434

435 436 437
    const QString testHeader1(testDataDir.file(_("header.h")));
    const QString testHeader2(testDataDir.file(_("defines.h")));
    const QString testCpp(testDataDir.file(_("source.cpp")));
438

439
    Project *project = helper.createProject(_("test_modelmanager_refresh_3"));
440
    ProjectInfo pi = ProjectInfo(project);
441 442

    ProjectPart::Ptr part(new ProjectPart);
443
    part->languageVersion = ProjectPart::CXX14;
444 445 446 447
    part->qtVersion = ProjectPart::Qt5;
    part->files.append(ProjectFile(testCpp, ProjectFile::CXXSource));
    part->files.append(ProjectFile(testHeader1, ProjectFile::CXXHeader));
    pi.appendProjectPart(part);
448
    pi.finish();
449 450

    CPlusPlus::Snapshot snapshot;
451
    QSet<QString> refreshedFiles;
452

453
    refreshedFiles = helper.updateProjectInfo(pi);
454 455 456 457 458 459 460 461 462 463

    QCOMPARE(refreshedFiles.size(), 2);
    QVERIFY(refreshedFiles.contains(testHeader1));
    QVERIFY(refreshedFiles.contains(testCpp));

    snapshot = mm->snapshot();
    QVERIFY(snapshot.contains(testHeader1));
    QVERIFY(snapshot.contains(testCpp));

    // Now add testHeader2 and remove testHeader1
464
    pi = ProjectInfo(project);
465
    ProjectPart::Ptr newPart(new ProjectPart);
466
    newPart->languageVersion = ProjectPart::CXX14;
467 468 469 470
    newPart->qtVersion = ProjectPart::Qt5;
    newPart->files.append(ProjectFile(testCpp, ProjectFile::CXXSource));
    newPart->files.append(ProjectFile(testHeader2, ProjectFile::CXXHeader));
    pi.appendProjectPart(newPart);
471
    pi.finish();
472

473
    refreshedFiles = helper.updateProjectInfo(pi);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

    // Only the added project file was reparsed
    QCOMPARE(refreshedFiles.size(), 1);
    QVERIFY(refreshedFiles.contains(testHeader2));

    snapshot = mm->snapshot();
    QVERIFY(snapshot.contains(testHeader2));
    QVERIFY(snapshot.contains(testCpp));
    // The removed project file is not anymore in the snapshot
    QVERIFY(!snapshot.contains(testHeader1));
}

/// Check: Timestamp modified files are reparsed if project files are added or removed
///        while the project configuration stays the same
void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_change()
{
    QFETCH(QString, fileToChange);
491 492 493 494 495 496 497 498
    QFETCH(QStringList, initialProjectFiles);
    QFETCH(QStringList, finalProjectFiles);

    Tests::TemporaryCopiedDir temporaryDir(
                MyTestDataDir(QLatin1String("testdata_refresh2")).path());
    fileToChange = temporaryDir.absolutePath(fileToChange.toUtf8());
    initialProjectFiles = toAbsolutePaths(initialProjectFiles, temporaryDir);
    finalProjectFiles = toAbsolutePaths(finalProjectFiles, temporaryDir);
499 500 501 502

    ModelManagerTestHelper helper;
    CppModelManager *mm = CppModelManager::instance();

503
    Project *project = helper.createProject(_("test_modelmanager_refresh_timeStampModified"));
504
    ProjectInfo pi = ProjectInfo(project);
505 506

    ProjectPart::Ptr part(new ProjectPart);
507
    part->languageVersion = ProjectPart::CXX14;
508
    part->qtVersion = ProjectPart::Qt5;
509 510
    foreach (const QString &file, initialProjectFiles)
        part->files.append(ProjectFile(file, ProjectFile::CXXSource));
511
    pi = ProjectInfo(project);
512
    pi.appendProjectPart(part);
513
    pi.finish();
514 515 516

    Document::Ptr document;
    CPlusPlus::Snapshot snapshot;
517
    QSet<QString> refreshedFiles;
518

519
    refreshedFiles = helper.updateProjectInfo(pi);
520 521 522

    QCOMPARE(refreshedFiles.size(), initialProjectFiles.size());
    snapshot = mm->snapshot();
523 524 525
    foreach (const QString &file, initialProjectFiles) {
        QVERIFY(refreshedFiles.contains(file));
        QVERIFY(snapshot.contains(file));
526 527 528 529 530 531 532 533 534 535 536 537 538
    }

    document = snapshot.document(fileToChange);
    const QDateTime lastModifiedBefore = document->lastModified();
    QCOMPARE(document->globalSymbolCount(), 1U);
    QCOMPARE(document->globalSymbolAt(0)->name()->identifier()->chars(), "someGlobal");

    // Modify the file
    QTest::qSleep(1000); // Make sure the timestamp is different
    FileChangerAndRestorer fileChangerAndRestorer(fileToChange);
    QByteArray originalContents;
    QVERIFY(fileChangerAndRestorer.readContents(&originalContents));
    const QByteArray newFileContentes = originalContents + "\nint addedOtherGlobal;";
539
    QVERIFY(fileChangerAndRestorer.writeContents(newFileContentes));
540 541 542

    // Add or remove source file. The configuration stays the same.
    part->files.clear();
543 544
    foreach (const QString &file, finalProjectFiles)
        part->files.append(ProjectFile(file, ProjectFile::CXXSource));
545
    pi = ProjectInfo(project);
546
    pi.appendProjectPart(part);
547
    pi.finish();
548

549
    refreshedFiles = helper.updateProjectInfo(pi);
550 551 552

    QCOMPARE(refreshedFiles.size(), finalProjectFiles.size());
    snapshot = mm->snapshot();
553 554 555
    foreach (const QString &file, finalProjectFiles) {
        QVERIFY(refreshedFiles.contains(file));
        QVERIFY(snapshot.contains(file));
556 557 558 559 560 561 562 563 564 565 566 567
    }
    document = snapshot.document(fileToChange);
    const QDateTime lastModifiedAfter = document->lastModified();
    QVERIFY(lastModifiedAfter > lastModifiedBefore);
    QCOMPARE(document->globalSymbolCount(), 2U);
    QCOMPARE(document->globalSymbolAt(0)->name()->identifier()->chars(), "someGlobal");
    QCOMPARE(document->globalSymbolAt(1)->name()->identifier()->chars(), "addedOtherGlobal");
}

void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_change_data()
{
    QTest::addColumn<QString>("fileToChange");
568 569
    QTest::addColumn<QStringList>("initialProjectFiles");
    QTest::addColumn<QStringList>("finalProjectFiles");
570

571 572
    const QString testCpp = QLatin1String("source.cpp");
    const QString testCpp2 = QLatin1String("source2.cpp");
573 574

    const QString fileToChange = testCpp;
575 576
    const QStringList projectFiles1 = QStringList() << testCpp;
    const QStringList projectFiles2 = QStringList() << testCpp << testCpp2;
577 578 579 580 581 582 583 584

    // Add a file
    QTest::newRow("case: add project file") << fileToChange << projectFiles1 << projectFiles2;

    // Remove a file
    QTest::newRow("case: remove project file") << fileToChange << projectFiles2 << projectFiles1;
}

585 586
/// Check: If a second project is opened, the code model is still aware of
///        files of the first project.
587 588
void CppToolsPlugin::test_modelmanager_snapshot_after_two_projects()
{
589
    QSet<QString> refreshedFiles;
590 591 592 593 594 595
    ModelManagerTestHelper helper;
    ProjectCreator project1(&helper);
    ProjectCreator project2(&helper);
    CppModelManager *mm = CppModelManager::instance();

    // Project 1
596 597 598 599 600
    project1.create(_("test_modelmanager_snapshot_after_two_projects.1"),
                    _("testdata_project1"),
                    QStringList() << _("foo.h")
                                  << _("foo.cpp")
                                  << _("main.cpp"));
601

602
    refreshedFiles = helper.updateProjectInfo(project1.projectInfo);
603
    QCOMPARE(refreshedFiles, project1.projectFiles.toSet());
604 605 606 607 608 609
    const int snapshotSizeAfterProject1 = mm->snapshot().size();

    foreach (const QString &file, project1.projectFiles)
        QVERIFY(mm->snapshot().contains(file));

    // Project 2
610 611 612 613 614
    project2.create(_("test_modelmanager_snapshot_after_two_projects.2"),
                    _("testdata_project2"),
                    QStringList() << _("bar.h")
                                  << _("bar.cpp")
                                  << _("main.cpp"));
615

616
    refreshedFiles = helper.updateProjectInfo(project2.projectInfo);
617
    QCOMPARE(refreshedFiles, project2.projectFiles.toSet());
618 619 620 621 622 623 624 625 626 627

    const int snapshotSizeAfterProject2 = mm->snapshot().size();
    QVERIFY(snapshotSizeAfterProject2 > snapshotSizeAfterProject1);
    QVERIFY(snapshotSizeAfterProject2 >= snapshotSizeAfterProject1 + project2.projectFiles.size());

    foreach (const QString &file, project1.projectFiles)
        QVERIFY(mm->snapshot().contains(file));
    foreach (const QString &file, project2.projectFiles)
        QVERIFY(mm->snapshot().contains(file));
}
628

629 630
/// Check: (1) For a project with a *.ui file an AbstractEditorSupport object
///            is added for the ui_* file.
631
/// Check: (2) The CppSourceProcessor can successfully resolve the ui_* file
632
///            though it might not be actually generated in the build dir.
633 634
///

635 636
void CppToolsPlugin::test_modelmanager_extraeditorsupport_uiFiles()
{
637
    VerifyCleanCppModelManager verify;
638

639 640 641
    TemporaryCopiedDir temporaryDir(MyTestDataDir(QLatin1String("testdata_guiproject1")).path());
    QVERIFY(temporaryDir.isValid());
    const QString projectFile = temporaryDir.absolutePath("testdata_guiproject1.pro");
642

643
    ProjectOpenerAndCloser projects;
644 645
    ProjectInfo projectInfo = projects.open(projectFile, /*configureAsExampleProject=*/ true);
    QVERIFY(projectInfo.isValid());
646 647 648

    // Check working copy.
    // An AbstractEditorSupport object should have been added for the ui_* file.
649
    CppModelManager *mm = CppModelManager::instance();
650
    WorkingCopy workingCopy = mm->workingCopy();
651 652 653 654

    QCOMPARE(workingCopy.size(), 2); // mm->configurationFileName() and "ui_*.h"

    QStringList fileNamesInWorkinCopy;
655
    QHashIterator<Utils::FileName, QPair<QByteArray, unsigned> > it = workingCopy.iterator();
656 657
    while (it.hasNext()) {
        it.next();
658
        fileNamesInWorkinCopy << Utils::FileName::fromString(it.key().toString()).fileName();
659 660
    }
    fileNamesInWorkinCopy.sort();
661
    const QString expectedUiHeaderFileName = _("ui_mainwindow.h");
662 663 664
    QCOMPARE(fileNamesInWorkinCopy.at(0), mm->configurationFileName());
    QCOMPARE(fileNamesInWorkinCopy.at(1), expectedUiHeaderFileName);

665 666
    // Check CppSourceProcessor / includes.
    // The CppSourceProcessor is expected to find the ui_* file in the working copy.
667
    const QString fileIncludingTheUiFile = temporaryDir.absolutePath("mainwindow.cpp");
668 669 670 671 672 673 674 675
    while (!mm->snapshot().document(fileIncludingTheUiFile))
        QCoreApplication::processEvents();

    const CPlusPlus::Snapshot snapshot = mm->snapshot();
    const Document::Ptr document = snapshot.document(fileIncludingTheUiFile);
    QVERIFY(document);
    const QStringList includedFiles = document->includedFiles();
    QCOMPARE(includedFiles.size(), 2);
676 677
    QCOMPARE(Utils::FileName::fromString(includedFiles.at(0)).fileName(), _("mainwindow.h"));
    QCOMPARE(Utils::FileName::fromString(includedFiles.at(1)).fileName(), _("ui_mainwindow.h"));
678
}
679 680 681 682 683

/// QTCREATORBUG-9828: Locator shows symbols of closed files
/// Check: The garbage collector should be run if the last CppEditor is closed.
void CppToolsPlugin::test_modelmanager_gc_if_last_cppeditor_closed()
{
684 685
    ModelManagerTestHelper helper;

686 687
    MyTestDataDir testDataDirectory(_("testdata_guiproject1"));
    const QString file = testDataDirectory.file(_("main.cpp"));
688 689

    CppModelManager *mm = CppModelManager::instance();
690
    helper.resetRefreshedSourceFiles();
691 692

    // Open a file in the editor
hjk's avatar
hjk committed
693
    QCOMPARE(Core::DocumentModel::openedDocuments().size(), 0);
694
    Core::IEditor *editor = Core::EditorManager::openEditor(file);
695
    QVERIFY(editor);
hjk's avatar
hjk committed
696
    QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
697 698 699
    QVERIFY(mm->isCppEditor(editor));
    QVERIFY(mm->workingCopy().contains(file));

700 701
    // Wait until the file is refreshed
    helper.waitForRefreshedSourceFiles();
702 703

    // Close file/editor
704
    Core::EditorManager::closeDocument(editor->document(), /*askAboutModifiedEditors=*/ false);
705
    helper.waitForFinishedGc();
706 707 708 709 710

    // Check: File is removed from the snapshpt
    QVERIFY(!mm->workingCopy().contains(file));
    QVERIFY(!mm->snapshot().contains(file));
}
711 712 713 714 715 716

/// Check: Files that are open in the editor are not garbage collected.
void CppToolsPlugin::test_modelmanager_dont_gc_opened_files()
{
    ModelManagerTestHelper helper;

717 718
    MyTestDataDir testDataDirectory(_("testdata_guiproject1"));
    const QString file = testDataDirectory.file(_("main.cpp"));
719 720

    CppModelManager *mm = CppModelManager::instance();
721
    helper.resetRefreshedSourceFiles();
722 723

    // Open a file in the editor
hjk's avatar
hjk committed
724
    QCOMPARE(Core::DocumentModel::openedDocuments().size(), 0);
725
    Core::IEditor *editor = Core::EditorManager::openEditor(file);
726
    QVERIFY(editor);
hjk's avatar
hjk committed
727
    QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
728 729
    QVERIFY(mm->isCppEditor(editor));

730 731
    // Wait until the file is refreshed and check whether it is in the working copy
    helper.waitForRefreshedSourceFiles();
732

733 734 735 736 737 738 739 740 741 742
    QVERIFY(mm->workingCopy().contains(file));

    // Run the garbage collector
    mm->GC();

    // Check: File is still there
    QVERIFY(mm->workingCopy().contains(file));
    QVERIFY(mm->snapshot().contains(file));

    // Close editor
743
    Core::EditorManager::closeDocument(editor->document());
744 745 746
    helper.waitForFinishedGc();
    QVERIFY(mm->snapshot().isEmpty());
}
747 748 749 750 751 752 753

namespace {
struct EditorCloser {
    Core::IEditor *editor;
    EditorCloser(Core::IEditor *editor): editor(editor) {}
    ~EditorCloser()
    {
754
        using namespace CppTools;
755
        if (editor)
756
            QVERIFY(Tests::TestCase::closeEditorWithoutGarbageCollectorInvocation(editor));
757 758
    }
};
759 760 761 762 763 764 765 766

QString nameOfFirstDeclaration(const Document::Ptr &doc)
{
    if (doc && doc->globalNamespace()) {
        if (CPlusPlus::Symbol *s = doc->globalSymbolAt(0)) {
            if (CPlusPlus::Declaration *decl = s->asDeclaration()) {
                if (const CPlusPlus::Name *name = decl->name()) {
                    if (const CPlusPlus::Identifier *identifier = name->identifier())
767
                        return QString::fromUtf8(identifier->chars(), identifier->size());
768 769 770 771 772 773
                }
            }
        }
    }
    return QString();
}
774 775 776 777 778 779
}

void CppToolsPlugin::test_modelmanager_defines_per_project()
{
    ModelManagerTestHelper helper;

780 781 782 783
    MyTestDataDir testDataDirectory(_("testdata_defines"));
    const QString main1File = testDataDirectory.file(_("main1.cpp"));
    const QString main2File = testDataDirectory.file(_("main2.cpp"));
    const QString header = testDataDirectory.file(_("header.h"));
784 785 786

    CppModelManager *mm = CppModelManager::instance();

787
    Project *project = helper.createProject(_("test_modelmanager_defines_per_project"));
788

789 790
    typedef ProjectPart::HeaderPath HeaderPath;

791
    ProjectPart::Ptr part1(new ProjectPart);
792
    part1->projectFile = QLatin1String("project1.projectfile");
793 794
    part1->files.append(ProjectFile(main1File, ProjectFile::CXXSource));
    part1->files.append(ProjectFile(header, ProjectFile::CXXHeader));
795
    part1->languageVersion = ProjectPart::CXX11;
796
    part1->qtVersion = ProjectPart::NoQt;
797
    part1->projectDefines = QByteArray("#define SUB1\n");
798 799
    part1->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDirectory.includeDir(false), HeaderPath::IncludePath);
800 801

    ProjectPart::Ptr part2(new ProjectPart);
802
    part2->projectFile = QLatin1String("project1.projectfile");
803 804
    part2->files.append(ProjectFile(main2File, ProjectFile::CXXSource));
    part2->files.append(ProjectFile(header, ProjectFile::CXXHeader));
805
    part2->languageVersion = ProjectPart::CXX11;
806
    part2->qtVersion = ProjectPart::NoQt;
807
    part2->projectDefines = QByteArray("#define SUB2\n");
808 809
    part2->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDirectory.includeDir(false), HeaderPath::IncludePath);
810

811
    ProjectInfo pi = ProjectInfo(project);
812 813
    pi.appendProjectPart(part1);
    pi.appendProjectPart(part2);
814
    pi.finish();
815

816
    helper.updateProjectInfo(pi);
817 818 819
    QCOMPARE(mm->snapshot().size(), 4);

    // Open a file in the editor
hjk's avatar
hjk committed
820
    QCOMPARE(Core::DocumentModel::openedDocuments().size(), 0);
821

822 823 824 825 826 827 828 829 830 831 832 833 834
    struct Data {
        QString firstDeclarationName;
        QString fileName;
    } d[] = {
        { _("one"), main1File },
        { _("two"), main2File }
    };
    const int size = sizeof(d) / sizeof(d[0]);
    for (int i = 0; i < size; ++i) {
        const QString firstDeclarationName = d[i].firstDeclarationName;
        const QString fileName = d[i].fileName;

        Core::IEditor *editor = Core::EditorManager::openEditor(fileName);
835 836
        EditorCloser closer(editor);
        QVERIFY(editor);
hjk's avatar
hjk committed
837
        QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
838 839
        QVERIFY(mm->isCppEditor(editor));

840
        Document::Ptr doc = mm->document(fileName);
841
        QCOMPARE(nameOfFirstDeclaration(doc), firstDeclarationName);
842 843
    }
}
844

845
void CppToolsPlugin::test_modelmanager_precompiled_headers()
846 847 848
{
    ModelManagerTestHelper helper;

849 850 851 852 853 854
    MyTestDataDir testDataDirectory(_("testdata_defines"));
    const QString main1File = testDataDirectory.file(_("main1.cpp"));
    const QString main2File = testDataDirectory.file(_("main2.cpp"));
    const QString header = testDataDirectory.file(_("header.h"));
    const QString pch1File = testDataDirectory.file(_("pch1.h"));
    const QString pch2File = testDataDirectory.file(_("pch2.h"));
855 856 857

    CppModelManager *mm = CppModelManager::instance();

858
    Project *project = helper.createProject(_("test_modelmanager_defines_per_project_pch"));
859

860 861
    typedef ProjectPart::HeaderPath HeaderPath;

862
    ProjectPart::Ptr part1(new ProjectPart);
863
    part1->projectFile = QLatin1String("project1.projectfile");
864 865
    part1->files.append(ProjectFile(main1File, ProjectFile::CXXSource));
    part1->files.append(ProjectFile(header, ProjectFile::CXXHeader));
866
    part1->languageVersion = ProjectPart::CXX11;
867 868
    part1->qtVersion = ProjectPart::NoQt;
    part1->precompiledHeaders.append(pch1File);
869 870
    part1->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDirectory.includeDir(false), HeaderPath::IncludePath);
871 872

    ProjectPart::Ptr part2(new ProjectPart);
873
    part2->projectFile = QLatin1String("project2.projectfile");
874 875
    part2->files.append(ProjectFile(main2File, ProjectFile::CXXSource));
    part2->files.append(ProjectFile(header, ProjectFile::CXXHeader));
876
    part2->languageVersion = ProjectPart::CXX11;
877 878
    part2->qtVersion = ProjectPart::NoQt;
    part2->precompiledHeaders.append(pch2File);
879 880
    part2->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDirectory.includeDir(false), HeaderPath::IncludePath);
881

882
    ProjectInfo pi = ProjectInfo(project);
883 884
    pi.appendProjectPart(part1);
    pi.appendProjectPart(part2);
885
    pi.finish();
886

887
    helper.updateProjectInfo(pi);
888 889 890
    QCOMPARE(mm->snapshot().size(), 4);

    // Open a file in the editor
hjk's avatar
hjk committed
891
    QCOMPARE(Core::DocumentModel::openedDocuments().size(), 0);
892

893 894
    struct Data {
        QString firstDeclarationName;
895
        QString firstClassInPchFile;
896 897
        QString fileName;
    } d[] = {
898 899
        { _("one"), _("ClassInPch1"), main1File },
        { _("two"), _("ClassInPch2"), main2File }
900 901 902 903
    };
    const int size = sizeof(d) / sizeof(d[0]);
    for (int i = 0; i < size; ++i) {
        const QString firstDeclarationName = d[i].firstDeclarationName;
904
        const QByteArray firstClassInPchFile = d[i].firstClassInPchFile.toUtf8();
905 906 907
        const QString fileName = d[i].fileName;

        Core::IEditor *editor = Core::EditorManager::openEditor(fileName);
908 909
        EditorCloser closer(editor);
        QVERIFY(editor);
hjk's avatar
hjk committed
910
        QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
911 912
        QVERIFY(mm->isCppEditor(editor));

913
        auto parser = BuiltinEditorDocumentParser::get(fileName);
914
        QVERIFY(parser);
915 916 917
        BaseEditorDocumentParser::Configuration config = parser->configuration();
        config.usePrecompiledHeaders = true;
        parser->setConfiguration(config);
918
        parser->update(BuiltinEditorDocumentParser::InMemoryInfo(false));
919 920 921 922 923 924

        // Check if defines from pch are considered
        Document::Ptr document = mm->document(fileName);
        QCOMPARE(nameOfFirstDeclaration(document), firstDeclarationName);

        // Check if declarations from pch are considered
925
        CPlusPlus::LookupContext context(document, parser->snapshot());
926 927 928 929 930 931
        const CPlusPlus::Identifier *identifier
            = document->control()->identifier(firstClassInPchFile.data());
        const QList<CPlusPlus::LookupItem> results = context.lookup(identifier,
                                                                    document->globalNamespace());
        QVERIFY(!results.isEmpty());
        QVERIFY(results.first().declaration()->type()->asClassType());
932 933
    }
}
934 935 936 937 938

void CppToolsPlugin::test_modelmanager_defines_per_editor()
{
    ModelManagerTestHelper helper;

939 940 941 942
    MyTestDataDir testDataDirectory(_("testdata_defines"));
    const QString main1File = testDataDirectory.file(_("main1.cpp"));
    const QString main2File = testDataDirectory.file(_("main2.cpp"));
    const QString header = testDataDirectory.file(_("header.h"));
943 944 945

    CppModelManager *mm = CppModelManager::instance();

946
    Project *project = helper.createProject(_("test_modelmanager_defines_per_editor"));
947

948 949
    typedef ProjectPart::HeaderPath HeaderPath;

950 951 952
    ProjectPart::Ptr part1(new ProjectPart);
    part1->files.append(ProjectFile(main1File, ProjectFile::CXXSource));
    part1->files.append(ProjectFile(header, ProjectFile::CXXHeader));
953
    part1->languageVersion = ProjectPart::CXX11;
954
    part1->qtVersion = ProjectPart::NoQt;