2 # -*- coding: utf-8 -*-
3 # This script generates hundreds of random keypresses per second,
4 # and sends them to the lyx window
5 # It requires xvkbd and wmctrl
6 # It generates a log of the KEYCODES it sends as development/keystest/out/KEYCODES
8 # Adapted by Tommaso Cucinotta from the original MonKey Test by
11 from __future__ import print_function
20 #from subprocess import call
23 print('Beginning keytest.py')
25 FNULL = open('/dev/null', 'w')
29 # Ignore status == "dead" if this is set. Used at the last commands after "\Cq"
32 def die(excode, text):
55 for k in range(97, 123):
58 for k in range(97, 123):
59 keycode[:0] = ["\A" + chr(k)]
61 for k in range(97, 123):
62 keycode[:0] = ["\A" + chr(k)]
64 for k in range(97, 123):
65 keycode[:0] = ["\C" + chr(k)]
67 self.keycode = keycode
72 self.count = self.count + 1
73 if self.count % 200 == 0:
75 elif self.count > self.count_max:
80 keystr = keystr + self.keycode[random.randint(1,
81 len(self.keycode)) - 1]
82 return 'KK: ' + keystr
85 class CommandSourceFromFile(CommandSource):
87 def __init__(self, filename, p):
89 self.infile = open(filename, 'r')
90 self.lines = self.infile.readlines()
94 print(p, self.p, 'self.p')
99 # Now we start randomly dropping lines, which we hope are redundant
100 # p is the probability that any given line will be removed
103 if random.uniform(0, 1) < 0.5:
104 print('randomdrop_independant\n')
105 self.randomdrop_independant()
107 print('randomdrop_slice\n')
108 self.randomdrop_slice()
109 if screenshot_out is None:
113 self.max_count = max(len(self.lines) + 20, count_atleast)
114 if len(self.lines) < 1:
115 self.lines = linesbak
117 def randomdrop_independant(self):
120 # The next couple of lines are to ensure that at least one line is dropped
122 drop = random.randint(0, len(self.lines) - 1)
124 #p = p - 1 / len(self.lines)
125 origlines = self.lines
128 if random.uniform(0, 1) < self.p:
129 print('Randomly dropping line ' + l + '\n')
136 def randomdrop_slice(self):
138 if random.uniform(0, 1) < 0.4:
139 lines.append(lines[0])
141 num_lines = len(lines)
142 max_drop = max(5, num_lines / 5)
143 num_drop = random.randint(1, 5)
144 drop_mid = random.randint(0, num_lines)
145 drop_start = max(drop_mid - num_drop / 2, 0)
146 drop_end = min(drop_start + num_drop, num_lines)
147 print(drop_start, drop_mid, drop_end)
149 del lines[drop_start:drop_end]
153 def getCommand(self):
154 if self.count >= self.max_count:
156 if self.i >= len(self.lines):
157 self.loops = self.loops + 1
158 if self.loops >= int(max_loops):
162 line = self.lines[self.i].rstrip('\n')
163 self.count = self.count + 1
170 self.control = re.compile(r'^(C[ONPpRrC]):\s*(.*)$')
171 self.fileformat = re.compile(r'^((\>\>?)[,\s]\s*)?([^\s]+)\s*$')
175 def open(self, filename):
176 if not self.cntrfile is None:
177 self.cntrfile.close()
180 m = self.fileformat.match(filename)
183 filename = m.group(3)
190 self.cntrname = filename
192 self.cntrfile = open(filename, 'a')
194 self.cntrfile = open(filename, 'w')
197 if not self.cntrfile is None:
198 self.cntrfile.close()
201 # make the method below 'private'
202 def __addline(self, pat):
203 self.cntrfile.writelines(pat + "\n")
208 def dispatch(self, c):
209 m = self.control.match(c)
216 elif command == "CC":
219 if not self.cntrfile is None:
221 self.__addline("Comment: " + text)
222 elif command == "CP":
223 self.__addline("Simple: " + text)
224 elif command == "Cp":
225 self.__addline("ErrSimple: " + text)
226 elif command == "CR":
227 self.__addline("Regex: " + text)
228 elif command == "Cr":
229 self.__addline("ErrRegex: " + text)
231 die(1,"Error, Unrecognised Command '" + command + "'")
235 def get_proc_pid(proc_name):
236 pid=os.popen("pidof " + proc_name).read().rstrip()
239 wlistreg = re.compile(r'^(0x[0-9a-f]{5,9})\s+[^\s]+\s+([0-9]+)\s.*$')
240 def get_proc_win_id(pid, ignoreid):
241 nlist = os.popen("wmctrl -l -p").read()
242 wlist = nlist.split("\n")
244 m = wlistreg.match(item)
249 if win_id != ignoreid:
256 fname = '/proc/' + lyx_pid + '/status'
257 return os.path.exists(fname)
260 # Interruptible os.system()
261 def intr_system(cmd, ignore_err = False):
262 print("Executing " + cmd)
263 # Assure the output of cmd does not overhaul
266 if os.WIFSIGNALED(ret):
267 raise KeyboardInterrupt
268 if ret != 0 and not ignore_err:
269 raise BaseException("command failed:" + cmd)
272 statreg = re.compile(r'^State:.*\(([a-z]+)\)')
275 def printresstatus():
276 for line in resstatus:
278 print(" " + line.rstrip())
279 print('End of /proc-lines')
281 def lyx_status_retry(pid):
286 fname = '/proc/' + pid + '/status'
292 resstatus.extend([line])
293 m = statreg.match(line)
302 print("I/O error({0}): {1}".format(e.errno, e.strerror))
305 print("Unexpected error:", sys.exc_info()[0])
307 print('This should not happen')
313 status = lyx_status_retry(pid)
314 if status != "retry":
317 print('Retrying check for status')
321 print('Retried to read status ' + str(count) + ' times')
322 #print('lys_status() returning ' + status)
325 # Return true if LyX (identified via lyx_pid) is sleeping
326 def lyx_sleeping(LYX_PID):
327 return lyx_status(LYX_PID) == "sleeping"
329 # Return true if LyX (identified via lyx_pid) is zombie
330 def lyx_zombie(LYX_PID):
331 return lyx_status(LYX_PID) == "zombie"
333 def lyx_dead(LYX_PID):
334 status = lyx_status(LYX_PID)
335 return (status == "dead") or (status == "zombie")
337 def wait_until_lyx_sleeping(LYX_PID):
338 before_secs = time.time()
340 status = lyx_status(LYX_PID)
341 if status == "sleeping":
343 if (status == "dead") or (status == "zombie"):
346 print('Lyx died while waiting for status == sleeping')
349 die(1,"Lyx is dead, exiting")
350 if time.time() - before_secs > 180:
351 # Do profiling, but sysprof has no command line interface?
352 # intr_system("killall -KILL lyx")
354 die(1,"Killing due to freeze (KILL_FREEZE)")
356 # Should be never reached
357 print('Wait for sleeping ends unexpectedly')
360 def sendKeystringLocal(keystr, LYX_PID):
361 is_sleeping = wait_until_lyx_sleeping(LYX_PID)
363 print("Not sending \"" + keystr + "\"")
365 if not screenshot_out is None:
366 print('Making Screenshot: ' + screenshot_out + ' OF ' + infilename)
368 intr_system('import -window root '+screenshot_out+str(x.count)+".png")
370 actual_delay = key_delay
371 if actual_delay == '':
372 actual_delay = def_delay
374 if qt_frontend == 'QT5':
375 xvpar.extend(["-jump-pointer", "-no-back-pointer"])
377 xvpar.extend(["-xsendevent"])
378 if lyx_other_window_name is None:
379 xvpar.extend(["-window", lyx_window_name])
381 xvpar.extend(["-window", lyx_other_window_name])
382 xvpar.extend(["-delay", actual_delay, "-text", keystr])
383 print("Sending \"" + keystr + "\"")
384 subprocess.call(xvpar, stdout = FNULL, stderr = FNULL)
387 Axreg = re.compile(r'^(.*)\\Ax([^\\]*)(.*)$')
388 returnreg = re.compile(r'(\\\[[A-Z][a-z0-9]+\])(.*)$')
390 # recursive wrapper around sendKeystringLocal()
391 # handling \Ax-entries
392 def sendKeystringAx(line, LYX_PID):
394 saved_delay = key_delay
395 m = Axreg.match(line)
401 # since (.*) is greedy, check prefix for '\Ax' again
402 sendKeystringAx(prefix, LYX_PID)
403 sendKeystringLocal('\Ax', LYX_PID)
405 m2 = returnreg.match(rest)
410 sendKeystringLocal(content + ctrlk, LYX_PID)
411 key_delay = saved_delay
412 time.sleep(controlkey_delay)
414 sendKeystringLocal(line, LYX_PID)
417 sendKeystringLocal(content, LYX_PID)
419 sendKeystringLocal(rest, LYX_PID)
422 sendKeystringLocal(line, LYX_PID)
424 specialkeyreg = re.compile(r'(.+)(\\[AC]([a-zA-Z]|\\\[[A-Z][a-z0-9]+\]).*)$')
425 # Split line at start of each meta or controll char
427 def sendKeystringAC(line, LYX_PID):
428 m = specialkeyreg.match(line)
432 sendKeystringAC(first, LYX_PID)
433 sendKeystringAC(second, LYX_PID)
435 sendKeystringAx(line, LYX_PID)
437 controlkeyreg = re.compile(r'^(.*\\\[[A-Z][a-z0-9]+\])(.*\\\[[A-Z][a-z0-9]+\])(.*)$')
438 # Make sure, only one of \[Return], \[Tab], \[Down], \[Home] etc are in one sent line
439 # e.g. split the input line on each keysym
440 def sendKeystringRT(line, LYX_PID):
441 m = controlkeyreg.match(line)
446 sendKeystringRT(first, LYX_PID)
447 time.sleep(controlkey_delay)
448 sendKeystringRT(second, LYX_PID)
449 time.sleep(controlkey_delay)
451 sendKeystringRT(third, LYX_PID)
453 sendKeystringAC(line, LYX_PID)
455 def system_retry(num_retry, cmd):
457 rtn = intr_system(cmd, True)
458 while ( ( i < num_retry ) and ( rtn != 0) ):
460 rtn = intr_system(cmd, True)
463 print("Command Failed: "+cmd)
467 #intr_system("echo x-session-manager PID: $X_PID.")
468 #intr_system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
469 ####intr_system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
470 print("lyx_window_name = " + lyx_window_name + "\n")
471 intr_system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
472 system_retry(30, "wmctrl -i -a '"+lyx_window_name+"'")
477 self.shortcut_entry = re.compile(r'^\s*"([^"]+)"\s*\"([^"]+)\"')
479 self.bind = re.compile(r'^\s*\\bind\s+"([^"]+)"')
480 if lyx_userdir_ver is None:
481 self.dir = lyx_userdir
483 self.dir = lyx_userdir_ver
485 def __UseShortcut(self, c):
486 m = self.shortcut_entry.match(c)
490 self.bindings[sh] = fkt
492 die(1, "cad shortcut spec(" + c + ")")
494 def __PrepareShortcuts(self):
495 if not self.dir is None:
496 tmp = tempfile.NamedTemporaryFile(suffix='.bind', delete=False)
498 old = open(self.dir + '/bind/user.bind', 'r')
502 lines = old.read().split("\n")
506 m = self.bind.match(line)
510 if val in self.bindings:
511 if self.bindings[val] != "":
512 tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
513 self.bindings[val] = ""
515 tmp.write(line + '\n')
517 tmp.write(line + '\n')
520 '## This file is used for keytests only\n\n' +
523 for val in self.bindings:
524 if not self.bindings[val] is None:
525 if self.bindings[val] != "":
526 tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
527 self.bindings[val] = ""
529 shutil.move(tmp.name, self.dir + '/bind/user.bind')
531 print("User dir not specified")
533 def dispatch(self, c):
534 if c[0:12] == 'UseShortcut ':
535 self.__UseShortcut(c[12:])
536 elif c == 'PrepareShortcuts':
537 print('Preparing usefull sortcuts for tests')
538 self.__PrepareShortcuts()
543 lyx_pid = os.environ.get('LYX_PID')
544 print('lyx_pid: ' + str(lyx_pid) + '\n')
545 infilename = os.environ.get('KEYTEST_INFILE')
546 outfilename = os.environ.get('KEYTEST_OUTFILE')
547 max_drop = os.environ.get('MAX_DROP')
548 lyx_window_name = os.environ.get('LYX_WINDOW_NAME')
549 lyx_other_window_name = None
550 screenshot_out = os.environ.get('SCREENSHOT_OUT')
551 lyx_userdir = os.environ.get('LYX_USERDIR')
552 lyx_userdir_ver = os.environ.get('LYX_USERDIR_23x')
554 max_loops = os.environ.get('MAX_LOOPS')
555 if max_loops is None:
558 extra_path = os.environ.get('EXTRA_PATH')
559 if not extra_path is None:
560 os.environ['PATH'] = extra_path + os.pathsep + os.environ['PATH']
561 print("Added " + extra_path + " to path")
562 print(os.environ['PATH'])
564 PACKAGE = os.environ.get('PACKAGE')
565 if not PACKAGE is None:
566 print("PACKAGE = " + PACKAGE + "\n")
568 PO_BUILD_DIR = os.environ.get('PO_BUILD_DIR')
569 if not PO_BUILD_DIR is None:
570 print("PO_BUILD_DIR = " + PO_BUILD_DIR + "\n")
572 lyx = os.environ.get('LYX')
576 lyx_exe = os.environ.get('LYX_EXE')
580 xvkbd_exe = os.environ.get('XVKBD_EXE')
581 if xvkbd_exe is None:
584 qt_frontend = os.environ.get('QT_FRONTEND')
585 if qt_frontend is None:
587 if qt_frontend == 'QT5':
588 controlkey_delay = 0.01
590 controlkey_delay = 0.4
592 locale_dir = os.environ.get('LOCALE_DIR')
593 if locale_dir is None:
596 def_delay = os.environ.get('XVKBD_DELAY')
597 if def_delay is None:
598 if qt_frontend == 'QT5':
603 file_new_command = os.environ.get('FILE_NEW_COMMAND')
604 if file_new_command is None:
605 file_new_command = "\Afn"
607 ResetCommand = os.environ.get('RESET_COMMAND')
608 if ResetCommand is None:
609 ResetCommand = "\[Escape]\[Escape]\[Escape]\[Escape]" + file_new_command
610 #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
612 if lyx_window_name is None:
613 lyx_window_name = 'LyX'
615 print('outfilename: ' + outfilename + '\n')
616 print('max_drop: ' + max_drop + '\n')
618 if infilename is None:
619 print('infilename is None\n')
621 print('Using x=CommandSource\n')
623 print('infilename: ' + infilename + '\n')
624 probability_we_drop_a_command = random.uniform(0, float(max_drop))
625 print('probability_we_drop_a_command: ')
626 print('%s' % probability_we_drop_a_command)
628 x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
629 print('Using x=CommandSourceFromFile\n')
631 outfile = open(outfilename, 'w')
633 if not lyx_pid is None:
635 # Next command is language dependent
636 #sendKeystringRT("\Afn", lyx_pid)
638 write_commands = True
640 lineempty = re.compile(r'^\s*$')
641 marked = ControlFile()
642 shortcuts = Shortcuts()
644 #intr_system('echo -n LOADAVG:; cat /proc/loadavg')
649 # Do not strip trailing spaces, only check for 'empty' lines
650 if lineempty.match(c):
652 outfile.writelines(c + '\n')
654 if marked.dispatch(c):
656 elif shortcuts.dispatch(c):
659 print("Ignoring comment line: " + c)
660 elif c[0:9] == 'TestBegin':
662 lyx_pid=get_proc_pid(lyx)
664 print("Found running instance(s) of LyX: " + lyx_pid + ": killing them all\n")
665 intr_system("killall " + lyx, True)
667 intr_system("killall -KILL " + lyx, True)
669 print("Starting LyX . . .")
670 if lyx_userdir is None:
671 intr_system(lyx_exe + c[9:] + "&")
673 intr_system(lyx_exe + " -userdir " + lyx_userdir + " " + c[9:] + "&")
676 old_lyx_window_name = None
677 print("Waiting for LyX to show up . . .")
679 lyx_pid=get_proc_pid(lyx)
680 if lyx_pid != old_lyx_pid:
681 print('lyx_pid=' + lyx_pid)
682 old_lyx_pid = lyx_pid
684 lyx_window_name=get_proc_win_id(lyx_pid, "")
685 if not lyx_window_name is None:
686 if old_lyx_window_name != lyx_window_name:
687 print('lyx_win=' + lyx_window_name, '\n')
688 old_lyx_window_name = lyx_window_name
694 print('Timeout: could not start ' + lyx_exe, '\n')
698 print('lyx_pid: ' + lyx_pid)
699 print('lyx_win: ' + lyx_window_name)
700 dead_expected = False
701 sendKeystringLocal("\C\[Home]", lyx_pid)
702 time.sleep(controlkey_delay)
703 elif c[0:5] == 'Sleep':
704 print("Sleeping for " + c[6:] + " seconds")
705 time.sleep(float(c[6:]))
706 elif c[0:4] == 'Exec':
711 outfile = open(outfilename + '+', 'w')
713 elif c == 'RaiseLyx':
716 elif c[0:4] == 'KK: ':
718 sendKeystringRT(c[4:], lyx_pid)
720 ##intr_system('killall lyx; sleep 2 ; killall -9 lyx')
722 die(1, 'No path /proc/xxxx/status, exiting')
724 die(1, 'No path /proc/' + lyx_pid + '/status, exiting')
725 elif c[0:4] == 'KD: ':
726 key_delay = c[4:].rstrip('\n')
727 print('Setting DELAY to ' + key_delay)
730 sendKeystringRT(ResetCommand, lyx_pid)
731 elif c[0:6] == 'Assert':
733 result = intr_system(cmd, True)
734 failed = failed or (result != 0)
735 print("result=" + str(result) + ", failed=" + str(failed))
736 elif c[0:15] == 'TestEndWithKill':
738 cmd = c[16:].rstrip()
739 if lyx_dead(lyx_pid):
740 print("LyX instance not found because of crash or assert !\n")
743 print(" ------------ Forcing kill of lyx instance: " + str(lyx_pid) + " ------------")
744 # This line below is there only to allow lyx to update its log-file
745 sendKeystringLocal("\[Escape]", lyx_pid)
747 while not lyx_dead(lyx_pid):
748 intr_system("kill -9 " + str(lyx_pid), True);
751 print("Executing " + cmd)
752 result = intr_system(cmd, True)
753 failed = failed or (result != 0)
754 print("result=" + str(result) + ", failed=" + str(failed))
756 print("failed=" + str(failed))
757 elif c[0:7] == 'TestEnd':
758 #lyx_other_window_name = None
759 if lyx_dead(lyx_pid):
760 print("LyX instance not found because of crash or assert !\n")
764 print(" ------------ Forcing quit of lyx instance: " + str(lyx_pid) + " ------------")
765 # \[Escape]+ should work as RESET focus to main window
766 sendKeystringAx("\[Escape]\[Escape]\[Escape]\[Escape]", lyx_pid)
767 time.sleep(controlkey_delay)
768 # now we should be outside any dialog
769 # and so the function lyx-quit should work
770 sendKeystringLocal("\Cq", lyx_pid)
771 marked.dispatch('CP: action=lyx-quit')
775 is_sleeping = wait_until_lyx_sleeping(lyx_pid)
777 print('wait_until_lyx_sleeping() indicated "sleeping"')
778 # For a short time lyx-status is 'sleeping', even if it is nearly dead.
779 # Without the wait below, the \[Tab]-char is sent to nirvana
782 # probably waiting for Save/Discard/Abort, we select 'Discard'
783 sendKeystringRT("\[Tab]\[Return]", lyx_pid)
787 while not lyx_dead(lyx_pid):
790 print("LyX still up, killing process and waiting for it to die...\n")
791 intr_system("kill -9 " + str(lyx_pid), True);
795 print("Executing " + cmd)
796 result = intr_system(cmd, True)
797 failed = failed or (result != 0)
798 print("result=" + str(result) + ", failed=" + str(failed))
800 print("failed=" + str(failed))
801 elif c[0:4] == 'Lang':
802 lang = c[5:].rstrip()
803 print("Setting LANG=" + lang)
804 os.environ['LANG'] = lang
805 os.environ['LC_ALL'] = lang
806 # If it doesn't exist, create a link <locale_dir>/<country-code>/LC_MESSAGES/lyx<version-suffix>.mo
807 # pointing to the corresponding .gmo file. Needed to let lyx find the right translation files.
808 # See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg165613.html
809 idx = lang.rfind(".")
815 print("Setting LANGUAGE=" + ccode)
816 os.environ['LANGUAGE'] = ccode
820 short_code = lang[0:idx]
823 lyx_dir = os.popen("dirname \"" + lyx_exe + "\"").read().rstrip()
825 # on cmake-build there is no Makefile in this directory
826 # so PACKAGE has to be provided
827 if os.path.exists(lyx_dir + "/Makefile"):
828 print("Executing: grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'")
829 lyx_name = os.popen("grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'").read().rstrip()
831 print('Could not determine PACKAGE name needed for translations\n')
835 intr_system("mkdir -p " + locale_dir + "/" + ccode + "/LC_MESSAGES")
836 intr_system("rm -f " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
837 if PO_BUILD_DIR is None:
838 if lyx_dir[0:3] == "../":
839 rel_dir = "../../" + lyx_dir
842 intr_system("ln -s " + rel_dir + "/../po/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
844 intr_system("ln -s " + PO_BUILD_DIR + "/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
846 print("Unrecognised Command '" + c + "'\n")
849 print("Test case terminated: ", end = '')