]> git.lyx.org Git - lyx.git/blob - development/autotests/keytest.py
138342f365f63cabbc158e27afb03003311ae55d
[lyx.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 import random
12 import os
13 import re
14 import sys
15 import time
16 #from subprocess import call
17 import subprocess
18
19 print 'Beginning keytest.py'
20
21 FNULL = open('/dev/null', 'w')
22
23 def_delay = '60'
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         #print '\nLine read: <<' + line + '>>\n'
154         sys.stdout.write('r')
155         return line
156
157 def lyx_exists():
158     if lyx_pid is None:
159         return False
160     fname = '/proc/' + lyx_pid + '/status'
161     return os.path.exists(fname)
162
163 def lyx_sleeping():
164     fname = '/proc/' + lyx_pid + '/status'
165     f = open(fname, 'r')
166     lines = f.readlines()
167     sleeping = lines[1].find('(sleeping)') > 0
168
169     # print 'LYX_STATE', lines[1] , 'SLEEPING=', sleeping
170
171     return sleeping
172
173
174 # Interruptible os.system()
175 def intr_system(cmd):
176     ret = os.system(cmd)
177     if os.WIFSIGNALED(ret):
178         raise KeyboardInterrupt
179     return ret
180
181
182 def sendKeystring(keystr, LYX_PID):
183
184     # print "sending keystring "+keystr+"\n"
185
186     if not re.match(".*\w.*", keystr):
187         print 'print .' + keystr + '.\n'
188         keystr = 'a'
189     before_secs = time.time()
190     while lyx_exists() and not lyx_sleeping():
191         time.sleep(0.02)
192         sys.stdout.write('.')
193         sys.stdout.flush()
194         if time.time() - before_secs > 180:
195             print 'Killing due to freeze (KILL_FREEZE)'
196
197             # Do profiling, but sysprof has no command line interface?
198             # intr_system("killall -KILL lyx")
199
200             os._exit(1)
201     if not screenshot_out is None:
202         while lyx_exists() and not lyx_sleeping():
203             time.sleep(0.02)
204             sys.stdout.write('.')
205             sys.stdout.flush()
206         print 'Making Screenshot: ' + screenshot_out + ' OF ' + infilename
207         time.sleep(0.2)
208         intr_system('import -window root '+screenshot_out+str(x.count)+".png")
209         time.sleep(0.1)
210     sys.stdout.flush()
211     actual_delay = key_delay
212     if actual_delay == '':
213         actual_delay = def_delay
214     if not xvkbd_hacked:
215         subprocess.call([xvkbd_exe, "-xsendevent", "-window", lyx_window_name,
216                          "-delay", actual_delay, "-text", keystr], stdout = FNULL, stderr = FNULL)
217     else:
218         subprocess.call([xvkbd_exe, "-no_root", "-wait_idle", lyx_pid,
219                          "-xsendevent", "-window", lyx_window_name,
220                          "-delay", actual_delay, "-text", keystr], stdout = FNULL, stderr = FNULL)
221
222 def system_retry(num_retry, cmd):
223     i = 0
224     rtn = intr_system(cmd)
225     while ( ( i < num_retry ) and ( rtn != 0) ):
226         i = i + 1
227         rtn = intr_system(cmd)
228         time.sleep(1)
229     if ( rtn != 0 ):
230         print "Command Failed: "+cmd
231         print " EXITING!\n"
232         os._exit(1)
233
234 def RaiseWindow():
235     #intr_system("echo x-session-manager PID: $X_PID.")
236     #intr_system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
237     ####intr_system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
238     #intr_system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
239     system_retry(30, "wmctrl -a '"+lyx_window_name+"'")
240
241
242 lyx_pid = os.environ.get('LYX_PID')
243 print 'lyx_pid: ' + str(lyx_pid) + '\n'
244 infilename = os.environ.get('KEYTEST_INFILE')
245 outfilename = os.environ.get('KEYTEST_OUTFILE')
246 max_drop = os.environ.get('MAX_DROP')
247 lyx_window_name = os.environ.get('LYX_WINDOW_NAME')
248 screenshot_out = os.environ.get('SCREENSHOT_OUT')
249 lyx_userdir = os.environ.get('LYX_USERDIR')
250
251 max_loops = os.environ.get('MAX_LOOPS')
252 if max_loops is None:
253     max_loops = 3
254
255 lyx_exe = os.environ.get('LYX_EXE')
256 if lyx_exe is None:
257     lyx_exe = "lyx"
258
259 xvkbd_exe = os.environ.get('XVKBD_EXE')
260 if xvkbd_exe is None:
261     xvkbd_exe = "xvkbd"
262
263 xvkbd_hacked = os.environ.get('XVKBD_HACKED') != None
264
265 file_new_command = os.environ.get('FILE_NEW_COMMAND')
266 if file_new_command is None:
267     file_new_command = "\Afn"
268
269 ResetCommand = os.environ.get('RESET_COMMAND')
270 if ResetCommand is None:
271     ResetCommand = "\[Escape]\[Escape]\[Escape]\[Escape]" + file_new_command
272     #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
273
274 if lyx_window_name is None:
275     lyx_window_name = 'LyX'
276
277 print 'outfilename: ' + outfilename + '\n'
278 print 'max_drop: ' + max_drop + '\n'
279
280 if infilename is None:
281     print 'infilename is None\n'
282     x = CommandSource()
283     print 'Using x=CommandSource\n'
284 else:
285     print 'infilename: ' + infilename + '\n'
286     probability_we_drop_a_command = random.uniform(0, float(max_drop))
287     print 'probability_we_drop_a_command: '
288     print '%s' % probability_we_drop_a_command
289     print '\n'
290     x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
291     print 'Using x=CommandSourceFromFile\n'
292
293 outfile = open(outfilename, 'w')
294
295 if not lyx_pid is None:
296     RaiseWindow()
297     sendKeystring("\Afn", lyx_pid)
298
299 write_commands = True
300 failed = False
301
302 while not failed:
303     #intr_system('echo -n LOADAVG:; cat /proc/loadavg')
304     c = x.getCommand()
305     if c is None:
306         break
307     if c.strip() == "":
308         continue
309     outfile.writelines(c + '\n')
310     outfile.flush()
311     if c[0] == '#':
312         print "\nIgnoring comment line: " + c
313     elif c[0:9] == 'TestBegin':
314         print "\n"
315         lyx_pid=os.popen("pidof lyx").read()
316         if lyx_pid != "":
317             print "Found running instance(s) of LyX: " + lyx_pid + ": killing them all\n"
318             intr_system("killall lyx")
319             time.sleep(0.5)
320             intr_system("killall -KILL lyx")
321         time.sleep(0.2)
322         print "Starting LyX . . ."
323         if lyx_userdir is None:
324             intr_system(lyx_exe + c[9:] + "&")
325         else:
326             intr_system(lyx_exe + " -userdir " + lyx_userdir + " " + c[9:] + "&")
327         while True:
328             lyx_pid=os.popen("pidof lyx").read().rstrip()
329             if lyx_pid != "":
330                 lyx_window_name=os.popen("wmctrl -l -p | grep ' " + str(lyx_pid) +  " ' | cut -d ' ' -f 1").read().rstrip()
331                 if lyx_window_name != "":
332                     break
333             print 'lyx_win: ' + lyx_window_name + '\n'
334             print "Waiting for LyX to show up . . ."
335             time.sleep(1)
336         print 'lyx_pid: ' + lyx_pid + '\n'
337         print 'lyx_win: ' + lyx_window_name + '\n'
338     elif c[0:5] == 'Sleep':
339         print "\nSleeping for " + c[6:] + "\n"
340         time.sleep(float(c[6:]))
341     elif c[0:4] == 'Exec':
342         cmd = c[5:].rstrip()
343         print "\nExecuting " + cmd + "\n"
344         intr_system(cmd)
345     elif c == 'Loop':
346         outfile.close()
347         outfile = open(outfilename + '+', 'w')
348         print 'Now Looping'
349     elif c == 'RaiseLyx':
350         print 'Raising Lyx'
351         RaiseWindow()
352     elif c[0:4] == 'KK: ':
353         if lyx_exists():
354             sendKeystring(c[4:], lyx_pid)
355         else:
356             ##intr_system('killall lyx; sleep 2 ; killall -9 lyx')
357             print 'No path /proc/' + lyx_pid + '/status, exiting'
358             os._exit(1)
359     elif c[0:4] == 'KD: ':
360         key_delay = c[4:].rstrip('\n')
361         print 'Setting DELAY to ' + key_delay + '.'
362     elif c == 'Loop':
363         RaiseWindow()
364         sendKeystring(ResetCommand, lyx_pid)
365     elif c[0:6] == 'Assert':
366         cmd = c[7:].rstrip()
367         print "\nExecuting " + cmd
368         result = intr_system(cmd)
369         failed = failed or (result != 0)
370         print "result=" + str(result) + ", failed=" + str(failed)
371     elif c[0:7] == 'TestEnd':
372         time.sleep(0.5)
373         print "\nTerminating lyx instance: " + str(lyx_pid) + "\n"
374         intr_system("kill -9 " + str(lyx_pid) + "\n");
375         while lyx_exists():
376             print "Waiting for lyx to die...\n"
377             time.sleep(0.5)
378         cmd = c[8:].rstrip()
379         print "\nExecuting " + cmd
380         result = intr_system(cmd)
381         failed = failed or (result != 0)
382         print "result=" + str(result) + ", failed=" + str(failed)
383     elif c[0:4] == 'Lang':
384         lang = c[5:].rstrip()
385         print "\nSetting LANG=" + lang
386         os.environ['LANG'] = lang
387     else:
388         print "Unrecognised Command '" + c + "'\n"
389         failed = True
390
391 print "Test case terminated: "
392 if failed:
393     print "FAIL\n"
394     os._exit(1)
395 else:
396     print "Ok\n"
397     os._exit(0)