on 0cfbca3 help sign in
unity8/tests/plugins/ScreenGrabber/ScreenGrabberTest.cpp
78 lines 2.5 kB history raw
1/*
2 * Copyright (C) 2015 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "screengrabber.h"
19#include <QtTest>
20#include <QtQuick/QQuickView>
21#include <QtQuick/QQuickItem>
22#include <QQmlEngine>
24#include <paths.h>
26class ScreenGrabberTest: public QObject
28 Q_OBJECT
30private Q_SLOTS:
32 void initTestCase()
33 {
34 m_view = new QQuickView();
35 m_view->setSource(QUrl::fromLocalFile(testDataDir() + "/" TEST_DIR "/grabber.qml"));
36 m_grabber = dynamic_cast<ScreenGrabber*>(m_view->rootObject()->property("grabber").value<QObject*>());
37 QVERIFY(m_grabber);
38 m_view->show();
39 QTest::qWaitForWindowExposed(m_view);
40 }
42 void cleanupTestCase()
43 {
44 delete m_view;
45 m_view = nullptr;
46 QTRY_COMPARE(QThreadPool::globalInstance()->activeThreadCount(), 0);
47 }
49 void testGrabScreenshot()
50 {
51 QSignalSpy grabberSpy(m_grabber, &ScreenGrabber::screenshotSaved);
52 m_grabber->captureAndSave();
53 QTRY_VERIFY(grabberSpy.count() == 1);
54 const QVariantList args = grabberSpy.takeFirst();
55 QVERIFY(args.count() == 1); // verify we got a non-empty filename where the screenshot has been saved
56 QVERIFY(!args.first().toString().isEmpty());
57 }
59 void testRotatedScreenshot()
60 {
61 QSignalSpy grabberSpy(m_grabber, &ScreenGrabber::screenshotSaved);
62 m_grabber->captureAndSave(90); // rotate by 90°
63 QTRY_VERIFY(grabberSpy.count() == 1);
64 const QVariantList args = grabberSpy.takeFirst();
65 const QString filename = args.first().toString();
66 QVERIFY(!filename.isEmpty());
67 QImage img(filename);
68 QVERIFY(img.height() > img.width()); // verify that the image got rotated by 90° (height > width)
69 }
71private:
72 QQuickView *m_view;
73 ScreenGrabber *m_grabber = nullptr;
74};
76QTEST_MAIN(ScreenGrabberTest)
78#include "ScreenGrabberTest.moc"