summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/painting/qpageranges
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2020-11-02 21:39:34 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2020-11-08 11:49:54 +0100
commitb14b1c99f8d122c7988cd6e59b0f3fef15923da4 (patch)
treec876d2428f7be32a73a1d066fa2d703b59708a1e /tests/auto/gui/painting/qpageranges
parent8bf36025f5602e3067448941ee4d0ccca3928a40 (diff)
Rename QRangeCollection to QPageRanges, make it a proper value type
The type is specific about printing, so give it a name in line with QPageLayout and QPageSize. As per API review comment, it's not clear why this type should not be a regular, copyable and movable value type. It stores a list of intervals. Give it value-type semantics, as an implicitly shared class. Convert the parse method into a static factory function. Add a Range type and use it instead of the semantic-free QPair. Move QPrinter getter into QPagedPainteDevice, make it return a copy rather than a pointer, and add a setter. Extend test case to cover all members and more merge cases. Fix bugs found that way. Fixes: QTBUG-88113 Change-Id: If17ea4d410d49f16b097e88b7979db5d72add820 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests/auto/gui/painting/qpageranges')
-rw-r--r--tests/auto/gui/painting/qpageranges/CMakeLists.txt14
-rw-r--r--tests/auto/gui/painting/qpageranges/qpageranges.pro6
-rw-r--r--tests/auto/gui/painting/qpageranges/tst_qpageranges.cpp183
3 files changed, 203 insertions, 0 deletions
diff --git a/tests/auto/gui/painting/qpageranges/CMakeLists.txt b/tests/auto/gui/painting/qpageranges/CMakeLists.txt
new file mode 100644
index 0000000000..660d4b4336
--- /dev/null
+++ b/tests/auto/gui/painting/qpageranges/CMakeLists.txt
@@ -0,0 +1,14 @@
+# Generated from qpageranges.pro.
+
+#####################################################################
+## tst_qpageranges Test:
+#####################################################################
+
+qt_internal_add_test(tst_qpageranges
+ SOURCES
+ tst_qpageranges.cpp
+ PUBLIC_LIBRARIES
+ Qt::CorePrivate
+ Qt::Gui
+ Qt::GuiPrivate
+)
diff --git a/tests/auto/gui/painting/qpageranges/qpageranges.pro b/tests/auto/gui/painting/qpageranges/qpageranges.pro
new file mode 100644
index 0000000000..9062a58ea3
--- /dev/null
+++ b/tests/auto/gui/painting/qpageranges/qpageranges.pro
@@ -0,0 +1,6 @@
+CONFIG += testcase
+TARGET = tst_qpageranges
+
+QT += testlib gui-private core-private
+
+SOURCES += tst_qpageranges.cpp
diff --git a/tests/auto/gui/painting/qpageranges/tst_qpageranges.cpp b/tests/auto/gui/painting/qpageranges/tst_qpageranges.cpp
new file mode 100644
index 0000000000..953f1c472a
--- /dev/null
+++ b/tests/auto/gui/painting/qpageranges/tst_qpageranges.cpp
@@ -0,0 +1,183 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** 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 The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtTest/QtTest>
+#include <qpageranges.h>
+
+typedef QList<QPageRanges::Range> PageRangeList;
+
+class tst_QPageRanges : public QObject
+{
+ Q_OBJECT
+
+private slots:
+ void basics();
+
+ void addPage_data();
+ void addPage();
+
+ void addRange_data();
+ void addRange();
+
+ void fromToString_data();
+ void fromToString();
+};
+
+void tst_QPageRanges::basics()
+{
+ QPageRanges empty;
+ QVERIFY(empty.isEmpty());
+ QVERIFY(!empty.contains(0));
+ QCOMPARE(empty.firstPage(), 0);
+ QCOMPARE(empty.lastPage(), 0);
+
+ QPageRanges single;
+ single.addPage(1);
+
+ QVERIFY(!single.isEmpty());
+ QVERIFY(single.contains(1));
+ QCOMPARE(single.firstPage(), 1);
+ QCOMPARE(single.firstPage(), 1);
+
+ QPageRanges copy(single);
+ QCOMPARE(copy, single);
+
+ QPageRanges moved(std::move(copy));
+ QCOMPARE(moved, single);
+ QVERIFY(copy.isEmpty());
+
+ single.clear();
+ QVERIFY(single.isEmpty());
+ QVERIFY(!moved.isEmpty());
+}
+
+void tst_QPageRanges::addPage_data()
+{
+ QTest::addColumn<QList<int>>("pageNumbers");
+ QTest::addColumn<PageRangeList>("expected");
+
+ QTest::newRow("single") << QList<int>{5} << PageRangeList{{5, 5}};
+ QTest::newRow("duplicate") << QList<int>{5, 5} << PageRangeList{{5, 5}};
+ QTest::newRow("adjacent") << QList<int>{5, 6, 7} << PageRangeList{{5, 7}};
+ QTest::newRow("separate") << QList<int>{1, 3, 5} << PageRangeList{{1, 1}, {3, 3}, {5, 5}};
+ QTest::newRow("unsorted") << QList<int>{5, 3, 4, 1} << PageRangeList{{1, 1}, {3, 5}};
+ QTest::newRow("invalid") << QList<int>{-1} << PageRangeList{};
+}
+
+void tst_QPageRanges::addPage()
+{
+ QFETCH(QList<int>, pageNumbers);
+ QFETCH(PageRangeList, expected);
+
+ QPageRanges result;
+ for (int pageNumber : qAsConst(pageNumbers)) {
+ if (QByteArrayView(QTest::currentDataTag()) == "invalid")
+ QTest::ignoreMessage(QtWarningMsg, "QPageRanges::addPage: 'pageNumber' must be greater than 0");
+ result.addPage(pageNumber);
+ }
+
+ QCOMPARE(result.toRangeList(), expected);
+}
+
+void tst_QPageRanges::addRange_data()
+{
+ QTest::addColumn<PageRangeList>("ranges");
+ QTest::addColumn<PageRangeList>("expected");
+
+ QTest::newRow("single") << PageRangeList{{5, 5}}
+ << PageRangeList{{5, 5}};
+ QTest::newRow("duplicate") << PageRangeList{{5, 5}, {5, 5}}
+ << PageRangeList{{5, 5}};
+ QTest::newRow("adjacent") << PageRangeList{{1, 3}, {4, 6}, {7, 10}}
+ << PageRangeList{{1, 10}};
+ QTest::newRow("separate") << PageRangeList{{1, 2}, {4, 5}, {7, 8}}
+ << PageRangeList{{1, 2}, {4, 5}, {7, 8}};
+ QTest::newRow("overlap") << PageRangeList{{1, 5}, {4, 8}, {8, 10}}
+ << PageRangeList{{1, 10}};
+ QTest::newRow("included") << PageRangeList{{1, 5}, {2, 3}, {8, 9}, {7, 10}}
+ << PageRangeList{{1, 5}, {7, 10}};
+ QTest::newRow("unsorted") << PageRangeList{{7, 8}, {1, 4}, {9, 10}}
+ << PageRangeList{{1, 4}, {7, 10}};
+ QTest::newRow("flipped") << PageRangeList{{5, 1}}
+ << PageRangeList{{1, 5}};
+ QTest::newRow("invalid1") << PageRangeList{{-1, 2}}
+ << PageRangeList{};
+ QTest::newRow("invalid2") << PageRangeList{{0, -1}}
+ << PageRangeList{};
+}
+
+void tst_QPageRanges::addRange()
+{
+ QFETCH(PageRangeList, ranges);
+ QFETCH(PageRangeList, expected);
+
+ QPageRanges result;
+ for (const auto &range : qAsConst(ranges)) {
+ const QByteArrayView testdata(QTest::currentDataTag());
+ if (testdata.startsWith("invalid"))
+ QTest::ignoreMessage(QtWarningMsg, "QPageRanges::addRange: 'from' and 'to' must be greater than 0");
+
+ result.addRange(range.from, range.to);
+ }
+
+ QCOMPARE(result.toRangeList(), expected);
+}
+
+void tst_QPageRanges::fromToString_data()
+{
+ QTest::addColumn<QString>("fromString");
+ QTest::addColumn<PageRangeList>("rangeList");
+ QTest::addColumn<QString>("toString");
+
+ QTest::newRow("invalid") << QString(",-8")
+ << PageRangeList{}
+ << QString();
+
+ QTest::newRow("overlapping") << QString("1-3,5-9,6-7,8-11")
+ << PageRangeList{{1, 3}, {5, 11}}
+ << QString("1-3,5-11");
+
+ QTest::newRow("spacy") << QString("1 -2, 3- 4 ,\t5-6\t,7 - 8 ,\n9 - 10")
+ << PageRangeList{{1, 10}}
+ << QString("1-10");
+}
+
+void tst_QPageRanges::fromToString()
+{
+ QFETCH(QString, fromString);
+ QFETCH(PageRangeList, rangeList);
+ QFETCH(QString, toString);
+
+ QPageRanges ranges = QPageRanges::fromString(fromString);
+ QCOMPARE(ranges.toRangeList(), rangeList);
+ QCOMPARE(ranges.toString(), toString);
+}
+
+QTEST_APPLESS_MAIN(tst_QPageRanges)
+
+#include "tst_qpageranges.moc"