diff options
Diffstat (limited to 'tests')
6 files changed, 297 insertions, 5 deletions
diff --git a/tests/auto/widgets/widgets/qopenglwidget/qopenglwidget.pro b/tests/auto/widgets/widgets/qopenglwidget/qopenglwidget.pro new file mode 100644 index 0000000000..bbc6e987af --- /dev/null +++ b/tests/auto/widgets/widgets/qopenglwidget/qopenglwidget.pro @@ -0,0 +1,8 @@ +CONFIG += testcase +CONFIG += parallel_test +TARGET = tst_qopenglwidget +QT += gui-private core-private testlib widgets + +SOURCES += tst_qopenglwidget.cpp + +win32-msvc2010:contains(QT_CONFIG, angle):CONFIG += insignificant_test # QTBUG-31611 diff --git a/tests/auto/widgets/widgets/qopenglwidget/tst_qopenglwidget.cpp b/tests/auto/widgets/widgets/qopenglwidget/tst_qopenglwidget.cpp new file mode 100644 index 0000000000..14d06e7111 --- /dev/null +++ b/tests/auto/widgets/widgets/qopenglwidget/tst_qopenglwidget.cpp @@ -0,0 +1,266 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets/QOpenGLWidget> +#include <QtGui/QOpenGLFunctions> +#include <QtGui/QPainter> +#include <QtTest/QtTest> +#include <QSignalSpy> + +class tst_QOpenGLWidget : public QObject +{ + Q_OBJECT + +private slots: + void create(); + void clearAndGrab(); + void clearAndResizeAndGrab(); + void createNonTopLevel(); + void painter(); + void reparentToAlreadyCreated(); + void reparentToNotYetCreated(); +}; + +void tst_QOpenGLWidget::create() +{ + QScopedPointer<QOpenGLWidget> w(new QOpenGLWidget); + QVERIFY(!w->isValid()); + QSignalSpy frameSwappedSpy(w.data(), SIGNAL(frameSwapped())); + w->show(); + QTest::qWaitForWindowExposed(w.data()); + QVERIFY(frameSwappedSpy.count() > 0); + + QVERIFY(w->isValid()); + QVERIFY(w->context()); + QVERIFY(w->context()->format() == w->format()); + QVERIFY(w->defaultFramebufferObject() != 0); +} + +class ClearWidget : public QOpenGLWidget, protected QOpenGLFunctions +{ +public: + ClearWidget(QWidget *parent, int expectedWidth, int expectedHeight) + : QOpenGLWidget(parent), + m_initCalled(false), m_paintCalled(false), m_resizeCalled(false), + m_resizeOk(false), + m_w(expectedWidth), m_h(expectedHeight) { } + + void initializeGL() Q_DECL_OVERRIDE { + m_initCalled = true; + initializeOpenGLFunctions(); + } + void paintGL() Q_DECL_OVERRIDE { + m_paintCalled = true; + glClearColor(1.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + } + void resizeGL(int w, int h) Q_DECL_OVERRIDE { + m_resizeCalled = true; + m_resizeOk = w == m_w && h == m_h; + } + + bool m_initCalled; + bool m_paintCalled; + bool m_resizeCalled; + bool m_resizeOk; + int m_w; + int m_h; +}; + +void tst_QOpenGLWidget::clearAndGrab() +{ + QScopedPointer<ClearWidget> w(new ClearWidget(0, 800, 600)); + w->resize(800, 600); + w->show(); + QTest::qWaitForWindowExposed(w.data()); + QVERIFY(w->m_initCalled); + QVERIFY(w->m_resizeCalled); + QVERIFY(w->m_resizeOk); + QVERIFY(w->m_paintCalled); + + QImage image = w->grabFramebuffer(); + QVERIFY(!image.isNull()); + QCOMPARE(image.width(), w->width()); + QCOMPARE(image.height(), w->height()); + QVERIFY(image.pixel(30, 40) == qRgb(255, 0, 0)); +} + +void tst_QOpenGLWidget::clearAndResizeAndGrab() +{ + QScopedPointer<QOpenGLWidget> w(new ClearWidget(0, 640, 480)); + w->resize(640, 480); + w->show(); + QTest::qWaitForWindowExposed(w.data()); + + QImage image = w->grabFramebuffer(); + QVERIFY(!image.isNull()); + QCOMPARE(image.width(), w->width()); + QCOMPARE(image.height(), w->height()); + w->resize(800, 600); + image = w->grabFramebuffer(); + QVERIFY(!image.isNull()); + QCOMPARE(image.width(), 800); + QCOMPARE(image.height(), 600); + QCOMPARE(image.width(), w->width()); + QCOMPARE(image.height(), w->height()); + QVERIFY(image.pixel(30, 40) == qRgb(255, 0, 0)); +} + +void tst_QOpenGLWidget::createNonTopLevel() +{ + QWidget w; + ClearWidget *glw = new ClearWidget(&w, 600, 700); + QSignalSpy frameSwappedSpy(glw, SIGNAL(frameSwapped())); + w.resize(400, 400); + w.show(); + QTest::qWaitForWindowExposed(&w); + QVERIFY(frameSwappedSpy.count() > 0); + + QVERIFY(glw->m_resizeCalled); + glw->m_resizeCalled = false; + QVERIFY(!glw->m_resizeOk); + glw->resize(600, 700); + + QVERIFY(glw->m_initCalled); + QVERIFY(glw->m_resizeCalled); + QVERIFY(glw->m_resizeOk); + QVERIFY(glw->m_paintCalled); + + QImage image = glw->grabFramebuffer(); + QVERIFY(!image.isNull()); + QCOMPARE(image.width(), glw->width()); + QCOMPARE(image.height(), glw->height()); + QVERIFY(image.pixel(30, 40) == qRgb(255, 0, 0)); + + glw->doneCurrent(); + QVERIFY(!QOpenGLContext::currentContext()); + glw->makeCurrent(); + QVERIFY(QOpenGLContext::currentContext() == glw->context() && glw->context()); +} + +class PainterWidget : public QOpenGLWidget, protected QOpenGLFunctions +{ +public: + PainterWidget(QWidget *parent) + : QOpenGLWidget(parent), m_clear(false) { } + + void initializeGL() Q_DECL_OVERRIDE { + initializeOpenGLFunctions(); + } + void paintGL() Q_DECL_OVERRIDE { + QPainter p(this); + QCOMPARE(p.device()->width(), width()); + QCOMPARE(p.device()->height(), height()); + p.fillRect(QRect(QPoint(0, 0), QSize(width(), height())), Qt::blue); + if (m_clear) { + p.beginNativePainting(); + glClearColor(0.0f, 1.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + p.endNativePainting(); + } + } + bool m_clear; +}; + +void tst_QOpenGLWidget::painter() +{ + QWidget w; + PainterWidget *glw = new PainterWidget(&w); + w.resize(640, 480); + glw->resize(320, 200); + w.show(); + QTest::qWaitForWindowExposed(&w); + + QImage image = glw->grabFramebuffer(); + QCOMPARE(image.width(), glw->width()); + QCOMPARE(image.height(), glw->height()); + QVERIFY(image.pixel(20, 10) == qRgb(0, 0, 255)); + + glw->m_clear = true; + image = glw->grabFramebuffer(); + QVERIFY(image.pixel(20, 10) == qRgb(0, 255, 0)); +} + +void tst_QOpenGLWidget::reparentToAlreadyCreated() +{ + QWidget w1; + PainterWidget *glw = new PainterWidget(&w1); + w1.resize(640, 480); + glw->resize(320, 200); + w1.show(); + QTest::qWaitForWindowExposed(&w1); + + QWidget w2; + w2.show(); + QTest::qWaitForWindowExposed(&w2); + + glw->setParent(&w2); + glw->show(); + + QImage image = glw->grabFramebuffer(); + QCOMPARE(image.width(), 320); + QCOMPARE(image.height(), 200); + QVERIFY(image.pixel(20, 10) == qRgb(0, 0, 255)); +} + +void tst_QOpenGLWidget::reparentToNotYetCreated() +{ + QWidget w1; + PainterWidget *glw = new PainterWidget(&w1); + w1.resize(640, 480); + glw->resize(320, 200); + w1.show(); + QTest::qWaitForWindowExposed(&w1); + + QWidget w2; + glw->setParent(&w2); + w2.show(); + QTest::qWaitForWindowExposed(&w2); + + QImage image = glw->grabFramebuffer(); + QCOMPARE(image.width(), 320); + QCOMPARE(image.height(), 200); + QVERIFY(image.pixel(20, 10) == qRgb(0, 0, 255)); +} + +QTEST_MAIN(tst_QOpenGLWidget) + +#include "tst_qopenglwidget.moc" diff --git a/tests/auto/widgets/widgets/widgets.pro b/tests/auto/widgets/widgets/widgets.pro index 29d1f7746c..423b3952d4 100644 --- a/tests/auto/widgets/widgets/widgets.pro +++ b/tests/auto/widgets/widgets/widgets.pro @@ -52,3 +52,5 @@ SUBDIRS=\ qmainwindow \ qtextedit \ qtoolbar \ + +contains(QT_CONFIG, opengl): SUBDIRS += qopenglwidget diff --git a/tests/manual/qopenglwidget/openglwidget/main.cpp b/tests/manual/qopenglwidget/openglwidget/main.cpp index 68f9be7199..0268d47767 100644 --- a/tests/manual/qopenglwidget/openglwidget/main.cpp +++ b/tests/manual/qopenglwidget/openglwidget/main.cpp @@ -45,20 +45,32 @@ #include <QMdiArea> #include <QLCDNumber> #include <QTimer> - +#include <QSurfaceFormat> +#include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); + QSurfaceFormat format; + if (QCoreApplication::arguments().contains(QLatin1String("--multisample"))) + format.setSamples(4); + if (QCoreApplication::arguments().contains(QLatin1String("--coreprofile"))) { + format.setVersion(3, 2); + format.setProfile(QSurfaceFormat::CoreProfile); + } + qDebug() << "Requesting" << format; + QMdiArea w; w.resize(400,400); OpenGLWidget *glw = new OpenGLWidget; + glw->setFormat(format); w.addSubWindow(glw); glw->setMinimumSize(100,100); OpenGLWidget *glw2 = new OpenGLWidget; + glw2->setFormat(format); glw2->setMinimumSize(100,100); w.addSubWindow(glw2); @@ -69,5 +81,8 @@ int main(int argc, char *argv[]) w.show(); + if (glw->isValid()) + qDebug() << "Got" << glw->format(); + return a.exec(); } diff --git a/tests/manual/qopenglwidget/openglwidget/openglwidget.cpp b/tests/manual/qopenglwidget/openglwidget/openglwidget.cpp index bec89b6b41..516de35ff8 100644 --- a/tests/manual/qopenglwidget/openglwidget/openglwidget.cpp +++ b/tests/manual/qopenglwidget/openglwidget/openglwidget.cpp @@ -58,7 +58,7 @@ #include <QtCore/qmath.h> #include <qopengl.h> -class OpenGLWidgetPrivate +class OpenGLWidgetPrivate : protected QOpenGLFunctions { public: OpenGLWidgetPrivate(QWidget *q) @@ -91,7 +91,7 @@ OpenGLWidget::OpenGLWidget(QWidget *parent) { d = new OpenGLWidgetPrivate(this); QTimer *timer = new QTimer(this); - connect(timer, SIGNAL(timeout()), this, SLOT(updateGL())); + connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(30); } @@ -137,6 +137,7 @@ static const char *fragmentShaderSource = void OpenGLWidgetPrivate::initialize() { + initializeOpenGLFunctions(); m_program = new QOpenGLShaderProgram; m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource); m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource); @@ -157,7 +158,7 @@ void OpenGLWidgetPrivate::render() m_program->bind(); QMatrix4x4 matrix; - matrix.perspective(60, 4.0/3.0, 0.1, 100.0); + matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f); matrix.translate(0, 0, -2); matrix.rotate(100.0f * m_frame / 30/*screen()->refreshRate()*/, 0, 1, 0); diff --git a/tests/manual/qopenglwidget/openglwidget/openglwidget.h b/tests/manual/qopenglwidget/openglwidget/openglwidget.h index eaba27df26..ada01d4cc1 100644 --- a/tests/manual/qopenglwidget/openglwidget/openglwidget.h +++ b/tests/manual/qopenglwidget/openglwidget/openglwidget.h @@ -42,7 +42,7 @@ #ifndef OPENGLWIDGET_H #define OPENGLWIDGET_H -#include <QtWidgets/private/qopenglwidget_p.h> +#include <QtWidgets/QOpenGLWidget> class OpenGLWidgetPrivate; class OpenGLWidget : public QOpenGLWidget |