aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorCharles Yin <yinyunqiao@gmail.com>2011-09-22 20:01:29 +1000
committerQt by Nokia <qt-info@nokia.com>2011-09-30 10:37:05 +0200
commit8f69461c480e10424401e95b16b507eec3e28e54 (patch)
tree58e13d4c7a470485ab5e14642a55bc35bd0ae2c3 /tests
parent20fb62f6040ad8415828092a2b09bd374433505f (diff)
tests for canvas and a few bug fixes
Change-Id: Icbbc7f2a0fe3b908963ce18afef51e25ea0170a0 Reviewed-on: http://codereview.qt-project.org/5805 Reviewed-by: Charles Yin <charles.yin@nokia.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/declarative/declarative.pro2
-rw-r--r--tests/auto/declarative/qsgcanvasitem/data/testhelper.js18
-rw-r--r--tests/auto/declarative/qsgcanvasitem/data/tst_colors.qml19
-rw-r--r--tests/auto/declarative/qsgcanvasitem/data/tst_fillStyle.qml113
-rw-r--r--tests/auto/declarative/qsgcanvasitem/data/tst_fillrect.qml23
-rw-r--r--tests/auto/declarative/qsgcanvasitem/data/tst_strokeStyle.qml48
-rw-r--r--tests/auto/declarative/qsgcanvasitem/qsgcanvasitem.pro5
-rw-r--r--tests/auto/declarative/qsgcanvasitem/tst_qsgcanvasitem.cpp42
8 files changed, 269 insertions, 1 deletions
diff --git a/tests/auto/declarative/declarative.pro b/tests/auto/declarative/declarative.pro
index 52abb02287..d8567db8bb 100644
--- a/tests/auto/declarative/declarative.pro
+++ b/tests/auto/declarative/declarative.pro
@@ -78,6 +78,7 @@ SGTESTS = \
qsgtextedit \
qsgtextinput \
qsgvisualdatamodel \
+ qsgcanvasitem \
SUBDIRS += $$PUBLICTESTS
@@ -91,4 +92,3 @@ contains(QT_CONFIG, private_tests) {
# Tests which should run in Pulse
PULSE_TESTS = $$SUBDIRS
-
diff --git a/tests/auto/declarative/qsgcanvasitem/data/testhelper.js b/tests/auto/declarative/qsgcanvasitem/data/testhelper.js
new file mode 100644
index 0000000000..bac0210e16
--- /dev/null
+++ b/tests/auto/declarative/qsgcanvasitem/data/testhelper.js
@@ -0,0 +1,18 @@
+function comparePixel(ctx,x,y,r,g,b,a, d)
+{
+ var c = ctx.getImageData(x,y,1,1).data;
+ if (d === undefined)
+ d = 0;
+ r = Math.round(r);
+ g = Math.round(g);
+ b = Math.round(b);
+ a = Math.round(a);
+
+ if (Math.abs(c[0]-r)>d || Math.abs(c[1]-g)>d || Math.abs(c[2]-b)>d || Math.abs(c[3]-a)>d) {
+ console.log('Pixel compare fail:\nactual :[' + c[0]+','+c[1]+','+c[2]+','+c[3] + ']\nexpected:['+r+','+g+','+b+','+a+'] +/- '+d);
+ return false;
+ }
+ return true;
+}
+
+
diff --git a/tests/auto/declarative/qsgcanvasitem/data/tst_colors.qml b/tests/auto/declarative/qsgcanvasitem/data/tst_colors.qml
new file mode 100644
index 0000000000..a20a7dfaf9
--- /dev/null
+++ b/tests/auto/declarative/qsgcanvasitem/data/tst_colors.qml
@@ -0,0 +1,19 @@
+import QtQuick 2.0
+import QtTest 1.0
+import "testhelper.js" as Helper
+
+Canvas {
+ id:canvas; width:1;height:1
+ TestCase {
+ name: "Colors"; when: windowShown
+ function test_globalAlpha() {
+ var ctx = canvas.getContext('2d');
+ ctx.reset();
+ ctx.fillStyle = Qt.rgba(1, 0.7, 0.2, 0.5);
+ ctx.globalAlpha = 0.5;
+ ctx.fillRect(0,0,1,1);
+ var d = ctx.getImageData(0,0,1,1).data;
+ verify(Helper.comparePixel(ctx, 0, 0, 255, 0.7 * 255, 0.2*255, 0.25 * 255));
+ }
+ }
+}
diff --git a/tests/auto/declarative/qsgcanvasitem/data/tst_fillStyle.qml b/tests/auto/declarative/qsgcanvasitem/data/tst_fillStyle.qml
new file mode 100644
index 0000000000..a4b77ec2b3
--- /dev/null
+++ b/tests/auto/declarative/qsgcanvasitem/data/tst_fillStyle.qml
@@ -0,0 +1,113 @@
+import QtQuick 2.0
+import QtTest 1.0
+import "testhelper.js" as Helper
+
+Canvas {
+ id:canvas; width:1;height:1
+ TestCase {
+ name: "fillStyle"; when: windowShown
+ function test_default() {
+ var ctx = canvas.getContext('2d');
+ ctx.reset();
+ verify(ctx.fillStyle, "#000000");
+ ctx.clearRect(0, 0, 1, 1);
+ compare(ctx.fillStyle, "#000000");
+ }
+ function test_get() {
+ var ctx = canvas.getContext('2d');
+ ctx.reset();
+ ctx.fillStyle = '#fa0';
+ compare(ctx.fillStyle, '#ffaa00');
+ ctx.fillStyle = Qt.rgba(0,0,0,0);
+ compare(ctx.fillStyle, 'rgba(0, 0, 0, 0.0)');
+ }
+ function test_hex() {
+ var ctx = canvas.getContext('2d');
+ ctx.reset();
+ ctx.fillStyle = '#f00';
+ compare(ctx.fillStyle, '#ff0000');
+ ctx.fillStyle = "#0f0";
+ compare(ctx.fillStyle, '#00ff00');
+ ctx.fillStyle = "#0fF";
+ compare(ctx.fillStyle, '#00ffff');
+ ctx.fillStyle = "#0aCCfb";
+ compare(ctx.fillStyle, '#0accfb');
+
+ }
+ function test_invalid() {
+ var ctx = canvas.getContext('2d');
+ ctx.reset();
+ ctx.fillStyle = '#fa0';
+ compare(ctx.fillStyle, '#ffaa00');
+ ctx.fillStyle = "invalid";
+ compare(ctx.fillStyle, '#ffaa00');
+ ctx.fillStyle = "rgb (1, 2, 3)";
+ compare(ctx.fillStyle, '#ffaa00');
+ ctx.fillStyle = "rgba(1, 2, 3)";
+ compare(ctx.fillStyle, '#ffaa00');
+ ctx.fillStyle = "rgb((3,4,1)";
+ compare(ctx.fillStyle, '#ffaa00');
+ ctx.fillStyle = "rgb(1, 3, 4, 0.5)";
+ compare(ctx.fillStyle, '#ffaa00');
+ ctx.fillStyle = "hsl(2, 3, 4, 0.8)";
+ compare(ctx.fillStyle, '#ffaa00');
+ ctx.fillStyle = "hsl(2, 3, 4";
+ compare(ctx.fillStyle, '#ffaa00');
+ }
+ function test_saverestore() {
+ var ctx = canvas.getContext('2d');
+ var old = ctx.fillStyle;
+ ctx.save();
+ ctx.fillStyle = "#ffaaff";
+ ctx.restore();
+ compare(ctx.fillStyle, old);
+
+ ctx.fillStyle = "#ffcc88";
+ old = ctx.fillStyle;
+ ctx.save();
+ compare(ctx.fillStyle, old);
+ ctx.restore();
+ }
+ function test_namedColor() {
+ var ctx = canvas.getContext('2d');
+ ctx.reset();
+ ctx.fillStyle = "red";
+ ctx.fillRect(0,0,1,1);
+ verify(Helper.comparePixel(ctx,0,0,255,0,0,255));
+
+ ctx.fillStyle = "black";
+ ctx.fillRect(0,0,1,1);
+ verify(Helper.comparePixel(ctx,0,0,0,0,0,255));
+
+ ctx.fillStyle = "white";
+ ctx.fillRect(0,0,1,1);
+ verify(Helper.comparePixel(ctx,0,0,255,255,255,255));
+ }
+ function test_rgba() {
+ var ctx = canvas.getContext('2d');
+ ctx.reset();
+ ctx.fillStyle = "rgb(-100, 300, 255)";
+ compare(ctx.fillStyle, "#00ffff");
+ ctx.fillStyle = "rgba(-100, 300, 255, 0.0)";
+ compare(ctx.fillStyle, "rgba(0, 255, 255, 0.0)");
+ ctx.fillStyle = "rgb(-10%, 110%, 50%)";
+ compare(ctx.fillStyle, "#00ff80");
+
+ ctx.clearRect(0, 0, 1, 1);
+ ctx.fillStyle = 'rgba(0%, 100%, 0%, 0.499)';
+ ctx.fillRect(0, 0, 1, 1);
+ //FIXME: currently we only return premultipled pixels
+ verify(Helper.comparePixel(ctx, 0,0, 0,127,0,255));
+ //verify(Helper.comparePixel(ctx, 0,0, 0,255,0,127));
+ }
+
+ function test_hsla() {
+ var ctx = canvas.getContext('2d');
+ ctx.reset();
+ ctx.fillStyle = "hsla(120, 100%, 50%, 0.499)";
+ ctx.fillRect(0, 0, 1, 1);
+ verify(Helper.comparePixel(ctx,0,0,0,127,0,255));
+ }
+
+ }
+}
diff --git a/tests/auto/declarative/qsgcanvasitem/data/tst_fillrect.qml b/tests/auto/declarative/qsgcanvasitem/data/tst_fillrect.qml
new file mode 100644
index 0000000000..07ddc59a07
--- /dev/null
+++ b/tests/auto/declarative/qsgcanvasitem/data/tst_fillrect.qml
@@ -0,0 +1,23 @@
+import QtQuick 2.0
+import QtTest 1.0
+
+Canvas {
+ id:canvas; width:1;height:1
+ onPaint: {
+ context.fillStyle = "red";
+ context.fillRect(0, 0, canvas.width, canvas.height);
+ }
+ TestCase {
+ name: "FillRect"; when: windowShown
+ function test_fillRect() {
+ var ctx = canvas.getContext('2d');
+ var imageData = ctx.getImageData(0, 0, 1, 1);
+ var d = imageData.data;
+ verify(d.length == 4);
+ verify(d[0] == 255);
+ verify(d[1] == 0);
+ verify(d[2] == 0);
+ verify(d[3] == 255);
+ }
+ }
+}
diff --git a/tests/auto/declarative/qsgcanvasitem/data/tst_strokeStyle.qml b/tests/auto/declarative/qsgcanvasitem/data/tst_strokeStyle.qml
new file mode 100644
index 0000000000..84f830d636
--- /dev/null
+++ b/tests/auto/declarative/qsgcanvasitem/data/tst_strokeStyle.qml
@@ -0,0 +1,48 @@
+import QtQuick 2.0
+import QtTest 1.0
+import "testhelper.js" as Helper
+
+Canvas {
+ id:canvas; width:1;height:1
+ TestCase {
+ name: "strokeStyle"; when: windowShown
+ function test_default() {
+ var ctx = canvas.getContext('2d');
+ ctx.reset();
+ compare(ctx.strokeStyle, "#000000")
+ ctx.clearRect(0, 0, 1, 1);
+ compare(ctx.strokeStyle, "#000000")
+ }
+ function test_saverestore() {
+ var ctx = canvas.getContext('2d');
+ var old = ctx.strokeStyle;
+ ctx.save();
+ ctx.strokeStyle = "#ffaaff";
+ ctx.restore();
+ compare(ctx.strokeStyle, old);
+
+ ctx.strokeStyle = "#ffcc88";
+ old = ctx.strokeStyle;
+ ctx.save();
+ compare(ctx.strokeStyle, old);
+ ctx.restore();
+ }
+ function test_namedColor() {
+ var ctx = canvas.getContext('2d');
+ ctx.reset();
+ ctx.strokeStyle = "red";
+ ctx.strokeRect(0,0,1,1);
+ verify(Helper.comparePixel(ctx,0,0,255,0,0,255));
+
+ ctx.strokeStyle = "black";
+ ctx.strokeRect(0,0,1,1);
+ verify(Helper.comparePixel(ctx,0,0,0,0,0,255));
+
+ ctx.strokeStyle = "white";
+ ctx.strokeRect(0,0,1,1);
+ verify(Helper.comparePixel(ctx,0,0,255,255,255,255));
+ }
+
+
+ }
+}
diff --git a/tests/auto/declarative/qsgcanvasitem/qsgcanvasitem.pro b/tests/auto/declarative/qsgcanvasitem/qsgcanvasitem.pro
new file mode 100644
index 0000000000..b22c49f379
--- /dev/null
+++ b/tests/auto/declarative/qsgcanvasitem/qsgcanvasitem.pro
@@ -0,0 +1,5 @@
+QT += core-private gui-private declarative-private widgets
+TE=app
+TARGET=tst_qsgcanvasitem
+CONFIG += warn_on qmltestcase
+SOURCES += tst_qsgcanvasitem.cpp \ No newline at end of file
diff --git a/tests/auto/declarative/qsgcanvasitem/tst_qsgcanvasitem.cpp b/tests/auto/declarative/qsgcanvasitem/tst_qsgcanvasitem.cpp
new file mode 100644
index 0000000000..680e45238e
--- /dev/null
+++ b/tests/auto/declarative/qsgcanvasitem/tst_qsgcanvasitem.cpp
@@ -0,0 +1,42 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 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 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+#include <QtQuickTest/quicktest.h>
+QUICK_TEST_MAIN(qsgcanvasitem)