cppmodelmanager_test.cpp 41.9 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 5 6 7 8 9 10 11 12
** 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
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** 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 25 26 27 28 29 30
**
** 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.
**
****************************************************************************/

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

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

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

47
#include <QDebug>
48
#include <QFileInfo>
49
#include <QTemporaryDir>
50
#include <QtTest>
51

52
using namespace CppTools;
53
using namespace CppTools::Internal;
54
using namespace CppTools::Tests;
hjk's avatar
hjk committed
55
using namespace ProjectExplorer;
56

57
typedef CPlusPlus::Document Document;
58

Orgad Shaneh's avatar
Orgad Shaneh committed
59
Q_DECLARE_METATYPE(QList<ProjectFile>)
60

61 62
namespace {

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

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

72
    QString includeDir(bool cleaned = true) const
73
    { return directory(_("include"), cleaned); }
74 75

    QString frameworksDir(bool cleaned = true) const
76
    { return directory(_("frameworks"), cleaned); }
77 78

    QString fileFromSourcesDir(const QString &fileName) const
79
    { return directory(_("sources")) + fileName; }
80 81
};

82 83 84 85 86 87 88 89 90
// 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'.
91
    void create(const QString &name, const QString &dir, const QStringList &files)
92
    {
93
        const MyTestDataDir projectDir(dir);
94 95 96 97
        foreach (const QString &file, files)
            projectFiles << projectDir.file(file);

        Project *project = modelManagerTestHelper->createProject(name);
98
        projectInfo = ProjectInfo(project);
99 100

        ProjectPart::Ptr part(new ProjectPart);
101
        part->languageVersion = ProjectPart::CXX14;
102 103 104 105 106
        part->qtVersion = ProjectPart::Qt5;
        foreach (const QString &file, projectFiles) {
            ProjectFile projectFile(file, ProjectFile::classify(file));
            part->files.append(projectFile);
        }
107
        projectInfo.appendProjectPart(part);
108
        projectInfo.finish();
109 110 111 112 113 114 115
    }

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

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/// 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;
    }

143
    bool writeContents(const QByteArray &contents) const
144
    {
145
        return CppTools::Tests::TestCase::writeFile(m_filePath, contents);
146 147 148 149 150
    }

private:
    void restoreContents() const
    {
151
        CppTools::Tests::TestCase::writeFile(m_filePath, m_originalFileContents);
152 153 154 155 156 157
    }

    QByteArray m_originalFileContents;
    const QString &m_filePath;
};

158
static QSet<QString> updateProjectInfo(CppModelManager *modelManager, ModelManagerTestHelper *helper,
159 160 161 162 163 164 165 166
                                     const ProjectInfo &projectInfo)
{
    helper->resetRefreshedSourceFiles();
    modelManager->updateProjectInfo(projectInfo).waitForFinished();
    QCoreApplication::processEvents();
    return helper->waitForRefreshedSourceFiles();
}

167 168
} // anonymous namespace

169 170
/// Check: The preprocessor cleans include and framework paths.
void CppToolsPlugin::test_modelmanager_paths_are_clean()
171 172 173 174
{
    ModelManagerTestHelper helper;
    CppModelManager *mm = CppModelManager::instance();

175
    const MyTestDataDir testDataDir(_("testdata"));
176

177
    Project *project = helper.createProject(_("test_modelmanager_paths_are_clean"));
178
    ProjectInfo pi = ProjectInfo(project);
179

180 181
    typedef ProjectPart::HeaderPath HeaderPath;

182
    ProjectPart::Ptr part(new ProjectPart);
183
    part->languageVersion = ProjectPart::CXX14;
184
    part->qtVersion = ProjectPart::Qt5;
185
    part->projectDefines = QByteArray("#define OH_BEHAVE -1\n");
186 187 188
    part->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDir.includeDir(false), HeaderPath::IncludePath)
            << HeaderPath(testDataDir.frameworksDir(false), HeaderPath::FrameworkPath);
189
    pi.appendProjectPart(part);
190
    pi.finish();
191 192 193

    mm->updateProjectInfo(pi);

194 195 196 197 198
    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)));
199
}
200

