|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
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 <private/qdeclarativetext_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 }; |
|
65 |
|
66 void tst_QDeclarativePropertyMap::insert() |
|
67 { |
|
68 QDeclarativePropertyMap map; |
|
69 map.insert(QLatin1String("key1"),100); |
|
70 map.insert(QLatin1String("key2"),200); |
|
71 QVERIFY(map.keys().count() == 2); |
|
72 QVERIFY(map.contains(QLatin1String("key1"))); |
|
73 |
|
74 QCOMPARE(map.value(QLatin1String("key1")), QVariant(100)); |
|
75 QCOMPARE(map.value(QLatin1String("key2")), QVariant(200)); |
|
76 |
|
77 map.insert(QLatin1String("key1"),"Hello World"); |
|
78 QCOMPARE(map.value(QLatin1String("key1")), QVariant("Hello World")); |
|
79 } |
|
80 |
|
81 void tst_QDeclarativePropertyMap::operatorInsert() |
|
82 { |
|
83 QDeclarativePropertyMap map; |
|
84 map[QLatin1String("key1")] = 100; |
|
85 map[QLatin1String("key2")] = 200; |
|
86 QVERIFY(map.keys().count() == 2); |
|
87 |
|
88 QCOMPARE(map.value(QLatin1String("key1")), QVariant(100)); |
|
89 QCOMPARE(map.value(QLatin1String("key2")), QVariant(200)); |
|
90 |
|
91 map[QLatin1String("key1")] = "Hello World"; |
|
92 QCOMPARE(map.value(QLatin1String("key1")), QVariant("Hello World")); |
|
93 } |
|
94 |
|
95 void tst_QDeclarativePropertyMap::operatorValue() |
|
96 { |
|
97 QDeclarativePropertyMap map; |
|
98 map.insert(QLatin1String("key1"),100); |
|
99 map.insert(QLatin1String("key2"),200); |
|
100 QVERIFY(map.count() == 2); |
|
101 QVERIFY(map.contains(QLatin1String("key1"))); |
|
102 |
|
103 const QDeclarativePropertyMap &constMap = map; |
|
104 |
|
105 QCOMPARE(constMap.value(QLatin1String("key1")), QVariant(100)); |
|
106 QCOMPARE(constMap.value(QLatin1String("key2")), QVariant(200)); |
|
107 QCOMPARE(constMap[QLatin1String("key1")], constMap.value(QLatin1String("key1"))); |
|
108 QCOMPARE(constMap[QLatin1String("key2")], constMap.value(QLatin1String("key2"))); |
|
109 } |
|
110 |
|
111 void tst_QDeclarativePropertyMap::clear() |
|
112 { |
|
113 QDeclarativePropertyMap map; |
|
114 map.insert(QLatin1String("key1"),100); |
|
115 QVERIFY(map.keys().count() == 1); |
|
116 |
|
117 QCOMPARE(map.value(QLatin1String("key1")), QVariant(100)); |
|
118 |
|
119 map.clear(QLatin1String("key1")); |
|
120 QVERIFY(map.keys().count() == 1); |
|
121 QVERIFY(map.contains(QLatin1String("key1"))); |
|
122 QCOMPARE(map.value(QLatin1String("key1")), QVariant()); |
|
123 } |
|
124 |
|
125 void tst_QDeclarativePropertyMap::changed() |
|
126 { |
|
127 QDeclarativePropertyMap map; |
|
128 QSignalSpy spy(&map, SIGNAL(valueChanged(const QString&, const QVariant&))); |
|
129 map.insert(QLatin1String("key1"),100); |
|
130 map.insert(QLatin1String("key2"),200); |
|
131 QCOMPARE(spy.count(), 0); |
|
132 |
|
133 map.clear(QLatin1String("key1")); |
|
134 QCOMPARE(spy.count(), 0); |
|
135 |
|
136 //make changes in QML |
|
137 QDeclarativeEngine engine; |
|
138 QDeclarativeContext *ctxt = engine.rootContext(); |
|
139 ctxt->setContextProperty(QLatin1String("testdata"), &map); |
|
140 QDeclarativeComponent component(&engine); |
|
141 component.setData("import Qt 4.7\nText { text: { testdata.key1 = 'Hello World'; 'X' } }", |
|
142 QUrl::fromLocalFile("")); |
|
143 QVERIFY(component.isReady()); |
|
144 QDeclarativeText *txt = qobject_cast<QDeclarativeText*>(component.create()); |
|
145 QVERIFY(txt); |
|
146 QCOMPARE(txt->text(), QString('X')); |
|
147 QCOMPARE(spy.count(), 1); |
|
148 QList<QVariant> arguments = spy.takeFirst(); |
|
149 QCOMPARE(arguments.count(), 2); |
|
150 QCOMPARE(arguments.at(0).toString(),QLatin1String("key1")); |
|
151 QCOMPARE(arguments.at(1).value<QVariant>(),QVariant("Hello World")); |
|
152 QCOMPARE(map.value(QLatin1String("key1")), QVariant("Hello World")); |
|
153 } |
|
154 |
|
155 void tst_QDeclarativePropertyMap::count() |
|
156 { |
|
157 QDeclarativePropertyMap map; |
|
158 QCOMPARE(map.isEmpty(), true); |
|
159 map.insert(QLatin1String("key1"),100); |
|
160 map.insert(QLatin1String("key2"),200); |
|
161 QCOMPARE(map.count(), 2); |
|
162 QCOMPARE(map.isEmpty(), false); |
|
163 |
|
164 map.insert(QLatin1String("key3"),"Hello World"); |
|
165 QCOMPARE(map.count(), 3); |
|
166 |
|
167 //clearing doesn't remove the key |
|
168 map.clear(QLatin1String("key3")); |
|
169 QCOMPARE(map.count(), 3); |
|
170 QCOMPARE(map.size(), map.count()); |
|
171 } |
|
172 |
|
173 void tst_QDeclarativePropertyMap::crashBug() |
|
174 { |
|
175 QDeclarativePropertyMap map; |
|
176 |
|
177 QDeclarativeEngine engine; |
|
178 QDeclarativeContext context(&engine); |
|
179 context.setContextProperty("map", &map); |
|
180 |
|
181 QDeclarativeComponent c(&engine); |
|
182 c.setData("import Qt 4.7\nBinding { target: map; property: \"myProp\"; value: 10 + 23 }",QUrl()); |
|
183 QObject *obj = c.create(&context); |
|
184 delete obj; |
|
185 } |
|
186 |
|
187 QTEST_MAIN(tst_QDeclarativePropertyMap) |
|
188 |
|
189 #include "tst_qdeclarativepropertymap.moc" |