ctkRangeSliderTest.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <QString>
  17. #include <QStyle>
  18. #include <QStyleOptionSlider>
  19. #include <QTimer>
  20. // CTK includes
  21. #include "ctkRangeSlider.h"
  22. #include "ctkTest.h"
  23. // ----------------------------------------------------------------------------
  24. class ctkRangeSliderTester: public QObject
  25. {
  26. Q_OBJECT
  27. private slots:
  28. void testGUIEvents();
  29. void testTooltips();
  30. void testSimpleMouseEvents();
  31. void testHandleMouseEvents();
  32. void testHandleMouseEvents_data();
  33. void testGrooveMouseEvents();
  34. void testGrooveMouseEvents_data();
  35. };
  36. // ----------------------------------------------------------------------------
  37. void ctkRangeSliderTester::testGUIEvents()
  38. {
  39. ctkRangeSlider rangeSlider;
  40. rangeSlider.show();
  41. qApp->processEvents();
  42. rangeSlider.resize(100, 100);
  43. qApp->processEvents();
  44. rangeSlider.resize(1, 100);
  45. qApp->processEvents();
  46. rangeSlider.resize(100, 1);
  47. qApp->processEvents();
  48. rangeSlider.hide();
  49. qApp->processEvents();
  50. rangeSlider.show();
  51. }
  52. // ----------------------------------------------------------------------------
  53. void ctkRangeSliderTester::testTooltips()
  54. {
  55. ctkRangeSlider rangeSlider;
  56. QVERIFY(rangeSlider.handleToolTip().isEmpty()); // default value
  57. rangeSlider.setHandleToolTip("custom tooltip");
  58. QCOMPARE(rangeSlider.handleToolTip(), QString("custom tooltip"));
  59. }
  60. // ----------------------------------------------------------------------------
  61. void ctkRangeSliderTester::testSimpleMouseEvents()
  62. {
  63. ctkRangeSlider rangeSlider;
  64. QPoint center = rangeSlider.rect().center();
  65. QTest::mouseMove(&rangeSlider, center);
  66. QCOMPARE(rangeSlider.isSliderDown(), false);
  67. QTest::mousePress(&rangeSlider, Qt::LeftButton, 0, center);
  68. QCOMPARE(rangeSlider.isSliderDown(), true);
  69. QTest::mouseRelease(&rangeSlider, Qt::LeftButton, 0, center);
  70. QCOMPARE(rangeSlider.isSliderDown(), false);
  71. }
  72. // ----------------------------------------------------------------------------
  73. void ctkRangeSliderTester::testGrooveMouseEvents()
  74. {
  75. ctkRangeSlider rangeSlider(Qt::Horizontal);
  76. rangeSlider.setMaximum(100);
  77. rangeSlider.setValues(10, 90);
  78. QStyleOptionSlider option;
  79. // Resize to enforce a 1 to 1 mapping between pixel position and slider value.
  80. // Size of the handle is added because of the following line in ctkRangeSlider:
  81. // sliderMax = gr.right() - sliderLength + 1;
  82. QRect sliderHandleSize = rangeSlider.style()->subControlRect(
  83. QStyle::CC_Slider, &option, QStyle::SC_SliderHandle, &rangeSlider );
  84. rangeSlider.resize(100 + sliderHandleSize.width(), 20);
  85. QFETCH(int, moveInPx);
  86. QFETCH(int, expectedMinValue);
  87. QFETCH(int, expectedMaxValue);
  88. QPoint currentCursorPos = rangeSlider.rect().center();
  89. QTest::mouseMove(&rangeSlider, currentCursorPos);
  90. QTest::mousePress(&rangeSlider, Qt::LeftButton, 0, currentCursorPos);
  91. currentCursorPos += QPoint(moveInPx, 0);
  92. ctkTest::mouseMove(&rangeSlider, Qt::LeftButton, 0, currentCursorPos);
  93. QTest::mouseRelease(&rangeSlider, Qt::LeftButton, 0, currentCursorPos);
  94. QCOMPARE(rangeSlider.minimumValue(), expectedMinValue);
  95. QCOMPARE(rangeSlider.maximumValue(), expectedMaxValue);
  96. }
  97. // ----------------------------------------------------------------------------
  98. void ctkRangeSliderTester::testGrooveMouseEvents_data()
  99. {
  100. QTest::addColumn<int>("moveInPx");
  101. QTest::addColumn<int>("expectedMinValue");
  102. QTest::addColumn<int>("expectedMaxValue");
  103. QTest::newRow("MinMax +5") << 5 << 15 << 95;
  104. QTest::newRow("MinMax -5") << -5 << 5 << 85;
  105. QTest::newRow("MinMax +10") << 10 << 20 << 100;
  106. QTest::newRow("MinMax -10") << -10 << 0 << 80;
  107. QTest::newRow("MinMax +20 -> bound to max") << 20 << 30 << 100;
  108. QTest::newRow("MinMax -20 -> bound to min") << -20 << 0 << 70;
  109. }
  110. // ----------------------------------------------------------------------------
  111. void ctkRangeSliderTester::testHandleMouseEvents()
  112. {
  113. ctkRangeSlider rangeSlider(Qt::Horizontal);
  114. rangeSlider.setMaximum(100);
  115. rangeSlider.setValues(10, 80);
  116. QStyleOptionSlider option;
  117. // Resize to enforce a 1 to 1 mapping between pixel position and slider value.
  118. // Size of the handle is added because of the following line in ctkRangeSlider:
  119. // sliderMax = gr.right() - sliderLength + 1;
  120. QRect sliderHandleSize = rangeSlider.style()->subControlRect(
  121. QStyle::CC_Slider, &option, QStyle::SC_SliderHandle, &rangeSlider );
  122. rangeSlider.resize(100 + sliderHandleSize.width(), 20);
  123. QFETCH(bool, minHandle);
  124. QFETCH(bool, symmetricMoves);
  125. QFETCH(int, moveInPx);
  126. QFETCH(int, expectedMinValue);
  127. QFETCH(int, expectedMaxValue);
  128. QPoint currentCursorPos = rangeSlider.rect().center();
  129. if (minHandle)
  130. {
  131. currentCursorPos = QPoint(10, 10);
  132. }
  133. else
  134. {
  135. currentCursorPos = QPoint(80, 10);
  136. }
  137. rangeSlider.setSymmetricMoves(symmetricMoves);
  138. QTest::mouseMove(&rangeSlider, currentCursorPos);
  139. QTest::mousePress(&rangeSlider, Qt::LeftButton, 0, currentCursorPos);
  140. currentCursorPos += QPoint(moveInPx, 0);
  141. ctkTest::mouseMove(&rangeSlider, Qt::LeftButton, 0, currentCursorPos);
  142. QTest::mouseRelease(&rangeSlider, Qt::LeftButton, 0, currentCursorPos);
  143. QCOMPARE(rangeSlider.minimumValue(), expectedMinValue);
  144. QCOMPARE(rangeSlider.maximumValue(), expectedMaxValue);
  145. }
  146. // ----------------------------------------------------------------------------
  147. void ctkRangeSliderTester::testHandleMouseEvents_data()
  148. {
  149. QTest::addColumn<bool>("minHandle");
  150. QTest::addColumn<bool>("symmetricMoves");
  151. QTest::addColumn<int>("moveInPx");
  152. QTest::addColumn<int>("expectedMinValue");
  153. QTest::addColumn<int>("expectedMaxValue");
  154. QTest::newRow("Min +5") << true << false << 5 << 15 << 80;
  155. QTest::newRow("Min -5") << true << false << -5 << 5 << 80;
  156. QTest::newRow("Min +10") << true << false << 10 << 20 << 80;
  157. QTest::newRow("Min -10") << true << false << -10 << 0 << 80;
  158. QTest::newRow("Min +20") << true << false << 20 << 30 << 80;
  159. QTest::newRow("Min -20 -> bound to min") << true << false << -20 << 0 << 80;
  160. QTest::newRow("Max +5") << false << false << 5 << 10 << 85;
  161. QTest::newRow("Max -5") << false << false << -5 << 10 << 75;
  162. QTest::newRow("Max +10") << false << false << 10 << 10 << 90;
  163. QTest::newRow("Max -10") << false << false << -10 << 10 << 70;
  164. QTest::newRow("Max +20 -> bound to max") << false << false << 20 << 10 << 100;
  165. QTest::newRow("Max -20") << false << false << -20 << 10 << 60;
  166. QTest::newRow("Sym Min +5") << true << true << 5 << 15 << 75;
  167. QTest::newRow("Sym Min -5") << true << true << -5 << 5 << 85;
  168. QTest::newRow("Sym Min +10") << true << true << 10 << 20 << 70;
  169. QTest::newRow("Sym Min -10") << true << true << -10 << 0 << 90;
  170. QTest::newRow("Sym Min +20") << true << true << 20 << 30 << 60;
  171. QTest::newRow("Sym Min -20 -> bound to min") << true << true << -20 << 0 << 90;
  172. QTest::newRow("Sym Max +20") << false << true << 20 << 0 << 100;
  173. }
  174. // ----------------------------------------------------------------------------
  175. CTK_TEST_MAIN(ctkRangeSliderTest)
  176. #include "moc_ctkRangeSliderTest.cpp"