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')
43 for k in range(97, 123):
46 for k in range(97, 123):
47 keycode[:0] = ["\A" + chr(k)]
49 for k in range(97, 123):
50 keycode[:0] = ["\A" + chr(k)]
52 for k in range(97, 123):
53 keycode[:0] = ["\C" + chr(k)]
55 self.keycode = keycode
60 self.count = self.count + 1
61 if self.count % 200 == 0:
63 elif self.count > self.count_max:
68 keystr = keystr + self.keycode[random.randint(1,
69 len(self.keycode)) - 1]
70 return 'KK: ' + keystr
73 class CommandSourceFromFile(CommandSource):
75 def __init__(self, filename, p):
77 self.infile = open(filename, 'r')
78 self.lines = self.infile.readlines()
82 print p, self.p, 'self.p'
87 # Now we start randomly dropping lines, which we hope are redundant
88 # p is the probability that any given line will be removed
91 if random.uniform(0, 1) < 0.5:
92 print 'randomdrop_independant\n'
93 self.randomdrop_independant()
95 print 'randomdrop_slice\n'
96 self.randomdrop_slice()
97 if screenshot_out is None:
101 self.max_count = max(len(self.lines) + 20, count_atleast)
102 if len(self.lines) < 1:
103 self.lines = linesbak
105 def randomdrop_independant(self):
108 # The next couple of lines are to ensure that at least one line is dropped
110 drop = random.randint(0, len(self.lines) - 1)
112 #p = p - 1 / len(self.lines)
113 origlines = self.lines
116 if random.uniform(0, 1) < self.p:
117 print 'Randomly dropping line ' + l + '\n'
124 def randomdrop_slice(self):
126 if random.uniform(0, 1) < 0.4:
127 lines.append(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
137 del lines[drop_start:drop_end]
141 def getCommand(self):
142 if self.count >= self.max_count:
144 if self.i >= len(self.lines):
145 self.loops = self.loops + 1
146 if self.loops >= int(max_loops):
150 line = self.lines[self.i].rstrip('\n')
151 self.count = self.count + 1
153 #print '\nLine read: <<' + line + '>>\n'
154 sys.stdout.write('r')
160 fname = '/proc/' + lyx_pid + '/status'
161 return os.path.exists(fname)
164 fname = '/proc/' + lyx_pid + '/status'
166 lines = f.readlines()
167 sleeping = lines[1].find('(sleeping)') > 0
169 # print 'LYX_STATE', lines[1] , 'SLEEPING=', sleeping
174 # Interruptible os.system()
175 def intr_system(cmd):
177 if os.WIFSIGNALED(ret):
178 raise KeyboardInterrupt
182 def sendKeystring(keystr, LYX_PID):
184 # print "sending keystring "+keystr+"\n"
186 if not re.match(".*\w.*", keystr):
187 print 'print .' + keystr + '.\n'
189 before_secs = time.time()
190 while lyx_exists() and not lyx_sleeping():
192 sys.stdout.write('.')
194 if time.time() - before_secs > 180:
195 print 'Killing due to freeze (KILL_FREEZE)'
197 # Do profiling, but sysprof has no command line interface?
198 # intr_system("killall -KILL lyx")
201 if not screenshot_out is None:
202 while lyx_exists() and not lyx_sleeping():
204 sys.stdout.write('.')
206 print 'Making Screenshot: ' + screenshot_out + ' OF ' + infilename
208 intr_system('import -window root '+screenshot_out+str(x.count)+".png")
211 actual_delay = key_delay
212 if actual_delay == '':
213 actual_delay = def_delay
215 subprocess.call([xvkbd_exe, "-xsendevent", "-window", lyx_window_name,
216 "-delay", actual_delay, "-text", keystr], stdout = FNULL, stderr = FNULL)
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)
222 def system_retry(num_retry, cmd):
224 rtn = intr_system(cmd)
225 while ( ( i < num_retry ) and ( rtn != 0) ):
227 rtn = intr_system(cmd)
230 print "Command Failed: "+cmd
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+"'")
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')
251 max_loops = os.environ.get('MAX_LOOPS')
252 if max_loops is None:
255 lyx_exe = os.environ.get('LYX_EXE')
259 xvkbd_exe = os.environ.get('XVKBD_EXE')
260 if xvkbd_exe is None:
263 xvkbd_hacked = os.environ.get('XVKBD_HACKED') != None
265 file_new_command = os.environ.get('FILE_NEW_COMMAND')
266 if file_new_command is None:
267 file_new_command = "\Afn"
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"
274 if lyx_window_name is None:
275 lyx_window_name = 'LyX'
277 print 'outfilename: ' + outfilename + '\n'
278 print 'max_drop: ' + max_drop + '\n'
280 if infilename is None:
281 print 'infilename is None\n'
283 print 'Using x=CommandSource\n'
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
290 x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
291 print 'Using x=CommandSourceFromFile\n'
293 outfile = open(outfilename, 'w')
295 if not lyx_pid is None:
297 sendKeystring("\Afn", lyx_pid)
299 write_commands = True
303 #intr_system('echo -n LOADAVG:; cat /proc/loadavg')
309 outfile.writelines(c + '\n')
312 print "\nIgnoring comment line: " + c
313 elif c[0:9] == 'TestBegin':
315 lyx_pid=os.popen("pidof lyx").read()
317 print "Found running instance(s) of LyX: " + lyx_pid + ": killing them all\n"
318 intr_system("killall lyx")
320 intr_system("killall -KILL lyx")
322 print "Starting LyX . . ."
323 if lyx_userdir is None:
324 intr_system(lyx_exe + c[9:] + "&")
326 intr_system(lyx_exe + " -userdir " + lyx_userdir + " " + c[9:] + "&")
328 lyx_pid=os.popen("pidof lyx").read().rstrip()
330 lyx_window_name=os.popen("wmctrl -l -p | grep ' " + str(lyx_pid) + " ' | cut -d ' ' -f 1").read().rstrip()
331 if lyx_window_name != "":
333 print 'lyx_win: ' + lyx_window_name + '\n'
334 print "Waiting for LyX to show up . . ."
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':
343 print "\nExecuting " + cmd + "\n"
347 outfile = open(outfilename + '+', 'w')
349 elif c == 'RaiseLyx':
352 elif c[0:4] == 'KK: ':
354 sendKeystring(c[4:], lyx_pid)
356 ##intr_system('killall lyx; sleep 2 ; killall -9 lyx')
357 print 'No path /proc/' + lyx_pid + '/status, exiting'
359 elif c[0:4] == 'KD: ':
360 key_delay = c[4:].rstrip('\n')
361 print 'Setting DELAY to ' + key_delay + '.'
364 sendKeystring(ResetCommand, lyx_pid)
365 elif c[0:6] == 'Assert':
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':
373 print "\nTerminating lyx instance: " + str(lyx_pid) + "\n"
374 intr_system("kill -9 " + str(lyx_pid) + "\n");
376 print "Waiting for lyx to die...\n"
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
388 print "Unrecognised Command '" + c + "'\n"
391 print "Test case terminated: "