summaryrefslogtreecommitdiff
path: root/t/11scroll.t
blob: 0d97fb2e42bad573aa6cec4528138a277f2e4b0d (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

use Tickit::Test 0.12;

use Tickit::Widget::Scroller;
use Tickit::Widget::Scroller::Item::Text;

# Tests are simpler if the terminal is much smaller
# TODO: mk_window once Tickit::Test can take a size there too
my ( $term, $win ) = mk_term_and_window lines => 5, cols => 15;

my $scrolled_delta = 0;
my $scroller = Tickit::Widget::Scroller->new(
   on_scrolled => sub { $scrolled_delta += $_[1] },
);

$scroller->push(
   map { Tickit::Widget::Scroller::Item::Text->new( "Item of text $_ which is long" ) } 1 .. 9
);

$scroller->set_window( $win );

flush_tickit;

is_termlog( [ GOTO(0,0),
              SETPEN, 
              PRINT("Item of text 1 "),
              GOTO(1,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(2,0),
              SETPEN, 
              PRINT("Item of text 2 "),
              GOTO(3,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(4,0),
              SETPEN,
              PRINT("Item of text 3 ") ],
            'Termlog initially' );

is_display( [ [TEXT("Item of text 1 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 2 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 3 ")] ],
            'Display initially' );

is( $scroller->item2line( 0,  0 ), 0, 'item2line 0, 0 initially' );
is( $scroller->item2line( 0, -1 ), 1, 'item2line 0, -1 initially' );
is( $scroller->item2line( 1,  0 ), 2, 'item2line 1, 0 initially' );
is( $scroller->item2line( 1, -1 ), 3, 'item2line 1, -1 initially' );
is( $scroller->item2line( 2,  0 ), 4, 'item2line 2, 0 initially' );
is( $scroller->item2line( 2, -1 ), undef, 'item2line 2, -1 initially offscreen' );

is_deeply( [ $scroller->item2line( 2, -1 )    ], [ undef, "below" ], 'list item2line 2, -1 initially below screen' );
is_deeply( [ $scroller->item2line( 2, -1, 1 ) ], [ 5,     "below" ], 'list item2line 2, -1 initially below screen with count_offscreen' );

is( $scroller->lines_above, 0, 'lines_above initially' );
is( $scroller->lines_below, 13, 'lines_below initially' );

$scroller->scroll( +10 );

is( $scrolled_delta, 10, '$scrolled_delta after ->scroll' );

flush_tickit;

is_termlog( [ GOTO(0,0),
              SETPEN, 
              PRINT("Item of text 6 "),
              GOTO(1,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(2,0),
              SETPEN, 
              PRINT("Item of text 7 "),
              GOTO(3,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(4,0),
              SETPEN,
              PRINT("Item of text 8 ") ],
            'Termlog after scroll +10' );

is_display( [ [TEXT("Item of text 6 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 7 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 8 ")] ],
            'Display after scroll +10' );

is( $scroller->item2line( 0,  0 ), undef, 'item2line 0, 0 offscreen after scroll +10' );
is( $scroller->item2line( 0, -1 ), undef, 'item2line 0, -1 offscreen after scroll +10' );
is( $scroller->item2line( 5,  0 ), 0, 'item2line 5, 0 after scroll +10' );
is( $scroller->item2line( 5, -1 ), 1, 'item2line 5, -1 after scroll +10' );
is( $scroller->item2line( 8,  0 ), undef, 'item2line 8, 0 offscreen after scroll +10' );

is_deeply( [ $scroller->item2line( 0, 0 )    ], [ undef, "above" ], 'list item2line 0, 0 above screen after scroll +10' );
is_deeply( [ $scroller->item2line( 0, 0, 1 ) ], [ -10,   "above" ], 'list item2line 0, 0 above screen after scroll +10 with count_offscreen' );
is_deeply( [ $scroller->item2line( 8, 0 )    ], [ undef, "below" ], 'list item2line 8, 0 below screen after scroll +10' );
is_deeply( [ $scroller->item2line( 8, 0, 1 ) ], [ 6,     "below" ], 'list item2line 8, 0 below screen after scroll +10 with count_offscreen' );

is( $scroller->lines_above, 10, 'lines_above after scroll +10' );
is( $scroller->lines_below,  3, 'lines_below after scroll +10' );

$scroller->scroll( -1 );

is( $scrolled_delta, 9, '$scrolled_delta after ->scroll -1' );

flush_tickit;

is_termlog( [ SETBG(undef),
              SCROLLRECT(0,0,5,15, -1,0),
              GOTO(0,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2) ],
            'Termlog after scroll -1' );

is_display( [ [TEXT("which is long")],
              [TEXT("Item of text 6 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 7 ")],
              [TEXT("which is long")] ],
            'Display after scroll -1' );

is( $scroller->lines_above,  9, 'lines_above after scroll -1' );
is( $scroller->lines_below,  4, 'lines_below after scroll -1' );

$scroller->scroll( +1 );

is( $scrolled_delta, 10, '$scrolled_delta after ->scroll +1' );

flush_tickit;

is_termlog( [ SETBG(undef),
              SCROLLRECT(0,0,5,15, +1,0),
              GOTO(4,0),
              SETPEN,
              PRINT("Item of text 8 ") ],
            'Termlog after scroll +1' );

is_display( [ [TEXT("Item of text 6 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 7 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 8 ")] ],
            'Display after scroll +1' );

$scroller->scroll( -10 );

is( $scrolled_delta, 0, '$scrolled_delta after ->scroll -10' );

flush_tickit;

is_termlog( [ GOTO(0,0),
              SETPEN, 
              PRINT("Item of text 1 "),
              GOTO(1,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(2,0),
              SETPEN, 
              PRINT("Item of text 2 "),
              GOTO(3,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(4,0),
              SETPEN,
              PRINT("Item of text 3 ") ],
            'Termlog after scroll -10' );

is_display( [ [TEXT("Item of text 1 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 2 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 3 ")] ],
            'Display after scroll -10' );

$scroller->scroll_to_bottom;

is( $scrolled_delta, 13, '$scrolled_delta after ->scroll_to_bottom' );

flush_tickit;

is_termlog( [ GOTO(0,0),
              SETPEN, 
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(1,0),
              SETPEN, 
              PRINT("Item of text 8 "),
              GOTO(2,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(3,0),
              SETPEN,
              PRINT("Item of text 9 "),
              GOTO(4,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2) ],
            'Termlog after scroll_to_bottom' );

is_display( [ [TEXT("which is long")],
              [TEXT("Item of text 8 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 9 ")],
              [TEXT("which is long")] ],
            'Display after scroll_to_bottom' );

$scroller->scroll_to_top;

is( $scrolled_delta, 0, '$scrolled_delta after ->scroll_to_top' );

flush_tickit;

is_termlog( [ GOTO(0,0),
              SETPEN, 
              PRINT("Item of text 1 "),
              GOTO(1,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(2,0),
              SETPEN, 
              PRINT("Item of text 2 "),
              GOTO(3,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(4,0),
              SETPEN,
              PRINT("Item of text 3 ") ],
            'Termlog after scroll_to_top' );

is_display( [ [TEXT("Item of text 1 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 2 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 3 ")] ],
            'Display after scroll_to_top' );

$scroller->scroll_to( 2, 4, 0 ); # About halfway

is( $scrolled_delta, 6, '$scrolled_delta after ->scroll_to halfway' );

flush_tickit;

is_termlog( [ GOTO(0,0),
              SETPEN, 
              PRINT("Item of text 4 "),
              GOTO(1,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(2,0),
              SETPEN, 
              PRINT("Item of text 5 "),
              GOTO(3,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2),
              GOTO(4,0),
              SETPEN,
              PRINT("Item of text 6 ") ],
            'Termlog after scroll_to middle' );

is_display( [ [TEXT("Item of text 4 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 5 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 6 ")] ],
            'Display after scroll_to middle' );

$scroller->scroll( +5 );
flush_tickit;
drain_termlog;

{
   my $pre_scroll_delta = $scrolled_delta;

   $scroller->scroll( +5 ); # over the end

   is( $scrolled_delta - $pre_scroll_delta, 2, 'on_scroll given actual delta, not requested' );
   is( $scrolled_delta, 13, '$scrolled_delta after ->scroll +5 over the end' );
}

flush_tickit;

is_termlog( [ SETBG(undef),
              SCROLLRECT(0,0,5,15, +2,0),
              GOTO(3,0),
              SETPEN, 
              PRINT("Item of text 9 "),
              GOTO(4,0),
              SETPEN,
              PRINT("which is long"),
              SETBG(undef),
              ERASECH(2) ],
            'Termlog down past the end' );

is_display( [ [TEXT("which is long")],
              [TEXT("Item of text 8 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 9 ")],
              [TEXT("which is long")] ],
            'Display after scroll down past the end' );

$scroller->scroll( -2 );
$scroller->scroll( -2 );
flush_tickit;

is_termlog( [ SETBG(undef),
              SCROLLRECT(0,0,5,15, -2,0),
              SETBG(undef),
              SCROLLRECT(0,0,5,15, -2,0),
              GOTO(0,0), SETPEN, PRINT("which is long"), SETBG(undef), ERASECH(2),
              GOTO(1,0), SETPEN, PRINT("Item of text 6 "),
              GOTO(2,0), SETPEN, PRINT("which is long"), SETBG(undef), ERASECH(2),
              GOTO(3,0), SETPEN, PRINT("Item of text 7 "), ],
            'Termlog after ->scroll(-2) x 2' );

is_display( [ [TEXT("which is long")],
              [TEXT("Item of text 6 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 7 ")],
              [TEXT("which is long")] ],
            'Display after ->scroll(-2) x 2' );

$scroller->scroll( +2 );
$scroller->scroll( +2 );
flush_tickit;

is_termlog( [ SETBG(undef),
              SCROLLRECT(0,0,5,15, +2,0),
              SETBG(undef),
              SCROLLRECT(0,0,5,15, +2,0),
              GOTO(1,0), SETPEN, PRINT("Item of text 8 "),
              GOTO(2,0), SETPEN, PRINT("which is long"), SETBG(undef), ERASECH(2),
              GOTO(3,0), SETPEN, PRINT("Item of text 9 "),
              GOTO(4,0), SETPEN, PRINT("which is long"), SETBG(undef), ERASECH(2), ],
            'Termlog after ->scroll(+2) x 2' );

is_display( [ [TEXT("which is long")],
              [TEXT("Item of text 8 ")],
              [TEXT("which is long")],
              [TEXT("Item of text 9 ")],
              [TEXT("which is long")] ],
            'Display after ->scroll(+2) x 2' );

is( $scrolled_delta, 13, '$scrolled_delta before EOF' );

done_testing;