File: exiftransform_test.cpp

package info (click to toggle)
qtox 1.18.3-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 28,996 kB
  • sloc: cpp: 48,067; xml: 8,560; python: 704; sh: 232; makefile: 14
file content (165 lines) | stat: -rw-r--r-- 4,441 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* SPDX-License-Identifier: GPL-3.0-or-later
 * Copyright © 2020 by The qTox Project Contributors
 * Copyright © 2024-2025 The TokTok team.
 */

#include "src/model/exiftransform.h"

#include <QPainter>
#include <QTest>

namespace {
const auto rowColor = QColor(Qt::green).rgb();
const auto colColor = QColor(Qt::blue).rgb();

enum class Side
{
    top,
    bottom,
    left,
    right
};

QPoint getPosition(Side side)
{
    int x;
    int y;
    switch (side) {
    case Side::top: {
        x = 1;
        y = 0;
        break;
    }
    case Side::bottom: {
        x = 1;
        y = 2;
        break;
    }
    case Side::left: {
        x = 0;
        y = 1;
        break;
    }
    case Side::right: {
        x = 2;
        y = 1;
        break;
    }
    }

    return {x, y};
}

QRgb getColor(const QImage& image, Side side)
{
    return image.pixel(getPosition(side));
}
} // namespace

class TestExifTransform : public QObject
{
    Q_OBJECT

private slots:
    void init();
    void testTopLeft();
    void testTopRight();
    void testBottomRight();
    void testBottomLeft();
    void testLeftTop();
    void testRightTop();
    void testRightBottom();
    void testLeftBottom();
    void testInputTooShort();

private:
    QImage inputImage;
};

void TestExifTransform::init()
{
    inputImage = QImage(QSize(3, 3), QImage::Format_RGB32);
    QPainter painter(&inputImage);
    painter.fillRect(QRect(0, 0, 3, 3), Qt::white);
    // First row has a green dot in the middle
    painter.setPen(rowColor);
    painter.drawPoint(getPosition(Side::top));
    // First column has a blue dot in the middle
    painter.setPen(colColor);
    painter.drawPoint(getPosition(Side::left));
}

void TestExifTransform::testTopLeft()
{
    auto image = ExifTransform::applyTransformation(inputImage, ExifTransform::Orientation::TopLeft);
    QVERIFY(getColor(image, Side::top) == rowColor);
    QVERIFY(getColor(image, Side::left) == colColor);
}

void TestExifTransform::testTopRight()
{
    auto image = ExifTransform::applyTransformation(inputImage, ExifTransform::Orientation::TopRight);
    QVERIFY(getColor(image, Side::top) == rowColor);
    QVERIFY(getColor(image, Side::right) == colColor);
}

void TestExifTransform::testBottomRight()
{
    auto image =
        ExifTransform::applyTransformation(inputImage, ExifTransform::Orientation::BottomRight);
    QVERIFY(getColor(image, Side::bottom) == rowColor);
    QVERIFY(getColor(image, Side::right) == colColor);
}

void TestExifTransform::testBottomLeft()
{
    auto image =
        ExifTransform::applyTransformation(inputImage, ExifTransform::Orientation::BottomLeft);
    QVERIFY(getColor(image, Side::bottom) == rowColor);
    QVERIFY(getColor(image, Side::left) == colColor);
}

void TestExifTransform::testLeftTop()
{
    auto image = ExifTransform::applyTransformation(inputImage, ExifTransform::Orientation::LeftTop);
    QVERIFY(getColor(image, Side::left) == rowColor);
    QVERIFY(getColor(image, Side::top) == colColor);
}

void TestExifTransform::testRightTop()
{
    auto image = ExifTransform::applyTransformation(inputImage, ExifTransform::Orientation::RightTop);
    QVERIFY(getColor(image, Side::right) == rowColor);
    QVERIFY(getColor(image, Side::top) == colColor);
}

void TestExifTransform::testRightBottom()
{
    auto image =
        ExifTransform::applyTransformation(inputImage, ExifTransform::Orientation::RightBottom);
    QVERIFY(getColor(image, Side::right) == rowColor);
    QVERIFY(getColor(image, Side::bottom) == colColor);
}

void TestExifTransform::testLeftBottom()
{
    auto image =
        ExifTransform::applyTransformation(inputImage, ExifTransform::Orientation::LeftBottom);
    QVERIFY(getColor(image, Side::left) == rowColor);
    QVERIFY(getColor(image, Side::bottom) == colColor);
}

void TestExifTransform::testInputTooShort()
{
    const QByteArray garbage = QByteArray::fromBase64(
        "AEV4aWYAAE1NACoAAAAA0v///7p6h2kAaWYAAAAAAABNeGlmAABRMAAjGEQAAAAAAAAAQAAAAAAA"
        "/////////////////wEBASMBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBARIAAQAAAAEB"
        "AAABAAEBAQEjAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEAAAEBAADCwsLCwsLCwsLCwsLCwsLCwsLC"
        "wsLCAQEBAQEBAQEBAQEBAQAAAQEAAAABAAEBAQEjAQEBAAAAAAAAAAD/////////////////BgD/"
        "/////wAAAQABAQEBIwEBAQEjAQEB/////wEBAQEBAQEBAQEBAQEBAQEB");

    ExifTransform::getOrientation(garbage.mid(1));
}

QTEST_APPLESS_MAIN(TestExifTransform)
#include "exiftransform_test.moc"