on xenial help sign in
add a test for ScreenGrabber plugin
commitec82547f056235fe7b6ba9122da48209b844460a authorLukáš Tinkl <lukas.tinkl@canonical.com> dateFri 17 Jul 2015, 21:59 (11 years ago) parent86a756f
6 files changed+138−17
plugins/ScreenGrabber/screengrabber.cpp+26−14 plugins/ScreenGrabber/screengrabber.h+4−3 tests/plugins/CMakeLists.txt+1 tests/plugins/ScreenGrabber/CMakeLists.txt+13 tests/plugins/ScreenGrabber/ScreenGrabberTest.cpp+63 tests/plugins/ScreenGrabber/grabber.qml+31
plugins/ScreenGrabber/screengrabber.cpp+26−14
@@ -12,8 +12,6 @@
1212 *
1313 * You should have received a copy of the GNU General Public License
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15− *
16− * Authors: Alberto Aguirre <alberto.aguirre@canonical.com>
1715 */
1816
1917#include "screengrabber.h"
@@ -28,26 +26,35 @@
2826
2927#include <QDebug>
3028
31−void saveScreenshot(QImage screenshot, const QString &filename, const QString &format, int quality)
29+bool saveScreenshot(const QImage &screenshot, const QString &filename, const QString &format, int quality)
3230{
33− if (!screenshot.save(filename, format.toLatin1().data(), quality))
34− qWarning() << "ScreenShotter: failed to save snapshot!";
31+ if (!screenshot.save(filename, format.toLatin1().data(), quality)) {
32+ qWarning() << "ScreenGrabber: failed to save snapshot!";
33+ return false;
34+ }
35+
36+ return true;
3537}
3638
3739ScreenGrabber::ScreenGrabber(QObject *parent)
3840 : QObject(parent)
3941{
40− QDir screenshotsDir(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
42+ QDir screenshotsDir;
43+ if (qEnvironmentVariableIsSet("UNITY_TESTING")) {
44+ qDebug() << "Using test environment";
45+ QStandardPaths::setTestModeEnabled(true);
46+ screenshotsDir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
47+ } else {
48+ qDebug() << "Using real environment";
49+ screenshotsDir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
50+ }
4151 screenshotsDir.mkpath("Screenshots");
4252 screenshotsDir.cd("Screenshots");
43− if (screenshotsDir.exists())
44− {
53+ if (screenshotsDir.exists()) {
4554 fileNamePrefix = screenshotsDir.absolutePath();
4655 fileNamePrefix.append("/screenshot");
47− }
48− else
49− {
50− qWarning() << "ScreenShotter: failed to create directory at: " << screenshotsDir.absolutePath();
56+ } else {
57+ qWarning() << "ScreenGrabber: failed to create directory at: " << screenshotsDir.absolutePath();
5158 }
5259}
5360
@@ -73,8 +80,13 @@ void ScreenGrabber::captureAndSave()
7380 return;
7481 }
7582
76− QImage screenshot = main_window->grabWindow();
77− QtConcurrent::run(saveScreenshot, screenshot, makeFileName(), getFormat(), screenshotQuality);
83+ const QImage screenshot = main_window->grabWindow();
84+ const QString filename = makeFileName();
85+ qDebug() << "Saving screenshot to" << filename;
86+ auto saveOp = QtConcurrent::run(saveScreenshot, screenshot, filename, getFormat(), screenshotQuality);
87+ if (saveOp.result()) {
88+ Q_EMIT screenshotSaved(filename);
89+ }
7890}
7991
8092QString ScreenGrabber::makeFileName() const
plugins/ScreenGrabber/screengrabber.h+4−3
@@ -1,5 +1,5 @@
11/*
2− * Copyright (C) 2014 Canonical, Ltd.
2+ * Copyright (C) 2014-2015 Canonical, Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -12,8 +12,6 @@
1212 *
1313 * You should have received a copy of the GNU General Public License
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15− *
16− * Authors: Alberto Aguirre <alberto.aguirre@canonical.com>
1715 */
1816
1917#ifndef SNAPSHOTTER_H
@@ -33,6 +31,9 @@ public:
3331public Q_SLOTS:
3432 void captureAndSave();
3533
34+Q_SIGNALS:
35+ void screenshotSaved(const QString &filename);
36+
3637private:
3738 QString makeFileName() const;
3839 QString getFormat() const;
tests/plugins/CMakeLists.txt+1
@@ -2,6 +2,7 @@ add_subdirectory(AccountsService)
22add_subdirectory(Greeter)
33add_subdirectory(LightDM)
44add_subdirectory(Dash)
5+add_subdirectory(ScreenGrabber)
56add_subdirectory(Ubuntu)
67add_subdirectory(Unity)
78add_subdirectory(Utils)
tests/plugins/ScreenGrabber/CMakeLists.txtnew file+13
@@ -0,0 +1,13 @@
1+include_directories(
2+ ${CMAKE_SOURCE_DIR}/plugins/ScreenGrabber
3+ ${CMAKE_CURRENT_BINARY_DIR}
4+ )
5+
6+add_definitions(
7+ -DCURRENT_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
8+ )
9+
10+add_executable(ScreenGrabberTestExec ScreenGrabberTest.cpp ${CMAKE_SOURCE_DIR}/plugins/ScreenGrabber/screengrabber.cpp)
11+qt5_use_modules(ScreenGrabberTestExec Test Quick)
12+target_link_libraries(ScreenGrabberTestExec ScreenGrabber-qml)
13+add_unity8_uitest(ScreenGrabber ScreenGrabberTestExec ADD_TEST)
tests/plugins/ScreenGrabber/ScreenGrabberTest.cppnew file+63
@@ -0,0 +1,63 @@
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+ */
16+
17+#include "screengrabber.h"
18+
19+#include <QtTest>
20+#include <QtQuick/QQuickView>
21+#include <QtQuick/QQuickItem>
22+
23+class ScreenGrabberTest: public QObject
24+{
25+ Q_OBJECT
26+
27+private Q_SLOTS:
28+
29+ void initTestCase()
30+ {
31+ m_view = new QQuickView();
32+ m_view->setSource(QUrl::fromLocalFile(CURRENT_SOURCE_DIR "/grabber.qml"));
33+ m_grabber = dynamic_cast<ScreenGrabber*>(m_view->rootObject()->property("grabber").value<QObject*>());
34+ QVERIFY(m_grabber);
35+ m_view->show();
36+ QTest::qWaitForWindowExposed(m_view);
37+ }
38+
39+ void cleanupTestCase()
40+ {
41+ delete m_view;
42+ m_view = nullptr;
43+ QTRY_COMPARE(QThreadPool::globalInstance()->activeThreadCount(), 0);
44+ }
45+
46+ void testGrabScreenshot()
47+ {
48+ QSignalSpy grabberSpy(m_grabber, &ScreenGrabber::screenshotSaved);
49+ m_grabber->captureAndSave();
50+ QTRY_VERIFY(grabberSpy.count() == 1);
51+ const QVariantList args = grabberSpy.takeFirst();
52+ QVERIFY(args.count() == 1); // verify we got a non-empty filename where the screenshot has been saved
53+ QVERIFY(!args.first().toString().isEmpty());
54+ }
55+
56+private:
57+ QQuickView *m_view;
58+ ScreenGrabber *m_grabber = nullptr;
59+};
60+
61+QTEST_MAIN(ScreenGrabberTest)
62+
63+#include "ScreenGrabberTest.moc"
tests/plugins/ScreenGrabber/grabber.qmlnew file+31
@@ -0,0 +1,31 @@
1+/*
2+ * Copyright 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+ */
16+
17+import QtQuick 2.4
18+import ScreenGrabber 0.1
19+
20+Rectangle {
21+ property var grabber: screenGrabber
22+
23+ width: 100
24+ height: 100
25+ color: "green"
26+
27+ ScreenGrabber {
28+ id: screenGrabber
29+ objectName: "screenGrabber"
30+ }
31+}