ctkSettingsPanelTest.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QSettings>
  17. #include <QSignalSpy>
  18. #include <QSpinBox>
  19. // CTK includes
  20. #include "ctkSettingsPanel.h"
  21. #include "ctkSettingsPanelTest2Helper.h"
  22. #include "ctkTest.h"
  23. // STD includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. // ----------------------------------------------------------------------------
  27. class ctkSettingsPanelTester: public QObject
  28. {
  29. Q_OBJECT
  30. private slots:
  31. void testChangeProperty();
  32. void testChangeProperty_data();
  33. void testEmptyQStringList();
  34. };
  35. //-----------------------------------------------------------------------------
  36. void ctkSettingsPanelTester::testChangeProperty()
  37. {
  38. QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Common ToolKit", "CTK");
  39. QSpinBox spinBox;
  40. ctkSettingsPanel panel;
  41. panel.setSettings(&settings);
  42. spinBox.setValue(1);
  43. QFETCH(QString, label);
  44. QFETCH(ctkSettingsPanel::SettingOptions, options);
  45. panel.registerProperty("property", &spinBox,
  46. "value", SIGNAL(valueChanged(int)),
  47. label, options);
  48. QCOMPARE(spinBox.value(), 1);
  49. QCOMPARE(panel.settingLabel("property"), label);
  50. QCOMPARE(panel.settingOptions("property"), options);
  51. qRegisterMetaType<QVariant>("QVariant");
  52. QSignalSpy spy(&panel, SIGNAL(settingChanged(QString,QVariant)));
  53. QFETCH(int, value);
  54. QFETCH(bool, setOnObject);
  55. if (setOnObject)
  56. {
  57. spinBox.setValue(value);
  58. }
  59. else
  60. {
  61. panel.setSetting("property", QVariant(value));
  62. }
  63. QFETCH(int, expectedSettingChangedCount);
  64. QCOMPARE(spy.count(), expectedSettingChangedCount);
  65. QFETCH(QStringList, expectedChangedSettings);
  66. QCOMPARE(panel.changedSettings(), expectedChangedSettings);
  67. // make sure it didn't change
  68. QCOMPARE(panel.settingLabel("property"), label);
  69. QCOMPARE(panel.settingOptions("property"), options);
  70. panel.resetSettings();
  71. }
  72. //-----------------------------------------------------------------------------
  73. void ctkSettingsPanelTester::testChangeProperty_data()
  74. {
  75. QTest::addColumn<QString>("label");
  76. QTest::addColumn<ctkSettingsPanel::SettingOptions>("options");
  77. QTest::addColumn<int>("value");
  78. QTest::addColumn<bool>("setOnObject");
  79. QTest::addColumn<int>("expectedSettingChangedCount");
  80. QTest::addColumn<QStringList>("expectedChangedSettings");
  81. QTest::newRow("null none changed obj") << QString() << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 2 << true << 1 << QStringList("property");
  82. QTest::newRow("null none changed panel") << QString() << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 2 << false << 1 << QStringList("property");
  83. QTest::newRow("null none unchanged obj") << QString() << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 1 << true << 0 << QStringList();
  84. QTest::newRow("null none unchanged panel") << QString() << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 1 << false << 0 << QStringList();
  85. QTest::newRow("null RequireRestart changed obj") << QString() << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 2 << true << 1 << QStringList("property");
  86. QTest::newRow("null RequireRestart changed panel") << QString() << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 2 << false << 1 << QStringList("property");
  87. QTest::newRow("null RequireRestart unchanged obj") << QString() << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 1 << true << 0 << QStringList();
  88. QTest::newRow("null RequireRestart unchanged panel") << QString() << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 1 << false << 0 << QStringList();
  89. QTest::newRow("empty none changed obj") << QString("") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 2 << true << 1 << QStringList("property");
  90. QTest::newRow("empty none changed panel") << QString("") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 2 << false << 1 << QStringList("property");
  91. QTest::newRow("empty none unchanged obj") << QString("") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 1 << true << 0 << QStringList();
  92. QTest::newRow("empty none unchanged panel") << QString("") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 1 << false << 0 << QStringList();
  93. QTest::newRow("empty RequireRestart changed obj") << QString("") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 2 << true << 1 << QStringList("property");
  94. QTest::newRow("empty RequireRestart changed panel") << QString("") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 2 << false << 1 << QStringList("property");
  95. QTest::newRow("empty RequireRestart unchanged obj") << QString("") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 1 << true << 0 << QStringList();
  96. QTest::newRow("empty RequireRestart unchanged panel") << QString("") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 1 << false << 0 << QStringList();
  97. QTest::newRow("label none changed obj") << QString("label") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 2 << true << 1 << QStringList("property");
  98. QTest::newRow("label none changed panel") << QString("label") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 2 << false << 1 << QStringList("property");
  99. QTest::newRow("label none unchanged obj") << QString("label") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 1 << true << 0 << QStringList();
  100. QTest::newRow("label none unchanged panel") << QString("label") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionNone) << 1 << false << 0 << QStringList();
  101. QTest::newRow("label RequireRestart changed obj") << QString("label") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 2 << true << 1 << QStringList("property");
  102. QTest::newRow("label RequireRestart changed panel") << QString("label") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 2 << false << 1 << QStringList("property");
  103. QTest::newRow("label RequireRestart unchanged obj") << QString("label") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 1 << true << 0 << QStringList();
  104. QTest::newRow("label RequireRestart unchanged panel") << QString("label") << ctkSettingsPanel::SettingOptions(ctkSettingsPanel::OptionRequireRestart) << 1 << false << 0 << QStringList();
  105. }
  106. //-----------------------------------------------------------------------------
  107. void ctkSettingsPanelTester::testEmptyQStringList()
  108. {
  109. {
  110. // When QSettings goes out of scope, we are the settings file is up-to-date
  111. QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Common ToolKit", "CTK");
  112. settings.clear();
  113. settings.setValue("list", QVariant(QStringList()));
  114. }
  115. // Regression: Reading empty QStringList property from settings should be handled properly
  116. ctkSettingsPanel settingsPanel;
  117. ctkSettingsPanelTest2Helper * list = new ctkSettingsPanelTest2Helper(&settingsPanel);
  118. settingsPanel.registerProperty("list", list, "list", SIGNAL(listChanged()));
  119. QSettings settings2(QSettings::IniFormat, QSettings::UserScope, "Common ToolKit", "CTK");
  120. settingsPanel.setSettings(&settings2);
  121. }
  122. // ----------------------------------------------------------------------------
  123. CTK_TEST_MAIN(ctkSettingsPanelTest)
  124. #include "moc_ctkSettingsPanelTest.cpp"