Changeset View
Changeset View
Standalone View
Standalone View
tests/testnotificationlistener.cpp
Show First 20 Lines • Show All 61 Lines • ▼ Show 20 Line(s) | |||||
62 | 62 | | |||
63 | }; | 63 | }; | ||
64 | 64 | | |||
65 | // Tweaked Device for testing: | 65 | // Tweaked Device for testing: | ||
66 | class TestDevice: public Device | 66 | class TestDevice: public Device | ||
67 | { | 67 | { | ||
68 | Q_OBJECT | 68 | Q_OBJECT | ||
69 | private: | 69 | private: | ||
70 | int sentPackages; | 70 | int sentPackets; | ||
71 | NetworkPackage* lastPackage; | 71 | NetworkPacket* lastPacket; | ||
72 | 72 | | |||
73 | public: | 73 | public: | ||
74 | explicit TestDevice(QObject* parent, const QString& id) | 74 | explicit TestDevice(QObject* parent, const QString& id) | ||
75 | : Device (parent, id) | 75 | : Device (parent, id) | ||
76 | , sentPackages(0) | 76 | , sentPackets(0) | ||
77 | , lastPackage(nullptr) | 77 | , lastPacket(nullptr) | ||
78 | {} | 78 | {} | ||
79 | 79 | | |||
80 | ~TestDevice() override | 80 | ~TestDevice() override | ||
81 | { | 81 | { | ||
82 | delete lastPackage; | 82 | delete lastPacket; | ||
83 | } | 83 | } | ||
84 | 84 | | |||
85 | int getSentPackages() const | 85 | int getSentPackets() const | ||
86 | { | 86 | { | ||
87 | return sentPackages; | 87 | return sentPackets; | ||
88 | } | 88 | } | ||
89 | 89 | | |||
90 | NetworkPackage* getLastPackage() | 90 | NetworkPacket* getLastPacket() | ||
91 | { | 91 | { | ||
92 | return lastPackage; | 92 | return lastPacket; | ||
93 | } | 93 | } | ||
94 | 94 | | |||
95 | void deleteLastPackage() | 95 | void deleteLastPacket() | ||
96 | { | 96 | { | ||
97 | delete lastPackage; | 97 | delete lastPacket; | ||
98 | lastPackage = nullptr; | 98 | lastPacket = nullptr; | ||
99 | } | 99 | } | ||
100 | 100 | | |||
101 | public Q_SLOTS: | 101 | public Q_SLOTS: | ||
102 | bool sendPackage(NetworkPackage& np) override | 102 | bool sendPacket(NetworkPacket& np) override | ||
103 | { | 103 | { | ||
104 | ++sentPackages; | 104 | ++sentPackets; | ||
105 | // copy package manually to allow for inspection (can't use copy-constructor) | 105 | // copy packet manually to allow for inspection (can't use copy-constructor) | ||
106 | deleteLastPackage(); | 106 | deleteLastPacket(); | ||
107 | lastPackage = new NetworkPackage(np.type()); | 107 | lastPacket = new NetworkPacket(np.type()); | ||
108 | Q_ASSERT(lastPackage); | 108 | Q_ASSERT(lastPacket); | ||
109 | for (QVariantMap::ConstIterator iter = np.body().constBegin(); | 109 | for (QVariantMap::ConstIterator iter = np.body().constBegin(); | ||
110 | iter != np.body().constEnd(); iter++) | 110 | iter != np.body().constEnd(); iter++) | ||
111 | lastPackage->set(iter.key(), iter.value()); | 111 | lastPacket->set(iter.key(), iter.value()); | ||
112 | lastPackage->setPayload(np.payload(), np.payloadSize()); | 112 | lastPacket->setPayload(np.payload(), np.payloadSize()); | ||
113 | return true; | 113 | return true; | ||
114 | } | 114 | } | ||
115 | }; | 115 | }; | ||
116 | 116 | | |||
117 | // Tweaked NotificationsListener for testing: | 117 | // Tweaked NotificationsListener for testing: | ||
118 | class TestedNotificationsListener: public NotificationsListener | 118 | class TestedNotificationsListener: public NotificationsListener | ||
119 | { | 119 | { | ||
120 | Q_OBJECT | 120 | Q_OBJECT | ||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Line(s) | 175 | { | |||
176 | // | 176 | // | ||
177 | // set things up: | 177 | // set things up: | ||
178 | // | 178 | // | ||
179 | 179 | | |||
180 | QString dId(QStringLiteral("testid")); | 180 | QString dId(QStringLiteral("testid")); | ||
181 | TestDevice* d = new TestDevice(nullptr, dId); | 181 | TestDevice* d = new TestDevice(nullptr, dId); | ||
182 | 182 | | |||
183 | int proxiedNotifications = 0; | 183 | int proxiedNotifications = 0; | ||
184 | QCOMPARE(proxiedNotifications, d->getSentPackages()); | 184 | QCOMPARE(proxiedNotifications, d->getSentPackets()); | ||
185 | plugin = new TestNotificationsPlugin(this, | 185 | plugin = new TestNotificationsPlugin(this, | ||
186 | QVariantList({ QVariant::fromValue<Device*>(d), | 186 | QVariantList({ QVariant::fromValue<Device*>(d), | ||
187 | "notifications_plugin", | 187 | "notifications_plugin", | ||
188 | {"kdeconnect.notification"}})); | 188 | {"kdeconnect.notification"}})); | ||
189 | QVERIFY(plugin->getNotificationsListener()); | 189 | QVERIFY(plugin->getNotificationsListener()); | ||
190 | delete plugin->getNotificationsListener(); | 190 | delete plugin->getNotificationsListener(); | ||
191 | 191 | | |||
192 | // inject our tweaked NotificationsListener: | 192 | // inject our tweaked NotificationsListener: | ||
Show All 24 Lines | |||||
217 | QString body(QStringLiteral("some-body")); | 217 | QString body(QStringLiteral("some-body")); | ||
218 | QString icon(QStringLiteral("some-icon")); | 218 | QString icon(QStringLiteral("some-icon")); | ||
219 | QString summary(QStringLiteral("some-summary")); | 219 | QString summary(QStringLiteral("some-summary")); | ||
220 | 220 | | |||
221 | // regular Notify call that is synchronized ... | 221 | // regular Notify call that is synchronized ... | ||
222 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0); | 222 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0); | ||
223 | // ... should return replacesId, | 223 | // ... should return replacesId, | ||
224 | QCOMPARE(retId, replacesId); | 224 | QCOMPARE(retId, replacesId); | ||
225 | // ... have triggered sending a package | 225 | // ... have triggered sending a packet | ||
226 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 226 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
227 | // ... with our properties, | 227 | // ... with our properties, | ||
228 | QCOMPARE(d->getLastPackage()->get<uint>("id"), replacesId); | 228 | QCOMPARE(d->getLastPacket()->get<uint>("id"), replacesId); | ||
229 | QCOMPARE(d->getLastPackage()->get<QString>("appName"), appName); | 229 | QCOMPARE(d->getLastPacket()->get<QString>("appName"), appName); | ||
230 | QCOMPARE(d->getLastPackage()->get<QString>("ticker"), summary + ": " + body); | 230 | QCOMPARE(d->getLastPacket()->get<QString>("ticker"), summary + ": " + body); | ||
231 | QCOMPARE(d->getLastPackage()->get<bool>("isClearable"), true); | 231 | QCOMPARE(d->getLastPacket()->get<bool>("isClearable"), true); | ||
232 | QCOMPARE(d->getLastPackage()->hasPayload(), false); | 232 | QCOMPARE(d->getLastPacket()->hasPayload(), false); | ||
233 | 233 | | |||
234 | // ... and create a new application internally that is initialized correctly: | 234 | // ... and create a new application internally that is initialized correctly: | ||
235 | QCOMPARE(listener->getApplications().count(), 1); | 235 | QCOMPARE(listener->getApplications().count(), 1); | ||
236 | QVERIFY(listener->getApplications().contains(appName)); | 236 | QVERIFY(listener->getApplications().contains(appName)); | ||
237 | QVERIFY(listener->getApplications()[appName].active); | 237 | QVERIFY(listener->getApplications()[appName].active); | ||
238 | QCOMPARE(listener->getApplications()[appName].name, appName); | 238 | QCOMPARE(listener->getApplications()[appName].name, appName); | ||
239 | QVERIFY(listener->getApplications()[appName].blacklistExpression.pattern().isEmpty()); | 239 | QVERIFY(listener->getApplications()[appName].blacklistExpression.pattern().isEmpty()); | ||
240 | QCOMPARE(listener->getApplications()[appName].name, appName); | 240 | QCOMPARE(listener->getApplications()[appName].name, appName); | ||
241 | QCOMPARE(listener->getApplications()[appName].icon, icon); | 241 | QCOMPARE(listener->getApplications()[appName].icon, icon); | ||
242 | 242 | | |||
243 | // another one, with other timeout and urgency values: | 243 | // another one, with other timeout and urgency values: | ||
244 | QString appName2(QStringLiteral("some-appName2")); | 244 | QString appName2(QStringLiteral("some-appName2")); | ||
245 | QString body2(QStringLiteral("some-body2")); | 245 | QString body2(QStringLiteral("some-body2")); | ||
246 | QString icon2(QStringLiteral("some-icon2")); | 246 | QString icon2(QStringLiteral("some-icon2")); | ||
247 | QString summary2(QStringLiteral("some-summary2")); | 247 | QString summary2(QStringLiteral("some-summary2")); | ||
248 | 248 | | |||
249 | retId = listener->Notify(appName2, replacesId+1, icon2, summary2, body2, {}, {{"urgency", 2}}, 10); | 249 | retId = listener->Notify(appName2, replacesId+1, icon2, summary2, body2, {}, {{"urgency", 2}}, 10); | ||
250 | QCOMPARE(retId, replacesId+1); | 250 | QCOMPARE(retId, replacesId+1); | ||
251 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 251 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
252 | QCOMPARE(d->getLastPackage()->get<uint>("id"), replacesId+1); | 252 | QCOMPARE(d->getLastPacket()->get<uint>("id"), replacesId+1); | ||
253 | QCOMPARE(d->getLastPackage()->get<QString>("appName"), appName2); | 253 | QCOMPARE(d->getLastPacket()->get<QString>("appName"), appName2); | ||
254 | QCOMPARE(d->getLastPackage()->get<QString>("ticker"), summary2 + ": " + body2); | 254 | QCOMPARE(d->getLastPacket()->get<QString>("ticker"), summary2 + ": " + body2); | ||
255 | QCOMPARE(d->getLastPackage()->get<bool>("isClearable"), false); // timeout != 0 | 255 | QCOMPARE(d->getLastPacket()->get<bool>("isClearable"), false); // timeout != 0 | ||
256 | QCOMPARE(d->getLastPackage()->hasPayload(), false); | 256 | QCOMPARE(d->getLastPacket()->hasPayload(), false); | ||
257 | QCOMPARE(listener->getApplications().count(), 2); | 257 | QCOMPARE(listener->getApplications().count(), 2); | ||
258 | QVERIFY(listener->getApplications().contains(appName2)); | 258 | QVERIFY(listener->getApplications().contains(appName2)); | ||
259 | QVERIFY(listener->getApplications().contains(appName)); | 259 | QVERIFY(listener->getApplications().contains(appName)); | ||
260 | 260 | | |||
261 | // if persistent-only is set, timeouts > 0 are not synced: | 261 | // if persistent-only is set, timeouts > 0 are not synced: | ||
262 | plugin->config()->set(QStringLiteral("generalPersistent"), true); | 262 | plugin->config()->set(QStringLiteral("generalPersistent"), true); | ||
263 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 1); | 263 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 1); | ||
264 | QCOMPARE(retId, 0U); | 264 | QCOMPARE(retId, 0U); | ||
265 | QCOMPARE(proxiedNotifications, d->getSentPackages()); | 265 | QCOMPARE(proxiedNotifications, d->getSentPackets()); | ||
266 | retId = listener->Notify(appName2, replacesId, icon2, summary2, body2, {}, {{}}, 3); | 266 | retId = listener->Notify(appName2, replacesId, icon2, summary2, body2, {}, {{}}, 3); | ||
267 | QCOMPARE(retId, 0U); | 267 | QCOMPARE(retId, 0U); | ||
268 | QCOMPARE(proxiedNotifications, d->getSentPackages()); | 268 | QCOMPARE(proxiedNotifications, d->getSentPackets()); | ||
269 | // but timeout == 0 is | 269 | // but timeout == 0 is | ||
270 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0); | 270 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0); | ||
271 | QCOMPARE(retId, replacesId); | 271 | QCOMPARE(retId, replacesId); | ||
272 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 272 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
273 | plugin->config()->set(QStringLiteral("generalPersistent"), false); | 273 | plugin->config()->set(QStringLiteral("generalPersistent"), false); | ||
274 | 274 | | |||
275 | // if min-urgency is set, lower urgency levels are not synced: | 275 | // if min-urgency is set, lower urgency levels are not synced: | ||
276 | plugin->config()->set(QStringLiteral("generalUrgency"), 1); | 276 | plugin->config()->set(QStringLiteral("generalUrgency"), 1); | ||
277 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{"urgency", 0}}, 0); | 277 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{"urgency", 0}}, 0); | ||
278 | QCOMPARE(retId, 0U); | 278 | QCOMPARE(retId, 0U); | ||
279 | QCOMPARE(proxiedNotifications, d->getSentPackages()); | 279 | QCOMPARE(proxiedNotifications, d->getSentPackets()); | ||
280 | // equal urgency is | 280 | // equal urgency is | ||
281 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{"urgency", 1}}, 0); | 281 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{"urgency", 1}}, 0); | ||
282 | QCOMPARE(retId, replacesId); | 282 | QCOMPARE(retId, replacesId); | ||
283 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 283 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
284 | // higher urgency as well | 284 | // higher urgency as well | ||
285 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{"urgency", 2}}, 0); | 285 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{"urgency", 2}}, 0); | ||
286 | QCOMPARE(retId, replacesId); | 286 | QCOMPARE(retId, replacesId); | ||
287 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 287 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
288 | plugin->config()->set(QStringLiteral("generalUrgency"), 0); | 288 | plugin->config()->set(QStringLiteral("generalUrgency"), 0); | ||
289 | 289 | | |||
290 | // notifications for a deactivated application are not synced: | 290 | // notifications for a deactivated application are not synced: | ||
291 | QVERIFY(listener->getApplications().contains(appName)); | 291 | QVERIFY(listener->getApplications().contains(appName)); | ||
292 | listener->getApplications()[appName].active = false; | 292 | listener->getApplications()[appName].active = false; | ||
293 | QVERIFY(!listener->getApplications()[appName].active); | 293 | QVERIFY(!listener->getApplications()[appName].active); | ||
294 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{"urgency", 0}}, 0); | 294 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{"urgency", 0}}, 0); | ||
295 | QCOMPARE(retId, 0U); | 295 | QCOMPARE(retId, 0U); | ||
296 | QCOMPARE(proxiedNotifications, d->getSentPackages()); | 296 | QCOMPARE(proxiedNotifications, d->getSentPackets()); | ||
297 | // others are still: | 297 | // others are still: | ||
298 | retId = listener->Notify(appName2, replacesId+1, icon2, summary2, body2, {}, {{}}, 0); | 298 | retId = listener->Notify(appName2, replacesId+1, icon2, summary2, body2, {}, {{}}, 0); | ||
299 | QCOMPARE(retId, replacesId+1); | 299 | QCOMPARE(retId, replacesId+1); | ||
300 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 300 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
301 | // back to normal: | 301 | // back to normal: | ||
302 | listener->getApplications()[appName].active = true; | 302 | listener->getApplications()[appName].active = true; | ||
303 | QVERIFY(listener->getApplications()[appName].active); | 303 | QVERIFY(listener->getApplications()[appName].active); | ||
304 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0); | 304 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0); | ||
305 | QCOMPARE(retId, replacesId); | 305 | QCOMPARE(retId, replacesId); | ||
306 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 306 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
307 | 307 | | |||
308 | // notifications with blacklisted subjects are not synced: | 308 | // notifications with blacklisted subjects are not synced: | ||
309 | QVERIFY(listener->getApplications().contains(appName)); | 309 | QVERIFY(listener->getApplications().contains(appName)); | ||
310 | listener->getApplications()[appName].blacklistExpression.setPattern(QStringLiteral("black[12]|foo(bar|baz)")); | 310 | listener->getApplications()[appName].blacklistExpression.setPattern(QStringLiteral("black[12]|foo(bar|baz)")); | ||
311 | retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary black1"), body, {}, {{}}, 0); | 311 | retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary black1"), body, {}, {{}}, 0); | ||
312 | QCOMPARE(retId, 0U); | 312 | QCOMPARE(retId, 0U); | ||
313 | QCOMPARE(proxiedNotifications, d->getSentPackages()); | 313 | QCOMPARE(proxiedNotifications, d->getSentPackets()); | ||
314 | retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary foobar"), body, {}, {{}}, 0); | 314 | retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary foobar"), body, {}, {{}}, 0); | ||
315 | QCOMPARE(retId, 0U); | 315 | QCOMPARE(retId, 0U); | ||
316 | QCOMPARE(proxiedNotifications, d->getSentPackages()); | 316 | QCOMPARE(proxiedNotifications, d->getSentPackets()); | ||
317 | // other subjects are synced: | 317 | // other subjects are synced: | ||
318 | retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary foo"), body, {}, {{}}, 0); | 318 | retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary foo"), body, {}, {{}}, 0); | ||
319 | QCOMPARE(retId, replacesId); | 319 | QCOMPARE(retId, replacesId); | ||
320 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 320 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
321 | retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary black3"), body, {}, {{}}, 0); | 321 | retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary black3"), body, {}, {{}}, 0); | ||
322 | QCOMPARE(retId, replacesId); | 322 | QCOMPARE(retId, replacesId); | ||
323 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 323 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
324 | // also body is checked by blacklist if requested: | 324 | // also body is checked by blacklist if requested: | ||
325 | plugin->config()->set(QStringLiteral("generalIncludeBody"), true); | 325 | plugin->config()->set(QStringLiteral("generalIncludeBody"), true); | ||
326 | retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body black1"), {}, {{}}, 0); | 326 | retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body black1"), {}, {{}}, 0); | ||
327 | QCOMPARE(retId, 0U); | 327 | QCOMPARE(retId, 0U); | ||
328 | QCOMPARE(proxiedNotifications, d->getSentPackages()); | 328 | QCOMPARE(proxiedNotifications, d->getSentPackets()); | ||
329 | retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body foobaz"), {}, {{}}, 0); | 329 | retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body foobaz"), {}, {{}}, 0); | ||
330 | QCOMPARE(retId, 0U); | 330 | QCOMPARE(retId, 0U); | ||
331 | QCOMPARE(proxiedNotifications, d->getSentPackages()); | 331 | QCOMPARE(proxiedNotifications, d->getSentPackets()); | ||
332 | // body does not matter if inclusion was not requested: | 332 | // body does not matter if inclusion was not requested: | ||
333 | plugin->config()->set(QStringLiteral("generalIncludeBody"), false); | 333 | plugin->config()->set(QStringLiteral("generalIncludeBody"), false); | ||
334 | retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body black1"), {}, {{}}, 0); | 334 | retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body black1"), {}, {{}}, 0); | ||
335 | QCOMPARE(retId, replacesId); | 335 | QCOMPARE(retId, replacesId); | ||
336 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 336 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
337 | // without body, also ticker value is different: | 337 | // without body, also ticker value is different: | ||
338 | QCOMPARE(d->getLastPackage()->get<QString>("ticker"), summary); | 338 | QCOMPARE(d->getLastPacket()->get<QString>("ticker"), summary); | ||
339 | retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body foobaz"), {}, {{}}, 0); | 339 | retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body foobaz"), {}, {{}}, 0); | ||
340 | QCOMPARE(retId, replacesId); | 340 | QCOMPARE(retId, replacesId); | ||
341 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 341 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
342 | 342 | | |||
343 | // back to normal: | 343 | // back to normal: | ||
344 | listener->getApplications()[appName].blacklistExpression.setPattern(QLatin1String("")); | 344 | listener->getApplications()[appName].blacklistExpression.setPattern(QLatin1String("")); | ||
345 | plugin->config()->set(QStringLiteral("generalIncludeBody"), true); | 345 | plugin->config()->set(QStringLiteral("generalIncludeBody"), true); | ||
346 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0); | 346 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0); | ||
347 | QCOMPARE(retId, replacesId); | 347 | QCOMPARE(retId, replacesId); | ||
348 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 348 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
349 | retId = listener->Notify(appName2, replacesId, icon2, summary2, body2, {}, {{}}, 0); | 349 | retId = listener->Notify(appName2, replacesId, icon2, summary2, body2, {}, {{}}, 0); | ||
350 | QCOMPARE(retId, replacesId); | 350 | QCOMPARE(retId, replacesId); | ||
351 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 351 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
352 | 352 | | |||
353 | // icon synchronization: | 353 | // icon synchronization: | ||
354 | QStringList iconPaths; | 354 | QStringList iconPaths; | ||
355 | // appIcon | 355 | // appIcon | ||
356 | int count = 0; | 356 | int count = 0; | ||
357 | const QStringList icons = KIconLoader::global()->queryIcons(-KIconLoader::SizeEnormous, KIconLoader::Application); | 357 | const QStringList icons = KIconLoader::global()->queryIcons(-KIconLoader::SizeEnormous, KIconLoader::Application); | ||
358 | for (const auto& iconName : icons) { | 358 | for (const auto& iconName : icons) { | ||
359 | if (!iconName.endsWith(QLatin1String(".png"))) | 359 | if (!iconName.endsWith(QLatin1String(".png"))) | ||
360 | continue; | 360 | continue; | ||
361 | if (count++ > 3) // max 3 iterations | 361 | if (count++ > 3) // max 3 iterations | ||
362 | break; | 362 | break; | ||
363 | iconPaths.append(iconName); // memorize some paths for later | 363 | iconPaths.append(iconName); // memorize some paths for later | ||
364 | 364 | | |||
365 | // existing icons are sync-ed if requested | 365 | // existing icons are sync-ed if requested | ||
366 | plugin->config()->set(QStringLiteral("generalSynchronizeIcons"), true); | 366 | plugin->config()->set(QStringLiteral("generalSynchronizeIcons"), true); | ||
367 | QFileInfo fi(iconName); | 367 | QFileInfo fi(iconName); | ||
368 | retId = listener->Notify(appName, replacesId, fi.baseName(), summary, body, {}, {{}}, 0); | 368 | retId = listener->Notify(appName, replacesId, fi.baseName(), summary, body, {}, {{}}, 0); | ||
369 | QCOMPARE(retId, replacesId); | 369 | QCOMPARE(retId, replacesId); | ||
370 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 370 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
371 | QVERIFY(d->getLastPackage()->hasPayload()); | 371 | QVERIFY(d->getLastPacket()->hasPayload()); | ||
372 | QCOMPARE(d->getLastPackage()->payloadSize(), fi.size()); | 372 | QCOMPARE(d->getLastPacket()->payloadSize(), fi.size()); | ||
373 | // works also with abolute paths | 373 | // works also with abolute paths | ||
374 | retId = listener->Notify(appName, replacesId, iconName, summary, body, {}, {{}}, 0); | 374 | retId = listener->Notify(appName, replacesId, iconName, summary, body, {}, {{}}, 0); | ||
375 | QCOMPARE(retId, replacesId); | 375 | QCOMPARE(retId, replacesId); | ||
376 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 376 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
377 | QVERIFY(d->getLastPackage()->hasPayload()); | 377 | QVERIFY(d->getLastPacket()->hasPayload()); | ||
378 | QCOMPARE(d->getLastPackage()->payloadSize(), fi.size()); | 378 | QCOMPARE(d->getLastPacket()->payloadSize(), fi.size()); | ||
379 | // extensions other than png are not accepted: | 379 | // extensions other than png are not accepted: | ||
380 | retId = listener->Notify(appName, replacesId, iconName + ".svg", summary, body, {}, {{}}, 0); | 380 | retId = listener->Notify(appName, replacesId, iconName + ".svg", summary, body, {}, {{}}, 0); | ||
381 | QCOMPARE(retId, replacesId); | 381 | QCOMPARE(retId, replacesId); | ||
382 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 382 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
383 | QVERIFY(!d->getLastPackage()->hasPayload()); | 383 | QVERIFY(!d->getLastPacket()->hasPayload()); | ||
384 | 384 | | |||
385 | // if sync not requested no payload: | 385 | // if sync not requested no payload: | ||
386 | plugin->config()->set(QStringLiteral("generalSynchronizeIcons"), false); | 386 | plugin->config()->set(QStringLiteral("generalSynchronizeIcons"), false); | ||
387 | retId = listener->Notify(appName, replacesId, fi.baseName(), summary, body, {}, {{}}, 0); | 387 | retId = listener->Notify(appName, replacesId, fi.baseName(), summary, body, {}, {{}}, 0); | ||
388 | QCOMPARE(retId, replacesId); | 388 | QCOMPARE(retId, replacesId); | ||
389 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 389 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
390 | QVERIFY(!d->getLastPackage()->hasPayload()); | 390 | QVERIFY(!d->getLastPacket()->hasPayload()); | ||
391 | QCOMPARE(d->getLastPackage()->payloadSize(), 0); | 391 | QCOMPARE(d->getLastPacket()->payloadSize(), 0); | ||
392 | } | 392 | } | ||
393 | plugin->config()->set(QStringLiteral("generalSynchronizeIcons"), true); | 393 | plugin->config()->set(QStringLiteral("generalSynchronizeIcons"), true); | ||
394 | 394 | | |||
395 | // image-path in hints | 395 | // image-path in hints | ||
396 | if (iconPaths.size() > 0) { | 396 | if (iconPaths.size() > 0) { | ||
397 | retId = listener->Notify(appName, replacesId, iconPaths.size() > 1 ? iconPaths[1] : icon, summary, body, {}, {{"image-path", iconPaths[0]}}, 0); | 397 | retId = listener->Notify(appName, replacesId, iconPaths.size() > 1 ? iconPaths[1] : icon, summary, body, {}, {{"image-path", iconPaths[0]}}, 0); | ||
398 | QCOMPARE(retId, replacesId); | 398 | QCOMPARE(retId, replacesId); | ||
399 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 399 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
400 | QVERIFY(d->getLastPackage()->hasPayload()); | 400 | QVERIFY(d->getLastPacket()->hasPayload()); | ||
401 | QFileInfo hintsFi(iconPaths[0]); | 401 | QFileInfo hintsFi(iconPaths[0]); | ||
402 | // image-path has priority over appIcon parameter: | 402 | // image-path has priority over appIcon parameter: | ||
403 | QCOMPARE(d->getLastPackage()->payloadSize(), hintsFi.size()); | 403 | QCOMPARE(d->getLastPacket()->payloadSize(), hintsFi.size()); | ||
404 | } | 404 | } | ||
405 | 405 | | |||
406 | // image_path in hints | 406 | // image_path in hints | ||
407 | if (iconPaths.size() > 0) { | 407 | if (iconPaths.size() > 0) { | ||
408 | retId = listener->Notify(appName, replacesId, iconPaths.size() > 1 ? iconPaths[1] : icon, summary, body, {}, {{"image_path", iconPaths[0]}}, 0); | 408 | retId = listener->Notify(appName, replacesId, iconPaths.size() > 1 ? iconPaths[1] : icon, summary, body, {}, {{"image_path", iconPaths[0]}}, 0); | ||
409 | QCOMPARE(retId, replacesId); | 409 | QCOMPARE(retId, replacesId); | ||
410 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 410 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
411 | QVERIFY(d->getLastPackage()->hasPayload()); | 411 | QVERIFY(d->getLastPacket()->hasPayload()); | ||
412 | QFileInfo hintsFi(iconPaths[0]); | 412 | QFileInfo hintsFi(iconPaths[0]); | ||
413 | // image_path has priority over appIcon parameter: | 413 | // image_path has priority over appIcon parameter: | ||
414 | QCOMPARE(d->getLastPackage()->payloadSize(), hintsFi.size()); | 414 | QCOMPARE(d->getLastPacket()->payloadSize(), hintsFi.size()); | ||
415 | } | 415 | } | ||
416 | 416 | | |||
417 | // image-data in hints | 417 | // image-data in hints | ||
418 | // set up: | 418 | // set up: | ||
419 | QBuffer* buffer; | 419 | QBuffer* buffer; | ||
420 | QImage image; | 420 | QImage image; | ||
421 | int width = 2, height = 2, rowStride = 4*width, bitsPerSample = 8, | 421 | int width = 2, height = 2, rowStride = 4*width, bitsPerSample = 8, | ||
422 | channels = 4; | 422 | channels = 4; | ||
Show All 12 Lines | 432 | #define COMPARE_PIXEL(x, y) \ | |||
435 | QCOMPARE(qBlue(image.pixel(x,y)), (int)rawData[x*4 + y*rowStride + 2]); \ | 435 | QCOMPARE(qBlue(image.pixel(x,y)), (int)rawData[x*4 + y*rowStride + 2]); \ | ||
436 | QCOMPARE(qAlpha(image.pixel(x,y)), (int)rawData[x*4 + y*rowStride + 3]); | 436 | QCOMPARE(qAlpha(image.pixel(x,y)), (int)rawData[x*4 + y*rowStride + 3]); | ||
437 | 437 | | |||
438 | hints.insert(QStringLiteral("image-data"), imageData); | 438 | hints.insert(QStringLiteral("image-data"), imageData); | ||
439 | if (iconPaths.size() > 0) | 439 | if (iconPaths.size() > 0) | ||
440 | hints.insert(QStringLiteral("image-path"), iconPaths[0]); | 440 | hints.insert(QStringLiteral("image-path"), iconPaths[0]); | ||
441 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, hints, 0); | 441 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, hints, 0); | ||
442 | QCOMPARE(retId, replacesId); | 442 | QCOMPARE(retId, replacesId); | ||
443 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 443 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
444 | QVERIFY(d->getLastPackage()->hasPayload()); | 444 | QVERIFY(d->getLastPacket()->hasPayload()); | ||
445 | buffer = dynamic_cast<QBuffer*>(d->getLastPackage()->payload().data()); | 445 | buffer = dynamic_cast<QBuffer*>(d->getLastPacket()->payload().data()); | ||
446 | QCOMPARE(d->getLastPackage()->payloadSize(), buffer->size()); | 446 | QCOMPARE(d->getLastPacket()->payloadSize(), buffer->size()); | ||
447 | // image-data is attached as png data | 447 | // image-data is attached as png data | ||
448 | QVERIFY(image.loadFromData(reinterpret_cast<const uchar*>(buffer->data().constData()), buffer->size(), "PNG")); | 448 | QVERIFY(image.loadFromData(reinterpret_cast<const uchar*>(buffer->data().constData()), buffer->size(), "PNG")); | ||
449 | // image-data has priority over image-path: | 449 | // image-data has priority over image-path: | ||
450 | QCOMPARE(image.byteCount(), rowStride*height); | 450 | QCOMPARE(image.byteCount(), rowStride*height); | ||
451 | // rgba -> argb conversion was done correctly: | 451 | // rgba -> argb conversion was done correctly: | ||
452 | COMPARE_PIXEL(0,0); | 452 | COMPARE_PIXEL(0,0); | ||
453 | COMPARE_PIXEL(1,0); | 453 | COMPARE_PIXEL(1,0); | ||
454 | COMPARE_PIXEL(0,1); | 454 | COMPARE_PIXEL(0,1); | ||
455 | COMPARE_PIXEL(1,1); | 455 | COMPARE_PIXEL(1,1); | ||
456 | 456 | | |||
457 | // same for image_data in hints | 457 | // same for image_data in hints | ||
458 | hints.clear(); | 458 | hints.clear(); | ||
459 | hints.insert(QStringLiteral("image-data"), imageData); | 459 | hints.insert(QStringLiteral("image-data"), imageData); | ||
460 | if (iconPaths.size() > 0) | 460 | if (iconPaths.size() > 0) | ||
461 | hints.insert(QStringLiteral("image_path"), iconPaths[0]); | 461 | hints.insert(QStringLiteral("image_path"), iconPaths[0]); | ||
462 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, hints, 0); | 462 | retId = listener->Notify(appName, replacesId, icon, summary, body, {}, hints, 0); | ||
463 | QCOMPARE(retId, replacesId); | 463 | QCOMPARE(retId, replacesId); | ||
464 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 464 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
465 | QVERIFY(d->getLastPackage()->hasPayload()); | 465 | QVERIFY(d->getLastPacket()->hasPayload()); | ||
466 | buffer = dynamic_cast<QBuffer*>(d->getLastPackage()->payload().data()); | 466 | buffer = dynamic_cast<QBuffer*>(d->getLastPacket()->payload().data()); | ||
467 | QCOMPARE(d->getLastPackage()->payloadSize(), buffer->size()); | 467 | QCOMPARE(d->getLastPacket()->payloadSize(), buffer->size()); | ||
468 | // image-data is attached as png data | 468 | // image-data is attached as png data | ||
469 | QVERIFY(image.loadFromData(reinterpret_cast<const uchar*>(buffer->data().constData()), buffer->size(), "PNG")); | 469 | QVERIFY(image.loadFromData(reinterpret_cast<const uchar*>(buffer->data().constData()), buffer->size(), "PNG")); | ||
470 | // image_data has priority over image_path/image-path: | 470 | // image_data has priority over image_path/image-path: | ||
471 | QCOMPARE(image.byteCount(), rowStride*height); | 471 | QCOMPARE(image.byteCount(), rowStride*height); | ||
472 | // rgba -> argb conversion was done correctly: | 472 | // rgba -> argb conversion was done correctly: | ||
473 | COMPARE_PIXEL(0,0); | 473 | COMPARE_PIXEL(0,0); | ||
474 | COMPARE_PIXEL(1,0); | 474 | COMPARE_PIXEL(1,0); | ||
475 | COMPARE_PIXEL(0,1); | 475 | COMPARE_PIXEL(0,1); | ||
476 | COMPARE_PIXEL(1,1); | 476 | COMPARE_PIXEL(1,1); | ||
477 | 477 | | |||
478 | // same for icon_data, which has lowest priority | 478 | // same for icon_data, which has lowest priority | ||
479 | hints.clear(); | 479 | hints.clear(); | ||
480 | hints.insert(QStringLiteral("icon_data"), imageData); | 480 | hints.insert(QStringLiteral("icon_data"), imageData); | ||
481 | retId = listener->Notify(appName, replacesId, QLatin1String(""), summary, body, {}, hints, 0); | 481 | retId = listener->Notify(appName, replacesId, QLatin1String(""), summary, body, {}, hints, 0); | ||
482 | QCOMPARE(retId, replacesId); | 482 | QCOMPARE(retId, replacesId); | ||
483 | QCOMPARE(++proxiedNotifications, d->getSentPackages()); | 483 | QCOMPARE(++proxiedNotifications, d->getSentPackets()); | ||
484 | QVERIFY(d->getLastPackage()); | 484 | QVERIFY(d->getLastPacket()); | ||
485 | QVERIFY(d->getLastPackage()->hasPayload()); | 485 | QVERIFY(d->getLastPacket()->hasPayload()); | ||
486 | buffer = dynamic_cast<QBuffer*>(d->getLastPackage()->payload().data()); | 486 | buffer = dynamic_cast<QBuffer*>(d->getLastPacket()->payload().data()); | ||
487 | // image-data is attached as png data | 487 | // image-data is attached as png data | ||
488 | QVERIFY(image.loadFromData(reinterpret_cast<const uchar*>(buffer->data().constData()), buffer->size(), "PNG")); | 488 | QVERIFY(image.loadFromData(reinterpret_cast<const uchar*>(buffer->data().constData()), buffer->size(), "PNG")); | ||
489 | QCOMPARE(image.byteCount(), rowStride*height); | 489 | QCOMPARE(image.byteCount(), rowStride*height); | ||
490 | // rgba -> argb conversion was done correctly: | 490 | // rgba -> argb conversion was done correctly: | ||
491 | COMPARE_PIXEL(0,0); | 491 | COMPARE_PIXEL(0,0); | ||
492 | COMPARE_PIXEL(1,0); | 492 | COMPARE_PIXEL(1,0); | ||
493 | COMPARE_PIXEL(0,1); | 493 | COMPARE_PIXEL(0,1); | ||
494 | COMPARE_PIXEL(1,1); | 494 | COMPARE_PIXEL(1,1); | ||
495 | 495 | | |||
496 | #undef COMPARE_PIXEL | 496 | #undef COMPARE_PIXEL | ||
497 | } | 497 | } | ||
498 | 498 | | |||
499 | 499 | | |||
500 | QTEST_GUILESS_MAIN(TestNotificationListener); | 500 | QTEST_GUILESS_MAIN(TestNotificationListener); | ||
501 | 501 | | |||
502 | #include "testnotificationlistener.moc" | 502 | #include "testnotificationlistener.moc" |