summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/scroll_off_margin_test.go
blob: 4059aaa4848f374bb98a3c9a65568a09d9a90937 (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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package controllers

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func Test_calculateLinesToScrollUp(t *testing.T) {
	scenarios := []struct {
		name                  string
		viewPortStart         int
		viewPortHeight        int
		scrollOffMargin       int
		lineIdxBefore         int
		lineIdxAfter          int
		expectedLinesToScroll int
	}{
		{
			name:                  "before position is above viewport - don't scroll",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       3,
			lineIdxBefore:         9,
			lineIdxAfter:          8,
			expectedLinesToScroll: 0,
		},
		{
			name:                  "before position is below viewport - don't scroll",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       3,
			lineIdxBefore:         20,
			lineIdxAfter:          19,
			expectedLinesToScroll: 0,
		},
		{
			name:                  "before and after positions are outside scroll-off margin - don't scroll",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       3,
			lineIdxBefore:         14,
			lineIdxAfter:          13,
			expectedLinesToScroll: 0,
		},
		{
			name:                  "before outside, after inside scroll-off margin - scroll by 1",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       3,
			lineIdxBefore:         13,
			lineIdxAfter:          12,
			expectedLinesToScroll: 1,
		},
		{
			name:                  "scroll-off margin is zero - scroll by 1 at end of view",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       0,
			lineIdxBefore:         10,
			lineIdxAfter:          9,
			expectedLinesToScroll: 1,
		},
		{
			name:                  "before inside scroll-off margin - scroll by more than 1",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       3,
			lineIdxBefore:         11,
			lineIdxAfter:          10,
			expectedLinesToScroll: 3,
		},
		{
			name:                  "very large scroll-off margin - keep view centered (even viewport height)",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       999,
			lineIdxBefore:         15,
			lineIdxAfter:          14,
			expectedLinesToScroll: 1,
		},
		{
			name:                  "very large scroll-off margin - keep view centered (odd viewport height)",
			viewPortStart:         10,
			viewPortHeight:        9,
			scrollOffMargin:       999,
			lineIdxBefore:         14,
			lineIdxAfter:          13,
			expectedLinesToScroll: 1,
		},
	}
	for _, scenario := range scenarios {
		t.Run(scenario.name, func(t *testing.T) {
			linesToScroll := calculateLinesToScrollUp(scenario.viewPortStart, scenario.viewPortHeight, scenario.scrollOffMargin, scenario.lineIdxBefore, scenario.lineIdxAfter)
			assert.Equal(t, scenario.expectedLinesToScroll, linesToScroll)
		})
	}
}

func Test_calculateLinesToScrollDown(t *testing.T) {
	scenarios := []struct {
		name                  string
		viewPortStart         int
		viewPortHeight        int
		scrollOffMargin       int
		lineIdxBefore         int
		lineIdxAfter          int
		expectedLinesToScroll int
	}{
		{
			name:                  "before position is above viewport - don't scroll",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       3,
			lineIdxBefore:         9,
			lineIdxAfter:          10,
			expectedLinesToScroll: 0,
		},
		{
			name:                  "before position is below viewport - don't scroll",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       3,
			lineIdxBefore:         20,
			lineIdxAfter:          21,
			expectedLinesToScroll: 0,
		},
		{
			name:                  "before and after positions are outside scroll-off margin - don't scroll",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       3,
			lineIdxBefore:         15,
			lineIdxAfter:          16,
			expectedLinesToScroll: 0,
		},
		{
			name:                  "before outside, after inside scroll-off margin - scroll by 1",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       3,
			lineIdxBefore:         16,
			lineIdxAfter:          17,
			expectedLinesToScroll: 1,
		},
		{
			name:                  "scroll-off margin is zero - scroll by 1 at end of view",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       0,
			lineIdxBefore:         19,
			lineIdxAfter:          20,
			expectedLinesToScroll: 1,
		},
		{
			name:                  "before inside scroll-off margin - scroll by more than 1",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       3,
			lineIdxBefore:         18,
			lineIdxAfter:          19,
			expectedLinesToScroll: 3,
		},
		{
			name:                  "very large scroll-off margin - keep view centered (even viewport height)",
			viewPortStart:         10,
			viewPortHeight:        10,
			scrollOffMargin:       999,
			lineIdxBefore:         15,
			lineIdxAfter:          16,
			expectedLinesToScroll: 1,
		},
		{
			name:                  "very large scroll-off margin - keep view centered (odd viewport height)",
			viewPortStart:         10,
			viewPortHeight:        9,
			scrollOffMargin:       999,
			lineIdxBefore:         14,
			lineIdxAfter:          15,
			expectedLinesToScroll: 1,
		},
	}
	for _, scenario := range scenarios {
		t.Run(scenario.name, func(t *testing.T) {
			linesToScroll := calculateLinesToScrollDown(scenario.viewPortStart, scenario.viewPortHeight, scenario.scrollOffMargin, scenario.lineIdxBefore, scenario.lineIdxAfter)
			assert.Equal(t, scenario.expectedLinesToScroll, linesToScroll)
		})
	}
}