ctkRangeSliderTest.cpp 8.1 KB

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