| 1 | /* |
| 2 | * Copyright 2014 Canonical Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include <QtCore/QDebug> |
| 18 | #include <QtCore/QFile> |
| 19 | #include <QtCore/QRegularExpression> |
| 20 | #include <QtCore/QString> |
| 21 | #include <QtTest/QSignalSpy> |
| 22 | #include <QtTest/QTest> |
| 23 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 24 | #include <LomiriToolkit-Qt6/private/ucserviceproperties_p_p.h> |
| 25 | #else |
| 26 | #include <LomiriToolkit/private/ucserviceproperties_p_p.h> |
| 27 | #endif |
| 28 | |
| 29 | #include "qstringliteral.h" |
| 30 | #include "uctestcase.h" |
| 31 | |
| 32 | UT_USE_NAMESPACE |
| 33 | |
| 34 | class tst_ServiceProperties : public QObject |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | |
| 38 | public: |
| 39 | tst_ServiceProperties() {} |
| 40 | |
| 41 | private: |
| 42 | |
| 43 | QString error; |
| 44 | |
| 45 | // FIXME use LomiriTestCase::ignoreWaring in Vivid |
| 46 | void ignoreWarning(const QString& fileName, uint line, uint column, const QString& message, uint occurences=1) |
| 47 | { |
| 48 | for (uint i = 0; i < occurences; i++) { |
| 49 | QString url(QUrl::fromLocalFile(QFileInfo(fileName).absoluteFilePath()).toEncoded()); |
| 50 | QString warning(QString("%1:%2:%3: %4").arg(url).arg(line).arg(column).arg(message)); |
| 51 | QTest::ignoreMessage(QtWarningMsg, warning.toUtf8()); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | private Q_SLOTS: |
| 57 | |
| 58 | void initTestCase() |
| 59 | { |
| 60 | // The test uses this schema, which might not be available on non-Ubuntu |
| 61 | // distros even though the AccountsService is available. |
| 62 | if (!QFile::exists( |
| 63 | QStringLiteral("/usr/share/accountsservice/interfaces/com.lomiri.touch.AccountsService.Sound.xml"))) { |
| 64 | error = QStringLiteral("Skip test: required schema not installed."); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | // check if the connection is possible, otherwise we must skip all tests |
| 69 | QScopedPointer<LomiriTestCase> test(new LomiriTestCase("IncomingCallVibrateWatcher.qml")); |
| 70 | UCServiceProperties *watcher = static_cast<UCServiceProperties*>(test->rootObject()->property("service").value<QObject*>()); |
| 71 | QVERIFY(watcher); |
| 72 | if (watcher->status() == UCServiceProperties::Synchronizing || |
| 73 | watcher->status() == UCServiceProperties::Inactive) { |
| 74 | QSignalSpy wait(watcher, SIGNAL(statusChanged())); |
| 75 | wait.wait(); |
| 76 | } |
| 77 | if (watcher->status() == UCServiceProperties::ConnectionError) { |
| 78 | error = "Skip test: " + watcher->error(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void cleanup() |
| 83 | { |
| 84 | // restore env var setting |
| 85 | qputenv("SHOW_SERVICEPROPERTIES_WARNINGS", QByteArray()); |
| 86 | } |
| 87 | |
| 88 | void test_change_property() |
| 89 | { |
| 90 | if (!error.isEmpty()) { |
| 91 | QSKIP(qPrintable(error)); |
| 92 | } |
| 93 | QScopedPointer<LomiriTestCase> test(new LomiriTestCase("IncomingCallVibrateWatcher.qml")); |
| 94 | UCServiceProperties *watcher = static_cast<UCServiceProperties*>(test->rootObject()->property("service").value<QObject*>()); |
| 95 | QVERIFY(watcher); |
| 96 | |
| 97 | bool backup = watcher->property("incomingCallVibrate").toBool(); |
| 98 | UCServicePropertiesPrivate *pWatcher = UCServicePropertiesPrivate::get(watcher); |
| 99 | QSignalSpy spy(watcher, SIGNAL(incomingCallVibrateChanged())); |
| 100 | pWatcher->testProperty("IncomingCallVibrate", !backup); |
| 101 | spy.wait(400); |
| 102 | QCOMPARE(spy.count(), 1); |
| 103 | QCOMPARE(watcher->property("incomingCallVibrate").toBool(), !backup); |
| 104 | |
| 105 | // restore value |
| 106 | spy.clear(); |
| 107 | pWatcher->testProperty("IncomingCallVibrate", backup); |
| 108 | spy.wait(400); |
| 109 | } |
| 110 | |
| 111 | void test_environment_variable_data() |
| 112 | { |
| 113 | QTest::addColumn<QByteArray>("value"); |
| 114 | QTest::addColumn<bool>("warning"); |
| 115 | |
| 116 | QTest::newRow("empty string, no warning") << QByteArray() << false; |
| 117 | QTest::newRow("0 integer, no warning") << QByteArray("0") << false; |
| 118 | QTest::newRow("boolean false, no warning") << QByteArray("false") << false; |
| 119 | QTest::newRow("boolean FALSE, no warning") << QByteArray("false") << false; |
| 120 | QTest::newRow("1 integer, warning") << QByteArray("1") << true; |
| 121 | QTest::newRow("boolean true, warning") << QByteArray("true") << true; |
| 122 | QTest::newRow("boolean TRUE, warning") << QByteArray("true") << true; |
| 123 | QTest::newRow("positive integer, no warning") << QByteArray("5") << false; |
| 124 | QTest::newRow("invalid integral value, no warning") << QByteArray("whatever") << false; |
| 125 | } |
| 126 | |
| 127 | void test_environment_variable() |
| 128 | { |
| 129 | if (!error.isEmpty()) { |
| 130 | QSKIP(qPrintable(error)); |
| 131 | } |
| 132 | QFETCH(QByteArray, value); |
| 133 | QFETCH(bool, warning); |
| 134 | qputenv("SHOW_SERVICEPROPERTIES_WARNINGS", value); |
| 135 | if (warning) { |
| 136 | // \u201C is a "Left Double Quotation Mark", and \u201D is "Right Double Quotation Mark" |
| 137 | QRegularExpression warningRe(QStringLiteral(u" QML ServiceProperties: No such property ['\u201C]ThisIsAnInvalidPropertyToWatch['\u201D]$")); |
| 138 | QTest::ignoreMessage(QtWarningMsg, warningRe); |
| 139 | |
| 140 | } |
| 141 | QScopedPointer<LomiriTestCase> test(new LomiriTestCase("InvalidPropertyWatcher.qml")); |
| 142 | UCServiceProperties *watcher = static_cast<UCServiceProperties*>(test->rootObject()->property("service").value<QObject*>()); |
| 143 | QVERIFY(watcher); |
| 144 | // error should contain the warning |
| 145 | // \u201C is a "Left Double Quotation Mark", and \u201D is "Right Double Quotation Mark" |
| 146 | // And sometimes they appear as '?' for some reason. However, that's beside the point of the test. |
| 147 | QRegularExpression errorRe(QStringLiteral(u"^No such property ['\u201C?]ThisIsAnInvalidPropertyToWatch['\u201D?]$")); |
| 148 | QTRY_VERIFY2(errorRe.match(watcher->property("error").toString()).hasMatch(), |
| 149 | qPrintable(QStringLiteral("The content of the \"error\" property is \"%1\"") |
| 150 | .arg(watcher->property("error").toString()))); |
| 151 | } |
| 152 | |
| 153 | void test_invalid_property_data() |
| 154 | { |
| 155 | QTest::addColumn<bool>("warning"); |
| 156 | |
| 157 | QTest::newRow("Without warning") << false; |
| 158 | QTest::newRow("With warning") << true; |
| 159 | } |
| 160 | void test_invalid_property() |
| 161 | { |
| 162 | if (!error.isEmpty()) { |
| 163 | QSKIP(qPrintable(error)); |
| 164 | } |
| 165 | QFETCH(bool, warning); |
| 166 | qputenv("SHOW_SERVICEPROPERTIES_WARNINGS", warning ? "1" : "0"); |
| 167 | if (warning) { |
| 168 | // \u201C is a "Left Double Quotation Mark", and \u201D is "Right Double Quotation Mark" |
| 169 | QRegularExpression warningRe(QStringLiteral(u" QML ServiceProperties: No such property ['\u201C]ThisIsAnInvalidPropertyToWatch['\u201D]$")); |
| 170 | QTest::ignoreMessage(QtWarningMsg, warningRe); |
| 171 | } |
| 172 | QScopedPointer<LomiriTestCase> test(new LomiriTestCase("InvalidPropertyWatcher.qml")); |
| 173 | UCServiceProperties *watcher = static_cast<UCServiceProperties*>(test->rootObject()->property("service").value<QObject*>()); |
| 174 | QVERIFY(watcher); |
| 175 | // error should contain the warning |
| 176 | // \u201C is a "Left Double Quotation Mark", and \u201D is "Right Double Quotation Mark" |
| 177 | // And sometimes they appear as '?' for some reason. However, that's beside the point of the test. |
| 178 | QRegularExpression errorRe(QStringLiteral(u"^No such property ['\u201C?]ThisIsAnInvalidPropertyToWatch['\u201D?]$")); |
| 179 | QTRY_VERIFY2(errorRe.match(watcher->property("error").toString()).hasMatch(), |
| 180 | qPrintable(QStringLiteral("The content of the \"error\" property is \"%1\"") |
| 181 | .arg(watcher->property("error").toString()))); |
| 182 | } |
| 183 | |
| 184 | void test_one_valid_one_invalid_property_data() |
| 185 | { |
| 186 | QTest::addColumn<bool>("warning"); |
| 187 | |
| 188 | QTest::newRow("Without warning") << false; |
| 189 | QTest::newRow("With warning") << true; |
| 190 | } |
| 191 | void test_one_valid_one_invalid_property() |
| 192 | { |
| 193 | if (!error.isEmpty()) { |
| 194 | QSKIP(qPrintable(error)); |
| 195 | } |
| 196 | QFETCH(bool, warning); |
| 197 | qputenv("SHOW_SERVICEPROPERTIES_WARNINGS", warning ? "1" : "0"); |
| 198 | if (warning) { |
| 199 | // \u201C is a "Left Double Quotation Mark", and \u201D is "Right Double Quotation Mark" |
| 200 | QRegularExpression warningRe(QStringLiteral(u" QML ServiceProperties: No such property ['\u201C]ThisIsAnInvalidPropertyToWatch['\u201D]$")); |
| 201 | QTest::ignoreMessage(QtWarningMsg, warningRe); |
| 202 | |
| 203 | } |
| 204 | QScopedPointer<LomiriTestCase> test(new LomiriTestCase("InvalidPropertyWatcher2.qml")); |
| 205 | UCServiceProperties *watcher = static_cast<UCServiceProperties*>(test->rootObject()->property("service").value<QObject*>()); |
| 206 | QVERIFY(watcher); |
| 207 | // error should contain the wearning |
| 208 | // \u201C is a "Left Double Quotation Mark", and \u201D is "Right Double Quotation Mark" |
| 209 | // And sometimes they appear as '?' for some reason. However, that's beside the point of the test. |
| 210 | QRegularExpression errorRe(QStringLiteral(u"^No such property ['\u201C?]ThisIsAnInvalidPropertyToWatch['\u201D?]$")); |
| 211 | QTRY_VERIFY2(errorRe.match(watcher->property("error").toString()).hasMatch(), |
| 212 | qPrintable(QStringLiteral("The content of \"error\" property is \"%1\"") |
| 213 | .arg(watcher->property("error").toString()))); |
| 214 | } |
| 215 | |
| 216 | void test_change_connection_props_data() |
| 217 | { |
| 218 | QTest::addColumn<QString>("property"); |
| 219 | QTest::addColumn<QString>("value"); |
| 220 | QTest::addColumn<bool>("warning"); |
| 221 | |
| 222 | QTest::newRow("Changing servcie, no warning") << "service" << "anything.else" << false; |
| 223 | QTest::newRow("Changing servcie, with warning") << "service" << "anything.else" << true; |
| 224 | QTest::newRow("Changing interface, no warning") << "serviceInterface" << "anything.else" << false; |
| 225 | QTest::newRow("Changing interface, with warning") << "serviceInterface" << "anything.else" << true; |
| 226 | QTest::newRow("Changing adaptor, no warning") << "adaptorInterface" << "anything.else" << false; |
| 227 | QTest::newRow("Changing adaptor, with warning") << "adaptorInterface" << "anything.else" << true; |
| 228 | } |
| 229 | void test_change_connection_props() |
| 230 | { |
| 231 | QFETCH(QString, property); |
| 232 | QFETCH(QString, value); |
| 233 | |
| 234 | if (!error.isEmpty()) { |
| 235 | QSKIP(qPrintable(error)); |
| 236 | } |
| 237 | QFETCH(bool, warning); |
| 238 | qputenv("SHOW_SERVICEPROPERTIES_WARNINGS", warning ? "1" : "0"); |
| 239 | if (warning) { |
| 240 | ignoreWarning("IncomingCallVibrateWatcher.qml", 22, 5, "QML ServiceProperties: Changing connection parameters forbidden."); |
| 241 | } |
| 242 | QScopedPointer<LomiriTestCase> test(new LomiriTestCase("IncomingCallVibrateWatcher.qml")); |
| 243 | UCServiceProperties *watcher = static_cast<UCServiceProperties*>(test->rootObject()->property("service").value<QObject*>()); |
| 244 | QVERIFY(watcher); |
| 245 | |
| 246 | // try to change the property |
| 247 | watcher->setProperty(property.toLocal8Bit().constData(), value); |
| 248 | // error shoudl also have the warning |
| 249 | QCOMPARE(watcher->property("error").toString(), QString("Changing connection parameters forbidden.")); |
| 250 | } |
| 251 | |
| 252 | }; |
| 253 | |
| 254 | QTEST_MAIN(tst_ServiceProperties) |
| 255 | |
| 256 | #include "tst_serviceproperties.moc" |