┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/tests/kitemlistsmoothscrollertest.cpp
blob: ac0a81d31da67502ee51368b9578735e30e0d90a (plain)
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
/*
 * SPDX-FileCopyrightText: 2025 Méven Car <[email protected]>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "kitemviews/private/kitemlistsmoothscroller.h"

#include <QScrollBar>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTest>

class KItemListSmoothScrollerTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void initTestCase();
    void testScrollToCurrentPositionEmitsScrollingStopped();
    void testScrollToClampedPositionEmitsScrollingStopped();
};

void KItemListSmoothScrollerTest::initTestCase()
{
    QStandardPaths::setTestModeEnabled(true);
}

void KItemListSmoothScrollerTest::testScrollToCurrentPositionEmitsScrollingStopped()
{
    // KItemListView::scrollToItem() delegates to KItemListSmoothScroller::scrollTo()
    // for any non-zero offset and then relies on scrollingStopped() to know when the
    // view settled. Callers such as DolphinView::renameSelectedItems() only open the
    // inline rename editor once scrollingStopped() arrives. If scrollTo() is asked to
    // move to the position the scrollbar is already at, no animation runs, so it must
    // still emit scrollingStopped() or the editor never appears.
    QScrollBar scrollBar(Qt::Vertical);
    scrollBar.setRange(0, 100);
    scrollBar.setValue(50);

    KItemListSmoothScroller scroller(&scrollBar);
    QSignalSpy spy(&scroller, &KItemListSmoothScroller::scrollingStopped);

    scroller.scrollTo(scrollBar.value());

    QCOMPARE(spy.count(), 1);
}

void KItemListSmoothScrollerTest::testScrollToClampedPositionEmitsScrollingStopped()
{
    // Reproduces renaming an item at the very bottom: the scrollbar is already at its
    // maximum, scrollToItem() still computes a non-zero offset and requests a further
    // scroll, but the clamped target equals the current value, so nothing moves.
    QScrollBar scrollBar(Qt::Vertical);
    scrollBar.setRange(0, 100);
    scrollBar.setValue(scrollBar.maximum());

    KItemListSmoothScroller scroller(&scrollBar);
    QSignalSpy spy(&scroller, &KItemListSmoothScroller::scrollingStopped);

    scroller.scrollTo(scrollBar.maximum() + 50);

    QCOMPARE(spy.count(), 1);
}

QTEST_MAIN(KItemListSmoothScrollerTest)

#include "kitemlistsmoothscrollertest.moc"