summaryrefslogtreecommitdiffstats
path: root/cucumber/features/support/helpers/display_helper.rb
blob: b4dce73395daa0d4027cb3bda9b5e4edd8ded48a (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
class Display

  def initialize(domain, x_display)
    @domain = domain
    @x_display = x_display
  end

  def active?
    p = IO.popen(["xprop", "-display", @x_display,
                  "-name", "#{@domain} (1) - Virt Viewer",
                  :err => ["/dev/null", "w"]])
    Process.wait(p.pid)
    $?.success?
  end

  def start
    @virtviewer = IO.popen(["virt-viewer", "--direct",
                                           "--kiosk",
                                           "--reconnect",
                                           "--connect", "qemu:///system",
                                           "--display", @x_display,
                                           @domain,
                                           :err => ["/dev/null", "w"]])
    # We wait for the display to be active to not lose actions
    # (e.g. key presses via sikuli) that come immediately after
    # starting (or restoring) a vm
    try_for(20, { :delay => 0.1, :msg => "virt-viewer failed to start"}) {
      active?
    }
  end

  def stop
    return if @virtviewer.nil?
    Process.kill("TERM", @virtviewer.pid)
    @virtviewer.close
  rescue IOError
    # IO.pid throws this if the process wasn't started yet. Possibly
    # there's a race when doing a start() and then quickly running
    # stop().
  end

  def restart
    stop
    start
  end

end