1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtDeclarative/qdeclarativeengine.h>
43 #include <QtDeclarative/qdeclarativecontext.h>
44 #include <QtDeclarative/qdeclarativepropertymap.h>
45 #include <QtDeclarative/qdeclarativecomponent.h>
46 #include <QtQuick/private/qquicktext_p.h>
49 class tst_QDeclarativePropertyMap : public QObject
53 tst_QDeclarativePropertyMap() {}
57 void operatorInsert();
67 void tst_QDeclarativePropertyMap::insert()
69 QDeclarativePropertyMap map;
70 map.insert(QLatin1String("key1"),100);
71 map.insert(QLatin1String("key2"),200);
72 QVERIFY(map.keys().count() == 2);
73 QVERIFY(map.contains(QLatin1String("key1")));
75 QCOMPARE(map.value(QLatin1String("key1")), QVariant(100));
76 QCOMPARE(map.value(QLatin1String("key2")), QVariant(200));
78 map.insert(QLatin1String("key1"),"Hello World");
79 QCOMPARE(map.value(QLatin1String("key1")), QVariant("Hello World"));
81 //inserting property names same with existing method(signal, slot, method) names is not allowed
82 //QDeclarativePropertyMap has an invokable keys() method
83 QTest::ignoreMessage(QtWarningMsg, "Creating property with name \"keys\" is not permitted, conflicts with internal symbols. ");
84 map.insert(QLatin1String("keys"), 1);
85 QVERIFY(map.keys().count() == 2);
86 QVERIFY(!map.contains(QLatin1String("keys")));
87 QVERIFY(map.value(QLatin1String("keys")).isNull());
89 //QDeclarativePropertyMap has a deleteLater() slot
90 QTest::ignoreMessage(QtWarningMsg, "Creating property with name \"deleteLater\" is not permitted, conflicts with internal symbols. ");
91 map.insert(QLatin1String("deleteLater"), 1);
92 QVERIFY(map.keys().count() == 2);
93 QVERIFY(!map.contains(QLatin1String("deleteLater")));
94 QVERIFY(map.value(QLatin1String("deleteLater")).isNull());
96 //QDeclarativePropertyMap has an valueChanged() signal
97 QTest::ignoreMessage(QtWarningMsg, "Creating property with name \"valueChanged\" is not permitted, conflicts with internal symbols. ");
98 map.insert(QLatin1String("valueChanged"), 1);
99 QVERIFY(map.keys().count() == 2);
100 QVERIFY(!map.contains(QLatin1String("valueChanged")));
101 QVERIFY(map.value(QLatin1String("valueChanged")).isNull());
103 //but 'valueChange' should be ok
104 map.insert(QLatin1String("valueChange"), 1);
105 QVERIFY(map.keys().count() == 3);
106 QVERIFY(map.contains(QLatin1String("valueChange")));
107 QCOMPARE(map.value(QLatin1String("valueChange")), QVariant(1));
109 //'valueCHANGED' should be ok, too
110 map.insert(QLatin1String("valueCHANGED"), 1);
111 QVERIFY(map.keys().count() == 4);
112 QVERIFY(map.contains(QLatin1String("valueCHANGED")));
113 QCOMPARE(map.value(QLatin1String("valueCHANGED")), QVariant(1));
116 void tst_QDeclarativePropertyMap::operatorInsert()
118 QDeclarativePropertyMap map;
119 map[QLatin1String("key1")] = 100;
120 map[QLatin1String("key2")] = 200;
121 QVERIFY(map.keys().count() == 2);
123 QCOMPARE(map.value(QLatin1String("key1")), QVariant(100));
124 QCOMPARE(map.value(QLatin1String("key2")), QVariant(200));
126 map[QLatin1String("key1")] = "Hello World";
127 QCOMPARE(map.value(QLatin1String("key1")), QVariant("Hello World"));
130 void tst_QDeclarativePropertyMap::operatorValue()
132 QDeclarativePropertyMap map;
133 map.insert(QLatin1String("key1"),100);
134 map.insert(QLatin1String("key2"),200);
135 QVERIFY(map.count() == 2);
136 QVERIFY(map.contains(QLatin1String("key1")));
138 const QDeclarativePropertyMap &constMap = map;
140 QCOMPARE(constMap.value(QLatin1String("key1")), QVariant(100));
141 QCOMPARE(constMap.value(QLatin1String("key2")), QVariant(200));
142 QCOMPARE(constMap[QLatin1String("key1")], constMap.value(QLatin1String("key1")));
143 QCOMPARE(constMap[QLatin1String("key2")], constMap.value(QLatin1String("key2")));
146 void tst_QDeclarativePropertyMap::clear()
148 QDeclarativePropertyMap map;
149 map.insert(QLatin1String("key1"),100);
150 QVERIFY(map.keys().count() == 1);
152 QCOMPARE(map.value(QLatin1String("key1")), QVariant(100));
154 map.clear(QLatin1String("key1"));
155 QVERIFY(map.keys().count() == 1);
156 QVERIFY(map.contains(QLatin1String("key1")));
157 QCOMPARE(map.value(QLatin1String("key1")), QVariant());
160 void tst_QDeclarativePropertyMap::changed()
162 QDeclarativePropertyMap map;
163 QSignalSpy spy(&map, SIGNAL(valueChanged(const QString&, const QVariant&)));
164 map.insert(QLatin1String("key1"),100);
165 map.insert(QLatin1String("key2"),200);
166 QCOMPARE(spy.count(), 0);
168 map.clear(QLatin1String("key1"));
169 QCOMPARE(spy.count(), 0);
171 //make changes in QML
172 QDeclarativeEngine engine;
173 QDeclarativeContext *ctxt = engine.rootContext();
174 ctxt->setContextProperty(QLatin1String("testdata"), &map);
175 QDeclarativeComponent component(&engine);
176 component.setData("import QtQuick 2.0\nText { text: { testdata.key1 = 'Hello World'; 'X' } }",
177 QUrl::fromLocalFile(""));
178 QVERIFY(component.isReady());
179 QQuickText *txt = qobject_cast<QQuickText*>(component.create());
181 QCOMPARE(txt->text(), QString('X'));
182 QCOMPARE(spy.count(), 1);
183 QList<QVariant> arguments = spy.takeFirst();
184 QCOMPARE(arguments.count(), 2);
185 QCOMPARE(arguments.at(0).toString(),QLatin1String("key1"));
186 QCOMPARE(arguments.at(1).value<QVariant>(),QVariant("Hello World"));
187 QCOMPARE(map.value(QLatin1String("key1")), QVariant("Hello World"));
190 void tst_QDeclarativePropertyMap::count()
192 QDeclarativePropertyMap map;
193 QCOMPARE(map.isEmpty(), true);
194 map.insert(QLatin1String("key1"),100);
195 map.insert(QLatin1String("key2"),200);
196 QCOMPARE(map.count(), 2);
197 QCOMPARE(map.isEmpty(), false);
199 map.insert(QLatin1String("key3"),"Hello World");
200 QCOMPARE(map.count(), 3);
202 //clearing doesn't remove the key
203 map.clear(QLatin1String("key3"));
204 QCOMPARE(map.count(), 3);
205 QCOMPARE(map.size(), map.count());
208 void tst_QDeclarativePropertyMap::crashBug()
210 QDeclarativePropertyMap map;
212 QDeclarativeEngine engine;
213 QDeclarativeContext context(&engine);
214 context.setContextProperty("map", &map);
216 QDeclarativeComponent c(&engine);
217 c.setData("import QtQuick 2.0\nBinding { target: map; property: \"myProp\"; value: 10 + 23 }",QUrl());
218 QObject *obj = c.create(&context);
222 void tst_QDeclarativePropertyMap::QTBUG_17868()
224 QDeclarativePropertyMap map;
226 QDeclarativeEngine engine;
227 QDeclarativeContext context(&engine);
228 context.setContextProperty("map", &map);
229 map.insert("key", 1);
230 QDeclarativeComponent c(&engine);
231 c.setData("import QtQuick 2.0\nItem {property bool error:false; Component.onCompleted: {try{ map.keys(); }catch(e) {error=true;}}}",QUrl());
232 QObject *obj = c.create(&context);
234 QVERIFY(!obj->property("error").toBool());
239 QTEST_MAIN(tst_QDeclarativePropertyMap)
241 #include "tst_qdeclarativepropertymap.moc"