ctkSliderWidgetTest.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <QDoubleSpinBox>
  17. #include <QLineEdit>
  18. #include <QString>
  19. #include <QStyle>
  20. #include <QStyleOptionSlider>
  21. #include <QTimer>
  22. // CTK includes
  23. #include "ctkDoubleSlider.h"
  24. #include "ctkDoubleSpinBox.h"
  25. #include "ctkSliderWidget.h"
  26. #include "ctkTest.h"
  27. // ----------------------------------------------------------------------------
  28. class ctkSliderWidgetTester: public QObject
  29. {
  30. Q_OBJECT
  31. private slots:
  32. void testUI();
  33. void testSetMinimum();
  34. void testSetMinimum_data();
  35. void testSetMaximum();
  36. void testSetMaximum_data();
  37. void testDecimalsByValue();
  38. /// This test makes sure the number of decimals increased with Ctrl+'+' does
  39. /// not break the synchronization between the value of the slider and the
  40. /// value of the spinbox.
  41. void testDecimalsByShortcuts();
  42. /// This test makes sure a valueChanged signal is fired when moving
  43. /// the handle to 0 for the first time.
  44. void testValueChangedWithNoTracking();
  45. };
  46. // ----------------------------------------------------------------------------
  47. void ctkSliderWidgetTester::testUI()
  48. {
  49. ctkSliderWidget slider;
  50. slider.setMinimum(-100.);
  51. slider.setMaximum(100.);
  52. slider.setValue(26.2110001);
  53. slider.setPrefix("A: ");
  54. slider.show();
  55. #if (QT_VERSION >= 0x50000)
  56. QTest::qWaitForWindowActive(&slider);
  57. #else
  58. QTest::qWaitForWindowShown(&slider);
  59. #endif
  60. QObject::connect(&slider, SIGNAL(valueChanged(double)),
  61. &slider, SLOT(setValue(double)), Qt::QueuedConnection);
  62. //qApp->exec();
  63. }
  64. // ----------------------------------------------------------------------------
  65. void ctkSliderWidgetTester::testSetMinimum()
  66. {
  67. ctkSliderWidget slider;
  68. QFETCH(double, minimum);
  69. slider.setMinimum(minimum);
  70. QFETCH(double, expectedMinimum);
  71. QFETCH(double, expectedValue);
  72. QCOMPARE(slider.minimum(), expectedMinimum);
  73. QCOMPARE(slider.value(), expectedValue);
  74. }
  75. // ----------------------------------------------------------------------------
  76. void ctkSliderWidgetTester::testSetMinimum_data()
  77. {
  78. QTest::addColumn<double>("minimum");
  79. QTest::addColumn<double>("expectedMinimum");
  80. QTest::addColumn<double>("expectedValue");
  81. QTest::newRow("0. -> 0.]") << 0. << 0. << 0.;
  82. QTest::newRow("10.0123 -> 10.0123]") << 10.0123 << 10.0123 << 10.0123;
  83. QTest::newRow("-10.0123 -> -10.0123]") << -10.0123 << -10.0123 << 0.;
  84. QTest::newRow("200.0123 -> 200.0123]") << 200.0123 << 200.0123 << 200.0123;
  85. }
  86. // ----------------------------------------------------------------------------
  87. void ctkSliderWidgetTester::testSetMaximum()
  88. {
  89. ctkSliderWidget slider;
  90. QFETCH(double, maximum);
  91. slider.setMaximum(maximum);
  92. QFETCH(double, expectedMaximum);
  93. QFETCH(double, expectedValue);
  94. QCOMPARE(slider.maximum(), expectedMaximum);
  95. QCOMPARE(slider.value(), expectedValue);
  96. }
  97. // ----------------------------------------------------------------------------
  98. void ctkSliderWidgetTester::testSetMaximum_data()
  99. {
  100. QTest::addColumn<double>("maximum");
  101. QTest::addColumn<double>("expectedMaximum");
  102. QTest::addColumn<double>("expectedValue");
  103. QTest::newRow("0. -> 0.") << 0. << 0. << 0.;
  104. QTest::newRow("10.0123 -> 0.") << 10.0123 << 10.0123 << 0.;
  105. QTest::newRow("-10.0123 -> -10.0123") << -10.0123 << -10.0123 << -10.0123;
  106. QTest::newRow("200.0123 -> 0.") << 200.0123 << 200.0123 << 0.;
  107. }
  108. // ----------------------------------------------------------------------------
  109. void ctkSliderWidgetTester::testDecimalsByValue()
  110. {
  111. ctkSliderWidget slider;
  112. slider.spinBox()->setDecimalsOption(
  113. ctkDoubleSpinBox::DecimalsByValue | ctkDoubleSpinBox::DecimalsByShortcuts );
  114. slider.setValue(-12.4);
  115. //slider.show();
  116. //QTest::qWaitForWindowShown(&slider);
  117. //qApp->exec();
  118. //QSignalSpy spy(&slider, SIGNAL(decimalsChanged(int)));
  119. slider.setSingleStep(1.3);
  120. slider.setRange(-87.2949, 81.7045);
  121. slider.setValue(-15);
  122. slider.slider()->triggerAction(QAbstractSlider::SliderSingleStepSub);
  123. slider.setSingleStep(1.3);
  124. }
  125. // ----------------------------------------------------------------------------
  126. void ctkSliderWidgetTester::testDecimalsByShortcuts()
  127. {
  128. ctkSliderWidget slider;
  129. slider.spinBox()->setDecimalsOption(ctkDoubleSpinBox::DecimalsByShortcuts);
  130. slider.setSingleStep(1.299995422363281);
  131. slider.setPageStep(1.299995422363281);
  132. slider.setRange(-100., 100.);
  133. slider.setValue( -2.145195007324205 );
  134. slider.show();
  135. #if (QT_VERSION >= 0x50000)
  136. QTest::qWaitForWindowActive(&slider);
  137. #else
  138. QTest::qWaitForWindowShown(&slider);
  139. #endif
  140. //qApp->exec();
  141. //QSignalSpy spy(&slider, SIGNAL(decimalsChanged(int)));
  142. slider.slider()->triggerAction(QAbstractSlider::SliderSingleStepAdd);
  143. slider.slider()->triggerAction(QAbstractSlider::SliderSingleStepAdd);
  144. slider.slider()->triggerAction(QAbstractSlider::SliderSingleStepAdd);
  145. QTest::keyClick(slider.spinBox(), Qt::Key_Plus, Qt::ControlModifier);
  146. QTest::keyClick(slider.spinBox(), Qt::Key_Plus, Qt::ControlModifier);
  147. QTest::keyClick(slider.spinBox(), Qt::Key_Plus, Qt::ControlModifier);
  148. QCOMPARE(slider.decimals(), 5);
  149. }
  150. // ----------------------------------------------------------------------------
  151. void ctkSliderWidgetTester::testValueChangedWithNoTracking()
  152. {
  153. ctkSliderWidget slider;
  154. slider.setValue((slider.maximum() + slider.minimum()) / 2);
  155. slider.show();
  156. #if (QT_VERSION >= 0x50000)
  157. QTest::qWaitForWindowActive(&slider);
  158. #else
  159. QTest::qWaitForWindowShown(&slider);
  160. #endif
  161. slider.setTracking(false);
  162. QSignalSpy spy(&slider, SIGNAL(valueChanged(double)));
  163. QPoint currentPoint = slider.slider()->rect().center();
  164. QPoint nextPoint = QPoint(slider.slider()->rect().left(), currentPoint.y());
  165. QTest::mousePress(slider.slider()->slider(), Qt::LeftButton, 0, currentPoint);
  166. ctkTest::mouseMove(slider.slider()->slider(), Qt::LeftButton, 0, nextPoint);
  167. QTest::mouseRelease(slider.slider()->slider(), Qt::LeftButton, 0, nextPoint);
  168. QCOMPARE(spy.count(), 1);
  169. }
  170. // ----------------------------------------------------------------------------
  171. CTK_TEST_MAIN(ctkSliderWidgetTest)
  172. #include "moc_ctkSliderWidgetTest.cpp"