summaryrefslogtreecommitdiff
path: root/tests/auto/extras/data/tst_picture.qml
blob: 5ddc4406ccbd0ad9e721e3b2869564e840b3514f (plain)
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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** 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 http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtTest 1.0
import QtQuick 2.4
import QtQuick.Extras 1.4
import QtQuick.Extras.Private 1.0

TestCase {
    id: testCase
    name: "Tests_Picture"
    visible: windowShown
    when: windowShown
    width: 400
    height: 400

    Rectangle {
        anchors.fill: parent
        color: "white"
    }

    property var picture

    /*
        picture.dat was generated with a 10 width pen, and:

        painter.drawEllipse(painter.pen().width() / 2, painter.pen().width() / 2,
            100 - painter.pen().width(), 100 - painter.pen().width());
    */
    property color pictureDotDatDefaultColor: Qt.rgba(0, 0, 0, 1)
    property size pictureDotDatImplicitSize: Qt.size(100, 100)

    function cleanup() {
        if (picture)
            picture.destroy();
    }

    function test_instance() {
        picture = Qt.createQmlObject("import QtQuick.Extras 1.4; Picture { }", testCase, "");
        verify(picture, "Picture: failed to create an instance");
    }

    function verifyColorEqualMessage(pictureImage, pixelX, pixelY, expectedPixelColor) {
        return "pixel " + pictureImage.pixel(pixelX, pixelY) + " at "
            + pixelX + "," + pixelY + " isn't equal to " + expectedPixelColor;
    }

    function test_source_data() {
        return [
            {
                tag: "picture.dat",
                implicitSize: pictureDotDatImplicitSize,
                pixels: [
                    { x: 0, y: 0, color: Qt.rgba(1, 1, 1, 1) },
                    { x: 18, y: 18, color: pictureDotDatDefaultColor },
                    { x: 50, y: 50, color: Qt.rgba(1, 1, 1, 1) }
                ]
            }
        ];
    }

    function test_source(data) {
        picture = Qt.createQmlObject("import QtQuick.Extras 1.4; Picture {}", testCase, "");
        verify(picture, "Picture: failed to create an instance");
        picture.source = data.tag;
        picture.width = data.implicitSize.width;
        picture.height = data.implicitSize.height;
        waitForRendering(picture);

        var pictureImage = grabImage(picture);

        for (var i = 0; i < data.pixels.length; ++i) {
            var pixel = data.pixels[i];
            // TODO: use compare when QTBUG-34878 is fixed
            verify(Qt.colorEqual(pictureImage.pixel(pixel.x, pixel.y), pixel.color),
                verifyColorEqualMessage(pictureImage, pixel.x, pixel.y, pixel.color));
        }
    }

    function test_color_data() {
        return [
            { tag: "undefined", color: undefined, expectedColor: pictureDotDatDefaultColor },
            { tag: "not a valid color", color: "not a valid color", expectedColor: pictureDotDatDefaultColor },
            { tag: "red", color: "red", expectedColor: Qt.rgba(1, 0, 0, 1) },
            { tag: "black", color: "black", expectedColor: Qt.rgba(0, 0, 0, 1) }
        ]
    }

    function test_color(data) {
        picture = Qt.createQmlObject("import QtQuick.Extras 1.4; Picture {}", testCase, "");
        verify(picture, "Picture: failed to create an instance");

        picture.width = pictureDotDatImplicitSize.width;
        picture.height = pictureDotDatImplicitSize.height;
        picture.source = "picture.dat";
        picture.color = data.color;
        // For some reason we need two waits here, otherwise the color detected is *sometimes* white instead of black.
        // Also, we use an explicit, shorter timeout, otherwise the default (5000 ms) seems to be exhausted.
        waitForRendering(picture, 200);
        waitForRendering(picture, 200);

        var pictureImage = grabImage(picture);

        verify(Qt.colorEqual(pictureImage.pixel(0, 0), Qt.rgba(1, 1, 1, 1)),
               verifyColorEqualMessage(pictureImage, 0, 0, Qt.rgba(1, 1, 1, 1)));
        verify(Qt.colorEqual(pictureImage.pixel(17, 17), data.expectedColor),
               verifyColorEqualMessage(pictureImage, 17, 17, data.expectedColor));
        verify(Qt.colorEqual(pictureImage.pixel(picture.width / 2, picture.height / 2), Qt.rgba(1, 1, 1, 1)),
               verifyColorEqualMessage(pictureImage, picture.width / 2, picture.height / 2, Qt.rgba(1, 1, 1, 1)));
    }

    FontMetrics {
        id: fontMetrics
    }

    function test_size() {
        picture = Qt.createQmlObject("import QtQuick.Extras 1.4; Picture {}", testCase, "");
        verify(picture, "Picture: failed to create an instance");

        compare(picture.implicitWidth, fontMetrics.height * 4);
        compare(picture.implicitHeight, fontMetrics.height * 4);
        compare(picture.width, picture.implicitWidth);
        compare(picture.height, picture.implicitHeight);

        picture.source = "picture.dat";
        compare(picture.implicitWidth, pictureDotDatImplicitSize.width);
        compare(picture.implicitHeight, pictureDotDatImplicitSize.height);
        compare(picture.width, picture.implicitWidth);
        compare(picture.height, picture.implicitHeight);
    }
}