201
/// Check: Frameworks headers are resolved.
202 203
void CppToolsPlugin::test_modelmanager_framework_headers()
{
204
    if (Utils::HostOsInfo::isWindowsHost())
205
        QSKIP("Can't resolve framework soft links on Windows.");
206

207 208 209
    ModelManagerTestHelper helper;
    CppModelManager *mm = CppModelManager::instance();

210
    const MyTestDataDir testDataDir(_("testdata"));
211

212
    Project *project = helper.createProject(_("test_modelmanager_framework_headers"));
213
    ProjectInfo pi = ProjectInfo(project);
214

215 216
    typedef ProjectPart::HeaderPath HeaderPath;

217
    ProjectPart::Ptr part(new ProjectPart);
218
    part->languageVersion = ProjectPart::CXX14;
219
    part->qtVersion = ProjectPart::Qt5;
220
    part->projectDefines = QByteArray("#define OH_BEHAVE -1\n");
221 222 223
    part->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDir.includeDir(false), HeaderPath::IncludePath)
            << HeaderPath(testDataDir.frameworksDir(false), HeaderPath::FrameworkPath);
224
    const QString &source = testDataDir.fileFromSourcesDir(
225
        _("test_modelmanager_framework_headers.cpp"));
226
    part->files << ProjectFile(source, ProjectFile::CXXSource);
227
    pi.appendProjectPart(part);
228
    pi.finish();
229

230
    mm->updateProjectInfo(pi).waitForFinished();
231 232
    QCoreApplication::processEvents();

233
    QVERIFY(mm->snapshot().contains(source));
234
    Document::Ptr doc = mm->document(source);
235 236 237 238 239 240 241 242 243 244 245 246 247 248
    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"));
    }
}
249 250

/// QTCREATORBUG-9056
251 252 253
/// 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()
254 255 256 257
{
    ModelManagerTestHelper helper;
    CppModelManager *mm = CppModelManager::instance();

258
    const MyTestDataDir testDataDir(_("testdata"));
259

260 261
    const QString testCpp(testDataDir.fileFromSourcesDir(_("test_modelmanager_refresh.cpp")));
    const QString testHeader(testDataDir.fileFromSourcesDir( _("test_modelmanager_refresh.h")));
262

263
    Project *project = helper.createProject(
264
                _("test_modelmanager_refresh_also_includes_of_project_files"));
265
    ProjectInfo pi = ProjectInfo(project);
266

267 268
    typedef ProjectPart::HeaderPath HeaderPath;

269
    ProjectPart::Ptr part(new ProjectPart);
270
    part->languageVersion = ProjectPart::CXX14;
271
    part->qtVersion = ProjectPart::Qt5;
272
    part->projectDefines = QByteArray("#define OH_BEHAVE -1\n");
273 274
    part->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDir.includeDir(false), HeaderPath::IncludePath);
275
    part->files.append(ProjectFile(testCpp, ProjectFile::CXXSource));
276
    pi.appendProjectPart(part);
277
    pi.finish();
278

279
    QSet<QString> refreshedFiles = updateProjectInfo(mm, &helper, pi);
280 281 282 283 284 285
    QCOMPARE(refreshedFiles.size(), 1);
    QVERIFY(refreshedFiles.contains(testCpp));
    CPlusPlus::Snapshot snapshot = mm->snapshot();
    QVERIFY(snapshot.contains(testHeader));
    QVERIFY(snapshot.contains(testCpp));

286 287 288 289
    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");
290

291
    // Introduce a define that will enable another define once the document is reparsed.
292
    part->projectDefines = QByteArray("#define TEST_DEFINE 1\n");
293
    pi = ProjectInfo(project);
294
    pi.appendProjectPart(part);
295
    pi.finish();
296

297
    refreshedFiles = updateProjectInfo(mm, &helper, pi);
298 299 300 301 302 303

    QCOMPARE(refreshedFiles.size(), 1);
    QVERIFY(refreshedFiles.contains(testCpp));
    snapshot = mm->snapshot();
    QVERIFY(snapshot.contains(testHeader));
    QVERIFY(snapshot.contains(testCpp));
304 305 306 307 308 309

    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");
310
}
311 312

/// QTCREATORBUG-9205
313
/// Check: When reparsing the same files again, no errors occur
314
///        (The CppSourceProcessor's already seen files are properly cleared!).
315
void CppToolsPlugin::test_modelmanager_refresh_several_times()
316 317 318 319
{
    ModelManagerTestHelper helper;
    CppModelManager *mm = CppModelManager::instance();

320
    const MyTestDataDir testDataDir(_("testdata_refresh"));
321

322 323 324
    const QString testHeader1(testDataDir.file(_("defines.h")));
    const QString testHeader2(testDataDir.file(_("header.h")));
    const QString testCpp(testDataDir.file(_("source.cpp")));
325

326
    Project *project = helper.createProject(_("test_modelmanager_refresh_several_times"));
327
    ProjectInfo pi = ProjectInfo(project);
328 329

    ProjectPart::Ptr part(new ProjectPart);
330
    part->languageVersion = ProjectPart::CXX14;
331 332 333 334
    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));
