diff options
author | Chris Adams <christopher.adams@nokia.com> | 2011-08-03 09:20:18 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-08-18 05:10:14 +0200 |
commit | 36767e3fe1f0038441ae06ef5b5e1cb19a3738fa (patch) | |
tree | b0585a341061534876ac1e5ce1ce85f78a504ef1 /tests | |
parent | 74f3a67fe80fecf7ba2fd76e1758b6c0f68ce918 (diff) |
Make QColor a value type
This commit allows direct access to the r, g, b and a components
of a color (in floating point format: 0 <= v <= 1). Since conversion
from color to string is a common operation, this commit also adds
unit tests to ensure that the previous behaviour is maintained in
other cases (comparison with toString value, etc).
Task-number: QTBUG-14731
Change-Id: I87b521dd4f9c1e96dfe5b20cf8053293cb14cfe4
Reviewed-on: http://codereview.qt.nokia.com/2527
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
Diffstat (limited to 'tests')
5 files changed, 132 insertions, 0 deletions
diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/color_compare.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/color_compare.qml new file mode 100644 index 0000000000..8701dae612 --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/color_compare.qml @@ -0,0 +1,37 @@ +import Test 1.0 + +MyTypeObject { + property real v_r: color.r + property real v_g: color.g + property real v_b: color.b + property real v_a: color.a + property variant copy: color + property string colorToString: color.toString() + + // compare different colors + property bool colorEqualsIdenticalRgba: (color == Qt.rgba(0.2, 0.88, 0.6, 0.34)) // true + property bool colorEqualsDifferentAlpha: (color == Qt.rgba(0.2, 0.88, 0.6, 0.44)) // false + property bool colorEqualsDifferentRgba: (color == Qt.rgba(0.3, 0.98, 0.7, 0.44)) // false + + // compare different color.toString()s + property bool colorToStringEqualsColorString: (color.toString() == colorToString) // true + property bool colorToStringEqualsDifferentAlphaString: (color.toString() == Qt.rgba(0.2, 0.88, 0.6, 0.44).toString()) // true + property bool colorToStringEqualsDifferentRgbaString: (color.toString() == Qt.rgba(0.3, 0.98, 0.7, 0.44).toString()) // false + + // compare colors to strings + property bool colorEqualsColorString: (color == colorToString) // false + property bool colorEqualsDifferentAlphaString: (color == Qt.rgba(0.2, 0.88, 0.6, 0.44).toString()) // false + property bool colorEqualsDifferentRgbaString: (color == Qt.rgba(0.3, 0.98, 0.7, 0.44).toString()) // false + + // compare colors to various value types + property bool equalsColor: (color == Qt.rgba(0.2, 0.88, 0.6, 0.34)) // true + property bool equalsVector3d: (color == Qt.vector3d(1, 2, 3)) // false + property bool equalsSize: (color == Qt.size(1912, 1913)) // false + property bool equalsPoint: (color == Qt.point(10, 4)) // false + property bool equalsRect: (color == Qt.rect(2, 3, 109, 102)) // false + + // ensure comparison directionality doesn't matter + property bool equalsColorRHS: (Qt.rgba(0.2, 0.88, 0.6, 0.34) == color) // true + property bool colorEqualsCopy: (color == copy) // true + property bool copyEqualsColor: (copy == color) // true +} diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/color_read.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/color_read.qml new file mode 100644 index 0000000000..bc92b1e5f9 --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/color_read.qml @@ -0,0 +1,9 @@ +import Test 1.0 + +MyTypeObject { + property real v_r: color.r + property real v_g: color.g + property real v_b: color.b + property real v_a: color.a + property variant copy: color +} diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/color_write.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/color_write.qml new file mode 100644 index 0000000000..3f1bad4aa6 --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/color_write.qml @@ -0,0 +1,8 @@ +import Test 1.0 + +MyTypeObject { + color.r: if (true) 0.5 + color.g: if (true) 0.38 + color.b: if (true) 0.3 + color.a: if (true) 0.7 +} diff --git a/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h b/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h index 1efab0c423..5eb6bf6b6d 100644 --- a/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h +++ b/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h @@ -54,6 +54,7 @@ #include <QQuaternion> #include <QMatrix4x4> #include <QFont> +#include <QColor> #include <qdeclarative.h> #include <QDeclarativePropertyValueSource> #include <QDeclarativeProperty> @@ -79,6 +80,7 @@ class MyTypeObject : public QObject Q_PROPERTY(QQuaternion quaternion READ quaternion WRITE setQuaternion NOTIFY changed) Q_PROPERTY(QMatrix4x4 matrix READ matrix WRITE setMatrix NOTIFY changed) Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY changed) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY changed) Q_PROPERTY(QVariant variant READ variant NOTIFY changed) public: @@ -109,6 +111,10 @@ public: m_font.setCapitalization(QFont::AllLowercase); m_font.setLetterSpacing(QFont::AbsoluteSpacing, 10.2); m_font.setWordSpacing(19.7); + m_color.setRedF(0.2); + m_color.setGreenF(0.88); + m_color.setBlueF(0.6); + m_color.setAlphaF(0.34); } QPoint m_point; @@ -171,6 +177,10 @@ public: QFont font() const { return m_font; } void setFont(const QFont &v) { m_font = v; emit changed(); } + QColor m_color; + QColor color() const { return m_color; } + void setColor(const QColor &v) { m_color = v; emit changed(); } + QVariant variant() const { return sizef(); } void emitRunScript() { emit runScript(); } diff --git a/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp b/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp index 5869310ef6..89d0e7e357 100644 --- a/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp +++ b/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp @@ -77,6 +77,7 @@ private slots: void quaternion(); void matrix4x4(); void font(); + void color(); void variant(); void bindingAssignment(); @@ -807,6 +808,73 @@ void tst_qdeclarativevaluetypes::font() } } +void tst_qdeclarativevaluetypes::color() +{ + { + QDeclarativeComponent component(&engine, TEST_FILE("color_read.qml")); + MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create()); + QVERIFY(object != 0); + + QCOMPARE((float)object->property("v_r").toDouble(), (float)0.2); + QCOMPARE((float)object->property("v_g").toDouble(), (float)0.88); + QCOMPARE((float)object->property("v_b").toDouble(), (float)0.6); + QCOMPARE((float)object->property("v_a").toDouble(), (float)0.34); + QColor comparison; + comparison.setRedF(0.2); + comparison.setGreenF(0.88); + comparison.setBlueF(0.6); + comparison.setAlphaF(0.34); + QCOMPARE(object->property("copy"), QVariant(comparison)); + + delete object; + } + + { + QDeclarativeComponent component(&engine, TEST_FILE("color_write.qml")); + MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create()); + QVERIFY(object != 0); + + QColor newColor; + newColor.setRedF(0.5); + newColor.setGreenF(0.38); + newColor.setBlueF(0.3); + newColor.setAlphaF(0.7); + QCOMPARE(object->color(), newColor); + + delete object; + } + + { + QDeclarativeComponent component(&engine, TEST_FILE("color_compare.qml")); + MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create()); + QVERIFY(object != 0); + QString colorString("#33e199"); + QCOMPARE(object->property("colorToString").toString(), colorString); + QCOMPARE(object->property("colorEqualsIdenticalRgba").toBool(), true); + QCOMPARE(object->property("colorEqualsDifferentAlpha").toBool(), false); + QCOMPARE(object->property("colorEqualsDifferentRgba").toBool(), false); + QCOMPARE(object->property("colorToStringEqualsColorString").toBool(), true); + QCOMPARE(object->property("colorToStringEqualsDifferentAlphaString").toBool(), true); + QCOMPARE(object->property("colorToStringEqualsDifferentRgbaString").toBool(), false); + QCOMPARE(object->property("colorEqualsColorString").toBool(), true); // maintaining behaviour with QtQuick 1.0 + QCOMPARE(object->property("colorEqualsDifferentAlphaString").toBool(), true); // maintaining behaviour with QtQuick 1.0 + QCOMPARE(object->property("colorEqualsDifferentRgbaString").toBool(), false); + + QCOMPARE(object->property("equalsColor").toBool(), true); + QCOMPARE(object->property("equalsVector3d").toBool(), false); + QCOMPARE(object->property("equalsSize").toBool(), false); + QCOMPARE(object->property("equalsPoint").toBool(), false); + QCOMPARE(object->property("equalsRect").toBool(), false); + + // Color == Property and Property == Color should return the same result. + QCOMPARE(object->property("equalsColorRHS").toBool(), object->property("equalsColor").toBool()); + QCOMPARE(object->property("colorEqualsCopy").toBool(), true); + QCOMPARE(object->property("copyEqualsColor").toBool(), object->property("colorEqualsCopy").toBool()); + + delete object; + } +} + // Test bindings can write to value types void tst_qdeclarativevaluetypes::bindingAssignment() { |