File indexing completed on 2024-05-05 04:35:20

0001 // This file is part of Washi Pad
0002 // SPDX-FileCopyrightText: 2018 Kevin Ottens <ervin@kde.org>
0003 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #include <QtTest>
0006 
0007 #include "stroke.h"
0008 
0009 class StrokeTest : public QObject
0010 {
0011     Q_OBJECT
0012 private slots:
0013     void shouldHaveDefaultState()
0014     {
0015         // GIVEN
0016         const auto stroke = Stroke{};
0017 
0018         // THEN
0019         QCOMPARE(stroke.type(), Stroke::Type::Outline);
0020         QCOMPARE(stroke.color(), QColor(Qt::black));
0021         QVERIFY(stroke.samples().isEmpty());
0022 
0023         // GIVEN
0024         const auto sample = StrokeSample{};
0025 
0026         // THEN
0027         QVERIFY(sample.position.isNull());
0028         QCOMPARE(sample.width, 1.0f);
0029     }
0030 
0031     void shouldHaveType()
0032     {
0033         // GIVEN
0034         auto stroke = Stroke{};
0035 
0036         // WHEN
0037         stroke.setType(Stroke::Type::Outline);
0038 
0039         // THEN
0040         QCOMPARE(stroke.type(), Stroke::Type::Outline);
0041     }
0042 
0043     void shouldHaveColor()
0044     {
0045         // GIVEN
0046         auto stroke = Stroke{};
0047 
0048         // WHEN
0049         stroke.setColor(Qt::red);
0050 
0051         // THEN
0052         QCOMPARE(stroke.color(), QColor(Qt::red));
0053     }
0054 
0055     void shouldHaveSamples()
0056     {
0057         // GIVEN
0058         auto stroke = Stroke{};
0059 
0060         // WHEN
0061         const auto sample = StrokeSample{{0.5f, 0.5f}, 0.1f};
0062         stroke.addSample(sample);
0063 
0064         // THEN
0065         QCOMPARE(stroke.samples().size(), 1);
0066         QCOMPARE(stroke.samples().first(), sample);
0067 
0068         // WHEN
0069         const auto samples = QVector<StrokeSample>{
0070             {{1.0f, 1.5f}, 0.2f},
0071             {{1.1f, 1.75f}, 0.4f}
0072         };
0073         stroke.addSamples(samples);
0074 
0075         // THEN
0076         QCOMPARE(stroke.samples().size(), 3);
0077         QCOMPARE(stroke.samples().at(0), sample);
0078         QCOMPARE(stroke.samples().at(1), samples.at(0));
0079         QCOMPARE(stroke.samples().at(2), samples.at(1));
0080     }
0081 
0082     void shouldEraseAreas_data()
0083     {
0084         QTest::addColumn<Stroke>("input");
0085         QTest::addColumn<QVector2D>("center");
0086         QTest::addColumn<float>("radius");
0087         QTest::addColumn<QVector<QVector<StrokeSample>>>("expectedSamples");
0088 
0089         const auto straightLine = [] {
0090             auto stroke = Stroke{};
0091             stroke.setType(Stroke::Type::Fill);
0092             stroke.setColor(Qt::blue);
0093             stroke.addSample({{0.0f, 0.0f}, 0.5f});
0094             stroke.addSample({{0.125f, 0.125f}, 0.5f});
0095             stroke.addSample({{0.25f, 0.25f}, 0.5f});
0096             stroke.addSample({{0.375f, 0.375f}, 0.5f});
0097             stroke.addSample({{0.5f, 0.5f}, 0.5f});
0098             stroke.addSample({{0.625f, 0.625f}, 0.5f});
0099             stroke.addSample({{0.75f, 0.75f}, 0.5f});
0100             stroke.addSample({{0.875f, 0.875f}, 0.5f});
0101             stroke.addSample({{1.0f, 1.0f}, 0.5f});
0102             return stroke;
0103         }();
0104 
0105         QTest::newRow("straight line, no hit")
0106                 << straightLine
0107                 << QVector2D{-1.0f, -1.0f} << 0.5f
0108                 << QVector<QVector<StrokeSample>>{straightLine.samples()};
0109 
0110         QTest::newRow("straight line, all hits")
0111                 << straightLine
0112                 << QVector2D{0.5f, 0.5f} << 1.5f
0113                 << QVector<QVector<StrokeSample>>{};
0114 
0115         QTest::newRow("straight line, begin hits with isolated sample")
0116                 << straightLine
0117                 << QVector2D{0.1875f, 0.1875f} << 0.125f
0118                 << QVector<QVector<StrokeSample>>{
0119                        {{{0.375f, 0.375f}, 0.5f}, {{0.5f, 0.5f}, 0.5f},
0120                         {{0.625f, 0.625f}, 0.5f}, {{0.75f, 0.75f}, 0.5f},
0121                         {{0.875f, 0.875f}, 0.5f}, {{1.0f, 1.0f}, 0.5f}}
0122                    };
0123 
0124         QTest::newRow("straight line, begin hits")
0125                 << straightLine
0126                 << QVector2D{0.0f, 0.0f} << 0.25f
0127                 << QVector<QVector<StrokeSample>>{
0128                        {{{0.25f, 0.25f}, 0.5f}, {{0.375f, 0.375f}, 0.5f},
0129                         {{0.5f, 0.5f}, 0.5f}, {{0.625f, 0.625f}, 0.5f},
0130                         {{0.75f, 0.75f}, 0.5f}, {{0.875f, 0.875f}, 0.5f},
0131                         {{1.0f, 1.0f}, 0.5f}}
0132                    };
0133 
0134         QTest::newRow("straight line, middle hits")
0135                 << straightLine
0136                 << QVector2D{0.375f, 0.375f} << 0.2f
0137                 << QVector<QVector<StrokeSample>>{
0138                        {{{0.0f, 0.0f}, 0.5f}, {{0.125f, 0.125f}, 0.5f}},
0139                        {{{0.625f, 0.625f}, 0.5f}, {{0.75f, 0.75f}, 0.5f}, {{0.875f, 0.875f}, 0.5f}, {{1.0f, 1.0f}, 0.5f}}
0140                    };
0141 
0142         QTest::newRow("straight line, end hits")
0143                 << straightLine
0144                 << QVector2D{1.0f, 1.0f} << 0.25f
0145                 << QVector<QVector<StrokeSample>>{
0146                        {{{0.0f, 0.0f}, 0.5f}, {{0.125f, 0.125f}, 0.5f},
0147                         {{0.25f, 0.25f}, 0.5f}, {{0.375f, 0.375f}, 0.5f},
0148                         {{0.5f, 0.5f}, 0.5f}, {{0.625f, 0.625f}, 0.5f},
0149                         {{0.75f, 0.75f}, 0.5f}}
0150                    };
0151 
0152 
0153         const auto complexLine = [] {
0154             auto stroke = Stroke{};
0155             stroke.setType(Stroke::Type::Outline);
0156             stroke.setColor(Qt::red);
0157             stroke.addSample({{0.0f, 0.0f}, 0.5f});
0158             stroke.addSample({{0.125f, 0.125f}, 0.5f});
0159             stroke.addSample({{0.25f, 0.25f}, 0.5f});
0160             stroke.addSample({{0.375f, 0.25f}, 0.5f});
0161             stroke.addSample({{0.5f, 0.125f}, 0.5f});
0162             stroke.addSample({{0.625f, 0.125f}, 0.5f});
0163             stroke.addSample({{0.75f, 0.25f}, 0.5f});
0164             stroke.addSample({{0.875f, 0.375f}, 0.5f});
0165             stroke.addSample({{1.0f, 0.25f}, 0.5f});
0166             stroke.addSample({{1.125f, 0.125f}, 0.5f});
0167             return stroke;
0168         }();
0169 
0170         QTest::newRow("complex line, multiple hits")
0171                 << complexLine
0172                 << QVector2D{0.5625f, 0.4375f} << 0.27f
0173                 << QVector<QVector<StrokeSample>>{
0174                        {{{0.0f, 0.0f}, 0.5f}, {{0.125f, 0.125f}, 0.5f}, {{0.25f, 0.25f}, 0.5f}},
0175                        {{{0.5f, 0.125f}, 0.5f}, {{0.625f, 0.125f}, 0.5f}},
0176                        {{{0.875f, 0.375f}, 0.5f}, {{1.0f, 0.25f}, 0.5f}, {{1.125f, 0.125f}, 0.5f}}
0177                    };
0178     }
0179 
0180     void shouldEraseAreas()
0181     {
0182         // GIVEN
0183         QFETCH(Stroke, input);
0184 
0185         // WHEN
0186         QFETCH(QVector2D, center);
0187         QFETCH(float, radius);
0188         const auto output = input.eraseArea(center, radius);
0189 
0190         // THEN
0191         QFETCH(QVector<QVector<StrokeSample>>, expectedSamples);
0192         QCOMPARE(output.size(), expectedSamples.size());
0193         for (int i = 0; i < output.size(); i++) {
0194             QCOMPARE(output.at(i).type(), input.type());
0195             QCOMPARE(output.at(i).color(), input.color());
0196             QCOMPARE(output.at(i).samples(), expectedSamples.at(i));
0197         }
0198     }
0199 
0200     void shouldHaveBoundingRect()
0201     {
0202         // GIVEN
0203         auto stroke = Stroke{};
0204         stroke.addSample({{0.5f, 0.5f}, 1.0f});
0205         stroke.addSample({{5.0f, -0.5f}, 1.0f});
0206         stroke.addSample({{-5.0f, 0.5f}, 1.0f});
0207         stroke.addSample({{0.25f, 1.5f}, 1.0f});
0208         stroke.addSample({{0.25f, 5.0f}, 1.0f});
0209         stroke.addSample({{0.25f, -5.0f}, 1.0f});
0210 
0211         // WHEN
0212         const auto rect = stroke.boundingRect();
0213 
0214         // THEN
0215         const auto expected = QRectF{QPointF{-5.0, -5.0}, QPointF{5.0, 5.0}};
0216         QCOMPARE(rect, expected);
0217     }
0218 };
0219 
0220 QTEST_APPLESS_MAIN(StrokeTest)
0221 
0222 #include "stroketest.moc"