335
    pi.appendProjectPart(part);
336
    pi.finish();
337 338 339
    mm->updateProjectInfo(pi);

    CPlusPlus::Snapshot snapshot;
340
    QSet<QString> refreshedFiles;
341 342
    CPlusPlus::Document::Ptr document;

343
    QByteArray defines = "#define FIRST_DEFINE";
344
    for (int i = 0; i < 2; ++i) {
345
        pi = ProjectInfo(project);
346 347 348
        ProjectPart::Ptr part(new ProjectPart);
        // Simulate project configuration change by having different defines each time.
        defines += "\n#define ANOTHER_DEFINE";
349
        part->projectDefines = defines;
350
        part->languageVersion = ProjectPart::CXX14;
351 352 353 354 355
        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);
356
        pi.finish();
357

358
        refreshedFiles = updateProjectInfo(mm, &helper, pi);
359
        QCOMPARE(refreshedFiles.size(), 3);
360

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
        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());
    }
}
381

382 383 384 385 386 387 388
/// 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();

389 390
    const MyTestDataDir testDataDir(_("testdata_refresh"));
    const QString testCpp(testDataDir.file(_("source.cpp")));
391

392
    Project *project = helper.createProject(_("test_modelmanager_refresh_2"));
393
    ProjectInfo pi = ProjectInfo(project);
394 395

    ProjectPart::Ptr part(new ProjectPart);
396
    part->languageVersion = ProjectPart::CXX14;
397 398 399
    part->qtVersion = ProjectPart::Qt5;
    part->files.append(ProjectFile(testCpp, ProjectFile::CXXSource));
    pi.appendProjectPart(part);
400
    pi.finish();
401 402

    // Reindexing triggers a reparsing thread
403
    helper.resetRefreshedSourceFiles();
404 405
    QFuture<void> firstFuture = mm->updateProjectInfo(pi);
    QVERIFY(firstFuture.isStarted() || firstFuture.isRunning());
406
    firstFuture.waitForFinished();
407
    const QSet<QString> refreshedFiles = helper.waitForRefreshedSourceFiles();
408 409 410 411 412 413 414 415
    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());
}

416 417 418 419 420 421 422
/// 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();

423
    const MyTestDataDir testDataDir(_("testdata_refresh"));
424

425 426 427
    const QString testHeader1(testDataDir.file(_("header.h")));
    const QString testHeader2(testDataDir.file(_("defines.h")));
    const QString testCpp(testDataDir.file(_("source.cpp")));
428

429
    Project *project = helper.createProject(_("test_modelmanager_refresh_3"));
430
    ProjectInfo pi = ProjectInfo(project);
431 432

    ProjectPart::Ptr part(new ProjectPart);
433
    part->languageVersion = ProjectPart::CXX14;
434 435 436 437
    part->qtVersion = ProjectPart::Qt5;
    part->files.append(ProjectFile(testCpp, ProjectFile::CXXSource));
    part->files.append(ProjectFile(testHeader1, ProjectFile::CXXHeader));
    pi.appendProjectPart(part);
438
    pi.finish();
439 440

    CPlusPlus::Snapshot snapshot;
441
    QSet<QString> refreshedFiles;
442

443
    refreshedFiles = updateProjectInfo(mm, &helper, pi);
444 445 446 447 448 449 450 451 452 453

    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
454
    pi = ProjectInfo(project);
455
    ProjectPart::Ptr newPart(new ProjectPart);
456
    newPart->languageVersion = ProjectPart::CXX14;
457 458 459 460
    newPart->qtVersion = ProjectPart::Qt5;
    newPart->files.append(ProjectFile(testCpp, ProjectFile::CXXSource));
    newPart->files.append(ProjectFile(testHeader2, ProjectFile::CXXHeader));
    pi.appendProjectPart(newPart);
461
    pi.finish();
462

463
    refreshedFiles = updateProjectInfo(mm, &helper, pi);
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

    // 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);
    QFETCH(QList<ProjectFile>, initialProjectFiles);
    QFETCH(QList<ProjectFile>, finalProjectFiles);

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

