tests/ide_tests/sikuliberemiz.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Fri, 18 Feb 2022 15:12:06 +0100
branchwxPython4
changeset 3432 40b8b2ffb3fd
parent 3431 5bcef31e276d
child 3437 ce366d67a5b7
permissions -rw-r--r--
Tests: Various enhancements in Makefile and sikuli common python code.
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
"Commons definitions for sikuli based beremiz IDE GUI tests"
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
import os
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
import sys
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
import subprocess
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
from threading import Thread, Event
3432
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
     7
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
     8
typeof=type
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
     9
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
from sikuli import *
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
beremiz_path = os.environ["BEREMIZPATH"]
3431
5bcef31e276d Tests: Added BEREMIZPYTHONPATH to point to Python interpreter to use for tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3430
diff changeset
    13
python_bin = os.environ.get("BEREMIZPYTHONPATH", "/usr/bin/python")
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
opj = os.path.join
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
def StartBeremizApp(projectpath=None, exemple=None):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
    """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
    Starts Beremiz IDE, waits for main window to appear, maximize it.
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
        Parameters: 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
            projectpath (str): path to project to open
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
            exemple (str): path relative to exemples directory
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
        Returns:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
            Sikuli App class instance
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
    """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
3431
5bcef31e276d Tests: Added BEREMIZPYTHONPATH to point to Python interpreter to use for tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3430
diff changeset
    29
    command = [python_bin, opj(beremiz_path,"Beremiz.py"), "--log=/dev/stdout"]
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
    if exemple is not None:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    32
        command.append(opj(beremiz_path,"exemples",exemple))
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    33
    elif projectpath is not None:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    34
        command.append(projectpath)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
    # App class is broken in Sikuli 2.0.5: can't start process with arguments.
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
    # 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
    # Workaround : - use subprocess module to spawn IDE process,
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
    #              - use wmctrl to find IDE window details and maximize it
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
    #              - pass exact window title to App class constructor
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    42
    proc = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=0)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    44
    # Window are macthed against process' PID
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    45
    ppid = proc.pid
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    46
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    47
    # Timeout 5s
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
    c = 50
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    49
    while c > 0:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    50
        # equiv to "wmctrl -l -p | grep $pid"
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    51
        try:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    52
            wlist = filter(lambda l:(len(l)>2 and l[2]==str(ppid)), map(lambda s:s.split(None,4), subprocess.check_output(["wmctrl", "-l", "-p"]).splitlines()))
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    53
        except subprocess.CalledProcessError:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    54
            wlist = []
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    56
        # window with no title only has 4 fields do describe it
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    57
        # beremiz splashcreen has no title
3430
c2171d87b587 Documentation/comment fix.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3424
diff changeset
    58
        # wait until main window is visible
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    59
        if len(wlist) == 1 and len(wlist[0]) == 5:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    60
            windowID,_zero,wpid,_XID,wtitle = wlist[0] 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    61
            break
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    62
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    63
        wait(0.1)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    64
        c = c - 1
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    65
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    66
    if c == 0:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    67
        raise Exception("Couldn't find Beremiz window")
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
    # Maximize window x and y
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    70
    subprocess.check_call(["wmctrl", "-i", "-r", windowID, "-b", "add,maximized_vert,maximized_horz"])
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    71
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    72
    # switchApp creates an App object by finding window by title, is not supposed to spawn a process
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    73
    return proc, switchApp(wtitle)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    74
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    75
class KBDShortcut:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    76
    """Send shortut to app by calling corresponding methods:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    77
          Stop   
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    78
          Run    
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    79
          Transfer
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    80
          Connect
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    81
          Clean  
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    82
          Build  
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    83
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    84
    example:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    85
        k = KBDShortcut(app)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    86
        k.Clean()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    87
    """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    88
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    89
    fkeys = {"Stop":     Key.F4,
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    90
             "Run":      Key.F5,
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    91
             "Transfer": Key.F6,
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    92
             "Connect":  Key.F7,
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    93
             "Clean":    Key.F9,
3432
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
    94
             "Build":    Key.F11,
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
    95
             "Save":     ("s",Key.CTRL),
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
    96
             "New":      ("n",Key.CTRL),
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
    97
             "Address":  ("l",Key.CTRL)}  # to reach address bar in GTK's file selector
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    98
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    99
    def __init__(self, app):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   100
        self.app = app
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   101
    
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   102
    def __getattr__(self, name):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   103
        fkey = self.fkeys[name]
