]> git.lyx.org Git - features.git/blob - development/autotests/keytest.py
Keytests: optimize for speed.
[features.git] / development / autotests / keytest.py
1 #!/usr/bin/python
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
7 #
8 # Adapted by Tommaso Cucinotta from the original MonKey Test by
9 # John McCabe-Dansted.
10
11 from __future__ import print_function
12 import random
13 import os
14 import re
15 import sys
16 import time
17 #from subprocess import call
18 import subprocess
19
20 print('Beginning keytest.py')
21
22 FNULL = open('/dev/null', 'w')
23
24 key_delay = ''
25
26 class CommandSource:
27
28     def __init__(self):
29         keycode = [
30             "\[Left]",
31             '\[Right]',
32             '\[Down]',
33             '\[Up]',
34             '\[BackSpace]',
35             '\[Delete]',
36             '\[Escape]',
37             ]
38         keycode[:0] = keycode
39         keycode[:0] = keycode
40
41         keycode[:0] = ['\\']
42
43         for k in range(97, 123):
44             keycode[:0] = chr(k)
45
46         for k in range(97, 123):
47             keycode[:0] = ["\A" + chr(k)]
48
49         for k in range(97, 123):
50             keycode[:0] = ["\A" + chr(k)]
51
52         for k in range(97, 123):
53             keycode[:0] = ["\C" + chr(k)]
54
55         self.keycode = keycode
56         self.count = 0
57         self.count_max = 1999
58
59     def getCommand(self):
60         self.count = self.count + 1
61         if self.count % 200 == 0:
62             return 'RaiseLyx'
63         elif self.count > self.count_max:
64             os._exit(0)
65         else:
66             keystr = ''
67             for k in range(1, 2):
68                 keystr = keystr + self.keycode[random.randint(1,
69                         len(self.keycode)) - 1]
70             return 'KK: ' + keystr
71
72
73 class CommandSourceFromFile(CommandSource):
74
75     def __init__(self, filename, p):
76
77         self.infile = open(filename, 'r')
78         self.lines = self.infile.readlines()
79         self.infile.close()
80         linesbak = self.lines
81         self.p = p
82         print(p, self.p, 'self.p')
83         self.i = 0
84         self.count = 0
85         self.loops = 0
86
87         # Now we start randomly dropping lines, which we hope are redundant
88         # p is the probability that any given line will be removed
89
90         if p > 0.001:
91             if random.uniform(0, 1) < 0.5:
92                 print('randomdrop_independant\n')
93                 self.randomdrop_independant()
94             else:
95                 print('randomdrop_slice\n')
96                 self.randomdrop_slice()
97         if screenshot_out is None:
98             count_atleast = 100
99         else:
100             count_atleast = 1
101         self.max_count = max(len(self.lines) + 20, count_atleast)
102         if len(self.lines) < 1:
103             self.lines = linesbak
104
105     def randomdrop_independant(self):
106         p = self.p
107
108         # The next couple of lines are to ensure that at least one line is dropped
109
110         drop = random.randint(0, len(self.lines) - 1)
111         del self.lines[drop]
112         #p = p - 1 / len(self.lines)
113         origlines = self.lines
114         self.lines = []
115         for l in origlines:
116             if random.uniform(0, 1) < self.p:
117                 print('Randomly dropping line ' + l + '\n')
118             else:
119                 self.lines.append(l)
120         print('LINES\n')
121         print(self.lines)
122         sys.stdout.flush()
123
124     def randomdrop_slice(self):
125         lines = self.lines
126         if random.uniform(0, 1) < 0.4:
127             lines.append(lines[0])
128             del lines[0]
129         num_lines = len(lines)
130         max_drop = max(5, num_lines / 5)
131         num_drop = random.randint(1, 5)
132         drop_mid = random.randint(0, num_lines)
133         drop_start = max(drop_mid - num_drop / 2, 0)
134         drop_end = min(drop_start + num_drop, num_lines)
135         print(drop_start, drop_mid, drop_end)
136         print(lines)
137         del lines[drop_start:drop_end]
138         print(lines)
139         self.lines = lines
140
141     def getCommand(self):
142         if self.count >= self.max_count:
143             os._exit(0)
144         if self.i >= len(self.lines):
145             self.loops = self.loops + 1
146             if self.loops >= int(max_loops):
147                 return None
148             self.i = 0
149             return 'Loop'
150         line = self.lines[self.i].rstrip('\n')
151         self.count = self.count + 1
152         self.i = self.i + 1
153         return line
154
155
156 def lyx_exists():
157     if lyx_pid is None:
158         return False
159     fname = '/proc/' + lyx_pid + '/status'
160     return os.path.exists(fname)
161
162
163 # Interruptible os.system()
164 def intr_system(cmd, ignore_err = False):
165     print("Executing " + cmd)
166     ret = os.system(cmd)
167     if os.WIFSIGNALED(ret):
168         raise KeyboardInterrupt
169     if ret != 0 and not ignore_err:
170         raise BaseException("command failed:" + cmd)
171     return ret
172
173 statreg = re.compile(r'^State:.*\(([a-z]+)\)')
174
175 def lyx_status(pid):
176   if lyx_pid is None:
177     return "dead"
178   fname = '/proc/' + pid + '/status'
179   try:
180     f = open(fname)
181     for line in f:
182       m = statreg.match(line)
183       if m:
184         status = m.group(1)
185         f.close()
186         return status
187     f.close()
188   except IOError as e:
189      print("I/O error({0}): {1}".format(e.errno, e.strerror))
190      return "dead"
191   except:
192     print("Unexpected error:", sys.exc_info()[0])
193   return "dead"
194
195 # Return true if LyX (identified via lyx_pid) is sleeping
196 def lyx_sleeping():
197     return lyx_status(lyx_pid) == "sleeping"
198
199 # Return true if LyX (identified via lyx_pid) is zombie
200 def lyx_zombie():
201     return lyx_status(lyx_pid) == "zombie"
202
203 def lyx_dead():
204     status = lyx_status(lyx_pid)
205     return (status == "dead") or (status == "zombie")
206
207 def sendKeystringLocal(keystr, LYX_PID):
208
209     #if not re.match(".*\w.*", keystr):
210     #    print('print .' + keystr)
211     #    keystr = 'a'
212     before_secs = time.time()
213     while lyx_exists() and not lyx_sleeping():
214         time.sleep(0.02)
215         sys.stdout.flush()
216         if time.time() - before_secs > 180:
217             print('Killing due to freeze (KILL_FREEZE)')
218
219             # Do profiling, but sysprof has no command line interface?
220             # intr_system("killall -KILL lyx")
221
222             os._exit(1)
223     if not screenshot_out is None:
224         while lyx_exists() and not lyx_sleeping():
225             time.sleep(0.02)
226             sys.stdout.flush()
227         print('Making Screenshot: ' + screenshot_out + ' OF ' + infilename)
228         time.sleep(0.2)
229         intr_system('import -window root '+screenshot_out+str(x.count)+".png")
230         time.sleep(0.1)
231     sys.stdout.flush()
232     actual_delay = key_delay
233     if actual_delay == '':
234         actual_delay = def_delay
235     xvpar = [xvkbd_exe]
236     if qt_frontend == 'QT5':
237         xvpar.extend(["-no-jump-pointer"])
238     else:
239         xvpar.extend(["-xsendevent"])
240     xvpar.extend(["-window", lyx_window_name, "-delay", actual_delay, "-text", keystr])
241
242     print("Sending \"" + keystr + "\"")
243     subprocess.call(xvpar, stdout = FNULL, stderr = FNULL)
244
245 Axreg = re.compile(r'^(.*)\\Ax([^\\]*)(.*)$')
246 returnreg = re.compile(r'\\\[Return\](.*)$')
247
248 # recursive wrapper around sendKeystringLocal()
249 # handling \Ax-entries
250 def sendKeystringAx(line, LYX_PID):
251     global key_delay
252     saved_delay = key_delay
253     m = Axreg.match(line)
254     if m:
255         prefix = m.group(1)
256         content = m.group(2)
257         rest = m.group(3);
258         if prefix != "":
259             # since (.*) is greedy, check prefix for '\Ax' again
260             sendKeystringAx(prefix, LYX_PID)
261         sendKeystringLocal('\Ax', LYX_PID)
262         time.sleep(0.1)
263         m2 = returnreg.match(rest)
264         if m2:
265             line = m2.group(1)
266             key_delay = "1"
267             sendKeystringLocal(content + '\[Return]', LYX_PID)
268             key_delay = saved_delay
269             time.sleep(0.1)
270             if line != "":
271                 sendKeystringLocal(line, LYX_PID)
272         else:
273             if content != "":
274                 sendKeystringLocal(content, LYX_PID)
275             if rest != "":
276                 sendKeystringLocal(rest, LYX_PID)
277     else:
278         if line != "":
279             sendKeystringLocal(line, LYX_PID)
280
281 specialkeyreg = re.compile(r'(.+)(\\[AC][a-zA-Z].*)$')
282 # Split line at start of each \[AC][a-zA-Z]
283
284 def sendKeystringAC(line, LYX_PID):
285     m = specialkeyreg.match(line)
286     if m:
287         first = m.group(1)
288         second = m.group(2)
289         sendKeystringAC(first, LYX_PID)
290         sendKeystringAC(second, LYX_PID)
291     else:
292         sendKeystringAx(line, LYX_PID)
293
294 controlkeyreg = re.compile(r'^(.*\\\[[A-Z][a-z]+\])(.*\\\[[A-Z][a-z]+\])(.*)$')
295 # Make sure, only one of \[Return], \[Tab], \[Down], \[Home] etc are in one sent line
296 # e.g. split the input line on each keysym
297 def sendKeystringRT(line, LYX_PID):
298     m = controlkeyreg.match(line)
299     if m:
300         first = m.group(1)
301         second = m.group(2)
302         third = m.group(3)
303         sendKeystringRT(first, LYX_PID)
304         sendKeystringRT(second, LYX_PID)
305         if third != "":
306             sendKeystringRT(third, LYX_PID)
307     else:
308         sendKeystringAC(line, LYX_PID)
309
310 def system_retry(num_retry, cmd):
311     i = 0
312     rtn = intr_system(cmd)
313     while ( ( i < num_retry ) and ( rtn != 0) ):
314         i = i + 1
315         rtn = intr_system(cmd)
316         time.sleep(1)
317     if ( rtn != 0 ):
318         print("Command Failed: "+cmd)
319         print(" EXITING!\n")
320         os._exit(1)
321
322 def RaiseWindow():
323     #intr_system("echo x-session-manager PID: $X_PID.")
324     #intr_system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
325     ####intr_system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
326     print("lyx_window_name = " + lyx_window_name + "\n")
327     intr_system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
328     system_retry(30, "wmctrl -i -a '"+lyx_window_name+"'")
329
330
331 lyx_pid = os.environ.get('LYX_PID')
332 print('lyx_pid: ' + str(lyx_pid) + '\n')
333 infilename = os.environ.get('KEYTEST_INFILE')
334 outfilename = os.environ.get('KEYTEST_OUTFILE')
335 max_drop = os.environ.get('MAX_DROP')
336 lyx_window_name = os.environ.get('LYX_WINDOW_NAME')
337 screenshot_out = os.environ.get('SCREENSHOT_OUT')
338 lyx_userdir = os.environ.get('LYX_USERDIR')
339
340 max_loops = os.environ.get('MAX_LOOPS')
341 if max_loops is None:
342     max_loops = 3
343
344 PACKAGE = os.environ.get('PACKAGE')
345 if not PACKAGE is None:
346   print("PACKAGE = " + PACKAGE + "\n")
347
348 PO_BUILD_DIR = os.environ.get('PO_BUILD_DIR')
349 if not PO_BUILD_DIR is None:
350   print("PO_BUILD_DIR = " + PO_BUILD_DIR + "\n")
351
352 lyx = os.environ.get('LYX')
353 if lyx is None:
354     lyx = "lyx"
355
356 lyx_exe = os.environ.get('LYX_EXE')
357 if lyx_exe is None:
358     lyx_exe = lyx
359
360 xvkbd_exe = os.environ.get('XVKBD_EXE')
361 if xvkbd_exe is None:
362     xvkbd_exe = "xvkbd"
363
364 qt_frontend = os.environ.get('QT_FRONTEND')
365 if qt_frontend is None:
366     qt_frontend = 'QT4'
367
368 locale_dir = os.environ.get('LOCALE_DIR')
369 if locale_dir is None:
370     locale_dir = '.'
371
372 def_delay = os.environ.get('XVKBD_DELAY')
373 if def_delay is None:
374     def_delay = '5'
375
376 file_new_command = os.environ.get('FILE_NEW_COMMAND')
377 if file_new_command is None:
378     file_new_command = "\Afn"
379
380 ResetCommand = os.environ.get('RESET_COMMAND')
381 if ResetCommand is None:
382     ResetCommand = "\[Escape]\[Escape]\[Escape]\[Escape]" + file_new_command
383     #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
384
385 if lyx_window_name is None:
386     lyx_window_name = 'LyX'
387
388 print('outfilename: ' + outfilename + '\n')
389 print('max_drop: ' + max_drop + '\n')
390
391 if infilename is None:
392     print('infilename is None\n')
393     x = CommandSource()
394     print('Using x=CommandSource\n')
395 else:
396     print('infilename: ' + infilename + '\n')
397     probability_we_drop_a_command = random.uniform(0, float(max_drop))
398     print('probability_we_drop_a_command: ')
399     print('%s' % probability_we_drop_a_command)
400     print('\n')
401     x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
402     print('Using x=CommandSourceFromFile\n')
403
404 outfile = open(outfilename, 'w')
405
406 if not lyx_pid is None:
407     RaiseWindow()
408     # Next command is language dependent
409     #sendKeystringRT("\Afn", lyx_pid)
410
411 write_commands = True
412 failed = False
413
414 while not failed:
415     #intr_system('echo -n LOADAVG:; cat /proc/loadavg')
416     c = x.getCommand()
417     if c is None:
418         break
419     if c.strip() == "":
420         continue
421     outfile.writelines(c + '\n')
422     outfile.flush()
423     if c[0] == '#':
424         print("Ignoring comment line: " + c)
425     elif c[0:9] == 'TestBegin':
426         print("\n")
427         lyx_pid=os.popen("pidof " + lyx).read()
428         if lyx_pid != "":
429             print("Found running instance(s) of LyX: " + lyx_pid + ": killing them all\n")
430             intr_system("killall " + lyx, True)
431             time.sleep(0.5)
432             intr_system("killall -KILL " + lyx, True)
433         time.sleep(0.2)
434         print("Starting LyX . . .")
435         if lyx_userdir is None:
436             intr_system(lyx_exe + c[9:] + "&")
437         else:
438             intr_system(lyx_exe + " -userdir " + lyx_userdir + " " + c[9:] + "&")
439         count = 5
440         old_lyx_pid = "-7"
441         old_lyx_window_name = "not set"
442         print("Waiting for LyX to show up . . .")
443         while count > 0:
444             lyx_pid=os.popen("pidof " + lyx).read().rstrip()
445             if lyx_pid != old_lyx_pid:
446                 print('lyx_pid=' + lyx_pid)
447                 old_lyx_pid = lyx_pid
448             if lyx_pid != "":
449                 lyx_window_name=os.popen("wmctrl -l -p | grep ' " + str(lyx_pid) +  " ' | cut -d ' ' -f 1").read().rstrip()
450                 if old_lyx_window_name != lyx_window_name:
451                     print('lyx_win=' + lyx_window_name, '\n')
452                     old_lyx_window_name = lyx_window_name
453                 if lyx_window_name != "":
454                     break
455             else:
456                 count = count - 1
457             time.sleep(1)
458         if count <= 0:
459             print('Timeout: could not start ' + lyx_exe, '\n')
460             sys.stdout.flush()
461             failed = True
462         print('lyx_pid: ' + lyx_pid)
463         print('lyx_win: ' + lyx_window_name)
464         sendKeystringLocal("\C\[Home]", lyx_pid)
465     elif c[0:5] == 'Sleep':
466         print("Sleeping for " + c[6:] + " seconds")
467         time.sleep(float(c[6:]))
468     elif c[0:4] == 'Exec':
469         cmd = c[5:].rstrip()
470         intr_system(cmd)
471     elif c == 'Loop':
472         outfile.close()
473         outfile = open(outfilename + '+', 'w')
474         print('Now Looping')
475     elif c == 'RaiseLyx':
476         print('Raising Lyx')
477         RaiseWindow()
478     elif c[0:4] == 'KK: ':
479         if lyx_exists():
480             sendKeystringRT(c[4:], lyx_pid)
481         else:
482             ##intr_system('killall lyx; sleep 2 ; killall -9 lyx')
483             if lyx_pid is None:
484               print('No path /proc/xxxx/status, exiting')
485             else:
486               print('No path /proc/' + lyx_pid + '/status, exiting')
487             os._exit(1)
488     elif c[0:4] == 'KD: ':
489         key_delay = c[4:].rstrip('\n')
490         print('Setting DELAY to ' + key_delay)
491     elif c == 'Loop':
492         RaiseWindow()
493         sendKeystringRT(ResetCommand, lyx_pid)
494     elif c[0:6] == 'Assert':
495         cmd = c[7:].rstrip()
496         result = intr_system(cmd)
497         failed = failed or (result != 0)
498         print("result=" + str(result) + ", failed=" + str(failed))
499     elif c[0:7] == 'TestEnd':
500 #        time.sleep(0.5)
501         if lyx_dead():
502             print("LyX instance not found because of crash or assert !\n")
503             failed = True
504         else:
505             print("    ------------    Forcing quit of lyx instance: " + str(lyx_pid) + "    ------------")
506             # \Ax Enter command line is sometimes blocked
507             # \[Escape] works after this
508             sendKeystringAx("\Ax\[Escape]", lyx_pid)
509             # now we should be outside any dialog
510             # and so the function lyx-quit should work
511             sendKeystringLocal("\Cq", lyx_pid)
512             time.sleep(0.5)
513             if lyx_sleeping():
514                 # probably waiting for Save/Discard/Abort, we select 'Discard'
515                 sendKeystringRT("\[Tab]\[Return]", lyx_pid)
516                 lcount = 0
517             else:
518                 lcount = 1
519             while not lyx_dead():
520                 lcount = lcount + 1
521                 if lcount > 20:
522                     print("LyX still up, killing process and waiting for it to die...\n")
523                     intr_system("kill -9 " + str(lyx_pid), True);
524                 time.sleep(0.5)
525         cmd = c[8:].rstrip()
526         if cmd != "":
527             print("Executing " + cmd)
528             result = intr_system(cmd)
529             failed = failed or (result != 0)
530             print("result=" + str(result) + ", failed=" + str(failed))
531         else:
532             print("failed=" + str(failed))
533     elif c[0:4] == 'Lang':
534         lang = c[5:].rstrip()
535         print("Setting LANG=" + lang)
536         os.environ['LANG'] = lang
537         os.environ['LC_ALL'] = lang
538 # If it doesn't exist, create a link <locale_dir>/<country-code>/LC_MESSAGES/lyx<version-suffix>.mo
539 # pointing to the corresponding .gmo file. Needed to let lyx find the right translation files.
540 # See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg165613.html
541         idx = lang.rfind(".")
542         if idx != -1:
543             ccode = lang[0:idx]
544         else:
545             ccode = lang
546
547         print("Setting LANGUAGE=" + ccode)
548         os.environ['LANGUAGE'] = ccode
549
550         idx = lang.find("_")
551         if idx != -1:
552             short_code = lang[0:idx]
553         else:
554             short_code = ccode
555         lyx_dir = os.popen("dirname \"" + lyx_exe + "\"").read().rstrip()
556         if PACKAGE is None:
557           # on cmake-build there is no Makefile in this directory
558           # so PACKAGE has to be provided
559           if os.path.exists(lyx_dir + "/Makefile"):
560             print("Executing: grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'")
561             lyx_name = os.popen("grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'").read().rstrip()
562           else:
563             print('Could not determine PACKAGE name needed for translations\n')
564             failed = True
565         else:
566           lyx_name = PACKAGE
567         intr_system("mkdir -p " + locale_dir + "/" + ccode + "/LC_MESSAGES")
568         intr_system("rm -f " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
569         if PO_BUILD_DIR is None:
570             if lyx_dir[0:3] == "../":
571                 rel_dir = "../../" + lyx_dir
572             else:
573                 rel_dir = lyx_dir
574             intr_system("ln -s " + rel_dir + "/../po/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
575         else:
576             intr_system("ln -s " + PO_BUILD_DIR + "/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
577     else:
578         print("Unrecognised Command '" + c + "'\n")
579         failed = True
580
581 print("Test case terminated: ")
582 if failed:
583     print("FAIL\n")
584     os._exit(1)
585 else:
586     print("Ok\n")
587     os._exit(0)