1 #!/usr/bin/env monkeyrunner 2 # Copyright 2010, The Android Open Source Project 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 import sys 17 from com.android.monkeyrunner import MonkeyRunner 18 19 # The format of the file we are parsing is very carfeully constructed. 20 # Each line corresponds to a single command. The line is split into 2 21 # parts with a | character. Text to the left of the pipe denotes 22 # which command to run. The text to the right of the pipe is a python 23 # dictionary (it can be evaled into existence) that specifies the 24 # arguments for the command. In most cases, this directly maps to the 25 # keyword argument dictionary that could be passed to the underlying 26 # command. 27 28 # Lookup table to map command strings to functions that implement that 29 # command. 30 CMD_MAP = { 31 'TOUCH': lambda dev, arg: dev.touch(**arg), 32 'DRAG': lambda dev, arg: dev.drag(**arg), 33 'PRESS': lambda dev, arg: dev.press(**arg), 34 'TYPE': lambda dev, arg: dev.type(**arg), 35 'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg) 36 } 37 38 # Process a single file for the specified device. 39 def process_file(fp, device): 40 for line in fp: 41 (cmd, rest) = line.split('|') 42 try: 43 # Parse the pydict 44 rest = eval(rest) 45 except: 46 print 'unable to parse options' 47 continue 48 49 if cmd not in CMD_MAP: 50 print 'unknown command: ' + cmd 51 continue 52 53 CMD_MAP[cmd](device, rest) 54 55 56 def main(): 57 file = sys.argv[1] 58 fp = open(file, 'r') 59 60 device = MonkeyRunner.waitForConnection() 61 62 process_file(fp, device) 63 fp.close(); 64 65 66 if __name__ == '__main__': 67 main() 68 69 70 71