Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativepropertymap / tst_qdeclarativepropertymap.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
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>
47 #include <QSignalSpy>
48
49 class tst_QDeclarativePropertyMap : public QObject
50 {
51     Q_OBJECT
52 public:
53     tst_QDeclarativePropertyMap() {}
54
55 private slots:
56     void insert();
57     void operatorInsert();
58     void operatorValue();
59     void clear();
60     void changed();
61     void count();
62
63     void crashBug();
64     void QTBUG_17868();
65 };
66
67 void tst_QDeclarativePropertyMap::insert()
68 {
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")));
74
75     QCOMPARE(map.value(QLatin1String("key1")), QVariant(100));
76     QCOMPARE(map.value(QLatin1String("key2")), QVariant(200));
77
78     map.insert(QLatin1String("key1"),"Hello World");
79     QCOMPARE(map.value(QLatin1String("key1")), QVariant("Hello World"));
80
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());
88
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());
95
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());
102
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));
108
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));
114 }
115
116 void tst_QDeclarativePropertyMap::operatorInsert()
117 {
118     QDeclarativePropertyMap map;
119     map[QLatin1String("key1")] = 100;
120     map[QLatin1String("key2")] = 200;
121     QVERIFY(map.keys().count() == 2);
122
123     QCOMPARE(map.value(QLatin1String("key1")), QVariant(100));
124     QCOMPARE(map.value(QLatin1String("key2")), QVariant(200));
125
126     map[QLatin1String("key1")] = "Hello World";
127     QCOMPARE(map.value(QLatin1String("key1")), QVariant("Hello World"));
128 }
129
130 void tst_QDeclarativePropertyMap::operatorValue()
131 {
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")));
137
138     const QDeclarativePropertyMap &constMap = map;
139
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")));
144 }
145
146 void tst_QDeclarativePropertyMap::clear()
147 {
148     QDeclarativePropertyMap map;
149     map.insert(QLatin1String("key1"),100);
150     QVERIFY(map.keys().count() == 1);
151
152     QCOMPARE(map.value(QLatin1String("key1")), QVariant(100));
153
154     map.clear(QLatin1String("key1"));
155     QVERIFY(map.keys().count() == 1);
156     QVERIFY(map.contains(QLatin1String("key1")));
157     QCOMPARE(map.value(QLatin1String("key1")), QVariant());
158 }
159
160 void tst_QDeclarativePropertyMap::changed()
161 {
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);
167
168     map.clear(QLatin1String("key1"));
169     QCOMPARE(spy.count(), 0);
170
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());
180     QVERIFY(txt);
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"));
188 }
189
190 void tst_QDeclarativePropertyMap::count()
191 {
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);
198
199     map.insert(QLatin1String("key3"),"Hello World");
200     QCOMPARE(map.count(), 3);
201
202     //clearing doesn't remove the key
203     map.clear(QLatin1String("key3"));
204     QCOMPARE(map.count(), 3);
205     QCOMPARE(map.size(), map.count());
206 }
207
208 void tst_QDeclarativePropertyMap::crashBug()
209 {
210     QDeclarativePropertyMap map;
211
212     QDeclarativeEngine engine;
213     QDeclarativeContext context(&engine);
214     context.setContextProperty("map", &map);
215
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);
219     delete obj;
220 }
221
222 void tst_QDeclarativePropertyMap::QTBUG_17868()
223 {
224     QDeclarativePropertyMap map;
225
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);
233     QVERIFY(obj);
234     QVERIFY(!obj->property("error").toBool());
235     delete obj;
236
237 }
238
239 QTEST_MAIN(tst_QDeclarativePropertyMap)
240
241 #include "tst_qdeclarativepropertymap.moc"