File: tickmarks_slider_test.cpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (142 lines) | stat: -rw-r--r-- 4,462 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/************************************************************************
 *
 * Copyright (C) 2025 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "tickmarks_slider_test.hpp"

#include "select.hpp"

#include <ui/test/tester.hpp>

#include <cppunit/extensions/HelperMacros.h>

#include <QApplication>
#include <QLabel>
#include <QSlider>

#include <QtTest/QSignalSpy>
#include <QtTest/QTest>

#include <QWidget>

#include <utility>

namespace sight::ui::test::helper
{

//------------------------------------------------------------------------------

inline std::string position_to_string(tickmarks_slider_test::position _pos)
{
    switch(_pos)
    {
        case tickmarks_slider_test::position::top:
            return "top";

        case tickmarks_slider_test::position::right:
            return "right";

        case tickmarks_slider_test::position::bottom:
            return "bottom";

        case tickmarks_slider_test::position::left:
            return "left";
    }

    return "";
}

//------------------------------------------------------------------------------

inline  QSlider* take(tester& _tester, const selector& _slider)
{
    _slider.select(_tester);

    if(!_tester.is_a<QSlider*>())
    {
        _tester.yields<QSlider*>("'" + _slider.get_description(_tester) + "' is not a QSlider");
    }

    return _tester.get<QSlider*>();
}

//------------------------------------------------------------------------------

inline void move_impl(tester& _tester, const selector& _slider, tickmarks_slider_test::position _pos, int _times = 1)
{
    const QAbstractSlider::SliderAction action = _pos == tickmarks_slider_test::position::left
                                                 || _pos == tickmarks_slider_test::position::top
                                                 ? QAbstractSlider::SliderAction::SliderPageStepSub
                                                 : QAbstractSlider::SliderAction::SliderPageStepAdd;

    QSlider* const slider = take(_tester, _slider);

    for(int i = 0 ; i < _times ; i++)
    {
        slider->triggerAction(action);
    }
}

//------------------------------------------------------------------------------
void tickmarks_slider_test::move(
    tester& _tester,
    const selector& _slider,
    tickmarks_slider_test::position _pos,
    int _times
)
{
    auto bt = _tester.add_in_backtrace(
        "moving \"" + _slider.get_description(_tester) + "\" slider "
        + std::to_string(_times) + " step(s) to the " + position_to_string(_pos)
    );
    move_impl(_tester, _slider, _pos, _times);
}

//----------------------------------------------------------------------------------
void tickmarks_slider_test::check_value(tester& _tester, const selector& _slider, int _expected)
{
    auto bt = _tester.add_in_backtrace(
        "checking \"" + _slider.get_description(_tester) + "\" value == " + std::to_string(_expected)
    );
    QSlider* slider = take(_tester, _slider);
    int value       = slider->value();
    CPPUNIT_ASSERT_EQUAL(_expected, value);
}

//----------------------------------------------------------------------------------
void tickmarks_slider_test::mouse_drag_test(tester& _tester, const selector& _slider, QPoint _from, QPoint _to)
{
    auto bt = _tester.add_in_backtrace("dragging \"" + _slider.get_description(_tester) + "\"");

    QSlider* s = take(_tester, _slider);

    QMetaObject::invokeMethod(
        s,
        [ = ]
        {
            QTest::mousePress(s, Qt::LeftButton, Qt::NoModifier, _from);
            QTest::mouseMove(s, _to);
            QTest::mouseRelease(s, Qt::LeftButton, Qt::NoModifier, _to);
        },
        Qt::BlockingQueuedConnection
    );
}

} // namespace sight::ui::test::helper