487
    Project *project = helper.createProject(_("test_modelmanager_refresh_timeStampModified"));
488
    ProjectInfo pi = ProjectInfo(project);
489 490

    ProjectPart::Ptr part(new ProjectPart);
491
    part->languageVersion = ProjectPart::CXX14;
492 493 494
    part->qtVersion = ProjectPart::Qt5;
    foreach (const ProjectFile &file, initialProjectFiles)
        part->files.append(file);
495
    pi = ProjectInfo(project);
496
    pi.appendProjectPart(part);
497
    pi.finish();
498 499 500

    Document::Ptr document;
    CPlusPlus::Snapshot snapshot;
501
    QSet<QString> refreshedFiles;
502

503
    refreshedFiles = updateProjectInfo(mm, &helper, pi);
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

    QCOMPARE(refreshedFiles.size(), initialProjectFiles.size());
    snapshot = mm->snapshot();
    foreach (const ProjectFile &file, initialProjectFiles) {
        QVERIFY(refreshedFiles.contains(file.path));
        QVERIFY(snapshot.contains(file.path));
    }

    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;";
523
    QVERIFY(fileChangerAndRestorer.writeContents(newFileContentes));
524 525 526 527 528

    // Add or remove source file. The configuration stays the same.
    part->files.clear();
    foreach (const ProjectFile &file, finalProjectFiles)
        part->files.append(file);
529
    pi = ProjectInfo(project);
530
    pi.appendProjectPart(part);
531
    pi.finish();
532

533
    refreshedFiles = updateProjectInfo(mm, &helper, pi);
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554

    QCOMPARE(refreshedFiles.size(), finalProjectFiles.size());
    snapshot = mm->snapshot();
    foreach (const ProjectFile &file, finalProjectFiles) {
        QVERIFY(refreshedFiles.contains(file.path));
        QVERIFY(snapshot.contains(file.path));
    }
    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");
    QTest::addColumn<QList<ProjectFile> >("initialProjectFiles");
    QTest::addColumn<QList<ProjectFile> >("finalProjectFiles");

555 556 557
    const MyTestDataDir testDataDir(_("testdata_refresh2"));
    const QString testCpp(testDataDir.file(_("source.cpp")));
    const QString testCpp2(testDataDir.file(_("source2.cpp")));
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

    const QString fileToChange = testCpp;
    QList<ProjectFile> projectFiles1 = QList<ProjectFile>()
        << ProjectFile(testCpp, ProjectFile::CXXSource);
    QList<ProjectFile> projectFiles2 = QList<ProjectFile>()
        << ProjectFile(testCpp, ProjectFile::CXXSource)
        << ProjectFile(testCpp2, ProjectFile::CXXSource);

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

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

573 574
/// Check: If a second project is opened, the code model is still aware of
///        files of the first project.
575 576
void CppToolsPlugin::test_modelmanager_snapshot_after_two_projects()
{
577
    QSet<QString> refreshedFiles;
578 579 580 581 582 583
    ModelManagerTestHelper helper;
    ProjectCreator project1(&helper);
    ProjectCreator project2(&helper);
    CppModelManager *mm = CppModelManager::instance();

    // Project 1
584 585 586 587 588
    project1.create(_("test_modelmanager_snapshot_after_two_projects.1"),
                    _("testdata_project1"),
                    QStringList() << _("foo.h")
                                  << _("foo.cpp")
                                  << _("main.cpp"));
589

590
    refreshedFiles = updateProjectInfo(mm, &helper, project1.projectInfo);
591
    QCOMPARE(refreshedFiles, project1.projectFiles.toSet());
592 593 594 595 596 597
    const int snapshotSizeAfterProject1 = mm->snapshot().size();

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

    // Project 2
598 599 600 601 602
    project2.create(_("test_modelmanager_snapshot_after_two_projects.2"),
                    _("testdata_project2"),
                    QStringList() << _("bar.h")
                                  << _("bar.cpp")
                                  << _("main.cpp"));
603

604
    refreshedFiles = updateProjectInfo(mm, &helper, project2.projectInfo);
605
    QCOMPARE(refreshedFiles, project2.projectFiles.toSet());
606 607 608 609 610 611 612 613 614 615

    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));
}
616

617 618
/// Check: (1) For a project with a *.ui file an AbstractEditorSupport object
///            is added for the ui_* file.
619
/// Check: (2) The CppSourceProcessor can successfully resolve the ui_* file
620
///            though it might not be actually generated in the build dir.
621 622
///

