1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "quicktestutil_p.h"
5
6#include <QtQuickTest/private/qtestoptions_p.h>
7#include <QtQml/private/qqmltype_p.h>
8#include <QtQml/private/qqmlmetatype_p.h>
9#include <QtQml/private/qv4engine_p.h>
10#include <QtQml/private/qv4scopedvalue_p.h>
11#include <QtQml/private/qjsvalue_p.h>
12
13#include <QtGui/qguiapplication.h>
14#include <QtGui/qclipboard.h>
15#include <QtGui/qstylehints.h>
16#include <QtQml/qqmlengine.h>
17
18QT_BEGIN_NAMESPACE
19
20using namespace Qt::StringLiterals;
21
22bool QuickTestUtil::printAvailableFunctions() const
23{
24 return QTest::printAvailableFunctions;
25}
26
27int QuickTestUtil::dragThreshold() const
28{
29 return QGuiApplication::styleHints()->startDragDistance();
30}
31
32void QuickTestUtil::populateClipboardText(int lineCount)
33{
34#if QT_CONFIG(clipboard)
35 QString fmt(u"%1 bottles of beer on the wall, %1 bottles of beer; "
36 "take one down, pass it around, %2 bottles of beer on the wall."_s);
37 QStringList lines;
38 for (int i = lineCount; i > 0; --i)
39 lines << fmt.arg(a: i).arg(a: i - 1);
40 QGuiApplication::clipboard()->setText(lines.join(sep: u'\n'));
41#else
42 Q_UNUSED(lineCount)
43#endif
44}
45
46QJSValue QuickTestUtil::typeName(const QVariant &v) const
47{
48 QString name = QString::fromUtf8(utf8: v.typeName());
49 if (v.canConvert<QObject*>()) {
50 QQmlType type;
51 const QMetaObject *mo = v.value<QObject*>()->metaObject();
52 while (!type.isValid() && mo) {
53 type = QQmlMetaType::qmlType(mo);
54 mo = mo->superClass();
55 }
56 if (type.isValid()) {
57 name = type.qmlTypeName();
58 }
59 }
60
61 QQmlEngine *engine = qmlEngine(this);
62 QV4::ExecutionEngine *v4 = engine->handle();
63 return QJSValuePrivate::fromReturnedValue(d: v4->newString(s: name)->asReturnedValue());
64}
65
66bool QuickTestUtil::compare(const QVariant &act, const QVariant &exp) const {
67 return act == exp;
68}
69
70QJSValue QuickTestUtil::callerFile(int frameIndex) const
71{
72 QQmlEngine *engine = qmlEngine(this);
73 QV4::ExecutionEngine *v4 = engine->handle();
74 QV4::Scope scope(v4);
75
76 QVector<QV4::StackFrame> stack = v4->stackTrace(frameLimit: frameIndex + 2);
77 return (stack.size() > frameIndex + 1)
78 ? QJSValuePrivate::fromReturnedValue(
79 d: v4->newString(s: stack.at(i: frameIndex + 1).source)->asReturnedValue())
80 : QJSValue();
81}
82
83int QuickTestUtil::callerLine(int frameIndex) const
84{
85 QQmlEngine *engine = qmlEngine(this);
86 QV4::ExecutionEngine *v4 = engine->handle();
87
88 QVector<QV4::StackFrame> stack = v4->stackTrace(frameLimit: frameIndex + 2);
89 if (stack.size() > frameIndex + 1)
90 return qAbs(t: stack.at(i: frameIndex + 1).line);
91 return -1;
92}
93
94QT_END_NAMESPACE
95
96#include "moc_quicktestutil_p.cpp"
97

source code of qtdeclarative/src/qmltest/quicktestutil.cpp