2013-07-24 17:21:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2013-07-24 17:21:13 +02:00
|
|
|
** 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 "formeditorplugin.h"
|
|
|
|
|
|
2013-08-22 13:29:46 +02:00
|
|
|
#if QT_VERSION < 0x050000
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
#else
|
2013-07-24 17:21:13 +02:00
|
|
|
#include "formeditorw.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2013-12-16 16:02:45 +01:00
|
|
|
#include <coreplugin/testdatadir.h>
|
2013-07-24 17:21:13 +02:00
|
|
|
#include <cpptools/cppmodelmanager.h>
|
2013-11-13 14:31:50 +01:00
|
|
|
#include <cpptools/cpptoolseditorsupport.h>
|
2013-12-16 16:02:45 +01:00
|
|
|
#include <cpptools/cpptoolstestcase.h>
|
2013-07-24 17:21:13 +02:00
|
|
|
|
|
|
|
|
#include <cplusplus/CppDocument.h>
|
|
|
|
|
#include <cplusplus/Overview.h>
|
2013-11-21 15:48:23 +01:00
|
|
|
#include <utils/fileutils.h>
|
2013-07-24 17:21:13 +02:00
|
|
|
|
|
|
|
|
#include <QDesignerFormEditorInterface>
|
|
|
|
|
#include <QDesignerIntegrationInterface>
|
|
|
|
|
#include <QStringList>
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
|
|
|
|
|
using namespace Core;
|
2013-12-19 01:30:49 +01:00
|
|
|
using namespace Core::Tests;
|
2013-07-24 17:21:13 +02:00
|
|
|
using namespace CppTools;
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
using namespace Designer;
|
|
|
|
|
using namespace Designer::Internal;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2013-12-16 16:02:45 +01:00
|
|
|
QTC_DECLARE_MYTESTDATADIR("../../../tests/designer/")
|
2013-07-24 17:21:13 +02:00
|
|
|
|
2013-11-26 09:48:05 +01:00
|
|
|
class DocumentContainsFunctionDefinition: protected SymbolVisitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
bool operator()(Scope *scope, const QString function)
|
|
|
|
|
{
|
|
|
|
|
if (!scope)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
m_referenceFunction = function;
|
|
|
|
|
m_result = false;
|
|
|
|
|
|
|
|
|
|
accept(scope);
|
|
|
|
|
return m_result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
bool preVisit(Symbol *) { return !m_result; }
|
|
|
|
|
|
|
|
|
|
bool visit(Function *symbol)
|
|
|
|
|
{
|
|
|
|
|
const QString function = m_overview.prettyName(symbol->name());
|
|
|
|
|
if (function == m_referenceFunction)
|
|
|
|
|
m_result = true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool m_result;
|
|
|
|
|
QString m_referenceFunction;
|
|
|
|
|
Overview m_overview;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class DocumentContainsDeclaration: protected SymbolVisitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
bool operator()(Scope *scope, const QString function)
|
|
|
|
|
{
|
|
|
|
|
if (!scope)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
m_referenceFunction = function;
|
|
|
|
|
m_result = false;
|
|
|
|
|
|
|
|
|
|
accept(scope);
|
|
|
|
|
return m_result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
bool preVisit(Symbol *) { return !m_result; }
|
|
|
|
|
|
|
|
|
|
void postVisit(Symbol *symbol)
|
|
|
|
|
{
|
|
|
|
|
if (symbol->isClass())
|
|
|
|
|
m_currentClass.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(Class *symbol)
|
|
|
|
|
{
|
|
|
|
|
m_currentClass = m_overview.prettyName(symbol->name());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(Declaration *symbol)
|
|
|
|
|
{
|
|
|
|
|
QString declaration = m_overview.prettyName(symbol->name());
|
|
|
|
|
if (!m_currentClass.isEmpty())
|
|
|
|
|
declaration = m_currentClass + QLatin1String("::") + declaration;
|
|
|
|
|
if (m_referenceFunction == declaration)
|
|
|
|
|
m_result = true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool m_result;
|
|
|
|
|
QString m_referenceFunction;
|
|
|
|
|
QString m_currentClass;
|
|
|
|
|
Overview m_overview;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool documentContainsFunctionDefinition(const Document::Ptr &document, const QString function)
|
|
|
|
|
{
|
|
|
|
|
return DocumentContainsFunctionDefinition()(document->globalNamespace(), function);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool documentContainsMemberFunctionDeclaration(const Document::Ptr &document,
|
|
|
|
|
const QString declaration)
|
|
|
|
|
{
|
|
|
|
|
return DocumentContainsDeclaration()(document->globalNamespace(), declaration);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-19 01:30:49 +01:00
|
|
|
class GoToSlotTestCase : public CppTools::Tests::TestCase
|
2013-07-24 17:21:13 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2013-12-19 01:30:49 +01:00
|
|
|
GoToSlotTestCase(const QStringList &files)
|
2013-11-21 15:48:23 +01:00
|
|
|
{
|
2013-12-30 19:44:42 +01:00
|
|
|
QVERIFY(succeededSoFar());
|
2013-11-21 15:48:23 +01:00
|
|
|
QCOMPARE(files.size(), 3);
|
2013-07-24 17:21:13 +02:00
|
|
|
|
2013-11-13 14:31:50 +01:00
|
|
|
QList<TextEditor::BaseTextEditor *> editors;
|
2013-12-30 19:44:42 +01:00
|
|
|
foreach (const QString &file, files) {
|
2013-11-13 14:31:50 +01:00
|
|
|
IEditor *editor = EditorManager::openEditor(file);
|
|
|
|
|
TextEditor::BaseTextEditor *e = qobject_cast<TextEditor::BaseTextEditor *>(editor);
|
|
|
|
|
QVERIFY(e);
|
2013-12-16 16:02:45 +01:00
|
|
|
closeEditorAtEndOfTestCase(editor);
|
2013-11-13 14:31:50 +01:00
|
|
|
editors << e;
|
|
|
|
|
}
|
2013-11-21 15:48:23 +01:00
|
|
|
TextEditor::BaseTextEditor *cppFileEditor = editors.at(0);
|
|
|
|
|
TextEditor::BaseTextEditor *hFileEditor = editors.at(1);
|
|
|
|
|
|
2013-12-30 19:44:42 +01:00
|
|
|
const QString cppFile = files.at(0);
|
|
|
|
|
const QString hFile = files.at(1);
|
2013-11-21 15:48:23 +01:00
|
|
|
|
2013-12-30 19:44:42 +01:00
|
|
|
QCOMPARE(EditorManager::documentModel()->openedDocuments().size(), files.size());
|
2013-12-16 16:02:45 +01:00
|
|
|
waitForFilesInGlobalSnapshot(QStringList() << cppFile << hFile);
|
2013-07-24 17:21:13 +02:00
|
|
|
|
|
|
|
|
// Execute "Go To Slot"
|
|
|
|
|
FormEditorW *few = FormEditorW::instance();
|
|
|
|
|
QDesignerIntegrationInterface *integration = few->designerEditor()->integration();
|
|
|
|
|
QVERIFY(integration);
|
|
|
|
|
integration->emitNavigateToSlot(QLatin1String("pushButton"), QLatin1String("clicked()"),
|
|
|
|
|
QStringList());
|
|
|
|
|
|
|
|
|
|
QCOMPARE(EditorManager::currentDocument()->filePath(), cppFile);
|
|
|
|
|
QVERIFY(EditorManager::currentDocument()->isModified());
|
|
|
|
|
|
2013-11-13 14:31:50 +01:00
|
|
|
// Wait for updated documents
|
|
|
|
|
foreach (TextEditor::BaseTextEditor *editor, editors) {
|
|
|
|
|
if (CppEditorSupport *editorSupport = m_modelManager->cppEditorSupport(editor)) {
|
|
|
|
|
while (editorSupport->isUpdatingDocument())
|
|
|
|
|
QApplication::processEvents();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-21 15:48:23 +01:00
|
|
|
// Compare
|
2013-11-26 09:48:05 +01:00
|
|
|
const Document::Ptr cppDocument
|
|
|
|
|
= m_modelManager->cppEditorSupport(cppFileEditor)->snapshotUpdater()->document();
|
2014-04-03 14:37:57 -04:00
|
|
|
QVERIFY(checkDiagsnosticMessages(cppDocument));
|
2013-11-26 09:48:05 +01:00
|
|
|
const Document::Ptr hDocument
|
|
|
|
|
= m_modelManager->cppEditorSupport(hFileEditor)->snapshotUpdater()->document();
|
2014-04-03 14:37:57 -04:00
|
|
|
QVERIFY(checkDiagsnosticMessages(hDocument));
|
|
|
|
|
|
2013-11-26 09:48:05 +01:00
|
|
|
QVERIFY(documentContainsFunctionDefinition(cppDocument,
|
|
|
|
|
QLatin1String("Form::on_pushButton_clicked")));
|
|
|
|
|
QVERIFY(documentContainsMemberFunctionDeclaration(hDocument,
|
|
|
|
|
QLatin1String("Form::on_pushButton_clicked")));
|
2013-07-24 17:21:13 +02:00
|
|
|
}
|
2014-04-03 14:37:57 -04:00
|
|
|
|
|
|
|
|
static bool checkDiagsnosticMessages(const Document::Ptr &document)
|
|
|
|
|
{
|
|
|
|
|
if (!document)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Since no project is opened and the ui_*.h is not generated,
|
|
|
|
|
// the following diagnostic messages will be ignored.
|
|
|
|
|
const QStringList ignoreList = QStringList()
|
|
|
|
|
<< QLatin1String("ui_form.h: No such file or directory")
|
|
|
|
|
<< QLatin1String("QWidget: No such file or directory");
|
|
|
|
|
QList<Document::DiagnosticMessage> cleanedDiagnosticMessages;
|
|
|
|
|
foreach (const Document::DiagnosticMessage &message, document->diagnosticMessages()) {
|
|
|
|
|
if (!ignoreList.contains(message.text()))
|
|
|
|
|
cleanedDiagnosticMessages << message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cleanedDiagnosticMessages.isEmpty();
|
|
|
|
|
}
|
2013-07-24 17:21:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
2013-08-22 13:29:46 +02:00
|
|
|
#endif
|
2013-07-24 17:21:13 +02:00
|
|
|
|
|
|
|
|
/// Check: Executes "Go To Slot..." on a QPushButton in a *.ui file and checks if the respective
|
2013-11-20 17:24:07 +01:00
|
|
|
/// header and source files are correctly updated.
|
2013-11-21 15:48:23 +01:00
|
|
|
void Designer::Internal::FormEditorPlugin::test_gotoslot()
|
2013-07-24 17:21:13 +02:00
|
|
|
{
|
2013-08-22 13:29:46 +02:00
|
|
|
#if QT_VERSION >= 0x050000
|
2013-11-21 15:48:23 +01:00
|
|
|
QFETCH(QStringList, files);
|
2013-12-30 19:44:42 +01:00
|
|
|
(GoToSlotTestCase(files));
|
2013-08-22 13:29:46 +02:00
|
|
|
#else
|
|
|
|
|
QSKIP("Available only with >= Qt5", SkipSingle);
|
|
|
|
|
#endif
|
2013-07-24 17:21:13 +02:00
|
|
|
}
|
2013-11-21 15:48:23 +01:00
|
|
|
|
2013-11-26 08:55:41 +01:00
|
|
|
void Designer::Internal::FormEditorPlugin::test_gotoslot_data()
|
2013-11-21 15:48:23 +01:00
|
|
|
{
|
2013-11-26 08:55:41 +01:00
|
|
|
#if QT_VERSION >= 0x050000
|
2013-11-21 15:48:23 +01:00
|
|
|
typedef QLatin1String _;
|
|
|
|
|
QTest::addColumn<QStringList>("files");
|
|
|
|
|
|
2013-11-20 17:24:07 +01:00
|
|
|
MyTestDataDir testDataDirWithoutProject(_("gotoslot_withoutProject"));
|
2013-11-21 15:48:23 +01:00
|
|
|
QTest::newRow("withoutProject")
|
|
|
|
|
<< (QStringList()
|
2013-11-20 17:24:07 +01:00
|
|
|
<< testDataDirWithoutProject.file(_("form.cpp"))
|
|
|
|
|
<< testDataDirWithoutProject.file(_("form.h"))
|
|
|
|
|
<< testDataDirWithoutProject.file(_("form.ui")));
|
|
|
|
|
|
|
|
|
|
// Finding the right class for inserting definitions/declarations is based on
|
|
|
|
|
// finding a class with a member whose type is the class from the "ui_xxx.h" header.
|
|
|
|
|
// In the following test data the header files contain an extra class referencing
|
|
|
|
|
// the same class name.
|
|
|
|
|
|
|
|
|
|
MyTestDataDir testDataDir;
|
|
|
|
|
|
|
|
|
|
testDataDir = MyTestDataDir(_("gotoslot_insertIntoCorrectClass_pointer"));
|
|
|
|
|
QTest::newRow("insertIntoCorrectClass_pointer")
|
|
|
|
|
<< (QStringList()
|
|
|
|
|
<< testDataDir.file(_("form.cpp"))
|
|
|
|
|
<< testDataDir.file(_("form.h"))
|
|
|
|
|
<< testDataDirWithoutProject.file(_("form.ui"))); // reuse
|
|
|
|
|
|
|
|
|
|
testDataDir = MyTestDataDir(_("gotoslot_insertIntoCorrectClass_non-pointer"));
|
|
|
|
|
QTest::newRow("insertIntoCorrectClass_non-pointer")
|
|
|
|
|
<< (QStringList()
|
|
|
|
|
<< testDataDir.file(_("form.cpp"))
|
|
|
|
|
<< testDataDir.file(_("form.h"))
|
|
|
|
|
<< testDataDirWithoutProject.file(_("form.ui"))); // reuse
|
|
|
|
|
|
|
|
|
|
testDataDir = MyTestDataDir(_("gotoslot_insertIntoCorrectClass_pointer_ns_using"));
|
|
|
|
|
QTest::newRow("insertIntoCorrectClass_pointer_ns_using")
|
|
|
|
|
<< (QStringList()
|
|
|
|
|
<< testDataDir.file(_("form.cpp"))
|
|
|
|
|
<< testDataDir.file(_("form.h"))
|
|
|
|
|
<< testDataDir.file(_("form.ui")));
|
2013-11-26 08:55:41 +01:00
|
|
|
#endif
|
2013-11-21 15:48:23 +01:00
|
|
|
}
|