aboutsummaryrefslogtreecommitdiff
path: root/test/functional/ui/mouse_spec.lua
blob: da9d6a0cd2e4de233b214652bd8d85522d11e27c (plain) (blame)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
local helpers = require('test.functional.helpers')
local Screen = require('test.functional.ui.screen')
local clear, feed, meths = helpers.clear, helpers.feed, helpers.meths
local insert, execute = helpers.insert, helpers.execute
local eq, funcs = helpers.eq, helpers.funcs

describe('Mouse input', function()
  local screen

  local hlgroup_colors = {
    NonText = Screen.colors.Blue,
    Visual = Screen.colors.LightGrey
  }

  before_each(function()
    clear()
    meths.set_option('mouse', 'a')
    meths.set_option('listchars', 'eol:$')
    -- set mouset to very high value to ensure that even in valgrind/travis,
    -- nvim will still pick multiple clicks
    meths.set_option('mouset', 5000)
    screen = Screen.new(25, 5)
    screen:attach()
    screen:set_default_attr_ids({
      [1] = {background = hlgroup_colors.Visual},
      [2] = {bold = true}
    })
    screen:set_default_attr_ignore( {{bold=true, foreground=hlgroup_colors.NonText}} )
    feed('itesting<cr>mouse<cr>support and selection<esc>')
    screen:expect([[
      testing                  |
      mouse                    |
      support and selectio^n    |
      ~                        |
                               |
    ]])
  end)

  after_each(function()
    screen:detach()
  end)

  it('left click moves cursor', function()
    feed('<LeftMouse><2,1>')
    screen:expect([[
      testing                  |
      mo^use                    |
      support and selection    |
      ~                        |
                               |
    ]])
    feed('<LeftMouse><0,0>')
    screen:expect([[
      ^testing                  |
      mouse                    |
      support and selection    |
      ~                        |
                               |
    ]])
  end)

  describe('tabline', function()
    local tab_attrs = {
      tab  = { background=Screen.colors.LightGrey, underline=true },
      sel  = { bold=true },
      fill = { reverse=true }
    }

    it('left click in default tabline (position 4) switches to tab', function()
      execute('%delete')
      insert('this is foo')
      execute('silent file foo | tabnew | file bar')
      insert('this is bar')
      screen:expect([[
        {tab: + foo }{sel: + bar }{fill:          }{tab:X}|
        this is ba^r              |
        ~                        |
        ~                        |
                                 |
      ]], tab_attrs)
      feed('<LeftMouse><4,0>')
      screen:expect([[
        {sel: + foo }{tab: + bar }{fill:          }{tab:X}|
        this is fo^o              |
        ~                        |
        ~                        |
                                 |
      ]], tab_attrs)
    end)

    it('left click in default tabline (position 24) closes tab', function()
      meths.set_option('hidden', true)
      execute('%delete')
      insert('this is foo')
      execute('silent file foo | tabnew | file bar')
      insert('this is bar')
      screen:expect([[
        {tab: + foo }{sel: + bar }{fill:          }{tab:X}|
        this is ba^r              |
        ~                        |
        ~                        |
                                 |
      ]], tab_attrs)
      feed('<LeftMouse><24,0>')
      screen:expect([[
        this is fo^o              |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], tab_attrs)
    end)

    it('double click in default tabline (position 4) opens new tab', function()
      meths.set_option('hidden', true)
      execute('%delete')
      insert('this is foo')
      execute('silent file foo | tabnew | file bar')
      insert('this is bar')
      screen:expect([[
        {tab: + foo }{sel: + bar }{fill:          }{tab:X}|
        this is ba^r              |
        ~                        |
        ~                        |
                                 |
      ]], tab_attrs)
      feed('<2-LeftMouse><4,0>')
      screen:expect([[
        {sel:  Name] }{tab: + foo  + bar }{fill:  }{tab:X}|
        ^                         |
        ~                        |
        ~                        |
                                 |
      ]], tab_attrs)
    end)

    describe('%@ label', function()
      before_each(function()
        execute([[
          function Test(...)
            let g:reply = a:000
            return copy(a:000)  " Check for memory leaks: return should be freed
          endfunction
        ]])
        execute([[
          function Test2(...)
            return call('Test', a:000 + [2])
          endfunction
        ]])
        meths.set_option('tabline', '%@Test@test%X-%5@Test2@test2')
        meths.set_option('showtabline', 2)
        screen:expect([[
          {fill:test-test2               }|
          mouse                    |
          support and selectio^n    |
          ~                        |
                                   |
        ]], tab_attrs)
        meths.set_var('reply', {})
      end)

      local check_reply = function(expected)
        eq(expected, meths.get_var('reply'))
        meths.set_var('reply', {})
      end

      local test_click = function(name, click_str, click_num, mouse_button,
                                  modifiers)
        it(name .. ' works', function()
          eq(1, funcs.has('tablineat'))
          feed(click_str .. '<3,0>')
          check_reply({0, click_num, mouse_button, modifiers})
          feed(click_str .. '<4,0>')
          check_reply({})
          feed(click_str .. '<6,0>')
          check_reply({5, click_num, mouse_button, modifiers, 2})
          feed(click_str .. '<13,0>')
          check_reply({5, click_num, mouse_button, modifiers, 2})
        end)
      end

      test_click('single left click', '<LeftMouse>', 1, 'l', '    ')
      test_click('shifted single left click', '<S-LeftMouse>', 1, 'l', 's   ')
      test_click('shifted single left click with alt modifier',
                 '<S-A-LeftMouse>', 1, 'l', 's a ')
      test_click('shifted single left click with alt and ctrl modifiers',
                 '<S-C-A-LeftMouse>', 1, 'l', 'sca ')
      -- <C-RightMouse> does not work
      test_click('shifted single right click with alt modifier',
                 '<S-A-RightMouse>', 1, 'r', 's a ')
      -- Modifiers do not work with MiddleMouse
      test_click('shifted single middle click with alt and ctrl modifiers',
                 '<MiddleMouse>', 1, 'm', '    ')
      -- Modifiers do not work with N-*Mouse
      test_click('double left click', '<2-LeftMouse>', 2, 'l', '    ')
      test_click('triple left click', '<3-LeftMouse>', 3, 'l', '    ')
      test_click('quadruple left click', '<4-LeftMouse>', 4, 'l', '    ')
      test_click('double right click', '<2-RightMouse>', 2, 'r', '    ')
      test_click('triple right click', '<3-RightMouse>', 3, 'r', '    ')
      test_click('quadruple right click', '<4-RightMouse>', 4, 'r', '    ')
      test_click('double middle click', '<2-MiddleMouse>', 2, 'm', '    ')
      test_click('triple middle click', '<3-MiddleMouse>', 3, 'm', '    ')
      test_click('quadruple middle click', '<4-MiddleMouse>', 4, 'm', '    ')
    end)
  end)

  it('left drag changes visual selection', function()
    -- drag events must be preceded by a click
    feed('<LeftMouse><2,1>')
    screen:expect([[
      testing                  |
      mo^use                    |
      support and selection    |
      ~                        |
                               |
    ]])
    feed('<LeftDrag><4,1>')
    screen:expect([[
      testing                  |
      mo{1:us}^e                    |
      support and selection    |
      ~                        |
      {2:-- VISUAL --}             |
    ]])
    feed('<LeftDrag><2,2>')
    screen:expect([[
      testing                  |
      mo{1:use }                   |
      {1:su}^pport and selection    |
      ~                        |
      {2:-- VISUAL --}             |
    ]])
    feed('<LeftDrag><0,0>')
    screen:expect([[
      ^t{1:esting }                 |
      {1:mou}se                    |
      support and selection    |
      ~                        |
      {2:-- VISUAL --}             |
    ]])
  end)

  it('left drag changes visual selection after tab click', function()
    local tab_attrs = {
      tab  = { background=Screen.colors.LightGrey, underline=true },
      sel  = { bold=true },
      fill = { reverse=true },
      vis  = { background=Screen.colors.LightGrey }
    }
    execute('silent file foo | tabnew | file bar')
    insert('this is bar')
    execute('tabprevious')  -- go to first tab
    screen:expect([[
      {sel: + foo }{tab: + bar }{fill:          }{tab:X}|
      mouse                    |
      support and selectio^n    |
      ~                        |
                               |
    ]], tab_attrs)
    feed('<LeftMouse><10,0><LeftRelease>')  -- go to second tab
    helpers.wait()
    feed('<LeftMouse><0,1>')
    screen:expect([[
      {tab: + foo }{sel: + bar }{fill:          }{tab:X}|
      ^this is bar              |
      ~                        |
      ~                        |
                               |
    ]], tab_attrs)
    feed('<LeftDrag><4,1>')
    screen:expect([[
      {tab: + foo }{sel: + bar }{fill:          }{tab:X}|
      {vis:this}^ is bar              |
      ~                        |
      ~                        |
      {sel:-- VISUAL --}             |
    ]], tab_attrs)
  end)

  it('two clicks will select the word and enter VISUAL', function()
    feed('<LeftMouse><2,2><LeftMouse><2,2>')
    screen:expect([[
      testing                  |
      mouse                    |
      {1:suppor}^t and selection    |
      ~                        |
      {2:-- VISUAL --}             |
    ]])
  end)

  it('three clicks will select the line and enter VISUAL LINE', function()
    feed('<LeftMouse><2,2><LeftMouse><2,2><LeftMouse><2,2>')
    screen:expect([[
      testing                  |
      mouse                    |
      {1:su}^p{1:port and selection }   |
      ~                        |
      {2:-- VISUAL LINE --}        |
    ]])
  end)

  it('four clicks will enter VISUAL BLOCK', function()
    feed('<LeftMouse><2,2><LeftMouse><2,2><LeftMouse><2,2><LeftMouse><2,2>')
    screen:expect([[
      testing                  |
      mouse                    |
      su^pport and selection    |
      ~                        |
      {2:-- VISUAL BLOCK --}       |
    ]])
  end)

  it('right click extends visual selection to the clicked location', function()
    feed('<LeftMouse><0,0>')
    screen:expect([[
      ^testing                  |
      mouse                    |
      support and selection    |
      ~                        |
                               |
    ]])
    feed('<RightMouse><2,2>')
    screen:expect([[
      {1:testing }                 |
      {1:mouse }                   |
      {1:su}^pport and selection    |
      ~                        |
      {2:-- VISUAL --}             |
    ]])
  end)

  it('ctrl + left click will search for a tag', function()
    meths.set_option('tags', './non-existent-tags-file')
    feed('<C-LeftMouse><0,0>')
    screen:expect([[
      E433: No tags file       |
      E426: tag not found: test|
      ing                      |
      Press ENTER or type comma|
      nd to continue^           |
    ]],nil,true)
    feed('<cr>')
  end)

  it('mouse whell will target the hovered window', function()
    feed('ggdG')
    insert([[
    Inserting
    text
    with
    many
    lines
    to
    test
    mouse scrolling
    ]])
    screen:try_resize(53, 14)
    execute('sp', 'vsp')
    screen:set_default_attr_ignore( {{bold=true, foreground=hlgroup_colors.NonText},
            {reverse=true}, {bold=true, reverse=true}} )
    screen:expect([[
      lines                     |lines                     |
      to                        |to                        |
      test                      |test                      |
      mouse scrolling           |mouse scrolling           |
      ^                          |                          |
      ~                         |~                         |
      [No Name] [+]              [No Name] [+]             |
      to                                                   |
      test                                                 |
      mouse scrolling                                      |
                                                           |
      ~                                                    |
      [No Name] [+]                                        |
      :vsp                                                 |
    ]])
    feed('<MouseUp><0,0>')
    screen:expect([[
      mouse scrolling           |lines                     |
      ^                          |to                        |
      ~                         |test                      |
      ~                         |mouse scrolling           |
      ~                         |                          |
      ~                         |~                         |
      [No Name] [+]              [No Name] [+]             |
      to                                                   |
      test                                                 |
      mouse scrolling                                      |
                                                           |
      ~                                                    |
      [No Name] [+]                                        |
                                                           |
    ]])
    feed('<MouseDown><27,0>')
    screen:expect([[
      mouse scrolling           |text                      |
      ^                          |with                      |
      ~                         |many                      |
      ~                         |lines                     |
      ~                         |to                        |
      ~                         |test                      |
      [No Name] [+]              [No Name] [+]             |
      to                                                   |
      test                                                 |
      mouse scrolling                                      |
                                                           |
      ~                                                    |
      [No Name] [+]                                        |
                                                           |
    ]])
    feed('<MouseDown><27,7><MouseDown>')
    screen:expect([[
      mouse scrolling           |text                      |
      ^                          |with                      |
      ~                         |many                      |
      ~                         |lines                     |
      ~                         |to                        |
      ~                         |test                      |
      [No Name] [+]              [No Name] [+]             |
      Inserting                                            |
      text                                                 |
      with                                                 |
      many                                                 |
      lines                                                |
      [No Name] [+]                                        |
                                                           |
    ]])
  end)
end)