diff --git a/libguh-core/guhconfiguration.cpp b/libguh-core/guhconfiguration.cpp index 4f2bc580..a14caddc 100644 --- a/libguh-core/guhconfiguration.cpp +++ b/libguh-core/guhconfiguration.cpp @@ -164,9 +164,14 @@ void GuhConfiguration::setServerName(const QString &serverName) GuhSettings settings(GuhSettings::SettingsRoleGlobal); settings.beginGroup("guhd"); - settings.setValue("name", serverName); - settings.endGroup(); - emit serverNameChanged(); + if (settings.value("name").toString() == serverName) { + qCDebug(dcApplication()) << "Configuration: Server name unchainged."; + settings.endGroup(); + } else { + settings.setValue("name", serverName); + settings.endGroup(); + emit serverNameChanged(); + } } QByteArray GuhConfiguration::timeZone() const @@ -182,9 +187,14 @@ void GuhConfiguration::setTimeZone(const QByteArray &timeZone) GuhSettings settings(GuhSettings::SettingsRoleGlobal); settings.beginGroup("guhd"); - settings.setValue("timeZone", timeZone); - settings.endGroup(); - emit timeZoneChanged(); + if (settings.value("timeZone").toByteArray() == timeZone) { + qCDebug(dcApplication()) << "Configuration: Time zone unchainged."; + settings.endGroup(); + } else { + settings.setValue("timeZone", timeZone); + settings.endGroup(); + emit timeZoneChanged(); + } } QLocale GuhConfiguration::locale() const @@ -200,9 +210,14 @@ void GuhConfiguration::setLocale(const QLocale &locale) GuhSettings settings(GuhSettings::SettingsRoleGlobal); settings.beginGroup("guhd"); - settings.setValue("language", locale.name()); - settings.endGroup(); - emit localeChanged(); + if (settings.value("language").toString() == locale.name()) { + qCDebug(dcApplication()) << "Configuration: Language unchainged."; + settings.endGroup(); + } else { + settings.setValue("language", locale.name()); + settings.endGroup(); + emit localeChanged(); + } } QHash GuhConfiguration::tcpServerConfigurations() const diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 3d4556f4..23e92a51 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -20,4 +20,3 @@ SUBDIRS = versioning \ #coap \ # temporary removed until fixed configurations \ timemanager \ - settings \ diff --git a/tests/auto/configurations/testconfigurations.cpp b/tests/auto/configurations/testconfigurations.cpp index c9c9a761..0611b389 100644 --- a/tests/auto/configurations/testconfigurations.cpp +++ b/tests/auto/configurations/testconfigurations.cpp @@ -43,6 +43,10 @@ private slots: void testTimeZones(); void testServerName(); + void testLanguages(); + +private: + QVariantMap loadBasicConfiguration(); }; @@ -62,19 +66,52 @@ void TestConfigurations::getConfigurations() void TestConfigurations::testTimeZones() { - QVariantMap params; QVariant response; QVariantMap configurations; + enableNotifications(); + + QVariantMap params; QVariant response; QVariantMap configurations; QVariantList configurationChangedNotifications; + + QSignalSpy notificationSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray))); QVariantList timeZones = injectAndWait("Configuration.GetTimeZones").toMap().value("params").toMap().value("timeZones").toList(); QVERIFY(timeZones.count() > 0); QVERIFY(timeZones.contains("America/Toronto")); QVERIFY(timeZones.contains("Europe/Vienna")); + QVERIFY(timeZones.contains("Africa/Dakar")); - // Set current timezone (Europe/Vienna) + // Get current configurations + QVariantMap basicConfigurationMap = loadBasicConfiguration(); + + // Set timezone unchainged params.clear(); response.clear(); configurations.clear(); - params.insert("timeZone", "Europe/Vienna"); + params.insert("timeZone", basicConfigurationMap.value("timeZone").toString()); response = injectAndWait("Configuration.SetTimeZone", params); verifyConfigurationError(response); + // Check notification not emitted + notificationSpy.wait(200); + configurationChangedNotifications = checkNotifications(notificationSpy, "Configuration.BasicConfigurationChanged"); + QVERIFY2(configurationChangedNotifications.count() == 0, "Got Configuration.BasicConfigurationChanged notification but should have not."); + + // Set new timezone (Africa/Dakar) + QString newTimeZone("Africa/Dakar"); + params.clear(); response.clear(); configurations.clear(); notificationSpy.clear(); + params.insert("timeZone", newTimeZone); + response = injectAndWait("Configuration.SetTimeZone", params); + verifyConfigurationError(response); + + notificationSpy.wait(200); + configurationChangedNotifications = checkNotifications(notificationSpy, "Configuration.BasicConfigurationChanged"); + QVERIFY2(configurationChangedNotifications.count() == 1, "Should get only one Configuration.BasicConfigurationChanged notification"); + QVariantMap notificationContent = configurationChangedNotifications.first().toMap().value("params").toMap(); + QVERIFY2(notificationContent.contains("basicConfiguration"), "Notification does not contain basicConfiguration"); + QVariantMap basicConfigurationNotificationMap = notificationContent.value("basicConfiguration").toMap(); + QVERIFY2(basicConfigurationNotificationMap.contains("language"), "Notification does not contain key language"); + QVERIFY2(basicConfigurationNotificationMap.contains("serverName"), "Notification does not contain key serverName"); + QVERIFY2(basicConfigurationNotificationMap.contains("serverTime"), "Notification does not contain key serverTime"); + QVERIFY2(basicConfigurationNotificationMap.contains("serverUuid"), "Notification does not contain key serverUuid"); + QVERIFY2(basicConfigurationNotificationMap.contains("timeZone"), "Notification does not contain key timeZone"); + QVERIFY2(basicConfigurationNotificationMap.value("timeZone").toString() == newTimeZone, "Notification does not contain the new timeZone"); + // Get current timezone and time params.clear(); response.clear(); configurations.clear(); configurations = injectAndWait("Configuration.GetConfigurations").toMap().value("params").toMap(); @@ -97,7 +134,7 @@ void TestConfigurations::testTimeZones() // Check new timezone params.clear(); response.clear(); configurations.clear(); configurations = injectAndWait("Configuration.GetConfigurations").toMap().value("params").toMap(); - QString newTimeZone = configurations.value("basicConfiguration").toMap().value("timeZone").toString(); + newTimeZone = configurations.value("basicConfiguration").toMap().value("timeZone").toString(); int newTime = configurations.value("basicConfiguration").toMap().value("serverTime").toInt(); qDebug() << newTimeZone << QDateTime::fromTime_t(newTime); QVERIFY(currentTimeZone != newTimeZone); @@ -109,33 +146,143 @@ void TestConfigurations::testTimeZones() QString reloadedTimeZone = configurations.value("basicConfiguration").toMap().value("timeZone").toString(); QVERIFY(newTimeZone == reloadedTimeZone); + // Reset the timezone params.clear(); response.clear(); params.insert("timeZone", "Europe/Vienna"); response = injectAndWait("Configuration.SetTimeZone", params); verifyConfigurationError(response); + + disableNotifications(); } void TestConfigurations::testServerName() { - QVariantMap params; QVariant response; QVariantMap configurations; - configurations = injectAndWait("Configuration.GetConfigurations").toMap().value("params").toMap(); - QString serverName = configurations.value("basicConfiguration").toMap().value("serverName").toString(); - QString serverUuid = configurations.value("basicConfiguration").toMap().value("serverUuid").toString(); + enableNotifications(); + + // Get current configurations + QVariantMap basicConfigurationMap = loadBasicConfiguration(); + + QString serverName = basicConfigurationMap.value("serverName").toString(); + QString serverUuid = basicConfigurationMap.value("serverUuid").toString(); qDebug() << "Server name" << serverName << "(" << serverUuid << ")"; - params.insert("serverName", "Test server"); + QSignalSpy notificationSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray))); + + // Set name unchainged + QVariantMap params; QVariant response; QVariantList configurationChangedNotifications; + params.insert("serverName", serverName); response = injectAndWait("Configuration.SetServerName", params); verifyConfigurationError(response); - configurations = injectAndWait("Configuration.GetConfigurations").toMap().value("params").toMap(); - QString newServerName = configurations.value("basicConfiguration").toMap().value("serverName").toString(); - QVERIFY(newServerName == "Test server"); + // Check notification not emitted + notificationSpy.wait(200); + configurationChangedNotifications = checkNotifications(notificationSpy, "Configuration.BasicConfigurationChanged"); + QVERIFY2(configurationChangedNotifications.count() == 0, "Got Configuration.BasicConfigurationChanged notification but should have not."); + + // TODO: verify notification + + // Set new server name + QString newServerName = QString("Test server %1").arg(QUuid::createUuid().toString()); + params.clear(); response.clear(); configurationChangedNotifications.clear(); + params.insert("serverName", newServerName); + + notificationSpy.clear(); + response = injectAndWait("Configuration.SetServerName", params); + verifyConfigurationError(response); + + // Check notification not emitted + notificationSpy.wait(500); + configurationChangedNotifications = checkNotifications(notificationSpy, "Configuration.BasicConfigurationChanged"); + QVERIFY2(configurationChangedNotifications.count() == 1, "Should get only one Configuration.BasicConfigurationChanged notification"); + + basicConfigurationMap = loadBasicConfiguration(); + QString loadedServerName = basicConfigurationMap.value("serverName").toString(); + QVERIFY2(loadedServerName == newServerName, "Server name not set correctly"); restartServer(); - configurations = injectAndWait("Configuration.GetConfigurations").toMap().value("params").toMap(); - QString reloadedServerName = configurations.value("basicConfiguration").toMap().value("serverName").toString(); - QVERIFY(newServerName == reloadedServerName); + basicConfigurationMap = loadBasicConfiguration(); + loadedServerName = basicConfigurationMap.value("serverName").toString(); + QVERIFY2(newServerName == loadedServerName, "Server name not loaded correctly after restart"); + + disableNotifications(); +} + +void TestConfigurations::testLanguages() +{ + enableNotifications(); + + // Get current configurations + QVariantMap basicConfigurationMap = loadBasicConfiguration(); + + QSignalSpy notificationSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray))); + + // Set language unchainged + QVariant response; QVariantMap params; + params.insert("language", basicConfigurationMap.value("language")); + response = injectAndWait("Configuration.SetLanguage", params); + verifyConfigurationError(response); + + // Check notification not emitted + notificationSpy.wait(200); + QVariantList languageChangedNotifications = checkNotifications(notificationSpy, "Configuration.LanguageChanged"); + QVERIFY2(languageChangedNotifications.count() == 0, "Got Configuration.LanguageChanged notification but should have not."); + + // Get available languages + response = injectAndWait("Configuration.GetAvailableLanguages"); + QVERIFY2(response.toMap().value("params").toMap().contains("languages"), "Did not get list of languages"); + QVariantMap responseMap = response.toMap().value("params").toMap(); + QVERIFY2(responseMap.value("languages").toList().count() >= 2, "Avaliable languages list to short: " + responseMap.value("languages").toList().count()); + + QVariantList languageVariantList = responseMap.value("languages").toList(); + foreach (const QVariant &languageVariant, languageVariantList) { + // Get current configurations + basicConfigurationMap = loadBasicConfiguration(); + + // Set language + params.clear(); response.clear(); notificationSpy.clear(); + params.insert("language", languageVariant); + QVariant response = injectAndWait("Configuration.SetLanguage", params); + verifyConfigurationError(response); + + // Check notification + notificationSpy.wait(200); + QVariantList languageChangedNotifications = checkNotifications(notificationSpy, "Configuration.LanguageChanged"); + + // If the language did not change no notification should be emited + if (basicConfigurationMap.value("language").toString() == languageVariant.toString()) { + QVERIFY2(languageChangedNotifications.count() == 0, "Got Configuration.LanguageChanged notification but should have not."); + } else { + QVERIFY2(languageChangedNotifications.count() == 1, "Should get only one Configuration.LanguageChanged notification"); + + // TODO: verify notification + + // Restart the server and check if the language will be loaded correctly + restartServer(); + + // Get configuration + basicConfigurationMap = loadBasicConfiguration(); + + QCOMPARE(basicConfigurationMap.value("language").toString(), languageVariant.toString()); + } + } + + // Reset the language to en_US + params.clear(); response.clear(); + params.insert("language", "en_US"); + + // Set language + response = injectAndWait("Configuration.SetLanguage", params); + verifyConfigurationError(response); + + disableNotifications(); +} + +QVariantMap TestConfigurations::loadBasicConfiguration() +{ + QVariant response = injectAndWait("Configuration.GetConfigurations"); + QVariantMap configurationMap = response.toMap().value("params").toMap(); + return configurationMap.value("basicConfiguration").toMap(); } #include "testconfigurations.moc" diff --git a/tests/auto/guhtestbase.cpp b/tests/auto/guhtestbase.cpp index 7c756ced..c9f4d5e2 100644 --- a/tests/auto/guhtestbase.cpp +++ b/tests/auto/guhtestbase.cpp @@ -116,27 +116,30 @@ GuhTestBase::GuhTestBase(QObject *parent) : qsrand(QDateTime::currentMSecsSinceEpoch()); m_mockDevice1Port = 1337 + (qrand() % 1000); m_mockDevice2Port = 7331 + (qrand() % 1000); + + // Important for settings QCoreApplication::instance()->setOrganizationName("guh-test"); - QSignalSpy spy(GuhCore::instance(), SIGNAL(initialized())); - spy.wait(); - - // Yes, we're intentionally mixing upper/lower case email here... username should not be case sensitive - GuhCore::instance()->userManager()->removeUser("dummy@guh.io"); - GuhCore::instance()->userManager()->createUser("dummy@guh.io", "DummyPW1!"); - m_apiToken = GuhCore::instance()->userManager()->authenticate("Dummy@guh.io", "DummyPW1!", "testcase"); } void GuhTestBase::initTestCase() { qDebug() << "GuhTestBase starting."; + // If testcase asserts cleanup won't do. Lets clear any previous test run settings leftovers + qDebug() << "Reset test settings"; GuhSettings rulesSettings(GuhSettings::SettingsRoleRules); rulesSettings.clear(); GuhSettings deviceSettings(GuhSettings::SettingsRoleDevices); deviceSettings.clear(); GuhSettings pluginSettings(GuhSettings::SettingsRolePlugins); pluginSettings.clear(); + GuhSettings statesSettings(GuhSettings::SettingsRoleDeviceStates); + statesSettings.clear(); + + // Reset to default settings + GuhSettings guhdSettings(GuhSettings::SettingsRoleGlobal); + guhdSettings.clear(); // debug categories // logging filers for core and libguh @@ -171,18 +174,29 @@ void GuhTestBase::initTestCase() QLoggingCategory::installFilter(loggingCategoryFilter); + // Start the server GuhCore::instance(); + // Wait unitl the server is initialized + QSignalSpy coreInitializedSpy(GuhCore::instance(), SIGNAL(initialized())); + coreInitializedSpy.wait(); + // Wait for the DeviceManager to signal that it has loaded plugins and everything - QSignalSpy spy(GuhCore::instance()->deviceManager(), SIGNAL(loaded())); - QVERIFY(spy.isValid()); - QVERIFY(spy.wait()); + QSignalSpy deviceManagerSpy(GuhCore::instance()->deviceManager(), SIGNAL(loaded())); + QVERIFY(deviceManagerSpy.isValid()); + QVERIFY(deviceManagerSpy.wait()); + + // Yes, we're intentionally mixing upper/lower case email here... username should not be case sensitive + GuhCore::instance()->userManager()->removeUser("dummy@guh.io"); + GuhCore::instance()->userManager()->createUser("dummy@guh.io", "DummyPW1!"); + m_apiToken = GuhCore::instance()->userManager()->authenticate("Dummy@guh.io", "DummyPW1!", "testcase"); if (MockTcpServer::servers().isEmpty()) { qWarning() << "no mock tcp server found"; exit(-1); } + // Add the mockdevice m_mockTcpServer = MockTcpServer::servers().first(); m_clientId = QUuid::createUuid(); diff --git a/tests/auto/guhtestbase.h b/tests/auto/guhtestbase.h index fdae7a27..d114ce5a 100644 --- a/tests/auto/guhtestbase.h +++ b/tests/auto/guhtestbase.h @@ -171,7 +171,7 @@ protected: // just for debugging inline void printJson(const QVariant &response) { QJsonDocument jsonDoc = QJsonDocument::fromVariant(response); - qDebug() << jsonDoc.toJson(); + qDebug() << qUtf8Printable(jsonDoc.toJson()); } void restartServer(); diff --git a/tests/auto/settings/testsettings.cpp b/tests/auto/settings/testsettings.cpp index 25acb99f..dd869166 100644 --- a/tests/auto/settings/testsettings.cpp +++ b/tests/auto/settings/testsettings.cpp @@ -46,54 +46,7 @@ private slots: void TestSettings::getSetLanguages() { - enableNotifications(); - QVariant response; QVariantMap params; - response = injectAndWait("Configuration.GetAvailableLanguages"); - QVERIFY2(response.toMap().value("params").toMap().contains("languages"), "Did not get list of languages"); - QVariantMap responseMap = response.toMap().value("params").toMap(); - QVERIFY2(responseMap.value("languages").toList().count() >= 2, "Avaliable languages list to short: " + responseMap.value("languages").toList().count()); - - QVariantList languageVariantList = responseMap.value("languages").toList(); - - - QSignalSpy notificationSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray))); - - foreach (const QVariant &languageVariant, languageVariantList) { - - // Get current configurations - response = injectAndWait("Configuration.GetConfigurations"); - QVariantMap configurationMap = response.toMap().value("params").toMap(); - - params.clear(); - params.insert("language", languageVariant); - - notificationSpy.clear(); - - // Set language - QVariant response = injectAndWait("Configuration.SetLanguage", params); - verifyConfigurationError(response); - - // Check notification - notificationSpy.wait(500); - QVariantList configurationChangedNotifications = checkNotifications(notificationSpy, "Configuration.LanguageChanged"); - printJson(configurationChangedNotifications); - - // If the language did not change no notification should be emited - if (configurationMap.value("basicConfiguration").toMap().value("language").toString() == languageVariant.toString()) { - QVERIFY2(configurationChangedNotifications.count() == 0, "Got Configuration.LanguageChanged notification but should have not."); - } else { - QVERIFY2(configurationChangedNotifications.count() == 1, "Should get only one Configuration.LanguageChanged notification"); - } - } - - // Reset the language to en_US - params.clear(); response.clear(); - params.insert("language", "en_US"); - - // Set language - response = injectAndWait("Configuration.SetLanguage", params); - verifyConfigurationError(response); } #include "testsettings.moc"