623 624
void CppToolsPlugin::test_modelmanager_extraeditorsupport_uiFiles()
{
625
    VerifyCleanCppModelManager verify;
626

627 628 629
    TemporaryCopiedDir temporaryDir(MyTestDataDir(QLatin1String("testdata_guiproject1")).path());
    QVERIFY(temporaryDir.isValid());
    const QString projectFile = temporaryDir.absolutePath("testdata_guiproject1.pro");
630

631
    ProjectOpenerAndCloser projects;
632 633
    ProjectInfo projectInfo = projects.open(projectFile, /*configureAsExampleProject=*/ true);
    QVERIFY(projectInfo.isValid());
634 635 636

    // Check working copy.
    // An AbstractEditorSupport object should have been added for the ui_* file.
637
    CppModelManager *mm = CppModelManager::instance();
638
    WorkingCopy workingCopy = mm->workingCopy();
639 640 641 642

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

    QStringList fileNamesInWorkinCopy;
643
    QHashIterator<Utils::FileName, QPair<QByteArray, unsigned> > it = workingCopy.iterator();
644 645
    while (it.hasNext()) {
        it.next();
646
        fileNamesInWorkinCopy << QFileInfo(it.key().toString()).fileName();
647 648
    }
    fileNamesInWorkinCopy.sort();
649
    const QString expectedUiHeaderFileName = _("ui_mainwindow.h");
650 651 652
    QCOMPARE(fileNamesInWorkinCopy.at(0), mm->configurationFileName());
    QCOMPARE(fileNamesInWorkinCopy.at(1), expectedUiHeaderFileName);

653 654
    // Check CppSourceProcessor / includes.
    // The CppSourceProcessor is expected to find the ui_* file in the working copy.
655
    const QString fileIncludingTheUiFile = temporaryDir.absolutePath("mainwindow.cpp");
656 657 658 659 660 661 662 663
    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);
664 665
    QCOMPARE(QFileInfo(includedFiles.at(0)).fileName(), _("mainwindow.h"));
    QCOMPARE(QFileInfo(includedFiles.at(1)).fileName(), _("ui_mainwindow.h"));
666
}
667 668 669 670 671

/// 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()
{
672 673
    ModelManagerTestHelper helper;

674 675
    MyTestDataDir testDataDirectory(_("testdata_guiproject1"));
    const QString file = testDataDirectory.file(_("main.cpp"));
676 677

    CppModelManager *mm = CppModelManager::instance();
678
    helper.resetRefreshedSourceFiles();
679 680

    // Open a file in the editor
hjk's avatar
hjk committed
681
    QCOMPARE(Core::DocumentModel::openedDocuments().size(), 0);
682
    Core::IEditor *editor = Core::EditorManager::openEditor(file);
683
    QVERIFY(editor);
hjk's avatar
hjk committed
684
    QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
685 686 687
    QVERIFY(mm->isCppEditor(editor));
    QVERIFY(mm->workingCopy().contains(file));

688 689
    // Wait until the file is refreshed
    helper.waitForRefreshedSourceFiles();
690 691

    // Close file/editor
692
    Core::EditorManager::closeDocument(editor->document(), /*askAboutModifiedEditors=*/ false);
693
    helper.waitForFinishedGc();
694 695 696 697 698

    // Check: File is removed from the snapshpt
    QVERIFY(!mm->workingCopy().contains(file));
    QVERIFY(!mm->snapshot().contains(file));
}
699 700 701 702 703 704

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

705 706
    MyTestDataDir testDataDirectory(_("testdata_guiproject1"));
    const QString file = testDataDirectory.file(_("main.cpp"));
707 708

    CppModelManager *mm = CppModelManager::instance();
709
    helper.resetRefreshedSourceFiles();
710 711

    // Open a file in the editor
hjk's avatar
hjk committed
712
    QCOMPARE(Core::DocumentModel::openedDocuments().size(), 0);
713
    Core::IEditor *editor = Core::EditorManager::openEditor(file);
714
    QVERIFY(editor);
hjk's avatar
hjk committed
715
    QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
716 717
    QVERIFY(mm->isCppEditor(editor));

718 719
    // Wait until the file is refreshed and check whether it is in the working copy
    helper.waitForRefreshedSourceFiles();
720

721 722 723 724 725 726 727 728 729 730
    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
731
    Core::EditorManager::closeDocument(editor->document());
