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
16 #from subprocess import call
19 print 'Beginning keytest.py'
21 FNULL = open('/dev/null', 'w')
42 for k in range(97, 123):
45 for k in range(97, 123):
46 keycode[:0] = ["\A" + chr(k)]
48 for k in range(97, 123):
49 keycode[:0] = ["\A" + chr(k)]
51 for k in range(97, 123):
52 keycode[:0] = ["\C" + chr(k)]
54 self.keycode = keycode
59 self.count = self.count + 1
60 if self.count % 200 == 0:
62 elif self.count > self.count_max:
67 keystr = keystr + self.keycode[random.randint(1,
68 len(self.keycode)) - 1]
69 return 'KK: ' + keystr
72 class CommandSourceFromFile(CommandSource):
74 def __init__(self, filename, p):
76 self.infile = open(filename, 'r')
77 self.lines = self.infile.readlines()
81 print p, self.p, 'self.p'
86 # Now we start randomly dropping lines, which we hope are redundant
87 # p is the probability that any given line will be removed
90 if random.uniform(0, 1) < 0.5:
91 print 'randomdrop_independant\n'
92 self.randomdrop_independant()
94 print 'randomdrop_slice\n'
95 self.randomdrop_slice()
96 if screenshot_out is None:
100 self.max_count = max(len(self.lines) + 20, count_atleast)
101 if len(self.lines) < 1:
102 self.lines = linesbak
104 def randomdrop_independant(self):
107 # The next couple of lines are to ensure that at least one line is dropped
109 drop = random.randint(0, len(self.lines) - 1)
111 #p = p - 1 / len(self.lines)
112 origlines = self.lines
115 if random.uniform(0, 1) < self.p:
116 print 'Randomly dropping line ' + l + '\n'
123 def randomdrop_slice(self):
125 if random.uniform(0, 1) < 0.4:
126 lines.append(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
136 del lines[drop_start:drop_end]
140 def getCommand(self):
141 if self.count >= self.max_count:
143 if self.i >= len(self.lines):
144 self.loops = self.loops + 1
145 if self.loops >= int(max_loops):
149 line = self.lines[self.i].rstrip('\n')
150 self.count = self.count + 1
152 #print '\nLine read: <<' + line + '>>\n'
153 sys.stdout.write('r')
159 fname = '/proc/' + lyx_pid + '/status'
160 return os.path.exists(fname)
163 fname = '/proc/' + lyx_pid + '/status'
165 lines = f.readlines()
166 sleeping = lines[1].find('(sleeping)') > 0
168 # print 'LYX_STATE', lines[1] , 'SLEEPING=', sleeping
173 def sendKeystring(keystr, LYX_PID):
175 # print "sending keystring "+keystr+"\n"
177 if not re.match(".*\w.*", keystr):
178 print 'print .' + keystr + '.\n'
180 before_secs = time.time()
181 while lyx_exists() and not lyx_sleeping():
183 sys.stdout.write('.')
185 if time.time() - before_secs > 180:
186 print 'Killing due to freeze (KILL_FREEZE)'
188 # Do profiling, but sysprof has no command line interface?
189 # os.system("killall -KILL lyx")
192 if not screenshot_out is None:
193 while lyx_exists() and not lyx_sleeping():
195 sys.stdout.write('.')
197 print 'Making Screenshot: ' + screenshot_out + ' OF ' + infilename
199 os.system('import -window root '+screenshot_out+str(x.count)+".png")
203 ["xvkbd", "-xsendevent", "-window", lyx_window_name, "-delay", DELAY, "-text", keystr],
204 stdout=FNULL,stderr=FNULL
206 sys.stdout.write('*')
208 sys.stdout.write('X')
210 def system_retry(num_retry, cmd):
213 while ( ( i < num_retry ) and ( rtn != 0) ):
218 print "Command Failed: "+cmd
223 #os.system("echo x-session-manager PID: $X_PID.")
224 #os.system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
225 ####os.system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
226 #os.system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
227 system_retry(30, "wmctrl -a '"+lyx_window_name+"'")
230 lyx_pid = os.environ.get('LYX_PID')
231 print 'lyx_pid: ' + str(lyx_pid) + '\n'
232 infilename = os.environ.get('KEYTEST_INFILE')
233 outfilename = os.environ.get('KEYTEST_OUTFILE')
234 max_drop = os.environ.get('MAX_DROP')
235 lyx_window_name = os.environ.get('LYX_WINDOW_NAME')
236 screenshot_out = os.environ.get('SCREENSHOT_OUT')
237 lyx_userdir = os.environ.get('LYX_USERDIR')
239 max_loops = os.environ.get('MAX_LOOPS')
240 if max_loops is None:
243 lyx_exe = os.environ.get('LYX_EXE')
247 file_new_command = os.environ.get('FILE_NEW_COMMAND')
248 if file_new_command is None:
249 file_new_command = "\Afn"
251 ResetCommand = os.environ.get('RESET_COMMAND')
252 if ResetCommand is None:
253 ResetCommand = "\[Escape]\[Escape]\[Escape]\[Escape]" + file_new_command
254 #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
256 if lyx_window_name is None:
257 lyx_window_name = 'LyX'
259 print 'outfilename: ' + outfilename + '\n'
260 print 'max_drop: ' + max_drop + '\n'
262 if infilename is None:
263 print 'infilename is None\n'
265 print 'Using x=CommandSource\n'
267 print 'infilename: ' + infilename + '\n'
268 probability_we_drop_a_command = random.uniform(0, float(max_drop))
269 print 'probability_we_drop_a_command: '
270 print '%s' % probability_we_drop_a_command
272 x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
273 print 'Using x=CommandSourceFromFile\n'
275 outfile = open(outfilename, 'w')
277 if not lyx_pid is None:
279 sendKeystring("\Afn", lyx_pid)
281 write_commands = True
285 #os.system('echo -n LOADAVG:; cat /proc/loadavg')
291 outfile.writelines(c + '\n')
294 print "\nIgnoring comment line: " + c
295 elif c[0:9] == 'TestBegin':
297 lyx_pid=os.popen("pidof lyx").read()
299 print "Found running instance(s) of LyX: " + lyx_pid + ": killing them all\n"
300 os.system("killall lyx")
302 os.system("killall -KILL lyx")
304 print "Starting LyX . . ."
305 if lyx_userdir is None:
306 os.system(lyx_exe + c[9:] + "&")
308 os.system(lyx_exe + " -userdir " + lyx_userdir + " " + c[9:] + "&")
310 lyx_pid=os.popen("pidof lyx").read().rstrip()
312 lyx_window_name=os.popen("wmctrl -l -p | grep ' " + str(lyx_pid) + " ' | cut -d ' ' -f 1").read().rstrip()
313 if lyx_window_name != "":
315 print 'lyx_win: ' + lyx_window_name + '\n'
316 print "Waiting for LyX to show up . . ."
318 print 'lyx_pid: ' + lyx_pid + '\n'
319 print 'lyx_win: ' + lyx_window_name + '\n'
320 elif c[0:5] == 'Sleep':
321 print "\nSleeping for " + c[6:] + "\n"
322 time.sleep(float(c[6:]))
323 elif c[0:4] == 'Exec':
325 print "\nExecuting " + cmd + "\n"
329 outfile = open(outfilename + '+', 'w')
331 elif c == 'RaiseLyx':
334 elif c[0:4] == 'KK: ':
336 sendKeystring(c[4:], lyx_pid)
338 ##os.system('killall lyx; sleep 2 ; killall -9 lyx')
339 print 'No path /proc/' + lyx_pid + '/status, exiting'
341 elif c[0:4] == 'KD: ':
342 DELAY = c[4:].rstrip('\n')
343 print 'Setting DELAY to ' + DELAY + '.'
346 sendKeystring(ResetCommand, lyx_pid)
347 elif c[0:6] == 'Assert':
349 print "\nExecuting " + cmd
350 result = os.system(cmd)
351 failed = failed or (result != 0)
352 print "result=" + str(result) + ", failed=" + str(failed)
353 elif c[0:7] == 'TestEnd':
355 print "\nTerminating lyx instance: " + str(lyx_pid) + "\n"
356 os.system("kill -9 " + str(lyx_pid) + "\n");
358 print "Waiting for lyx to die...\n"
361 print "\nExecuting " + cmd
362 result = os.system(cmd)
363 failed = failed or (result != 0)
364 print "result=" + str(result) + ", failed=" + str(failed)
365 elif c[0:4] == 'Lang':
366 lang = c[5:].rstrip()
367 print "\nSetting LANG=" + lang
368 os.environ['LANG'] = lang
370 print "Unrecognised Command '" + c + "'\n"
373 print "Test case terminated: "