| 1 | /* |
| 2 | * Copyright (C) 2014 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of lomiri-online-accounts-ui |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 3, as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranties of |
| 14 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <Accounts/Account> |
| 22 | #include <Accounts/Manager> |
| 23 | #include <QDebug> |
| 24 | #include <QDir> |
| 25 | #include <QDomDocument> |
| 26 | #include <QDomElement> |
| 27 | #include <QFile> |
| 28 | #include <QProcess> |
| 29 | #include <QSignalSpy> |
| 30 | #include <QTest> |
| 31 | #include <sys/types.h> |
| 32 | #include <time.h> |
| 33 | #include <utime.h> |
| 34 | |
| 35 | #define TEST_DIR TMP_DIR "/hooks-test" |
| 36 | |
| 37 | class OnlineAccountsHooksTest: public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | |
| 41 | public: |
| 42 | OnlineAccountsHooksTest(); |
| 43 | |
| 44 | private Q_SLOTS: |
| 45 | void initTestCase(); |
| 46 | void testNoFiles(); |
| 47 | void testValidHooks_data(); |
| 48 | void testValidHooks(); |
| 49 | void testRemoval(); |
| 50 | void testAccountRemoval(); |
| 51 | void testUpdate(); |
| 52 | void testDesktopEntry_data(); |
| 53 | void testDesktopEntry(); |
| 54 | void testServiceType_data(); |
| 55 | void testServiceType(); |
| 56 | |
| 57 | private: |
| 58 | void clearHooksDir(); |
| 59 | void clearInstallDir(); |
| 60 | bool runHookProcess(); |
| 61 | void writeHookFile(const QString &name, const QString &contents); |
| 62 | void writeInstalledFile(const QString &name, const QString &contents); |
| 63 | void writePackageFile(const QString &name, |
| 64 | const QString &contents = QString()); |
| 65 | |
| 66 | private: |
| 67 | QDir m_testDir; |
| 68 | QDir m_hooksDir; |
| 69 | QDir m_installDir; |
| 70 | QDir m_packageDir; |
| 71 | }; |
| 72 | |
| 73 | OnlineAccountsHooksTest::OnlineAccountsHooksTest(): |
| 74 | QObject(0), |
| 75 | m_testDir(TEST_DIR), |
| 76 | m_hooksDir(TEST_DIR "/lomiri-online-accounts-hooks"), |
| 77 | m_installDir(TEST_DIR "/accounts"), |
| 78 | m_packageDir(TEST_DIR "/package") |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | void OnlineAccountsHooksTest::clearHooksDir() |
| 83 | { |
| 84 | m_hooksDir.removeRecursively(); |
| 85 | m_hooksDir.mkpath("."); |
| 86 | } |
| 87 | |
| 88 | void OnlineAccountsHooksTest::clearInstallDir() |
| 89 | { |
| 90 | m_installDir.removeRecursively(); |
| 91 | m_installDir.mkpath("."); |
| 92 | } |
| 93 | |
| 94 | bool OnlineAccountsHooksTest::runHookProcess() |
| 95 | { |
| 96 | QProcess process; |
| 97 | process.setProcessChannelMode(QProcess::ForwardedChannels); |
| 98 | process.start(HOOK_PROCESS); |
| 99 | if (!process.waitForFinished()) return false; |
| 100 | |
| 101 | return process.exitCode() == EXIT_SUCCESS; |
| 102 | } |
| 103 | |
| 104 | void OnlineAccountsHooksTest::writeHookFile(const QString &name, |
| 105 | const QString &contents) |
| 106 | { |
| 107 | QFile file(m_hooksDir.filePath(name)); |
| 108 | if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { |
| 109 | qWarning() << "Could not write file" << name; |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | file.write(contents.toUtf8()); |
| 114 | } |
| 115 | |
| 116 | void OnlineAccountsHooksTest::writeInstalledFile(const QString &name, |
| 117 | const QString &contents) |
| 118 | { |
| 119 | QFileInfo fileInfo(name); |
| 120 | m_installDir.mkpath(fileInfo.path()); |
| 121 | QFile file(m_installDir.filePath(name)); |
| 122 | if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { |
| 123 | qWarning() << "Could not write file" << name; |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | file.write(contents.toUtf8()); |
| 128 | } |
| 129 | |
| 130 | void OnlineAccountsHooksTest::writePackageFile(const QString &name, |
| 131 | const QString &contents) |
| 132 | { |
| 133 | QFileInfo fileInfo(name); |
| 134 | m_packageDir.mkpath(fileInfo.path()); |
| 135 | QFile file(m_packageDir.filePath(name)); |
| 136 | if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { |
| 137 | qWarning() << "Could not write file" << name; |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | file.write(contents.toUtf8()); |
| 142 | } |
| 143 | |
| 144 | void OnlineAccountsHooksTest::initTestCase() |
| 145 | { |
| 146 | qputenv("XDG_DATA_HOME", TEST_DIR); |
| 147 | qputenv("OAH_CLICK_DIR", m_packageDir.path().toUtf8()); |
| 148 | qputenv("ACCOUNTS", TEST_DIR); |
| 149 | |
| 150 | // The hook must be able to run without a D-Bus session |
| 151 | qunsetenv("DBUS_SESSION_BUS_ADDRESS"); |
| 152 | |
| 153 | clearHooksDir(); |
| 154 | clearInstallDir(); |
| 155 | } |
| 156 | |
| 157 | void OnlineAccountsHooksTest::testNoFiles() |
| 158 | { |
| 159 | clearHooksDir(); |
| 160 | clearInstallDir(); |
| 161 | |
| 162 | QVERIFY(runHookProcess()); |
| 163 | QVERIFY(m_hooksDir.entryList(QDir::Files).isEmpty()); |
| 164 | QVERIFY(m_installDir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot).isEmpty()); |
| 165 | |
| 166 | /* Create an unsupported hook file */ |
| 167 | writeHookFile("file.unsupported", |
| 168 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 169 | "<service></service>"); |
| 170 | QVERIFY(!m_hooksDir.entryList(QDir::Files).isEmpty()); |
| 171 | |
| 172 | QVERIFY(runHookProcess()); |
| 173 | QVERIFY(m_installDir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot).isEmpty()); |
| 174 | } |
| 175 | |
| 176 | void OnlineAccountsHooksTest::testValidHooks_data() |
| 177 | { |
| 178 | QTest::addColumn<QString>("hookName"); |
| 179 | QTest::addColumn<QString>("contents"); |
| 180 | QTest::addColumn<QString>("installedName"); |
| 181 | QTest::addColumn<QString>("profile"); |
| 182 | QTest::addColumn<QString>("icon"); |
| 183 | QTest::addColumn<bool>("isValid"); |
| 184 | |
| 185 | QTest::newRow("service") << |
| 186 | "com.lomiri.test_MyApp_0.1.service" << |
| 187 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 188 | "<service>\n" |
| 189 | " <type>some type</type>\n" |
| 190 | " <name>my app service</name>\n" |
| 191 | " <provider>example</provider>\n" |
| 192 | "</service>" << |
| 193 | "services/com.lomiri.test_MyApp.service" << |
| 194 | "com.lomiri.test_MyApp_0.1" << |
| 195 | QString() << |
| 196 | true; |
| 197 | |
| 198 | QTest::newRow("application") << |
| 199 | "com.lomiri.test_MyApp_0.2.application" << |
| 200 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 201 | "<application>\n" |
| 202 | " <description>My application</description>\n" |
| 203 | " <service-types>\n" |
| 204 | " <service-type>some type</service-type>\n" |
| 205 | " </service-types>\n" |
| 206 | "</application>" << |
| 207 | "applications/com.lomiri.test_MyApp.application" << |
| 208 | "com.lomiri.test_MyApp_0.2" << |
| 209 | QString() << |
| 210 | true; |
| 211 | |
| 212 | QTest::newRow("provider") << |
| 213 | "com.lomiri.test_Plugin_0.1.provider" << |
| 214 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 215 | "<provider>\n" |
| 216 | " <name>My provider</name>\n" |
| 217 | "</provider>" << |
| 218 | "providers/com.lomiri.test_Plugin.provider" << |
| 219 | "com.lomiri.test_Plugin_0.1" << |
| 220 | QString() << |
| 221 | true; |
| 222 | |
| 223 | QTest::newRow("provider-with-icon") << |
| 224 | "com.lomiri.test_Plugin2_0.1.provider" << |
| 225 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 226 | "<provider>\n" |
| 227 | " <name>My provider</name>\n" |
| 228 | " <icon>MyApp.svg</icon>\n" |
| 229 | "</provider>" << |
| 230 | "providers/com.lomiri.test_Plugin2.provider" << |
| 231 | "com.lomiri.test_Plugin2_0.1" << |
| 232 | m_packageDir.filePath("MyApp.svg") << |
| 233 | true; |
| 234 | |
| 235 | QTest::newRow("provider-with-wrong-icon") << |
| 236 | "com.lomiri.test_Plugin3_0.1.provider" << |
| 237 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 238 | "<provider>\n" |
| 239 | " <name>My provider</name>\n" |
| 240 | " <icon>NonExisting.svg</icon>\n" |
| 241 | "</provider>" << |
| 242 | "providers/com.lomiri.test_Plugin3.provider" << |
| 243 | "com.lomiri.test_Plugin3_0.1" << |
| 244 | "NonExisting.svg" << |
| 245 | true; |
| 246 | |
| 247 | QTest::newRow("provider-with-icon-subdir") << |
| 248 | "com.lomiri.test_Plugin4_0.1.provider" << |
| 249 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 250 | "<provider>\n" |
| 251 | " <name>My provider</name>\n" |
| 252 | " <icon>subdir/pict.png</icon>\n" |
| 253 | "</provider>" << |
| 254 | "providers/com.lomiri.test_Plugin4.provider" << |
| 255 | "com.lomiri.test_Plugin4_0.1" << |
| 256 | m_packageDir.filePath("subdir/pict.png") << |
| 257 | true; |
| 258 | |
| 259 | QTest::newRow("invalid application") << |
| 260 | "com.lomiri.test_Invalid_0.1.application" << |
| 261 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 262 | "<application>\n" |
| 263 | " <description>My application</description>\n" |
| 264 | " <service-types>\n" |
| 265 | " <service-type>some type</service-type>\n" |
| 266 | " </service-types>\n" |
| 267 | "</application /* invalid XML!" << |
| 268 | "applications/com.lomiri.test_Invalid.application" << |
| 269 | "com.lomiri.test_Invalid_0.1" << |
| 270 | QString() << |
| 271 | false; |
| 272 | |
| 273 | QTest::newRow("application with wrong id") << |
| 274 | "com.lomiri.test_MyAppId_0.1.application" << |
| 275 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 276 | "<application id=\"wrongid\">\n" |
| 277 | " <description>My application</description>\n" |
| 278 | " <service-types>\n" |
| 279 | " <service-type>some type</service-type>\n" |
| 280 | " </service-types>\n" |
| 281 | "</application>" << |
| 282 | "applications/com.lomiri.test_MyAppId.application" << |
| 283 | "com.lomiri.test_MyAppId_0.1" << |
| 284 | QString() << |
| 285 | true; |
| 286 | } |
| 287 | |
| 288 | void OnlineAccountsHooksTest::testValidHooks() |
| 289 | { |
| 290 | QFETCH(QString, hookName); |
| 291 | QFETCH(QString, contents); |
| 292 | QFETCH(QString, installedName); |
| 293 | QFETCH(QString, profile); |
| 294 | QFETCH(QString, icon); |
| 295 | QFETCH(bool, isValid); |
| 296 | |
| 297 | /* Create the needed files in the package directory */ |
| 298 | writePackageFile("MyApp.svg"); |
| 299 | writePackageFile("subdir/pict.png"); |
| 300 | |
| 301 | writeHookFile(hookName, contents); |
| 302 | QVERIFY(runHookProcess()); |
| 303 | |
| 304 | // check that the file has been created, if the file was valid |
| 305 | QFileInfo fileInfo(m_installDir.absoluteFilePath(installedName)); |
| 306 | QCOMPARE(fileInfo.exists(), isValid); |
| 307 | |
| 308 | if (!isValid) return; |
| 309 | |
| 310 | QFile file(fileInfo.absoluteFilePath()); |
| 311 | QVERIFY(file.open(QIODevice::ReadOnly)); |
| 312 | |
| 313 | // check that's a valid XML file |
| 314 | QDomDocument doc; |
| 315 | QVERIFY(doc.setContent(&file)); |
| 316 | |
| 317 | // Check that the "id" attribute matches the file name |
| 318 | QDomElement root = doc.documentElement(); |
| 319 | QCOMPARE(root.attribute("id"), fileInfo.completeBaseName()); |
| 320 | |
| 321 | /* Check that a "profile" element has been added and that matches the |
| 322 | * expected profile. */ |
| 323 | QDomElement profileElement = root.firstChildElement("profile"); |
| 324 | QVERIFY(!profileElement.isNull()); |
| 325 | QCOMPARE(profileElement.text(), profile); |
| 326 | |
| 327 | /* Check that the icon is correct */ |
| 328 | QDomElement iconElement = root.firstChildElement("icon"); |
| 329 | QCOMPARE(iconElement.text(), icon); |
| 330 | } |
| 331 | |
| 332 | void OnlineAccountsHooksTest::testRemoval() |
| 333 | { |
| 334 | clearHooksDir(); |
| 335 | clearInstallDir(); |
| 336 | |
| 337 | QString stillInstalled("applications/com.lomiri.test_StillInstalled.application"); |
| 338 | writeInstalledFile(stillInstalled, |
| 339 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 340 | "<!--this file is auto-generated by lomiri-online-accounts-hooks; do not modify-->\n" |
| 341 | "<application>\n" |
| 342 | " <description>My application</description>\n" |
| 343 | " <service-types>\n" |
| 344 | " <service-type>some type</service-type>\n" |
| 345 | " </service-types>\n" |
| 346 | " <profile>com-lomiri.test_StillInstalled_2.0</profile>\n" |
| 347 | "</application>"); |
| 348 | QVERIFY(m_installDir.exists(stillInstalled)); |
| 349 | |
| 350 | QString myApp("applications/com.lomiri.test_MyApp.application"); |
| 351 | writeInstalledFile(myApp, |
| 352 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 353 | "<!--this file is auto-generated by lomiri-online-accounts-hooks; do not modify-->\n" |
| 354 | "<application>\n" |
| 355 | " <description>My application</description>\n" |
| 356 | " <service-types>\n" |
| 357 | " <service-type>some type</service-type>\n" |
| 358 | " </service-types>\n" |
| 359 | " <profile>com-lomiri.test_MyApp_3.0</profile>\n" |
| 360 | "</application>"); |
| 361 | QVERIFY(m_installDir.exists(myApp)); |
| 362 | |
| 363 | QString noProfile("applications/com.lomiri.test_NoProfile.application"); |
| 364 | writeInstalledFile(noProfile, |
| 365 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 366 | "<application>\n" |
| 367 | " <description>My application</description>\n" |
| 368 | " <service-types>\n" |
| 369 | " <service-type>some type</service-type>\n" |
| 370 | " </service-types>\n" |
| 371 | "</application>"); |
| 372 | QVERIFY(m_installDir.exists(noProfile)); |
| 373 | |
| 374 | QString notOurs("applications/com.lomiri.test_NotOurs.application"); |
| 375 | writeInstalledFile(notOurs, |
| 376 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 377 | "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n" |
| 378 | "<application>\n" |
| 379 | " <description>My application</description>\n" |
| 380 | " <service-types>\n" |
| 381 | " <service-type>some type</service-type>\n" |
| 382 | " </service-types>\n" |
| 383 | " <profile>com-lomiri.test_NotOurs_0.1.0</profile>\n" |
| 384 | "</application>"); |
| 385 | QVERIFY(m_installDir.exists(notOurs)); |
| 386 | |
| 387 | writeHookFile("com-lomiri.test_StillInstalled_2.0.application", |
| 388 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 389 | "<application>\n" |
| 390 | " <description>My application</description>\n" |
| 391 | " <service-types>\n" |
| 392 | " <service-type>some type</service-type>\n" |
| 393 | " </service-types>\n" |
| 394 | "</application>"); |
| 395 | |
| 396 | QVERIFY(runHookProcess()); |
| 397 | |
| 398 | QVERIFY(m_installDir.exists(stillInstalled)); |
| 399 | QVERIFY(!m_installDir.exists(myApp)); |
| 400 | QVERIFY(m_installDir.exists(noProfile)); |
| 401 | QVERIFY(m_installDir.exists(noProfile)); |
| 402 | } |
| 403 | |
| 404 | void OnlineAccountsHooksTest::testAccountRemoval() |
| 405 | { |
| 406 | clearHooksDir(); |
| 407 | clearInstallDir(); |
| 408 | |
| 409 | Accounts::Manager *manager = |
| 410 | new Accounts::Manager(Accounts::Manager::DisableNotifications); |
| 411 | |
| 412 | QString stillInstalled("providers/com.lomiri.test_StillInstalled.provider"); |
| 413 | writeInstalledFile(stillInstalled, |
| 414 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 415 | "<!--this file is auto-generated by lomiri-online-accounts-hooks; do not modify-->\n" |
| 416 | "<provider>\n" |
| 417 | " <profile>com-lomiri.test_StillInstalled_2.0</profile>\n" |
| 418 | "</provider>"); |
| 419 | QVERIFY(m_installDir.exists(stillInstalled)); |
| 420 | |
| 421 | /* Write a newer version of the hook, to make sure that accounts created |
| 422 | * with older versions of the app are preserved */ |
| 423 | writeHookFile("com-lomiri.test_StillInstalled_3.0.provider", |
| 424 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 425 | "<provider>\n" |
| 426 | "</provider>"); |
| 427 | |
| 428 | Accounts::Account *account = |
| 429 | manager->createAccount("com.lomiri.test_StillInstalled"); |
| 430 | account->setDisplayName("Still alive"); |
| 431 | account->syncAndBlock(); |
| 432 | Accounts::AccountId idPreserved = account->id(); |
| 433 | QVERIFY(idPreserved > 0); |
| 434 | |
| 435 | QString myProvider("providers/com.lomiri.test_MyProvider.provider"); |
| 436 | writeInstalledFile(myProvider, |
| 437 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 438 | "<!--this file is auto-generated by lomiri-online-accounts-hooks; do not modify-->\n" |
| 439 | "<provider>\n" |
| 440 | " <profile>com-lomiri.test_MyProvider_3.0</profile>\n" |
| 441 | "</provider>"); |
| 442 | QVERIFY(m_installDir.exists(myProvider)); |
| 443 | |
| 444 | account = manager->createAccount("com.lomiri.test_MyProvider"); |
| 445 | account->setDisplayName("Must die"); |
| 446 | account->syncAndBlock(); |
| 447 | Accounts::AccountId idRemoved = account->id(); |
| 448 | QVERIFY(idRemoved > 0); |
| 449 | |
| 450 | delete manager; |
| 451 | |
| 452 | QVERIFY(runHookProcess()); |
| 453 | |
| 454 | QVERIFY(m_installDir.exists(stillInstalled)); |
| 455 | QVERIFY(!m_installDir.exists(myProvider)); |
| 456 | |
| 457 | manager = new Accounts::Manager(Accounts::Manager::DisableNotifications); |
| 458 | account = manager->account(idPreserved); |
| 459 | QVERIFY(account != 0); |
| 460 | account = manager->account(idRemoved); |
| 461 | QVERIFY(account == 0); |
| 462 | |
| 463 | delete manager; |
| 464 | } |
| 465 | |
| 466 | void OnlineAccountsHooksTest::testUpdate() |
| 467 | { |
| 468 | clearHooksDir(); |
| 469 | clearInstallDir(); |
| 470 | |
| 471 | time_t oldTime = time(NULL) - 3; |
| 472 | |
| 473 | writeHookFile("com-lomiri.test_MyApp_1.0.application", |
| 474 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 475 | "<application>\n" |
| 476 | " <description>My application</description>\n" |
| 477 | " <service-types>\n" |
| 478 | " <service-type>some type</service-type>\n" |
| 479 | " </service-types>\n" |
| 480 | "</application>"); |
| 481 | |
| 482 | QVERIFY(runHookProcess()); |
| 483 | |
| 484 | QString installedName("applications/com-lomiri.test_MyApp.application"); |
| 485 | |
| 486 | // check that the file has been created, and with the correct profile |
| 487 | QFile file(m_installDir.absoluteFilePath(installedName)); |
| 488 | QVERIFY(file.open(QIODevice::ReadOnly)); |
| 489 | |
| 490 | // check that's a valid XML file |
| 491 | QDomDocument doc; |
| 492 | QVERIFY(doc.setContent(&file)); |
| 493 | QDomElement root = doc.documentElement(); |
| 494 | QCOMPARE(root.firstChildElement("profile").text(), |
| 495 | QString("com-lomiri.test_MyApp_1.0")); |
| 496 | |
| 497 | /* Now remove the hook file and write a newer version of it */ |
| 498 | clearHooksDir(); |
| 499 | QTest::qWait(1500); // make sure that the timestamp is newer |
| 500 | writeHookFile("com-lomiri.test_MyApp_1.1.application", |
| 501 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 502 | "<application>\n" |
| 503 | " <description>My application</description>\n" |
| 504 | " <service-types>\n" |
| 505 | " <service-type>some type</service-type>\n" |
| 506 | " </service-types>\n" |
| 507 | "</application>"); |
| 508 | |
| 509 | QVERIFY(runHookProcess()); |
| 510 | |
| 511 | // check that the file has been updated with the correct profile |
| 512 | file.close(); |
| 513 | file.setFileName(m_installDir.absoluteFilePath(installedName)); |
| 514 | QVERIFY(file.open(QIODevice::ReadOnly)); |
| 515 | QVERIFY(doc.setContent(&file)); |
| 516 | root = doc.documentElement(); |
| 517 | QCOMPARE(root.firstChildElement("profile").text(), |
| 518 | QString("com-lomiri.test_MyApp_1.1")); |
| 519 | |
| 520 | // Create an *older* file |
| 521 | clearHooksDir(); |
| 522 | QString fileName("com-lomiri.test_MyApp_1.2.application"); |
| 523 | writeHookFile(fileName, |
| 524 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 525 | "<application>\n" |
| 526 | " <description>My application</description>\n" |
| 527 | " <service-types>\n" |
| 528 | " <service-type>some type</service-type>\n" |
| 529 | " </service-types>\n" |
| 530 | "</application>"); |
| 531 | struct utimbuf sourceTime; |
| 532 | sourceTime.actime = sourceTime.modtime = oldTime; |
| 533 | utime(m_hooksDir.filePath(fileName).toUtf8().constData(), &sourceTime); |
| 534 | |
| 535 | QVERIFY(runHookProcess()); |
| 536 | |
| 537 | // check that the file has been updated with the correct profile |
| 538 | file.close(); |
| 539 | file.setFileName(m_installDir.absoluteFilePath(installedName)); |
| 540 | QVERIFY(file.open(QIODevice::ReadOnly)); |
| 541 | QVERIFY(doc.setContent(&file)); |
| 542 | root = doc.documentElement(); |
| 543 | QCOMPARE(root.firstChildElement("profile").text(), |
| 544 | QString("com-lomiri.test_MyApp_1.2")); |
| 545 | } |
| 546 | |
| 547 | void OnlineAccountsHooksTest::testDesktopEntry_data() |
| 548 | { |
| 549 | QTest::addColumn<QString>("hookName"); |
| 550 | QTest::addColumn<QString>("contents"); |
| 551 | QTest::addColumn<QString>("installedName"); |
| 552 | QTest::addColumn<QString>("expectedDesktopEntry"); |
| 553 | |
| 554 | QTest::newRow("no-entry") << |
| 555 | "com.lomiri.test_MyApp_0.2.application" << |
| 556 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 557 | "<application>\n" |
| 558 | " <description>My application</description>\n" |
| 559 | " <service-types>\n" |
| 560 | " <service-type>some type</service-type>\n" |
| 561 | " </service-types>\n" |
| 562 | "</application>" << |
| 563 | "applications/com.lomiri.test_MyApp.application" << |
| 564 | "com.lomiri.test_MyApp_0.2"; |
| 565 | |
| 566 | QTest::newRow("with-desktop-entry") << |
| 567 | "com.lomiri.test_Desktop_0.1.application" << |
| 568 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 569 | "<application>\n" |
| 570 | " <description>My application</description>\n" |
| 571 | " <desktop-entry>something here</desktop-entry>\n" |
| 572 | " <service-types>\n" |
| 573 | " <service-type>some type</service-type>\n" |
| 574 | " </service-types>\n" |
| 575 | "</application>" << |
| 576 | "applications/com.lomiri.test_Desktop.application" << |
| 577 | "something here"; |
| 578 | } |
| 579 | |
| 580 | void OnlineAccountsHooksTest::testDesktopEntry() |
| 581 | { |
| 582 | QFETCH(QString, hookName); |
| 583 | QFETCH(QString, contents); |
| 584 | QFETCH(QString, installedName); |
| 585 | QFETCH(QString, expectedDesktopEntry); |
| 586 | |
| 587 | writeHookFile(hookName, contents); |
| 588 | QVERIFY(runHookProcess()); |
| 589 | |
| 590 | // check that the file has been created, if the file was valid |
| 591 | QFileInfo fileInfo(m_installDir.absoluteFilePath(installedName)); |
| 592 | QVERIFY(fileInfo.exists()); |
| 593 | |
| 594 | QFile file(fileInfo.absoluteFilePath()); |
| 595 | QVERIFY(file.open(QIODevice::ReadOnly)); |
| 596 | |
| 597 | // check that's a valid XML file |
| 598 | QDomDocument doc; |
| 599 | QVERIFY(doc.setContent(&file)); |
| 600 | |
| 601 | QDomElement root = doc.documentElement(); |
| 602 | |
| 603 | /* Check that a "desktop-entry" element has been added and that matches the |
| 604 | * expected value. */ |
| 605 | QDomElement desktopEntryElement = root.firstChildElement("desktop-entry"); |
| 606 | QCOMPARE(desktopEntryElement.text(), expectedDesktopEntry); |
| 607 | } |
| 608 | |
| 609 | void OnlineAccountsHooksTest::testServiceType_data() |
| 610 | { |
| 611 | QTest::addColumn<QString>("hookName"); |
| 612 | QTest::addColumn<QString>("contents"); |
| 613 | QTest::addColumn<QString>("installedName"); |
| 614 | QTest::addColumn<QString>("expectedServiceType"); |
| 615 | |
| 616 | QTest::newRow("no type") << |
| 617 | "com.lomiri.test_MyApp_0.2.service" << |
| 618 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 619 | "<service>\n" |
| 620 | " <provider>google</provider>\n" |
| 621 | "</service>" << |
| 622 | "services/com.lomiri.test_MyApp.service" << |
| 623 | "com.lomiri.test_MyApp"; |
| 624 | |
| 625 | QTest::newRow("with service type") << |
| 626 | "com.lomiri.test_Desktop_0.1.service" << |
| 627 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 628 | "<service>\n" |
| 629 | " <provider>facebook</provider>\n" |
| 630 | " <type>a random type</type>\n" |
| 631 | "</service>" << |
| 632 | "services/com.lomiri.test_Desktop.service" << |
| 633 | "a random type"; |
| 634 | } |
| 635 | |
| 636 | void OnlineAccountsHooksTest::testServiceType() |
| 637 | { |
| 638 | QFETCH(QString, hookName); |
| 639 | QFETCH(QString, contents); |
| 640 | QFETCH(QString, installedName); |
| 641 | QFETCH(QString, expectedServiceType); |
| 642 | |
| 643 | writeHookFile(hookName, contents); |
| 644 | QVERIFY(runHookProcess()); |
| 645 | |
| 646 | // check that the file has been created, if the file was valid |
| 647 | QFileInfo fileInfo(m_installDir.absoluteFilePath(installedName)); |
| 648 | QVERIFY(fileInfo.exists()); |
| 649 | |
| 650 | QFile file(fileInfo.absoluteFilePath()); |
| 651 | QVERIFY(file.open(QIODevice::ReadOnly)); |
| 652 | |
| 653 | // check that's a valid XML file |
| 654 | QDomDocument doc; |
| 655 | QVERIFY(doc.setContent(&file)); |
| 656 | |
| 657 | QDomElement root = doc.documentElement(); |
| 658 | |
| 659 | /* Check that a "type" element has been added and that matches the |
| 660 | * expected value. */ |
| 661 | QDomElement serviceTypeElement = root.firstChildElement("type"); |
| 662 | QCOMPARE(serviceTypeElement.text(), expectedServiceType); |
| 663 | } |
| 664 | |
| 665 | QTEST_MAIN(OnlineAccountsHooksTest); |
| 666 | |
| 667 | #include "tst_online_accounts_hooks.moc" |