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')
30 # Ignore status == "dead" if this is set. Used at the last commands after "\Cq"
33 def die(excode, text):
56 for k in range(97, 123):
59 for k in range(97, 123):
60 keycode[:0] = ["\A" + chr(k)]
62 for k in range(97, 123):
63 keycode[:0] = ["\A" + chr(k)]
65 for k in range(97, 123):
66 keycode[:0] = ["\C" + chr(k)]
68 self.keycode = keycode
73 self.count = self.count + 1
74 if self.count % 200 == 0:
76 elif self.count > self.count_max:
81 keystr = keystr + self.keycode[random.randint(1,
82 len(self.keycode)) - 1]
83 return 'KK: ' + keystr
86 class CommandSourceFromFile(CommandSource):
88 def __init__(self, filename, p):
90 self.infile = open(filename, 'r')
91 self.lines = self.infile.readlines()
95 print(p, self.p, 'self.p')
100 # Now we start randomly dropping lines, which we hope are redundant
101 # p is the probability that any given line will be removed
104 if random.uniform(0, 1) < 0.5:
105 print('randomdrop_independant\n')
106 self.randomdrop_independant()
108 print('randomdrop_slice\n')
109 self.randomdrop_slice()
110 if screenshot_out is None:
114 self.max_count = max(len(self.lines) + 20, count_atleast)
115 if len(self.lines) < 1:
116 self.lines = linesbak
118 def randomdrop_independant(self):
121 # The next couple of lines are to ensure that at least one line is dropped
123 drop = random.randint(0, len(self.lines) - 1)
125 #p = p - 1 / len(self.lines)
126 origlines = self.lines
129 if random.uniform(0, 1) < self.p:
130 print('Randomly dropping line ' + l + '\n')
137 def randomdrop_slice(self):
139 if random.uniform(0, 1) < 0.4:
140 lines.append(lines[0])
142 num_lines = len(lines)
143 max_drop = max(5, num_lines / 5)
144 num_drop = random.randint(1, 5)
145 drop_mid = random.randint(0, num_lines)
146 drop_start = max(drop_mid - num_drop / 2, 0)
147 drop_end = min(drop_start + num_drop, num_lines)
148 print(drop_start, drop_mid, drop_end)
150 del lines[drop_start:drop_end]
154 def getCommand(self):
155 if self.count >= self.max_count:
157 if self.i >= len(self.lines):
158 self.loops = self.loops + 1
159 if self.loops >= int(max_loops):
163 line = self.lines[self.i].rstrip('\n')
164 self.count = self.count + 1
171 self.control = re.compile(r'^(C[ONPpRrC]):\s*(.*)$')
172 self.fileformat = re.compile(r'^((\>\>?)[,\s]\s*)?([^\s]+)\s*$')
176 def open(self, filename):
177 if not self.cntrfile is None:
178 self.cntrfile.close()
181 m = self.fileformat.match(filename)
184 filename = m.group(3)
191 self.cntrname = filename
193 self.cntrfile = open(filename, 'a')
195 self.cntrfile = open(filename, 'w')
198 if not self.cntrfile is None:
199 self.cntrfile.close()
202 # make the method below 'private'
203 def __addline(self, pat):
204 self.cntrfile.writelines(pat + "\n")
209 def dispatch(self, c):
210 m = self.control.match(c)
217 elif command == "CC":
220 if not self.cntrfile is None:
222 self.__addline("Comment: " + text)
223 elif command == "CP":
224 self.__addline("Simple: " + text)
225 elif command == "Cp":
226 self.__addline("ErrSimple: " + text)
227 elif command == "CR":
228 self.__addline("Regex: " + text)
229 elif command == "Cr":
230 self.__addline("ErrRegex: " + text)
232 die(1,"Error, Unrecognised Command '" + command + "'")
236 def get_proc_pid(proc_name):
237 pid=os.popen("pidof " + proc_name).read().rstrip()
240 wlistreg = re.compile(r'^(0x[0-9a-f]{5,9})\s+[^\s]+\s+([0-9]+)\s.*$')
241 def get_proc_win_id(pid, ignoreid):
242 nlist = os.popen("wmctrl -l -p").read()
243 wlist = nlist.split("\n")
245 m = wlistreg.match(item)
250 if win_id != ignoreid:
257 fname = '/proc/' + lyx_pid + '/status'
258 return os.path.exists(fname)
261 # Interruptible os.system()
262 def intr_system(cmd, ignore_err = False):
263 print("Executing " + cmd)
264 # Assure the output of cmd does not overhaul
267 if os.WIFSIGNALED(ret):
268 raise KeyboardInterrupt
269 if ret != 0 and not ignore_err:
270 raise BaseException("command failed:" + cmd)
273 statreg = re.compile(r'^State:.*\(([a-z]+)\)')
276 def printresstatus():
277 for line in resstatus:
279 print(" " + line.rstrip())
280 print('End of /proc-lines')
282 def lyx_status_retry(pid):
287 fname = '/proc/' + pid + '/status'
293 resstatus.extend([line])
294 m = statreg.match(line)
303 print("I/O error({0}): {1}".format(e.errno, e.strerror))
306 print("Unexpected error:", sys.exc_info()[0])
308 print('This should not happen')
314 status = lyx_status_retry(pid)
315 if status != "retry":
318 print('Retrying check for status')
322 print('Retried to read status ' + str(count) + ' times')
323 #print('lys_status() returning ' + status)
326 # Return true if LyX (identified via lyx_pid) is sleeping
327 def lyx_sleeping(LYX_PID):
328 return lyx_status(LYX_PID) == "sleeping"
330 # Return true if LyX (identified via lyx_pid) is zombie
331 def lyx_zombie(LYX_PID):
332 return lyx_status(LYX_PID) == "zombie"
334 def lyx_dead(LYX_PID):
335 status = lyx_status(LYX_PID)
336 return (status == "dead") or (status == "zombie")
338 def wait_until_lyx_sleeping(LYX_PID):
339 before_secs = time.time()
341 status = lyx_status(LYX_PID)
342 if status == "sleeping":
344 if (status == "dead") or (status == "zombie"):
347 print('Lyx died while waiting for status == sleeping')
350 die(1,"Lyx is dead, exiting")
351 if time.time() - before_secs > 180:
352 # Do profiling, but sysprof has no command line interface?
353 # intr_system("killall -KILL lyx")
355 die(1,"Killing due to freeze (KILL_FREEZE)")
357 # Should be never reached
358 print('Wait for sleeping ends unexpectedly')
361 def sendKeystringLocal(keystr, LYX_PID):
362 is_sleeping = wait_until_lyx_sleeping(LYX_PID)
364 print("Not sending \"" + keystr + "\"")
366 if not screenshot_out is None:
367 print('Making Screenshot: ' + screenshot_out + ' OF ' + infilename)
369 intr_system('import -window root '+screenshot_out+str(x.count)+".png")
371 actual_delay = key_delay
372 if actual_delay == '':
373 actual_delay = def_delay
375 if qt_frontend == 'QT5':
376 xvpar.extend(["-jump-pointer", "-no-back-pointer"])
378 xvpar.extend(["-xsendevent"])
379 if lyx_other_window_name is None:
380 xvpar.extend(["-window", lyx_window_name])
382 xvpar.extend(["-window", lyx_other_window_name])
383 xvpar.extend(["-delay", actual_delay, "-text", keystr])
384 print("Sending \"" + keystr + "\"")
385 subprocess.call(xvpar, stdout = FNULL, stderr = FNULL)
388 Axreg = re.compile(r'^(.*)\\Ax([^\\]*)(.*)$')
389 returnreg = re.compile(r'(\\\[[A-Z][a-z0-9]+\])(.*)$')
391 # recursive wrapper around sendKeystringLocal()
392 # handling \Ax-entries
393 def sendKeystringAx(line, LYX_PID):
395 saved_delay = key_delay
396 m = Axreg.match(line)
402 # since (.*) is greedy, check prefix for '\Ax' again
403 sendKeystringAx(prefix, LYX_PID)
404 sendKeystringLocal('\Ax', LYX_PID)
406 m2 = returnreg.match(rest)
411 sendKeystringLocal(content + ctrlk, LYX_PID)
412 key_delay = saved_delay
413 time.sleep(controlkey_delay)
415 sendKeystringLocal(line, LYX_PID)
418 sendKeystringLocal(content, LYX_PID)
420 sendKeystringLocal(rest, LYX_PID)
423 sendKeystringLocal(line, LYX_PID)
425 specialkeyreg = re.compile(r'(.+)(\\[AC]([a-zA-Z]|\\\[[A-Z][a-z0-9]+\]).*)$')
426 # Split line at start of each meta or controll char
428 def sendKeystringAC(line, LYX_PID):
429 m = specialkeyreg.match(line)
433 sendKeystringAC(first, LYX_PID)
434 sendKeystringAC(second, LYX_PID)
436 sendKeystringAx(line, LYX_PID)
438 controlkeyreg = re.compile(r'^(.*\\\[[A-Z][a-z0-9]+\])(.*\\\[[A-Z][a-z0-9]+\])(.*)$')
439 # Make sure, only one of \[Return], \[Tab], \[Down], \[Home] etc are in one sent line
440 # e.g. split the input line on each keysym
441 def sendKeystringRT(line, LYX_PID):
442 m = controlkeyreg.match(line)
447 sendKeystringRT(first, LYX_PID)
448 time.sleep(controlkey_delay)
449 sendKeystringRT(second, LYX_PID)
450 time.sleep(controlkey_delay)
452 sendKeystringRT(third, LYX_PID)
454 sendKeystringAC(line, LYX_PID)
456 def system_retry(num_retry, cmd):
458 rtn = intr_system(cmd, True)
459 while ( ( i < num_retry ) and ( rtn != 0) ):
461 rtn = intr_system(cmd, True)
464 print("Command Failed: "+cmd)
468 #intr_system("echo x-session-manager PID: $X_PID.")
469 #intr_system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
470 ####intr_system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
471 print("lyx_window_name = " + lyx_window_name + "\n")
472 intr_system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
473 system_retry(30, "wmctrl -i -a '"+lyx_window_name+"'")
475 shortcut_entry = re.compile(r'^\s*"([^"]+)"\s*\"([^"]+)\"')
477 m = shortcut_entry.match(c)
483 die(1, "cad shortcut spec(" + c + ")")
485 def PrepareShortcuts():
486 bind = re.compile(r'^\s*\\bind\s+"([^"]+)"')
487 if lyx_userdir_ver is None:
490 dir = lyx_userdir_ver
492 tmp = tempfile.NamedTemporaryFile(suffix='.bind', delete=False)
494 old = open(dir + '/bind/user.bind', 'r')
498 lines = old.read().split("\n")
507 if bindings[val] != "":
508 tmp.write("\\bind \"" + val + "\" \"" + bindings[val] + "\"\n")
511 tmp.write(line + '\n')
513 tmp.write(line + '\n')
516 '## This file is used for keytests only\n\n' +
520 if not bindings[val] is None:
521 if bindings[val] != "":
522 tmp.write("\\bind \"" + val + "\" \"" + bindings[val] + "\"\n")
525 shutil.move(tmp.name, dir + '/bind/user.bind')
527 print("User dir not specified")
529 lyx_pid = os.environ.get('LYX_PID')
530 print('lyx_pid: ' + str(lyx_pid) + '\n')
531 infilename = os.environ.get('KEYTEST_INFILE')
532 outfilename = os.environ.get('KEYTEST_OUTFILE')
533 max_drop = os.environ.get('MAX_DROP')
534 lyx_window_name = os.environ.get('LYX_WINDOW_NAME')
535 lyx_other_window_name = None
536 screenshot_out = os.environ.get('SCREENSHOT_OUT')
537 lyx_userdir = os.environ.get('LYX_USERDIR')
538 lyx_userdir_ver = os.environ.get('LYX_USERDIR_23x')
540 max_loops = os.environ.get('MAX_LOOPS')
541 if max_loops is None:
544 extra_path = os.environ.get('EXTRA_PATH')
545 if not extra_path is None:
546 os.environ['PATH'] = extra_path + os.pathsep + os.environ['PATH']
547 print("Added " + extra_path + " to path")
548 print(os.environ['PATH'])
550 PACKAGE = os.environ.get('PACKAGE')
551 if not PACKAGE is None:
552 print("PACKAGE = " + PACKAGE + "\n")
554 PO_BUILD_DIR = os.environ.get('PO_BUILD_DIR')
555 if not PO_BUILD_DIR is None:
556 print("PO_BUILD_DIR = " + PO_BUILD_DIR + "\n")
558 lyx = os.environ.get('LYX')
562 lyx_exe = os.environ.get('LYX_EXE')
566 xvkbd_exe = os.environ.get('XVKBD_EXE')
567 if xvkbd_exe is None:
570 qt_frontend = os.environ.get('QT_FRONTEND')
571 if qt_frontend is None:
573 if qt_frontend == 'QT5':
574 controlkey_delay = 0.01
576 controlkey_delay = 0.4
578 locale_dir = os.environ.get('LOCALE_DIR')
579 if locale_dir is None:
582 def_delay = os.environ.get('XVKBD_DELAY')
583 if def_delay is None:
584 if qt_frontend == 'QT5':
589 file_new_command = os.environ.get('FILE_NEW_COMMAND')
590 if file_new_command is None:
591 file_new_command = "\Afn"
593 ResetCommand = os.environ.get('RESET_COMMAND')
594 if ResetCommand is None:
595 ResetCommand = "\[Escape]\[Escape]\[Escape]\[Escape]" + file_new_command
596 #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
598 if lyx_window_name is None:
599 lyx_window_name = 'LyX'
601 print('outfilename: ' + outfilename + '\n')
602 print('max_drop: ' + max_drop + '\n')
604 if infilename is None:
605 print('infilename is None\n')
607 print('Using x=CommandSource\n')
609 print('infilename: ' + infilename + '\n')
610 probability_we_drop_a_command = random.uniform(0, float(max_drop))
611 print('probability_we_drop_a_command: ')
612 print('%s' % probability_we_drop_a_command)
614 x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
615 print('Using x=CommandSourceFromFile\n')
617 outfile = open(outfilename, 'w')
619 if not lyx_pid is None:
621 # Next command is language dependent
622 #sendKeystringRT("\Afn", lyx_pid)
624 write_commands = True
626 lineempty = re.compile(r'^\s*$')
627 marked = ControlFile()
629 #intr_system('echo -n LOADAVG:; cat /proc/loadavg')
634 # Do not strip trailing spaces, only check for 'empty' lines
635 if lineempty.match(c):
637 outfile.writelines(c + '\n')
639 if marked.dispatch(c):
642 print("Ignoring comment line: " + c)
643 elif c[0:9] == 'TestBegin':
645 lyx_pid=get_proc_pid(lyx)
647 print("Found running instance(s) of LyX: " + lyx_pid + ": killing them all\n")
648 intr_system("killall " + lyx, True)
650 intr_system("killall -KILL " + lyx, True)
652 print("Starting LyX . . .")
653 if lyx_userdir is None:
654 intr_system(lyx_exe + c[9:] + "&")
656 intr_system(lyx_exe + " -userdir " + lyx_userdir + " " + c[9:] + "&")
659 old_lyx_window_name = None
660 print("Waiting for LyX to show up . . .")
662 lyx_pid=get_proc_pid(lyx)
663 if lyx_pid != old_lyx_pid:
664 print('lyx_pid=' + lyx_pid)
665 old_lyx_pid = lyx_pid
667 lyx_window_name=get_proc_win_id(lyx_pid, "")
668 if not lyx_window_name is None:
669 if old_lyx_window_name != lyx_window_name:
670 print('lyx_win=' + lyx_window_name, '\n')
671 old_lyx_window_name = lyx_window_name
677 print('Timeout: could not start ' + lyx_exe, '\n')
681 print('lyx_pid: ' + lyx_pid)
682 print('lyx_win: ' + lyx_window_name)
683 dead_expected = False
684 sendKeystringLocal("\C\[Home]", lyx_pid)
685 time.sleep(controlkey_delay)
686 elif c[0:5] == 'Sleep':
687 print("Sleeping for " + c[6:] + " seconds")
688 time.sleep(float(c[6:]))
689 elif c[0:4] == 'Exec':
694 outfile = open(outfilename + '+', 'w')
696 elif c == 'RaiseLyx':
699 elif c == 'PrepareShortcuts':
700 print('Preparing usefull sortcuts for tests')
702 elif c[0:12] == 'UseShortcut ':
704 elif c[0:4] == 'KK: ':
706 sendKeystringRT(c[4:], lyx_pid)
708 ##intr_system('killall lyx; sleep 2 ; killall -9 lyx')
710 die(1, 'No path /proc/xxxx/status, exiting')
712 die(1, 'No path /proc/' + lyx_pid + '/status, exiting')
713 elif c[0:4] == 'KD: ':
714 key_delay = c[4:].rstrip('\n')
715 print('Setting DELAY to ' + key_delay)
718 sendKeystringRT(ResetCommand, lyx_pid)
719 elif c[0:6] == 'Assert':
721 result = intr_system(cmd, True)
722 failed = failed or (result != 0)
723 print("result=" + str(result) + ", failed=" + str(failed))
724 elif c[0:15] == 'TestEndWithKill':
726 cmd = c[16:].rstrip()
727 if lyx_dead(lyx_pid):
728 print("LyX instance not found because of crash or assert !\n")
731 print(" ------------ Forcing kill of lyx instance: " + str(lyx_pid) + " ------------")
732 # This line below is there only to allow lyx to update its log-file
733 sendKeystringLocal("\[Escape]", lyx_pid)
735 while not lyx_dead(lyx_pid):
736 intr_system("kill -9 " + str(lyx_pid), True);
739 print("Executing " + cmd)
740 result = intr_system(cmd, True)
741 failed = failed or (result != 0)
742 print("result=" + str(result) + ", failed=" + str(failed))
744 print("failed=" + str(failed))
745 elif c[0:7] == 'TestEnd':
746 #lyx_other_window_name = None
747 if lyx_dead(lyx_pid):
748 print("LyX instance not found because of crash or assert !\n")
752 print(" ------------ Forcing quit of lyx instance: " + str(lyx_pid) + " ------------")
753 # \[Escape]+ should work as RESET focus to main window
754 sendKeystringAx("\[Escape]\[Escape]\[Escape]\[Escape]", lyx_pid)
755 time.sleep(controlkey_delay)
756 # now we should be outside any dialog
757 # and so the function lyx-quit should work
758 sendKeystringLocal("\Cq", lyx_pid)
759 marked.dispatch('CP: action=lyx-quit')
763 is_sleeping = wait_until_lyx_sleeping(lyx_pid)
765 print('wait_until_lyx_sleeping() indicated "sleeping"')
766 # For a short time lyx-status is 'sleeping', even if it is nearly dead.
767 # Without the wait below, the \[Tab]-char is sent to nirvana
770 # probably waiting for Save/Discard/Abort, we select 'Discard'
771 sendKeystringRT("\[Tab]\[Return]", lyx_pid)
775 while not lyx_dead(lyx_pid):
778 print("LyX still up, killing process and waiting for it to die...\n")
779 intr_system("kill -9 " + str(lyx_pid), True);
783 print("Executing " + cmd)
784 result = intr_system(cmd, True)
785 failed = failed or (result != 0)
786 print("result=" + str(result) + ", failed=" + str(failed))
788 print("failed=" + str(failed))
789 elif c[0:4] == 'Lang':
790 lang = c[5:].rstrip()
791 print("Setting LANG=" + lang)
792 os.environ['LANG'] = lang
793 os.environ['LC_ALL'] = lang
794 # If it doesn't exist, create a link <locale_dir>/<country-code>/LC_MESSAGES/lyx<version-suffix>.mo
795 # pointing to the corresponding .gmo file. Needed to let lyx find the right translation files.
796 # See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg165613.html
797 idx = lang.rfind(".")
803 print("Setting LANGUAGE=" + ccode)
804 os.environ['LANGUAGE'] = ccode
808 short_code = lang[0:idx]
811 lyx_dir = os.popen("dirname \"" + lyx_exe + "\"").read().rstrip()
813 # on cmake-build there is no Makefile in this directory
814 # so PACKAGE has to be provided
815 if os.path.exists(lyx_dir + "/Makefile"):
816 print("Executing: grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'")
817 lyx_name = os.popen("grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'").read().rstrip()
819 print('Could not determine PACKAGE name needed for translations\n')
823 intr_system("mkdir -p " + locale_dir + "/" + ccode + "/LC_MESSAGES")
824 intr_system("rm -f " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
825 if PO_BUILD_DIR is None:
826 if lyx_dir[0:3] == "../":
827 rel_dir = "../../" + lyx_dir
830 intr_system("ln -s " + rel_dir + "/../po/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
832 intr_system("ln -s " + PO_BUILD_DIR + "/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
834 print("Unrecognised Command '" + c + "'\n")
837 print("Test case terminated: ")