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 def extractmultiple(line, regex):
388 #print("extractmultiple " + line)
390 m = regex.match(line)
397 norm = extractmultiple(m.group(2), regex)
398 res[0] = chr + norm[0]
405 normal_re = re.compile(r'^([^\\]|\\\\)(.*)$')
406 def extractnormal(line):
407 # collect non-special chars from start of line
408 return extractmultiple(line, normal_re)
410 modifier_re = re.compile(r'^(\\[CAS])(.+)$')
411 def extractmodifiers(line):
412 # collect modifiers like '\\A' at start of line
413 return extractmultiple(line, modifier_re)
415 special_re = re.compile(r'^(\\\[[A-Z][a-z0-9]+\])(.*)$')
416 def extractsingle(line):
417 # check for single key following a modifier
418 # either ascii like 'a'
419 # or special like '\[Return]'
420 res = [False, "", ""]
421 m = normal_re.match(line)
427 m = special_re.match(line)
433 die(1, "Undecodable key for line \'" + line + "\"")
436 def sendKeystring(line, LYX_PID):
439 normalchars = extractnormal(line)
440 line = normalchars[1]
441 if normalchars[0] != "":
442 sendKeystringLocal(normalchars[0], LYX_PID)
445 modchars = extractmodifiers(line)
448 die(1, "Missing modified key")
449 modifiedchar = extractsingle(line)
450 line = modifiedchar[2]
451 special = modchars[0] != "" or modifiedchar[0]
452 sendKeystringLocal(modchars[0] + modifiedchar[1], LYX_PID)
454 # give the os time to update the status info (in /proc)
455 time.sleep(controlkey_delay)
456 sendKeystring(line, LYX_PID)
458 def system_retry(num_retry, cmd):
460 rtn = intr_system(cmd, True)
461 while ( ( i < num_retry ) and ( rtn != 0) ):
463 rtn = intr_system(cmd, True)
466 print("Command Failed: "+cmd)
470 #intr_system("echo x-session-manager PID: $X_PID.")
471 #intr_system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
472 ####intr_system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
473 print("lyx_window_name = " + lyx_window_name + "\n")
474 intr_system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
475 system_retry(30, "wmctrl -i -a '"+lyx_window_name+"'")
480 self.shortcut_entry = re.compile(r'^\s*"([^"]+)"\s*\"([^"]+)\"')
482 self.bind = re.compile(r'^\s*\\bind\s+"([^"]+)"')
483 if lyx_userdir_ver is None:
484 self.dir = lyx_userdir
486 self.dir = lyx_userdir_ver
488 def __UseShortcut(self, c):
489 m = self.shortcut_entry.match(c)
493 self.bindings[sh] = fkt
495 die(1, "cad shortcut spec(" + c + ")")
497 def __PrepareShortcuts(self):
498 if not self.dir is None:
499 tmp = tempfile.NamedTemporaryFile(suffix='.bind', delete=False)
501 old = open(self.dir + '/bind/user.bind', 'r')
505 lines = old.read().split("\n")
509 m = self.bind.match(line)
513 if val in self.bindings:
514 if self.bindings[val] != "":
515 tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
516 self.bindings[val] = ""
518 tmp.write(line + '\n')
520 tmp.write(line + '\n')
523 '## This file is used for keytests only\n\n' +
526 for val in self.bindings:
527 if not self.bindings[val] is None:
528 if self.bindings[val] != "":
529 tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
530 self.bindings[val] = ""
532 shutil.move(tmp.name, self.dir + '/bind/user.bind')
534 print("User dir not specified")
536 def dispatch(self, c):
537 if c[0:12] == 'UseShortcut ':
538 self.__UseShortcut(c[12:])
539 elif c == 'PrepareShortcuts':
540 print('Preparing usefull sortcuts for tests')
541 self.__PrepareShortcuts()
546 lyx_pid = os.environ.get('LYX_PID')
547 print('lyx_pid: ' + str(lyx_pid) + '\n')
548 infilename = os.environ.get('KEYTEST_INFILE')
549 outfilename = os.environ.get('KEYTEST_OUTFILE')
550 max_drop = os.environ.get('MAX_DROP')
551 lyx_window_name = os.environ.get('LYX_WINDOW_NAME')
552 lyx_other_window_name = None
553 screenshot_out = os.environ.get('SCREENSHOT_OUT')
554 lyx_userdir = os.environ.get('LYX_USERDIR')
555 lyx_userdir_ver = os.environ.get('LYX_USERDIR_23x')
557 max_loops = os.environ.get('MAX_LOOPS')
558 if max_loops is None:
561 extra_path = os.environ.get('EXTRA_PATH')
562 if not extra_path is None:
563 os.environ['PATH'] = extra_path + os.pathsep + os.environ['PATH']
564 print("Added " + extra_path + " to path")
565 print(os.environ['PATH'])
567 PACKAGE = os.environ.get('PACKAGE')
568 if not PACKAGE is None:
569 print("PACKAGE = " + PACKAGE + "\n")
571 PO_BUILD_DIR = os.environ.get('PO_BUILD_DIR')
572 if not PO_BUILD_DIR is None:
573 print("PO_BUILD_DIR = " + PO_BUILD_DIR + "\n")
575 lyx = os.environ.get('LYX')
579 lyx_exe = os.environ.get('LYX_EXE')
583 xvkbd_exe = os.environ.get('XVKBD_EXE')
584 if xvkbd_exe is None:
587 qt_frontend = os.environ.get('QT_FRONTEND')
588 if qt_frontend is None:
590 if qt_frontend == 'QT5':
591 controlkey_delay = 0.01
593 controlkey_delay = 0.4
595 locale_dir = os.environ.get('LOCALE_DIR')
596 if locale_dir is None:
599 def_delay = os.environ.get('XVKBD_DELAY')
600 if def_delay is None:
601 if qt_frontend == 'QT5':
606 file_new_command = os.environ.get('FILE_NEW_COMMAND')
607 if file_new_command is None:
608 file_new_command = "\Afn"
610 ResetCommand = os.environ.get('RESET_COMMAND')
611 if ResetCommand is None:
612 ResetCommand = "\[Escape]\[Escape]\[Escape]\[Escape]" + file_new_command
613 #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
615 if lyx_window_name is None:
616 lyx_window_name = 'LyX'
618 print('outfilename: ' + outfilename + '\n')
619 print('max_drop: ' + max_drop + '\n')
621 if infilename is None:
622 print('infilename is None\n')
624 print('Using x=CommandSource\n')
626 print('infilename: ' + infilename + '\n')
627 probability_we_drop_a_command = random.uniform(0, float(max_drop))
628 print('probability_we_drop_a_command: ')
629 print('%s' % probability_we_drop_a_command)
631 x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
632 print('Using x=CommandSourceFromFile\n')
634 outfile = open(outfilename, 'w')
636 if not lyx_pid is None:
638 # Next command is language dependent
639 #sendKeystring("\Afn", lyx_pid)
641 write_commands = True
643 lineempty = re.compile(r'^\s*$')
644 marked = ControlFile()
645 shortcuts = Shortcuts()
647 #intr_system('echo -n LOADAVG:; cat /proc/loadavg')
652 # Do not strip trailing spaces, only check for 'empty' lines
653 if lineempty.match(c):
655 outfile.writelines(c + '\n')
657 if marked.dispatch(c):
659 elif shortcuts.dispatch(c):
662 print("Ignoring comment line: " + c)
663 elif c[0:9] == 'TestBegin':
665 lyx_pid=get_proc_pid(lyx)
667 print("Found running instance(s) of LyX: " + lyx_pid + ": killing them all\n")
668 intr_system("killall " + lyx, True)
670 intr_system("killall -KILL " + lyx, True)
672 print("Starting LyX . . .")
673 if lyx_userdir is None:
674 intr_system(lyx_exe + c[9:] + "&")
676 intr_system(lyx_exe + " -userdir " + lyx_userdir + " " + c[9:] + "&")
679 old_lyx_window_name = None
680 print("Waiting for LyX to show up . . .")
682 lyx_pid=get_proc_pid(lyx)
683 if lyx_pid != old_lyx_pid:
684 print('lyx_pid=' + lyx_pid)
685 old_lyx_pid = lyx_pid
687 lyx_window_name=get_proc_win_id(lyx_pid, "")
688 if not lyx_window_name is None:
689 if old_lyx_window_name != lyx_window_name:
690 print('lyx_win=' + lyx_window_name, '\n')
691 old_lyx_window_name = lyx_window_name
697 print('Timeout: could not start ' + lyx_exe, '\n')
701 print('lyx_pid: ' + lyx_pid)
702 print('lyx_win: ' + lyx_window_name)
703 dead_expected = False
704 sendKeystring("\C\[Home]", lyx_pid)
705 elif c[0:5] == 'Sleep':
706 print("Sleeping for " + c[6:] + " seconds")
707 time.sleep(float(c[6:]))
708 elif c[0:4] == 'Exec':
713 outfile = open(outfilename + '+', 'w')
715 elif c == 'RaiseLyx':
718 elif c[0:4] == 'KK: ':
720 sendKeystring(c[4:], lyx_pid)
722 ##intr_system('killall lyx; sleep 2 ; killall -9 lyx')
724 die(1, 'No path /proc/xxxx/status, exiting')
726 die(1, 'No path /proc/' + lyx_pid + '/status, exiting')
727 elif c[0:4] == 'KD: ':
728 key_delay = c[4:].rstrip('\n')
729 print('Setting DELAY to ' + key_delay)
732 sendKeystring(ResetCommand, lyx_pid)
733 elif c[0:6] == 'Assert':
735 result = intr_system(cmd, True)
736 failed = failed or (result != 0)
737 print("result=" + str(result) + ", failed=" + str(failed))
738 elif c[0:15] == 'TestEndWithKill':
740 cmd = c[16:].rstrip()
741 if lyx_dead(lyx_pid):
742 print("LyX instance not found because of crash or assert !\n")
745 print(" ------------ Forcing kill of lyx instance: " + str(lyx_pid) + " ------------")
746 # This line below is there only to allow lyx to update its log-file
747 sendKeystring("\[Escape]", lyx_pid)
749 while not lyx_dead(lyx_pid):
750 intr_system("kill -9 " + str(lyx_pid), True);
753 print("Executing " + cmd)
754 result = intr_system(cmd, True)
755 failed = failed or (result != 0)
756 print("result=" + str(result) + ", failed=" + str(failed))
758 print("failed=" + str(failed))
759 elif c[0:7] == 'TestEnd':
760 #lyx_other_window_name = None
761 if lyx_dead(lyx_pid):
762 print("LyX instance not found because of crash or assert !\n")
766 print(" ------------ Forcing quit of lyx instance: " + str(lyx_pid) + " ------------")
767 # \[Escape]+ should work as RESET focus to main window
768 sendKeystring("\[Escape]\[Escape]\[Escape]\[Escape]", lyx_pid)
769 # now we should be outside any dialog
770 # and so the function lyx-quit should work
771 sendKeystring("\Cq", lyx_pid)
772 marked.dispatch('CP: action=lyx-quit')
776 is_sleeping = wait_until_lyx_sleeping(lyx_pid)
778 print('wait_until_lyx_sleeping() indicated "sleeping"')
779 # For a short time lyx-status is 'sleeping', even if it is nearly dead.
780 # Without the wait below, the \[Tab]-char is sent to nirvana
783 # probably waiting for Save/Discard/Abort, we select 'Discard'
784 sendKeystring("\[Tab]\[Return]", lyx_pid)
788 while not lyx_dead(lyx_pid):
791 print("LyX still up, killing process and waiting for it to die...\n")
792 intr_system("kill -9 " + str(lyx_pid), True);
796 print("Executing " + cmd)
797 result = intr_system(cmd, True)
798 failed = failed or (result != 0)
799 print("result=" + str(result) + ", failed=" + str(failed))
801 print("failed=" + str(failed))
802 elif c[0:4] == 'Lang':
803 lang = c[5:].rstrip()
804 print("Setting LANG=" + lang)
805 os.environ['LANG'] = lang
806 os.environ['LC_ALL'] = lang
807 # If it doesn't exist, create a link <locale_dir>/<country-code>/LC_MESSAGES/lyx<version-suffix>.mo
808 # pointing to the corresponding .gmo file. Needed to let lyx find the right translation files.
809 # See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg165613.html
810 idx = lang.rfind(".")
816 print("Setting LANGUAGE=" + ccode)
817 os.environ['LANGUAGE'] = ccode
821 short_code = lang[0:idx]
824 lyx_dir = os.popen("dirname \"" + lyx_exe + "\"").read().rstrip()
826 # on cmake-build there is no Makefile in this directory
827 # so PACKAGE has to be provided
828 if os.path.exists(lyx_dir + "/Makefile"):
829 print("Executing: grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'")
830 lyx_name = os.popen("grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'").read().rstrip()
832 print('Could not determine PACKAGE name needed for translations\n')
836 intr_system("mkdir -p " + locale_dir + "/" + ccode + "/LC_MESSAGES")
837 intr_system("rm -f " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
838 if PO_BUILD_DIR is None:
839 if lyx_dir[0:3] == "../":
840 rel_dir = "../../" + lyx_dir
843 intr_system("ln -s " + rel_dir + "/../po/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
845 intr_system("ln -s " + PO_BUILD_DIR + "/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
847 print("Unrecognised Command '" + c + "'\n")
850 print("Test case terminated: ", end = '')