Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
quicktestutil.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
6
7#include <QtQuickTest/private/qtestoptions_p.h>
8#include <QtQml/private/qqmltype_p.h>
9#include <QtQml/private/qqmlmetatype_p.h>
10#include <QtQml/private/qv4engine_p.h>
11#include <QtQml/private/qv4scopedvalue_p.h>
12#include <QtQml/private/qjsvalue_p.h>
13
14#include <QtGui/qguiapplication.h>
15#include <QtGui/qclipboard.h>
16#include <QtGui/qstylehints.h>
17#include <QtQml/qqmlengine.h>
18
20
21using namespace Qt::StringLiterals;
22
23bool QuickTestUtil::printAvailableFunctions() const
24{
25 return QTest::printAvailableFunctions;
26}
27
28int QuickTestUtil::dragThreshold() const
29{
30 return QGuiApplication::styleHints()->startDragDistance();
31}
32
33void QuickTestUtil::populateClipboardText(int lineCount)
34{
35#if QT_CONFIG(clipboard)
36 QString fmt(u"%1 bottles of beer on the wall, %1 bottles of beer; "
37 "take one down, pass it around, %2 bottles of beer on the wall."_s);
38 QStringList lines;
39 for (int i = lineCount; i > 0; --i)
40 lines << fmt.arg(i).arg(i - 1);
41 QGuiApplication::clipboard()->setText(lines.join(u'\n'));
42#else
43 Q_UNUSED(lineCount)
44#endif
45}
46
47QJSValue QuickTestUtil::typeName(const QVariant &v) const
48{
49 QString name = QString::fromUtf8(v.typeName());
50 if (v.canConvert<QObject*>()) {
51 QQmlType type;
52 const QMetaObject *mo = v.value<QObject*>()->metaObject();
53 while (!type.isValid() && mo) {
54 type = QQmlMetaType::qmlType(mo);
55 mo = mo->superClass();
56 }
57 if (type.isValid()) {
58 name = type.qmlTypeName();
59 }
60 }
61
62 QQmlEngine *engine = qmlEngine(this);
63 QV4::ExecutionEngine *v4 = engine->handle();
64 return QJSValuePrivate::fromReturnedValue(v4->newString(name)->asReturnedValue());
65}
66
67bool QuickTestUtil::compare(const QVariant &act, const QVariant &exp) const {
68 return act == exp;
69}
70
71QJSValue QuickTestUtil::callerFile(int frameIndex) const
72{
73 QQmlEngine *engine = qmlEngine(this);
74 QV4::ExecutionEngine *v4 = engine->handle();
75 QV4::Scope scope(v4);
76
77 QVector<QV4::StackFrame> stack = v4->stackTrace(frameIndex + 2);
78 return (stack.size() > frameIndex + 1)
79 ? QJSValuePrivate::fromReturnedValue(
80 v4->newString(stack.at(frameIndex + 1).source)->asReturnedValue())
81 : QJSValue();
82}
83
84int QuickTestUtil::callerLine(int frameIndex) const
85{
86 QQmlEngine *engine = qmlEngine(this);
87 QV4::ExecutionEngine *v4 = engine->handle();
88
89 QVector<QV4::StackFrame> stack = v4->stackTrace(frameIndex + 2);
90 if (stack.size() > frameIndex + 1)
91 return qAbs(stack.at(frameIndex + 1).line);
92 return -1;
93}
94
95QT_END_NAMESPACE
96
97#include "moc_quicktestutil_p.cpp"