on c2ea397 help sign in
unity8/tests/plugins/Unity/resultstest.cpp
248 lines 11 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Michal Hruby <michal.hruby@canonical.com>
18 */
20#include <QTest>
21#include <dee.h>
22#include <glib.h>
24#include "resultstest.h"
25#include "categoryresults.h"
26#include "variantutils.h"
28static DeeModel* createBackendModel()
30 auto model = dee_sequence_model_new();
31 dee_model_set_schema(model, "s", "s", "u", "u", "s", "s", "s", "s", "a{sv}", NULL);
33 return model;
36void ResultsTest::testAllColumns()
38 auto deeModel = createBackendModel();
39 GVariantBuilder builder;
40 g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
41 g_variant_builder_add(&builder, "{sv}", "metadata-field", g_variant_new_string("foo"));
42 dee_model_append (deeModel,
43 "test:uri",
44 "themed-icon",
45 4,
46 0,
47 "application/octet-stream",
48 "Test",
49 "Comment",
50 "test:dnd-uri",
51 g_variant_builder_end(&builder));
53 CategoryResults* results = new CategoryResults(this);
54 results->setModel(deeModel);
56 auto index = results->index(0, 0); // there's just one result
57 QCOMPARE(index.data(CategoryResults::Roles::RoleUri).toString(), QString("test:uri"));
58 QCOMPARE(index.data(CategoryResults::Roles::RoleIconHint).toString(), QString("image://theme/themed-icon"));
59 QCOMPARE(index.data(CategoryResults::Roles::RoleCategory).toInt(), 4);
60 QCOMPARE(index.data(CategoryResults::Roles::RoleMimetype).toString(), QString("application/octet-stream"));
61 QCOMPARE(index.data(CategoryResults::Roles::RoleTitle).toString(), QString("Test"));
62 QCOMPARE(index.data(CategoryResults::Roles::RoleComment).toString(), QString("Comment"));
63 QCOMPARE(index.data(CategoryResults::Roles::RoleDndUri).toString(), QString("test:dnd-uri"));
64 auto metadata = index.data(CategoryResults::Roles::RoleMetadata).toHash();
65 QCOMPARE(metadata["metadata-field"].toString(), QString("foo"));
68void ResultsTest::testIconColumn_data()
70 QTest::addColumn<QString>("uri");
71 QTest::addColumn<QString>("giconString");
72 QTest::addColumn<QString>("result");
74 QTest::newRow("unspecified") << "test:uri" << "" << "";
75 QTest::newRow("absolute path") << "test:uri" << "/usr/share/icons/example.png" << "/usr/share/icons/example.png";
76 QTest::newRow("file uri") << "test:uri" << "file:///usr/share/icons/example.png" << "file:///usr/share/icons/example.png";
77 QTest::newRow("http uri") << "test:uri" << "http://images.ubuntu.com/example.jpg" << "http://images.ubuntu.com/example.jpg";
78 QTest::newRow("image uri") << "test:uri" << "image://thumbnail/with/arguments?passed_to=ImageProvider" << "image://thumbnail/with/arguments?passed_to=ImageProvider";
79 QTest::newRow("themed icon") << "test:uri" << "accessories-other" << "image://theme/accessories-other";
80 QTest::newRow("fileicon") << "test:uri" << ". GFileIcon http://example.org/resource.gif" << "http://example.org/resource.gif";
81 QTest::newRow("themedicon") << "test:uri" << ". GThemedIcon accessories-other accessories generic" << "image://theme/accessories-other,accessories,generic";
82 QTest::newRow("annotatedicon") << "test:uri" << ". UnityProtocolAnnotatedIcon %7B'base-icon':%20%3C'.%20GThemedIcon%20accessories-other%20accessories%20generic'%3E%7D" << "image://theme/accessories-other,accessories,generic";
83 QTest::newRow("thumbnailer icon") << "file:///usr/share/samples/video/foo.avi" << "" << "image://thumbnailer//usr/share/samples/video/foo.avi";
86void ResultsTest::testIconColumn()
88 QFETCH(QString, uri);
89 QFETCH(QString, giconString);
90 QFETCH(QString, result);
91 auto deeModel = createBackendModel();
92 dee_model_append (deeModel,
93 uri.toLocal8Bit().constData(),
94 giconString.toLocal8Bit().constData(),
95 0,
96 0,
97 "application/octet-stream",
98 "Test",
99 "",
100 "test:dnd-uri",
101 g_variant_new_array(g_variant_type_element(G_VARIANT_TYPE_VARDICT), NULL, 0));
103 CategoryResults* results = new CategoryResults(this);
104 results->setModel(deeModel);
106 auto index = results->index(0, 0); // there's just one result
107 auto transformedIcon = index.data(CategoryResults::Roles::RoleIconHint).toString();
108 QCOMPARE(transformedIcon, result);
111class GVariantWrapper
113public:
114 GVariantWrapper() : variant(NULL) {}
115 GVariantWrapper(GVariant* v) {
116 variant = v ? g_variant_ref_sink(v) : NULL;
117 }
118 GVariantWrapper(const GVariantWrapper& other) {
119 variant = other.variant ? g_variant_ref(other.variant) : NULL;
120 }
121 ~GVariantWrapper() {
122 if (variant) g_variant_unref(variant);
123 }
125 GVariant* variant;
126};
128Q_DECLARE_METATYPE(GVariantWrapper)
130void ResultsTest::testSpecialIcons_data()
132 QTest::addColumn<QString>("uri");
133 QTest::addColumn<GVariantWrapper>("metadata");
134 QTest::addColumn<QString>("result");
136 GVariant *inner;
137 GVariantBuilder builder, inner_builder;
139 g_variant_builder_init(&inner_builder, G_VARIANT_TYPE_VARDICT);
140 g_variant_builder_add(&inner_builder, "{sv}", "artist", g_variant_new_string("U2"));
141 g_variant_builder_add(&inner_builder, "{sv}", "album", g_variant_new_string("War"));
143 g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
144 g_variant_builder_add(&builder, "{sv}", "content", g_variant_builder_end(&inner_builder));
146 QTest::newRow("simple") << "file:///foo.mp3" << GVariantWrapper(g_variant_builder_end(&builder)) << "image://albumart/artist=U2&album=War";
148 g_variant_builder_init(&inner_builder, G_VARIANT_TYPE_VARDICT);
149 g_variant_builder_add(&inner_builder, "{sv}", "artist", g_variant_new_string("U2"));
150 g_variant_builder_add(&inner_builder, "{sv}", "album", g_variant_new_string("War/Joshua tree"));
151 inner = g_variant_builder_end(&inner_builder);
153 g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
154 g_variant_builder_add(&builder, "{sv}", "content", inner);
156 QTest::newRow("with-slash") << "file:///foo.mp3" << GVariantWrapper(g_variant_builder_end(&builder)) << "image://albumart/artist=U2&album=War/Joshua tree";
158 g_variant_builder_init(&inner_builder, G_VARIANT_TYPE_VARDICT);
159 g_variant_builder_add(&inner_builder, "{sv}", "artist", g_variant_new_string("U2"));
160 g_variant_builder_add(&inner_builder, "{sv}", "album", g_variant_new_string("War"));
161 inner = g_variant_builder_end(&inner_builder);
163 g_variant_builder_init(&inner_builder, G_VARIANT_TYPE_VARDICT);
164 g_variant_builder_add(&inner_builder, "{sv}", "content", inner);
166 g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
167 g_variant_builder_add(&builder, "{sv}", "content", g_variant_builder_end(&inner_builder));
169 QTest::newRow("nested") << "file:///foo.mp3" << GVariantWrapper(g_variant_builder_end(&builder)) << "image://albumart/artist=U2&album=War";
172void ResultsTest::testSpecialIcons()
174 QFETCH(QString, uri);
175 QFETCH(GVariantWrapper, metadata);
176 QFETCH(QString, result);
177 auto deeModel = createBackendModel();
178 dee_model_append (deeModel,
179 uri.toLocal8Bit().constData(),
180 "",
181 0,
182 0,
183 "audio/mp3",
184 "Test",
185 "",
186 uri.toLocal8Bit().constData(),
187 metadata.variant);
189 CategoryResults* results = new CategoryResults(this);
190 results->setModel(deeModel);
192 auto index = results->index(0, 0); // there's just one result
193 auto transformedIcon = index.data(CategoryResults::Roles::RoleIconHint).toString();
194 QCOMPARE(transformedIcon, result);
197void ResultsTest::testMetadataOverride_data()
199 QTest::addColumn<QString>("uri");
200 QTest::addColumn<QVariantHash>("result");
202 QVariantHash expected_result;
203 QVariantHash inner_hash;
205 expected_result = QVariantHash();
206 expected_result["scope-id"] = QVariant::fromValue(QString("applications.scope"));
207 expected_result["content"] = QVariant::fromValue(QVariantHash());
209 QTest::newRow("single scope") << "subscope:applications.scope" << expected_result;
211 expected_result = QVariantHash();
212 expected_result["scope-id"] = QVariant::fromValue(QString("applications.scope"));
213 inner_hash = QVariantHash();
214 inner_hash["foo"] = QVariant::fromValue(QString("bar"));
215 inner_hash["qoo"] = QVariant::fromValue(QString("baz"));
216 expected_result["content"] = QVariant::fromValue(inner_hash);
218 QTest::newRow("single scope with data") << "subscope:applications.scope?foo=bar&qoo=baz" << expected_result;
220 expected_result = QVariantHash();
221 inner_hash = QVariantHash();
222 inner_hash["scope-id"] = QVariant::fromValue(QString("applications-local.scope"));
223 inner_hash["content"] = QVariant::fromValue(QVariantHash());
224 expected_result["scope-id"] = QVariant::fromValue(QString("applications.scope"));
225 expected_result["content"] = QVariant::fromValue(inner_hash);
226 QTest::newRow("master scope") << "subscope:applications.scope/applications-local.scope" << expected_result;
228 expected_result = QVariantHash();
229 expected_result["foo"] = QVariant::fromValue(QString("baz"));
230 expected_result["qoo"] = QVariant::fromValue(QString("baz"));
231 inner_hash = QVariantHash();
232 inner_hash["scope-id"] = QVariant::fromValue(QString("applications-local.scope"));
233 inner_hash["content"] = QVariant::fromValue(expected_result);
234 expected_result = QVariantHash();
235 expected_result["scope-id"] = QVariant::fromValue(QString("applications.scope"));
236 expected_result["content"] = QVariant::fromValue(inner_hash);
237 QTest::newRow("nested with data") << "subscope:applications.scope/applications-local.scope?foo=baz&qoo=baz" << expected_result;
240void ResultsTest::testMetadataOverride()
242 QFETCH(QString, uri);
243 QFETCH(QVariantHash, result);
245 QCOMPARE(subscopeUriToMetadataHash(uri), result);
248QTEST_MAIN(ResultsTest)