summaryrefslogtreecommitdiff
path: root/mnv/src/testdir/util/window_manager.mnv
blob: 7a8771f8bf34e1e66019c51bbe85fde979b44b45 (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
CheckFeature job
CheckUnix

let g:xdisplay_num = 100

" Each key is the display name and its value is the compositor/wm job
let s:wayland_displays = {}
let s:x11_displays = {}

command -nargs=0 CheckWaylandCompositor call CheckWaylandCompositor()
command -nargs=0 CheckXServer call CheckXServer()

func CheckWaylandCompositor()
  CheckFeature wayland

  if executable("labwc") != 1
    throw "Skipped: labwc is not available"
  endif
endfunc

func CheckXServer()
  CheckFeature x11

  if executable("Xvfb") != 1
    throw "Skipped: Xvfb is not available"
  endif
  if executable("xdpyinfo") != 1
    throw "Skipped: xdpyinfo is not available"
  endif
endfunc

func s:StartCompositorOutput(channel, msg)
  let l:display = matchstr(a:msg, 'WAYLAND_DISPLAY=\zs.\+')

  if !empty(l:display)
    let s:wayland_display_name = l:display
  endif
endfunc

func s:StartCompositorExit(job, status)
    if s:wayland_display_name == ""
      throw "Skipped: Error: Wayland compositor exited when starting up"
    endif
endfunc

func StartWaylandCompositor()
  let s:wayland_display_name = ""

  let l:wayland_compositor_job = job_start(
        \ ['labwc', '-c', 'NONE', '-d'], {
        \ 'err_io': 'pipe',
        \ 'err_cb': function('s:StartCompositorOutput'),
        \ 'err_mode': 'nl',
        \ 'exit_cb': function('s:StartCompositorExit'),
        \ 'env': { 'WLR_BACKENDS': 'headless' }
        \ })

  call WaitForAssert({-> assert_equal("run",
        \ job_status(l:wayland_compositor_job))})
  call WaitForAssert({-> assert_match('.\+', s:wayland_display_name)})

  let s:wayland_displays[s:wayland_display_name] = l:wayland_compositor_job

  return s:wayland_display_name
endfunc

func EndWaylandCompositor(display)
  let l:job = s:wayland_displays[a:display]

  call job_stop(l:job, 'term')

  " Block until compositor is actually gone
  call WaitForAssert({-> assert_equal("dead", job_status(l:job))})

  unlet s:wayland_displays[a:display]
endfunc

" Start a separate X11 server instance
func StartXServer()
  let l:xdisplay = ':' .. g:xdisplay_num

  let l:x11_server_job = job_start(['Xvfb', l:xdisplay], {})

  call WaitForAssert({-> assert_equal("run", job_status(l:x11_server_job))})
  " Check if server is ready. Not sure if this is the best way though...
  call WaitFor({-> system("DISPLAY=" .. l:xdisplay .. " xdpyinfo 2> /dev/null")
        \ =~? '.\+'})

  g:xdisplay_num += 1

  let s:x11_displays[l:xdisplay] = l:x11_server_job

  return l:xdisplay
endfunc

func EndXServer(display)
  let l:job = s:x11_displays[a:display]

  call job_stop(l:job)

  " Block until X server is actually gone
  call WaitForAssert({-> assert_equal("dead", job_status(l:job))})

  unlet s:x11_displays[a:display]
endfunc

" mnv: shiftwidth=2 sts=2 expandtab