732 733 734
    helper.waitForFinishedGc();
    QVERIFY(mm->snapshot().isEmpty());
}
735 736 737 738 739 740 741

namespace {
struct EditorCloser {
    Core::IEditor *editor;
    EditorCloser(Core::IEditor *editor): editor(editor) {}
    ~EditorCloser()
    {
742
        using namespace CppTools;
743
        if (editor)
744
            QVERIFY(Tests::TestCase::closeEditorWithoutGarbageCollectorInvocation(editor));
745 746
    }
};
747 748 749 750 751 752 753 754

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())
755
                        return QString::fromUtf8(identifier->chars(), identifier->size());
756 757 758 759 760 761
                }
            }
        }
    }
    return QString();
}
762 763 764 765 766 767
}

void CppToolsPlugin::test_modelmanager_defines_per_project()
{
    ModelManagerTestHelper helper;

768 769 770 771
    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"));
772 773 774

    CppModelManager *mm = CppModelManager::instance();

775
    Project *project = helper.createProject(_("test_modelmanager_defines_per_project"));
776

777 778
    typedef ProjectPart::HeaderPath HeaderPath;

779
    ProjectPart::Ptr part1(new ProjectPart);
780
    part1->projectFile = QLatin1String("project1.projectfile");
781 782
    part1->files.append(ProjectFile(main1File, ProjectFile::CXXSource));
    part1->files.append(ProjectFile(header, ProjectFile::CXXHeader));
783
    part1->languageVersion = ProjectPart::CXX11;
784
    part1->qtVersion = ProjectPart::NoQt;
785
    part1->projectDefines = QByteArray("#define SUB1\n");
786 787
    part1->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDirectory.includeDir(false), HeaderPath::IncludePath);
788 789

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

799
    ProjectInfo pi = ProjectInfo(project);
800 801
    pi.appendProjectPart(part1);
    pi.appendProjectPart(part2);
802
    pi.finish();
803

804
    updateProjectInfo(mm, &helper, pi);
805 806 807
    QCOMPARE(mm->snapshot().size(), 4);

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

810 811 812 813 814 815 816 817 818 819 820 821 822
    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);
823 824
        EditorCloser closer(editor);
        QVERIFY(editor);
hjk's avatar
hjk committed
825
        QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
826 827
        QVERIFY(mm->isCppEditor(editor));

828
        Document::Ptr doc = mm->document(fileName);
829
        QCOMPARE(nameOfFirstDeclaration(doc), firstDeclarationName);
830 831
    }
}
832

833
void CppToolsPlugin::test_modelmanager_precompiled_headers()
834 835 836
{
    ModelManagerTestHelper helper;

837 838 839 840 841 842
    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"));
843 844 845

    CppModelManager *mm = CppModelManager::instance();

846
    Project *project = helper.createProject(_("test_modelmanager_defines_per_project_pch"));
847

848 849
    typedef ProjectPart::HeaderPath HeaderPath;

850
    ProjectPart::Ptr part1(new ProjectPart);
851
    part1->projectFile = QLatin1String("project1.projectfile");
852 853
    part1->files.append(ProjectFile(main1File, ProjectFile::CXXSource));
    part1->files.append(ProjectFile(header, ProjectFile::CXXHeader));
854
    part1->languageVersion = ProjectPart::CXX11;
855 856
    part1->qtVersion = ProjectPart::NoQt;
    part1->precompiledHeaders.append(pch1File);
857 858
    part1->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDirectory.includeDir(false), HeaderPath::IncludePath);
859 860

    ProjectPart::Ptr part2(new ProjectPart);
861
    part2->projectFile = QLatin1String("project2.projectfile");
862 863
    part2->files.append(ProjectFile(main2File, ProjectFile::CXXSource));
    part2->files.append(ProjectFile(header, ProjectFile::CXXHeader));
864
    part2->languageVersion = ProjectPart::CXX11;
865 866
    part2->qtVersion = ProjectPart::NoQt;
    part2->precompiledHeaders.append(pch2File);
867 868
    part2->headerPaths = QList<HeaderPath>()
            << HeaderPath(testDataDirectory.includeDir(false), HeaderPath::IncludePath);
869

870
    ProjectInfo pi = ProjectInfo(project);
871 872
    pi.appendProjectPart(part1);
    pi.appendProjectPart(part2);
873
    pi.finish();
874

875
    updateProjectInfo(mm, &helper, pi);
876 877 878
    QCOMPARE(mm->snapshot().size(), 4);

    // Open a file in the editor