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])([A-Za-z0-9]*):\s*(.*)$')
171 self.fileformat = re.compile(r'^((\>\>?)[,\s]\s*)?([^\s]+)\s*$')
172 self.cntrfile = dict()
173 # Map keytest marker to pattern-file-marker for searchPatterns.pl
174 self.convertSearchMark = { 'CN': 'Comment: ',
175 'CP': 'Simple: ', 'Cp': 'ErrSimple: ',
176 'CR': 'Regex: ', 'Cr': 'ErrRegex: '}
178 def __open(self, handle, filename):
179 if handle in self.cntrfile:
180 self.cntrfile[handle].close()
181 del self.cntrfile[handle]
182 m = self.fileformat.match(filename)
185 filename = m.group(3)
193 self.cntrfile[handle] = open(filename, 'a')
195 self.cntrfile[handle] = open(filename, 'w')
198 handles = self.cntrfile.keys()
199 for handle in handles:
202 def __close(self, handle):
203 if handle in self.cntrfile:
204 name = self.cntrfile[handle].name
205 self.cntrfile[handle].close()
206 del self.cntrfile[handle]
207 print("Closed ctrl " + handle + " (" + name + ")")
209 # make the method below 'private'
210 def __addline(self, handle, pat):
211 self.cntrfile[handle].writelines(pat + "\n")
213 def dispatch(self, c):
214 m = self.control.match(c)
223 self.__open(handle, text);
224 elif command == "CC":
227 if handle in self.cntrfile:
228 if command in self.convertSearchMark:
229 self.__addline(handle, self.convertSearchMark[command] + text)
231 die(1,"Error, Unrecognised Command '" + command + "'")
233 die(1, "Ctrl-file " + handle + " not in use")
237 def get_proc_pid(proc_name):
238 pid=os.popen("pidof " + proc_name).read().rstrip()
241 wlistreg = re.compile(r'^(0x[0-9a-f]{5,9})\s+[^\s]+\s+([0-9]+)\s.*$')
242 def get_proc_win_id(pid, ignoreid):
243 nlist = os.popen("wmctrl -l -p").read()
244 wlist = nlist.split("\n")
246 m = wlistreg.match(item)
251 if win_id != ignoreid:
258 fname = '/proc/' + lyx_pid + '/status'
259 return os.path.exists(fname)
262 # Interruptible os.system()
263 def intr_system(cmd, ignore_err = False):
264 print("Executing " + cmd)
265 # Assure the output of cmd does not overhaul
268 if os.WIFSIGNALED(ret):
269 raise KeyboardInterrupt
270 if ret != 0 and not ignore_err:
271 raise BaseException("command failed:" + cmd)
274 statreg = re.compile(r'^State:.*\(([a-z]+)\)')
277 def printresstatus():
278 for line in resstatus:
280 print(" " + line.rstrip())
281 print('End of /proc-lines')
283 def lyx_status_retry(pid):
288 fname = '/proc/' + pid + '/status'
294 resstatus.extend([line])
295 m = statreg.match(line)
304 print("I/O error({0}): {1}".format(e.errno, e.strerror))
307 print("Unexpected error:", sys.exc_info()[0])
309 print('This should not happen')
315 status = lyx_status_retry(pid)
316 if status != "retry":
319 print('Retrying check for status')
323 print('Retried to read status ' + str(count) + ' times')
324 #print('lys_status() returning ' + status)
327 # Return true if LyX (identified via lyx_pid) is sleeping
328 def lyx_sleeping(LYX_PID):
329 return lyx_status(LYX_PID) == "sleeping"
331 # Return true if LyX (identified via lyx_pid) is zombie
332 def lyx_zombie(LYX_PID):
333 return lyx_status(LYX_PID) == "zombie"
335 def lyx_dead(LYX_PID):
336 status = lyx_status(LYX_PID)
337 return (status == "dead") or (status == "zombie")
339 def wait_until_lyx_sleeping(LYX_PID):
340 before_secs = time.time()
342 status = lyx_status(LYX_PID)
343 if status == "sleeping":
345 if (status == "dead") or (status == "zombie"):
348 print('Lyx died while waiting for status == sleeping')
351 die(1,"Lyx is dead, exiting")
352 if time.time() - before_secs > 180:
353 # Do profiling, but sysprof has no command line interface?
354 # intr_system("killall -KILL lyx")
356 die(1,"Killing due to freeze (KILL_FREEZE)")
358 # Should be never reached
359 print('Wait for sleeping ends unexpectedly')
362 def sendKeystringLocal(keystr, LYX_PID):
363 is_sleeping = wait_until_lyx_sleeping(LYX_PID)
365 print("Not sending \"" + keystr + "\"")
367 if not screenshot_out is None:
368 print('Making Screenshot: ' + screenshot_out + ' OF ' + infilename)
370 intr_system('import -window root '+screenshot_out+str(x.count)+".png")
372 actual_delay = key_delay
373 if actual_delay == '':
374 actual_delay = def_delay
376 if qt_frontend == 'QT5':
377 xvpar.extend(["-jump-pointer", "-no-back-pointer"])
379 xvpar.extend(["-xsendevent"])
380 if lyx_other_window_name is None:
381 xvpar.extend(["-window", lyx_window_name])
383 xvpar.extend(["-window", lyx_other_window_name])
384 xvpar.extend(["-delay", actual_delay, "-text", keystr])
385 print("Sending \"" + keystr + "\"")
386 subprocess.call(xvpar, stdout = FNULL, stderr = FNULL)
389 def extractmultiple(line, regex):
390 #print("extractmultiple " + line)
392 m = regex.match(line)
399 norm = extractmultiple(m.group(2), regex)
400 res[0] = chr + norm[0]
407 normal_re = re.compile(r'^([^\\]|\\\\)(.*)$')
408 def extractnormal(line):
409 # collect non-special chars from start of line
410 return extractmultiple(line, normal_re)
412 modifier_re = re.compile(r'^(\\[CAS])(.+)$')
413 def extractmodifiers(line):
414 # collect modifiers like '\\A' at start of line
415 return extractmultiple(line, modifier_re)
417 special_re = re.compile(r'^(\\\[[A-Z][a-z0-9]+\])(.*)$')
418 def extractsingle(line):
419 # check for single key following a modifier
420 # either ascii like 'a'
421 # or special like '\[Return]'
422 res = [False, "", ""]
423 m = normal_re.match(line)
429 m = special_re.match(line)
435 die(1, "Undecodable key for line \'" + line + "\"")
438 def sendKeystring(line, LYX_PID):
441 normalchars = extractnormal(line)
442 line = normalchars[1]
443 if normalchars[0] != "":
444 sendKeystringLocal(normalchars[0], LYX_PID)
447 modchars = extractmodifiers(line)
450 die(1, "Missing modified key")
451 modifiedchar = extractsingle(line)
452 line = modifiedchar[2]
453 special = modchars[0] != "" or modifiedchar[0]
454 sendKeystringLocal(modchars[0] + modifiedchar[1], LYX_PID)
456 # give the os time to update the status info (in /proc)
457 time.sleep(controlkey_delay)
458 sendKeystring(line, LYX_PID)
460 def system_retry(num_retry, cmd):
462 rtn = intr_system(cmd, True)
463 while ( ( i < num_retry ) and ( rtn != 0) ):
465 rtn = intr_system(cmd, True)
468 print("Command Failed: "+cmd)
472 #intr_system("echo x-session-manager PID: $X_PID.")
473 #intr_system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
474 ####intr_system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
475 print("lyx_window_name = " + lyx_window_name + "\n")
476 intr_system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
477 system_retry(30, "wmctrl -i -a '"+lyx_window_name+"'")
482 self.shortcut_entry = re.compile(r'^\s*"([^"]+)"\s*\"([^"]+)\"')
484 self.bind = re.compile(r'^\s*\\bind\s+"([^"]+)"')
485 if lyx_userdir_ver is None:
486 self.dir = lyx_userdir
488 self.dir = lyx_userdir_ver
490 def __UseShortcut(self, c):
491 m = self.shortcut_entry.match(c)
495 self.bindings[sh] = fkt
497 die(1, "cad shortcut spec(" + c + ")")
499 def __PrepareShortcuts(self):
500 if not self.dir is None:
501 tmp = tempfile.NamedTemporaryFile(suffix='.bind', delete=False)
503 old = open(self.dir + '/bind/user.bind', 'r')
507 lines = old.read().split("\n")
511 m = self.bind.match(line)
515 if val in self.bindings:
516 if self.bindings[val] != "":
517 tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
518 self.bindings[val] = ""
520 tmp.write(line + '\n')
522 tmp.write(line + '\n')
525 '## This file is used for keytests only\n\n' +
528 for val in self.bindings:
529 if not self.bindings[val] is None:
530 if self.bindings[val] != "":
531 tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
532 self.bindings[val] = ""
534 shutil.move(tmp.name, self.dir + '/bind/user.bind')
536 print("User dir not specified")
538 def dispatch(self, c):
539 if c[0:12] == 'UseShortcut ':
540 self.__UseShortcut(c[12:])
541 elif c == 'PrepareShortcuts':
542 print('Preparing usefull sortcuts for tests')
543 self.__PrepareShortcuts()
548 lyx_pid = os.environ.get('LYX_PID')
549 print('lyx_pid: ' + str(lyx_pid) + '\n')
550 infilename = os.environ.get('KEYTEST_INFILE')
551 outfilename = os.environ.get('KEYTEST_OUTFILE')
552 max_drop = os.environ.get('MAX_DROP')
553 lyx_window_name = os.environ.get('LYX_WINDOW_NAME')
554 lyx_other_window_name = None
555 screenshot_out = os.environ.get('SCREENSHOT_OUT')
556 lyx_userdir = os.environ.get('LYX_USERDIR')
557 lyx_userdir_ver = os.environ.get('LYX_USERDIR_24x')
558 if lyx_userdir is None:
559 lyx_userdir = lyx_userdir_ver
561 max_loops = os.environ.get('MAX_LOOPS')
562 if max_loops is None:
565 extra_path = os.environ.get('EXTRA_PATH')
566 if not extra_path is None:
567 os.environ['PATH'] = extra_path + os.pathsep + os.environ['PATH']
568 print("Added " + extra_path + " to path")
569 print(os.environ['PATH'])
571 PACKAGE = os.environ.get('PACKAGE')
572 if not PACKAGE is None:
573 print("PACKAGE = " + PACKAGE + "\n")
575 PO_BUILD_DIR = os.environ.get('PO_BUILD_DIR')
576 if not PO_BUILD_DIR is None:
577 print("PO_BUILD_DIR = " + PO_BUILD_DIR + "\n")
579 lyx = os.environ.get('LYX')
583 lyx_exe = os.environ.get('LYX_EXE')
587 xvkbd_exe = os.environ.get('XVKBD_EXE')
588 if xvkbd_exe is None:
591 qt_frontend = os.environ.get('QT_FRONTEND')
592 if qt_frontend is None:
594 if qt_frontend == 'QT5':
595 # Some tests sometimes failed with value 0.01 on Qt5.8
596 controlkey_delay = 0.4
598 controlkey_delay = 0.4
600 locale_dir = os.environ.get('LOCALE_DIR')
601 if locale_dir is None:
604 def_delay = os.environ.get('XVKBD_DELAY')
605 if def_delay is None:
606 if qt_frontend == 'QT5':
611 file_new_command = os.environ.get('FILE_NEW_COMMAND')
612 if file_new_command is None:
613 file_new_command = "\Afn"
615 ResetCommand = os.environ.get('RESET_COMMAND')
616 if ResetCommand is None:
617 ResetCommand = "\[Escape]\[Escape]\[Escape]\[Escape]" + file_new_command
618 #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
620 if lyx_window_name is None:
621 lyx_window_name = 'LyX'
623 print('outfilename: ' + outfilename + '\n')
624 print('max_drop: ' + max_drop + '\n')
626 if infilename is None:
627 print('infilename is None\n')
629 print('Using x=CommandSource\n')
631 print('infilename: ' + infilename + '\n')
632 probability_we_drop_a_command = random.uniform(0, float(max_drop))
633 print('probability_we_drop_a_command: ')
634 print('%s' % probability_we_drop_a_command)
636 x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
637 print('Using x=CommandSourceFromFile\n')
639 outfile = open(outfilename, 'w')
641 if not lyx_pid is None:
643 # Next command is language dependent
644 #sendKeystring("\Afn", lyx_pid)
646 write_commands = True
648 lineempty = re.compile(r'^\s*$')
649 marked = ControlFile()
650 shortcuts = Shortcuts()
652 #intr_system('echo -n LOADAVG:; cat /proc/loadavg')
657 # Do not strip trailing spaces, only check for 'empty' lines
658 if lineempty.match(c):
660 outfile.writelines(c + '\n')
662 if marked.dispatch(c):
664 elif shortcuts.dispatch(c):
667 print("Ignoring comment line: " + c)
668 elif c[0:9] == 'TestBegin':
670 lyx_pid=get_proc_pid(lyx)
672 print("Found running instance(s) of LyX: " + lyx_pid + ": killing them all\n")
673 intr_system("killall " + lyx, True)
675 intr_system("killall -KILL " + lyx, True)
677 print("Starting LyX . . .")
678 if lyx_userdir is None:
679 intr_system(lyx_exe + c[9:] + "&")
681 intr_system(lyx_exe + " -userdir " + lyx_userdir + " " + c[9:] + "&")
684 old_lyx_window_name = None
685 print("Waiting for LyX to show up . . .")
687 lyx_pid=get_proc_pid(lyx)
688 if lyx_pid != old_lyx_pid:
689 print('lyx_pid=' + lyx_pid)
690 old_lyx_pid = lyx_pid
692 lyx_window_name=get_proc_win_id(lyx_pid, "")
693 if not lyx_window_name is None:
694 if old_lyx_window_name != lyx_window_name:
695 print('lyx_win=' + lyx_window_name, '\n')
696 old_lyx_window_name = lyx_window_name
702 print('Timeout: could not start ' + lyx_exe, '\n')
706 print('lyx_pid: ' + lyx_pid)
707 print('lyx_win: ' + lyx_window_name)
708 dead_expected = False
709 sendKeystring("\C\[Home]", lyx_pid)
710 elif c[0:5] == 'Sleep':
711 print("Sleeping for " + c[6:] + " seconds")
712 time.sleep(float(c[6:]))
713 elif c[0:4] == 'Exec':
718 outfile = open(outfilename + '+', 'w')
720 elif c == 'RaiseLyx':
723 elif c[0:4] == 'KK: ':
725 sendKeystring(c[4:], lyx_pid)
727 ##intr_system('killall lyx; sleep 2 ; killall -9 lyx')
729 die(1, 'No path /proc/xxxx/status, exiting')
731 die(1, 'No path /proc/' + lyx_pid + '/status, exiting')
732 elif c[0:4] == 'KD: ':
733 key_delay = c[4:].rstrip('\n')
734 print('Setting DELAY to ' + key_delay)
737 sendKeystring(ResetCommand, lyx_pid)
738 elif c[0:6] == 'Assert':
740 result = intr_system(cmd, True)
741 failed = failed or (result != 0)
742 print("result=" + str(result) + ", failed=" + str(failed))
743 elif c[0:15] == 'TestEndWithKill':
745 cmd = c[16:].rstrip()
746 if lyx_dead(lyx_pid):
747 print("LyX instance not found because of crash or assert !\n")
750 print(" ------------ Forcing kill of lyx instance: " + str(lyx_pid) + " ------------")
751 # This line below is there only to allow lyx to update its log-file
752 sendKeystring("\[Escape]", lyx_pid)
754 while not lyx_dead(lyx_pid):
755 intr_system("kill -9 " + str(lyx_pid), True);
758 print("Executing " + cmd)
759 result = intr_system(cmd, True)
760 failed = failed or (result != 0)
761 print("result=" + str(result) + ", failed=" + str(failed))
763 print("failed=" + str(failed))
764 elif c[0:7] == 'TestEnd':
765 #lyx_other_window_name = None
766 if lyx_dead(lyx_pid):
767 print("LyX instance not found because of crash or assert !\n")
771 print(" ------------ Forcing quit of lyx instance: " + str(lyx_pid) + " ------------")
772 # \[Escape]+ should work as RESET focus to main window
773 sendKeystring("\[Escape]\[Escape]\[Escape]\[Escape]", lyx_pid)
774 # now we should be outside any dialog
775 # and so the function lyx-quit should work
776 sendKeystring("\Cq", lyx_pid)
777 marked.dispatch('CP: action=lyx-quit')
778 marked.dispatch('CC:')
781 is_sleeping = wait_until_lyx_sleeping(lyx_pid)
783 print('wait_until_lyx_sleeping() indicated "sleeping"')
784 # For a short time lyx-status is 'sleeping', even if it is nearly dead.
785 # Without the wait below, the \[Tab]-char is sent to nirvana
788 # probably waiting for Save/Discard/Abort, we select 'Discard'
789 sendKeystring("\[Tab]\[Return]", lyx_pid)
793 while not lyx_dead(lyx_pid):
796 print("LyX still up, killing process and waiting for it to die...\n")
797 intr_system("kill -9 " + str(lyx_pid), True);
801 print("Executing " + cmd)
802 result = intr_system(cmd, True)
803 failed = failed or (result != 0)
804 print("result=" + str(result) + ", failed=" + str(failed))
806 print("failed=" + str(failed))
807 elif c[0:4] == 'Lang':
808 lang = c[5:].rstrip()
809 print("Setting LANG=" + lang)
810 os.environ['LANG'] = lang
811 os.environ['LC_ALL'] = lang
812 # If it doesn't exist, create a link <locale_dir>/<country-code>/LC_MESSAGES/lyx<version-suffix>.mo
813 # pointing to the corresponding .gmo file. Needed to let lyx find the right translation files.
814 # See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg165613.html
815 idx = lang.rfind(".")
821 print("Setting LANGUAGE=" + ccode)
822 os.environ['LANGUAGE'] = ccode
826 short_code = lang[0:idx]
829 lyx_dir = os.popen("dirname \"" + lyx_exe + "\"").read().rstrip()
831 # on cmake-build there is no Makefile in this directory
832 # so PACKAGE has to be provided
833 if os.path.exists(lyx_dir + "/Makefile"):
834 print("Executing: grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'")
835 lyx_name = os.popen("grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'").read().rstrip()
837 print('Could not determine PACKAGE name needed for translations\n')
841 intr_system("mkdir -p " + locale_dir + "/" + ccode + "/LC_MESSAGES")
842 intr_system("rm -f " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
843 if PO_BUILD_DIR is None:
844 if lyx_dir[0:3] == "../":
845 rel_dir = "../../" + lyx_dir
848 intr_system("ln -s " + rel_dir + "/../po/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
850 intr_system("ln -s " + PO_BUILD_DIR + "/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
852 print("Unrecognised Command '" + c + "'\n")
855 print("Test case terminated: ", end = '')