ctkSliderWidgetValueProxyTest.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "ctkLinearValueProxy.h"
  26. #include "ctkSliderWidget.h"
  27. #include "ctkTest.h"
  28. #include "ctkValueProxy.h"
  29. namespace
  30. {
  31. //-----------------------------------------------------------------------------
  32. void getSpyReport(QSignalSpy& spy, double expectedValue)
  33. {
  34. QCOMPARE(spy.count(), 1);
  35. QList<QVariant> arguments = spy.takeFirst(); // take the first signal
  36. ctkTest::COMPARE(arguments.at(0).toDouble(), expectedValue);
  37. }
  38. } // end namespace
  39. // ----------------------------------------------------------------------------
  40. class ctkSliderWidgetValueProxyTester: public QObject
  41. {
  42. Q_OBJECT
  43. private slots:
  44. void testSetValue();
  45. void testSetValue_data();
  46. };
  47. //-----------------------------------------------------------------------------
  48. void ctkSliderWidgetValueProxyTester::testSetValue()
  49. {
  50. // Setup
  51. ctkSliderWidget slider;
  52. slider.setMinimum(-200);
  53. slider.setMaximum(200);
  54. slider.setValue(-32.6);
  55. QFETCH(double, coefficient);
  56. QFETCH(double, offset);
  57. ctkLinearValueProxy proxy;
  58. proxy.setCoefficient(coefficient);
  59. proxy.setOffset(offset);
  60. slider.setValueProxy(&proxy);
  61. // Spy
  62. QSignalSpy valueSpy(&slider, SIGNAL(valueChanged(double)));
  63. // Test
  64. QFETCH(double, value);
  65. slider.setValue(value);
  66. QFETCH(double, expectedValue);
  67. getSpyReport(valueSpy, expectedValue);
  68. ctkTest::COMPARE(slider.value(), expectedValue);
  69. }
  70. //-----------------------------------------------------------------------------
  71. void ctkSliderWidgetValueProxyTester::testSetValue_data()
  72. {
  73. QTest::addColumn<double>("coefficient");
  74. QTest::addColumn<double>("offset");
  75. QTest::addColumn<double>("value");
  76. QTest::addColumn<double>("expectedValue");
  77. //---------------------------------------------------------------------------
  78. // Offset
  79. QTest::newRow("Offset only") << 1.0 << 42.19176 << 0.1 << 0.1;
  80. QTest::newRow("Offset only: less than min")
  81. << 1.0 << 42.19 << -510.0 << -242.19;
  82. QTest::newRow("Offset only: less than min but ok with offset")
  83. << 1.0 << 42.19 << -230.0 << -230.0;
  84. QTest::newRow("Offset only: less than min with offset")
  85. << 1.0 << -42.19 << -190.0 << -157.81;
  86. QTest::newRow("Offset only: more than max with offset")
  87. << 1.0 << 42.19 << 160.0 << 157.81;
  88. QTest::newRow("Offset only: more than max")
  89. << 1.0 << -42.19 << 65010.0 << 242.19;
  90. QTest::newRow("Offset only: less than max but ok with offset")
  91. << 1.0 << -42.19 << 229.1 << 229.1;
  92. QTest::newRow("Offset only: max")
  93. << 1.0 << 42.19 << std::numeric_limits<double>::max() << 157.81;
  94. QTest::newRow("Offset only: min")
  95. << 1.0 << 42.19 << -std::numeric_limits<double>::max() << -242.19;
  96. QTest::newRow("Offset only: infinity")
  97. << 1.0 << 42.19 << std::numeric_limits<double>::infinity() << 157.81;
  98. QTest::newRow("Offset only: - infinity")
  99. << 1.0 << 42.19 << -std::numeric_limits<double>::infinity() << -242.19;
  100. QTest::newRow("Offset only: Nan")
  101. << 1.0 << 42.19 << std::numeric_limits<double>::quiet_NaN() << 157.81;
  102. // coeff // offset // value // expectedValue
  103. //---------------------------------------------------------------------------
  104. // Coefficient
  105. QTest::newRow("Coeff only") << 5.0 << 0.0 << 0.1 << 0.1;
  106. QTest::newRow("Coeff only: less than min")
  107. << 5.0 << 0.0 << -510.0 << -40.0;
  108. QTest::newRow("Coeff only: less than min but ok with coeff")
  109. << 0.5 << 0.0 << -230.0 << -230.0;
  110. QTest::newRow("Coeff only: less than min with coeff")
  111. << 5.0 << 0.0 << -190.0 << -40.0;
  112. QTest::newRow("Coeff only: more than max with coeff")
  113. << 5.0 << 0.0 << 160.0 << 40.0;
  114. QTest::newRow("Coeff only: more than max")
  115. << 5.0 << 0.0 << 65010.0 << 40.0;
  116. QTest::newRow("Offset only: less than max but ok with coeff")
  117. << 0.5 << 0.0 << 229.2 << 229.2;
  118. QTest::newRow("Coeff only: max")
  119. << 5.0 << 0.0 << std::numeric_limits<double>::max() << 40.0;
  120. QTest::newRow("Coeff only: min")
  121. << 5.0 << 0.0 << -std::numeric_limits<double>::max() << -40.0;
  122. QTest::newRow("Coeff only: infinity")
  123. << 5.0 << 0.0 << std::numeric_limits<double>::infinity() << 40.0;
  124. QTest::newRow("Coeff only: - infinity")
  125. << 5.0 << 0.0 << -std::numeric_limits<double>::infinity() << -40.0;
  126. QTest::newRow("Coeff only: Nan")
  127. << 5.0 << 0.0 << std::numeric_limits<double>::quiet_NaN() << 40.0;
  128. // coeff // offset // value // expectedValue
  129. //---------------------------------------------------------------------------
  130. // Linear
  131. QTest::newRow("Linear") << 5.0 << 0.0 << 0.1 << 0.1;
  132. QTest::newRow("Linear: less than min")
  133. << 5.0 << 12.0 << -510.0 << -42.4;
  134. QTest::newRow("Linear: less than min but ok with function")
  135. << 0.5 << 12.0 << -230.0 << -230.0;
  136. QTest::newRow("Linear: less than min with function")
  137. << 5.0 << 12.0 << -61.5 << -42.4;
  138. QTest::newRow("Linear: more than max with function")
  139. << 5.0 << 12.0 << 160.0 << 37.6;
  140. QTest::newRow("Linear: more than max")
  141. << 5.0 << 12.0 << 65010.0 << 37.6;
  142. QTest::newRow("Offset only: less than max but ok with function")
  143. << 0.5 << 12.0 << 229.2 << 229.2;
  144. QTest::newRow("Linear: max")
  145. << 5.0 << 12.0 << std::numeric_limits<double>::max() << 37.6;
  146. QTest::newRow("Linear: min")
  147. << 5.0 << 12.0 << -std::numeric_limits<double>::max() << -42.4;
  148. QTest::newRow("Linear: infinity")
  149. << 5.0 << 12.0 << std::numeric_limits<double>::infinity() << 37.6;
  150. QTest::newRow("Linear: - infinity")
  151. << 5.0 << 12.0 << -std::numeric_limits<double>::infinity() << -42.4;
  152. QTest::newRow("Linear: Nan")
  153. << 5.0 << 12.0 << std::numeric_limits<double>::quiet_NaN() << 37.6;
  154. }
  155. // ----------------------------------------------------------------------------
  156. CTK_TEST_MAIN(ctkSliderWidgetValueProxyTest)
  157. #include "moc_ctkSliderWidgetValueProxyTest.cpp"