]> git.lyx.org Git - features.git/blob - development/autotests/keytest.py
Added basic automated testing capability, based on the MonKey Test (keytest.py) by...
[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 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 DELAY = '59'
24
25 class CommandSource:
26
27     def __init__(self):
28         keycode = [
29             "\[Left]",
30             '\[Right]',
31             '\[Down]',
32             '\[Up]',
33             '\[BackSpace]',
34             '\[Delete]',
35             '\[Escape]',
36             ]
37         keycode[:0] = keycode
38         keycode[:0] = keycode
39
40         keycode[:0] = ['\\']
41
42         for k in range(97, 123):
43             keycode[:0] = chr(k)
44
45         for k in range(97, 123):
46             keycode[:0] = ["\A" + chr(k)]
47
48         for k in range(97, 123):
49             keycode[:0] = ["\A" + chr(k)]
50
51         for k in range(97, 123):
52             keycode[:0] = ["\C" + chr(k)]
53
54         self.keycode = keycode
55         self.count = 0
56         self.count_max = 1999
57
58     def getCommand(self):
59         self.count = self.count + 1
60         if self.count % 200 == 0:
61             return 'RaiseLyx'
62         elif self.count > self.count_max:
63             os._exit(0)
64         else:
65             keystr = ''
66             for k in range(1, 2):
67                 keystr = keystr + self.keycode[random.randint(1,
68                         len(self.keycode)) - 1]
69             return 'KK: ' + keystr
70
71
72 class CommandSourceFromFile(CommandSource):
73
74     def __init__(self, filename, p):
75
76         self.infile = open(filename, 'r')
77         self.lines = self.infile.readlines()
78         self.infile.close()
79         linesbak = self.lines
80         self.p = p
81         print p, self.p, 'self.p'
82         self.i = 0
83         self.count = 0
84         self.loops = 0
85
86         # Now we start randomly dropping lines, which we hope are redundant
87         # p is the probability that any given line will be removed
88
89         if p > 0.001:
90             if random.uniform(0, 1) < 0.5:
91                 print 'randomdrop_independant\n'
92                 self.randomdrop_independant()
93             else:
94                 print 'randomdrop_slice\n'
95                 self.randomdrop_slice()
96         if screenshot_out is None:
97             count_atleast = 100
98         else:
99             count_atleast = 1
100         self.max_count = max(len(self.lines) + 20, count_atleast)
101         if len(self.lines) < 1:
102             self.lines = linesbak
103
104     def randomdrop_independant(self):
105         p = self.p
106
107         # The next couple of lines are to ensure that at least one line is dropped
108
109         drop = random.randint(0, len(self.lines) - 1)
110         del self.lines[drop]
111         #p = p - 1 / len(self.lines)
112         origlines = self.lines
113         self.lines = []
114         for l in origlines:
115             if random.uniform(0, 1) < self.p:
116                 print 'Randomly dropping line ' + l + '\n'
117             else:
118                 self.lines.append(l)
119         print 'LINES\n'
120         print self.lines
121         sys.stdout.flush()
122
123     def randomdrop_slice(self):
124         lines = self.lines
125         if random.uniform(0, 1) < 0.4:
126             lines.append(lines[0])
127             del lines[0]
128         num_lines = len(lines)
129         max_drop = max(5, num_lines / 5)
130         num_drop = random.randint(1, 5)
131         drop_mid = random.randint(0, num_lines)
132         drop_start = max(drop_mid - num_drop / 2, 0)
133         drop_end = min(drop_start + num_drop, num_lines)
134         print drop_start, drop_mid, drop_end
135         print lines
136         del lines[drop_start:drop_end]
137         print lines
138         self.lines = lines
139
140     def getCommand(self):
141         if self.count >= self.max_count:
142             os._exit(0)
143         if self.i >= len(self.lines):
144             self.loops = self.loops + 1
145             if self.loops >= max_loops:
146                 os._exit(0)
147             self.i = 0
148             return 'Loop'
149         line = self.lines[self.i]
150         self.count = self.count + 1
151         self.i = self.i + 1
152         #print 'Line read: <<' + line + '>>\n'
153         sys.stdout.write('r')
154         return line.rstrip('\n').rstrip()
155
156
157 def lyx_sleeping():
158     if lyx_pid is None:
159         return True
160     fname = '/proc/' + lyx_pid + '/status'
161     if not os.path.exists(fname):
162         return False
163     f = open(fname, 'r')
164     lines = f.readlines()
165     sleeping = lines[1].find('(sleeping)') > 0
166
167     # print 'LYX_STATE', lines[1] , 'SLEEPING=', sleeping
168
169     return sleeping
170
171
172 def sendKeystring(keystr, LYX_PID):
173
174     # print "sending keystring "+keystr+"\n"
175
176     if not re.match(".*\w.*", keystr):
177         print 'print .' + keystr + '.\n'
178         keystr = 'a'
179     before_secs = time.time()
180     while not lyx_sleeping():
181         time.sleep(0.02)
182         sys.stdout.write('.')
183         sys.stdout.flush()
184         if time.time() - before_secs > 180:
185             print 'Killing due to freeze (KILL_FREEZE)'
186
187             # Do profiling, but sysprof has no command line interface?
188             # os.system("killall -KILL lyx")
189
190             os._exit(1)
191     if not screenshot_out is None:
192         while not lyx_sleeping():
193             time.sleep(0.02)
194             sys.stdout.write('.')
195             sys.stdout.flush()
196         print 'Making Screenshot: ' + screenshot_out + ' OF ' + infilename
197         time.sleep(0.2)
198         os.system('import -window root '+screenshot_out+str(x.count)+".png")
199         time.sleep(0.1)
200     sys.stdout.flush()
201     if (subprocess.call(
202             ["xvkbd", "-xsendevent", "-delay", DELAY, "-text", keystr],
203             stdout=FNULL,stderr=FNULL
204             ) == 0):
205         sys.stdout.write('*')
206     else:
207         sys.stdout.write('X')
208
209 def system_retry(num_retry, cmd):
210     i = 0
211     rtn = os.system(cmd)
212     while ( ( i < num_retry ) and ( rtn != 0) ):
213         i = i + 1
214         rtn=os.system(cmd)
215         time.sleep(1)
216     if ( rtn != 0 ):
217         print "Command Failed: "+cmd
218         print " EXITING!\n"
219         os._exit(1)
220
221 def RaiseWindow():
222     #os.system("echo x-session-manager PID: $X_PID.")
223     #os.system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
224     ####os.system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
225     #os.system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
226     system_retry(30, "wmctrl -R '"+lyx_window_name+"'")
227
228
229 lyx_pid = os.environ.get('LYX_PID')
230 print 'lyx_pid: ' + str(lyx_pid) + '\n'
231 infilename = os.environ.get('KEYTEST_INFILE')
232 outfilename = os.environ.get('KEYTEST_OUTFILE')
233 max_drop = os.environ.get('MAX_DROP')
234 lyx_window_name = os.environ.get('LYX_WINDOW_NAME')
235 screenshot_out = os.environ.get('SCREENSHOT_OUT')
236
237 max_loops = os.environ.get('MAX_LOOPS')
238 if max_loops is None:
239     max_loops = 3
240
241 lyx_exe = os.environ.get('LYX_EXE')
242 if lyx_exe is None:
243     lyx_exe = "lyx"
244
245 file_new_command = os.environ.get('FILE_NEW_COMMAND')
246 if file_new_command is None:
247     file_new_command = "\Afn"
248
249 ResetCommand = os.environ.get('RESET_COMMAND')
250 if ResetCommand is None:
251     ResetCommand = "\[Escape]\[Escape]\[Escape]\[Escape]" + file_new_command
252     #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
253
254 if lyx_window_name is None:
255     lyx_window_name = 'LyX'
256
257 print 'outfilename: ' + outfilename + '\n'
258 print 'max_drop: ' + max_drop + '\n'
259
260 if infilename is None:
261     print 'infilename is None\n'
262     x = CommandSource()
263     print 'Using x=CommandSource\n'
264 else:
265     print 'infilename: ' + infilename + '\n'
266     probability_we_drop_a_command = random.uniform(0, float(max_drop))
267     print 'probability_we_drop_a_command: '
268     print '%s' % probability_we_drop_a_command
269     print '\n'
270     x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
271     print 'Using x=CommandSourceFromFile\n'
272
273 outfile = open(outfilename, 'w')
274
275 if lyx_pid != "":
276     RaiseWindow()
277     sendKeystring("\Afn", lyx_pid)
278
279 write_commands = True
280
281 while True:
282     #os.system('echo -n LOADAVG:; cat /proc/loadavg')
283     c = x.getCommand()
284     outfile.writelines(c + '\n')
285     outfile.flush()
286     if c[0] == '#':
287         print "\nIgnoring comment line: " + c
288     elif c[0:9] == 'TestBegin':
289         print "\n"
290         lyx_pid=os.popen("pidof lyx").read()
291         if lyx_pid != "":
292             print "Found running instance(s) of LyX: " + lyx_pid + ": killing them all\n"
293             os.system("killall lyx")
294         time.sleep(0.2)
295         print "Starting LyX . . ."
296         os.system(lyx_exe + c[9:] + "&")
297         while True:
298             lyx_pid=os.popen("pidof lyx").read().rstrip()
299             lyx_window_name=os.popen("wmctrl -l | grep 'lyx$\\|LyX:' | sed -e 's/.*\\([Ll]y[Xx].*\\)$/\\1/'").read().rstrip()
300             if lyx_window_name != "":
301                 break
302             print 'lyx_win: ' + lyx_window_name + '\n'
303             print "Waiting for LyX to show up . . ."
304             time.sleep(1)
305         print 'lyx_pid: ' + lyx_pid + '\n'
306         print 'lyx_win: ' + lyx_window_name + '\n'
307         #RaiseWindow()
308         #sendKeystring("\Afn", lyx_pid)
309     elif c[0:5] == 'Sleep':
310         print "\nSleeping for " + c[6:] + "\n"
311         time.sleep(float(c[6:]))
312     elif c[0:4] == 'Exec':
313         cmd = c[5:].rstrip()
314         print "\nExecuting " + cmd + "\n"
315         os.system(cmd)
316     elif c == 'Loop':
317         outfile.close()
318         outfile = open(outfilename + '+', 'w')
319         print 'Now Looping'
320     elif c == 'RaiseLyx':
321         print 'Raising Lyx'
322         RaiseWindow()
323     elif c[0:4] == 'KK: ':
324         if os.path.exists('/proc/' + lyx_pid + '/status'):
325             sendKeystring(c[4:], lyx_pid)
326         else:
327             ##os.system('killall lyx; sleep 2 ; killall -9 lyx')
328             print 'No path /proc/' + lyx_pid + '/status, exiting'
329             os._exit(1)
330     elif c[0:4] == 'KD: ':
331         DELAY = c[4:].rstrip('\n')
332         print 'Setting DELAY to ' + DELAY + '.'
333     elif c == 'Loop':
334         RaiseWindow()
335         sendKeystring(ResetCommand, lyx_pid)
336     elif c[0:7] == 'TestEnd':
337         time.sleep(1)
338         print "\nTerminating lyx instance: " + str(lyx_pid) + "\n"
339         os.system("kill -9 " + str(lyx_pid) + "\n");
340         cmd = c[7:].rstrip()
341         result = os.system(cmd)
342         print "Test case terminated: "
343         if result == 0:
344             print "Ok\n"
345             os._exit(0)
346         else:
347             print "FAIL\n"
348             os._exit(1)
349     else:
350         print "Unrecognised Command '" + c + "'\n"