3432
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
   104
        if typeof(fkey) != tuple:
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
   105
            fkey = (fkey,)
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   106
        app = self.app
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   107
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   108
        def PressShortCut():
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   109
            app.focus()
3432
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
   110
            type(*fkey)
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   111
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   112
        return PressShortCut
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   113
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   114
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   115
class IDEIdleObserver:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   116
    "Detects when IDE is idle. This is particularly handy when staring an operation and witing for the en of it."
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   117
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   118
    def __init__(self, app):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   119
        """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   120
        Parameters: 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   121
            app (class App): Sikuli app given by StartBeremizApp
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   122
        """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   123
        self.r = Region(app.window())
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   124
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   125
        self.idechanged = False
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   126
        
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   127
        # 200 was selected because default 50 was still catching cursor blinking in console
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   128
        # FIXME : remove blinking cursor in console
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   129
        self.r.onChange(200,self._OnIDEWindowChange)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   130
        self.r.observeInBackground()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   131
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   132
    def __del__(self):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   133
        self.r.stopObserver()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   134
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   135
    def _OnIDEWindowChange(self, event):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   136
        self.idechanged = True
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   137
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   138
    def Wait(self, period, timeout):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   139
        """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   140
        Wait for IDE to stop changing
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   141
        Parameters: 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   142
            period (int): how many seconds with no change to consider idle
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   143
            timeout (int): how long to wait for idle, in seconds
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   144
        """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   145
        c = timeout/period
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   146
        while c > 0:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   147
            self.idechanged = False
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   148
            wait(period)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   149
            if not self.idechanged:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   150
                break
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   151
            c = c - 1
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   152
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   153
        if c == 0:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   154
            raise Exception("Window did not idle before timeout")
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   155
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   156
 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   157
class stdoutIdleObserver:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   158
    "Detects when IDE's stdout is idle. Can be more reliable than pixel based version (false changes ?)"
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   159
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   160
    def __init__(self, proc):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   161
        """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   162
        Parameters: 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   163
            proc (subprocess.Popen): Beremiz process, given by StartBeremizApp
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   164
        """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   165
        self.proc = proc
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   166
        self.stdoutchanged = False
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   167
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   168
        self.thread = Thread(target = self._waitStdoutProc).start()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   169
3432
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
   170
    def _waitStdoutProc(self):
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   171
        while True:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   172
            a = self.proc.stdout.read(1)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   173
            if len(a) == 0 or a is None: 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   174
                break
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   175
            sys.stdout.write(a)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   176
            self.idechanged = True
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   177
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   178
    def Wait(self, period, timeout):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   179
        """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   180
        Wait for IDE'stdout to stop changing
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   181
        Parameters: 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   182
            period (int): how many seconds with no change to consider idle
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   183
            timeout (int): how long to wait for idle, in seconds
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   184
        """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   185
        c = timeout/period
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   186
        while c > 0:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   187
            self.idechanged = False
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   188
            wait(period)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   189
            if not self.idechanged:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   190
                break
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   191
            c = c - 1
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   192
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   193
        if c == 0:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   194
            raise Exception("Stdout did not idle before timeout")
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   195
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   196
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   197
def waitPatternInStdout(proc, pattern, timeout, count=1):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   198
    
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   199
    success_event = Event()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   200
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   201
    def waitPatternInStdoutProc():
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   202
        found = 0
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   203
        while True:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   204
            a = proc.stdout.readline()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   205
            if len(a) == 0 or a is None: 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   206
                raise Exception("App finished before producing expected stdout pattern")
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   207
            sys.stdout.write(a)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   208
            if a.find(pattern) >= 0:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   209
                found = found + 1
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   210
                if found >= count:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   211
                    success_event.set()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   212
                    break
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   213
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   214
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   215
    Thread(target = waitPatternInStdoutProc).start()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   216
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   217
    if not success_event.wait(timeout):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   218
        # test timed out
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   219
        return False
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   220
    else:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   221
        return True
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   222
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   223
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   224