1/****************************************************************************
2**
3** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtDocGallery module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42//TESTED_COMPONENT=src/gallery
43
44#include <qgalleryresource.h>
45
46#include <QtTest/QtTest>
47
48Q_DECLARE_METATYPE(QT_DOCGALLERY_PREPEND_NAMESPACE(QGalleryResource))
49
50QT_USE_DOCGALLERY_NAMESPACE
51
52class tst_QGalleryResource : public QObject
53{
54 Q_OBJECT
55private Q_SLOTS:
56 void null();
57 void url();
58 void attributes();
59 void equality_data();
60 void equality();
61 void copy();
62};
63
64void tst_QGalleryResource::null()
65{
66 const int sampleRateKey = 3;
67 const int audioCodecKey = 6;
68 const int videoCodecKey = 9;
69 const int invalidKey = 4;
70
71 const QMap<int, QVariant> attributes;
72
73 QGalleryResource resource;
74
75 QCOMPARE(resource.url(), QUrl());
76 QCOMPARE(resource.attributes(), attributes);
77 QCOMPARE(resource.attribute(sampleRateKey), QVariant());
78 QCOMPARE(resource.attribute(audioCodecKey), QVariant());
79 QCOMPARE(resource.attribute(videoCodecKey), QVariant());
80 QCOMPARE(resource.attribute(invalidKey), QVariant());
81}
82
83void tst_QGalleryResource::url()
84{
85 const int sampleRateKey = 3;
86 const int audioCodecKey = 6;
87 const int videoCodecKey = 9;
88 const int invalidKey = 4;
89
90 const QMap<int, QVariant> attributes;
91
92 const QUrl url("file:///a/local/video/file.m4v");
93
94 QGalleryResource resource(url);
95
96 QCOMPARE(resource.url(), url);
97 QCOMPARE(resource.attributes(), attributes);
98 QCOMPARE(resource.attribute(sampleRateKey), QVariant());
99 QCOMPARE(resource.attribute(audioCodecKey), QVariant());
100 QCOMPARE(resource.attribute(videoCodecKey), QVariant());
101 QCOMPARE(resource.attribute(invalidKey), QVariant());
102}
103
104void tst_QGalleryResource::attributes()
105{
106 const int sampleRateKey = 3;
107 const int audioCodecKey = 6;
108 const int videoCodecKey = 9;
109 const int invalidKey = 4;
110
111 const QVariant sampleRate = 44100;
112 const QVariant audioCodec = QLatin1String("aac");
113 const QVariant videoCodec = QLatin1String("h264");
114
115 QMap<int, QVariant> attributes;
116 attributes.insert(akey: sampleRateKey, avalue: sampleRate);
117 attributes.insert(akey: audioCodecKey, avalue: audioCodec);
118 attributes.insert(akey: videoCodecKey, avalue: videoCodec);
119
120 const QUrl url("file:///a/local/video/file.m4v");
121
122 QGalleryResource resource(url, attributes);
123
124 QCOMPARE(resource.url(), url);
125 QCOMPARE(resource.attributes(), attributes);
126 QCOMPARE(resource.attribute(sampleRateKey), sampleRate);
127 QCOMPARE(resource.attribute(audioCodecKey), audioCodec);
128 QCOMPARE(resource.attribute(videoCodecKey), videoCodec);
129 QCOMPARE(resource.attribute(invalidKey), QVariant());
130}
131
132void tst_QGalleryResource::equality_data()
133{
134 const int sampleRateKey = 3;
135 const int audioCodecKey = 6;
136 const int videoCodecKey = 9;
137
138 const QUrl url1("file:///a/local/video/file.m4v");
139 const QUrl url2("http://example.com/video.mpg");
140
141 QMap<int, QVariant> attributes1;
142 attributes1.insert(akey: sampleRateKey, avalue: 44100);
143 attributes1.insert(akey: audioCodecKey, avalue: QLatin1String("aac"));
144 attributes1.insert(akey: videoCodecKey, avalue: QLatin1String("h264"));
145
146 QMap<int, QVariant> attributes2;
147 attributes2.insert(akey: sampleRateKey, avalue: 22050);
148
149 const QGalleryResource resource1(url1);
150 const QGalleryResource resource2(url2);
151 const QGalleryResource resource3(url1, attributes1);
152 const QGalleryResource resource4(url1, attributes2);
153 const QGalleryResource resource5(url2, attributes1);
154
155 QTest::addColumn<QGalleryResource>(name: "resource1");
156 QTest::addColumn<QGalleryResource>(name: "resource2");
157 QTest::addColumn<bool>(name: "isEqual");
158
159 QTest::newRow(dataTag: "Null resources")
160 << QGalleryResource()
161 << QGalleryResource()
162 << true;
163 QTest::newRow(dataTag: "Same resource (No attributes)")
164 << resource1
165 << resource1
166 << true;
167 QTest::newRow(dataTag: "Same resource (Attributes)")
168 << resource3
169 << resource3
170 << true;
171 QTest::newRow(dataTag: "Equal resource (No attributes)")
172 << QGalleryResource(url1)
173 << QGalleryResource(url1)
174 << true;
175 QTest::newRow(dataTag: "Equal resource (Attributes)")
176 << QGalleryResource(url1, attributes1)
177 << QGalleryResource(url1, attributes1)
178 << true;
179
180 QTest::newRow(dataTag: "Null resource != Non-null (No attributes)")
181 << QGalleryResource()
182 << resource1
183 << false;
184 QTest::newRow(dataTag: "Null resource != Non-null (Attributes)")
185 << QGalleryResource()
186 << resource3
187 << false;
188 QTest::newRow(dataTag: "Different URL, no attributes")
189 << resource1
190 << resource2
191 << false;
192 QTest::newRow(dataTag: "Same URL, With/without attributes")
193 << resource1
194 << resource3
195 << false;
196 QTest::newRow(dataTag: "Same Url, Different attributes")
197 << resource3
198 << resource4
199 << false;
200 QTest::newRow(dataTag: "Different URL, same attributes")
201 << resource3
202 << resource5
203 << false;
204}
205
206void tst_QGalleryResource::equality()
207{
208 QFETCH(QGalleryResource, resource1);
209 QFETCH(QGalleryResource, resource2);
210 QFETCH(bool, isEqual);
211
212 QCOMPARE(resource1 == resource2, isEqual);
213 QCOMPARE(resource2 == resource1 ,isEqual);
214 QCOMPARE(resource1 != resource2, !isEqual);
215 QCOMPARE(resource2 != resource1, !isEqual);
216}
217
218
219void tst_QGalleryResource::copy()
220{
221 const int sampleRateKey = 3;
222 const int audioCodecKey = 6;
223 const int videoCodecKey = 9;
224
225 const QUrl url1("file:///a/local/video/file.m4v");
226 const QUrl url2("http://example.com/video.mpg");
227
228 QMap<int, QVariant> attributes1;
229 attributes1.insert(akey: sampleRateKey, avalue: 44100);
230 attributes1.insert(akey: audioCodecKey, avalue: QLatin1String("aac"));
231 attributes1.insert(akey: videoCodecKey, avalue: QLatin1String("h264"));
232
233 QMap<int, QVariant> attributes2;
234 attributes2.insert(akey: sampleRateKey, avalue: 22050);
235
236 QGalleryResource resource1(url1);
237 QGalleryResource resource2(url1, attributes1);
238 QGalleryResource resource3(url2, attributes2);
239
240 QCOMPARE(resource1.url(), url1);
241 QCOMPARE(resource1.attributes().isEmpty(), true);
242
243 QCOMPARE(resource2.url(), url1);
244 QCOMPARE(resource2.attributes(), attributes1);
245
246 QCOMPARE(resource3.url(), url2);
247 QCOMPARE(resource3.attributes(), attributes2);
248
249 resource1 = resource2;
250 QCOMPARE(resource1.url(), url1);
251 QCOMPARE(resource1.attributes(), attributes1);
252
253 resource2 = resource3;
254 QCOMPARE(resource2.url(), url2);
255 QCOMPARE(resource2.attributes(), attributes2);
256}
257
258
259QTEST_MAIN(tst_QGalleryResource)
260
261#include "tst_qgalleryresource.moc"
262

source code of qtdocgallery/tests/auto/qgalleryresource/tst_qgalleryresource.cpp