This repository has been archived by the owner on Mar 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
test-orng.py
executable file
·156 lines (132 loc) · 6 KB
/
test-orng.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
import mozdevice
import optparse
import os
import sys
import tempfile
class DeviceController(object):
def __init__(self, dimensions, swipe_padding, input_device, orng_path):
self.dimensions = dimensions
self.swipe_padding = swipe_padding
self.dm = mozdevice.DeviceManagerADB()
self.input_device = input_device
self.orng_path = orng_path
def get_drag_event(self, touchstart_x1, touchstart_y1, touchend_x1,
touchend_y1, duration=1000, num_steps=5):
return "drag %s %s %s %s %s %s" % (int(touchstart_x1), int(touchstart_y1),
int(touchend_x1), int(touchend_y1),
num_steps, duration)
def get_sleep_event(self, duration=1.0):
return "sleep %s" % int(float(duration) * 1000.0)
def get_tap_event(self, x, y, times=1):
return "tap %s %s %s 100" % (int(x), int(y), times)
def get_scroll_events(self, direction, numtimes=1, numsteps=10, duration=100):
events = []
x = int(self.dimensions[0] / 2)
ybottom = self.dimensions[1] - self.swipe_padding[3]
ytop = self.swipe_padding[0]
(p1, p2) = ((x, ybottom), (x, ytop))
if direction == "up":
(p1, p2) = (p2, p1)
for i in range(int(numtimes)):
events.append(self.get_drag_event(p1[0], p1[1], p2[0], p2[1], duration,
int(numsteps)))
return events
def get_swipe_events(self, direction, numtimes=1, numsteps=10, duration=100):
events = []
y = (self.dimensions[1] / 2)
(x1, x2) = (self.swipe_padding[2],
self.dimensions[0] - self.swipe_padding[0])
if direction == "left":
(x1, x2) = (x2, x1)
for i in range(int(numtimes)):
events.append(self.get_drag_event(x1, y, x2, y, duration,
int(numsteps)))
return events
def get_pinch_event(self, touch1_x1, touch1_y1, touch1_x2, touch1_y2,
touch2_x1, touch2_y1, touch2_x2, touch2_y2,
numsteps=10, duration=1000):
return "pinch %s %s %s %s %s %s %s %s %s %s" % (touch1_x1, touch1_y1,
touch1_x2, touch1_y2,
touch2_x1, touch2_y1,
touch2_x2, touch2_y2,
numsteps,
duration)
def get_cmd_events(self, cmd, args):
if cmd == "scroll_down":
cmdevents = self.get_scroll_events("down", *args)
elif cmd == "scroll_up":
cmdevents = self.get_scroll_events("up", *args)
elif cmd == "swipe_left":
cmdevents = self.get_swipe_events("left", *args)
elif cmd == "swipe_right":
cmdevents = self.get_swipe_events("right", *args)
elif cmd == "drag":
cmdevents = [self.get_drag_event(*args)]
elif cmd == "tap":
cmdevents = [self.get_tap_event(*args)]
elif cmd == "double_tap":
cmdevents = [self.get_tap_event(*args, times=2)]
elif cmd == "pinch":
cmdevents = [self.get_pinch_event(*args)]
elif cmd == "keydown":
cmdevents = ["keydown %s" % args[0]]
elif cmd == "keyup":
cmdevents = ["keyup %s" % args[0]]
elif cmd == "sleep":
if len(args):
cmdevents = [self.get_sleep_event(duration=args[0])]
else:
cmdevents = [self.get_sleep_event()]
else:
raise Exception("Unknown command")
return cmdevents
def execute_script(self, events):
'''Executes a set of orangutan commands on the device'''
with tempfile.NamedTemporaryFile() as f:
f.write("\n".join(events) + "\n")
f.flush()
remotefilename = os.path.join(self.dm.getDeviceRoot(),
os.path.basename(f.name))
self.dm.pushFile(f.name, remotefilename)
self.dm.shellCheckOutput([self.orng_path, self.input_device,
remotefilename])
self.dm.removeFile(remotefilename)
def execute_command(self, cmd, args):
cmdevents = self.get_cmd_events(cmd, args)
if cmdevents:
self.execute_script(cmdevents)
def main(args=sys.argv[1:]):
parser = optparse.OptionParser(usage="usage: %prog")
parser.add_option("--device-dimensions", dest="device_dimensions",
help="device dimensions in array format [x,y]",
default="[320,480]")
parser.add_option("--swipe-padding", dest="swipe_padding",
help="swipe padding in array format [x1,y1,x2,y2]",
default="[40,40,40,40]")
parser.add_option("--input-device", dest="input_device",
help="input device to use (default: /dev/input/event2)",
default="/dev/input/event2")
parser.add_option("--orng-path", dest="orng_path",
help="path to orng executable (default: /data/local/orng)",
default="/data/local/orng")
options, args = parser.parse_args()
controller = DeviceController(eval(options.device_dimensions),
eval(options.swipe_padding),
options.input_device, options.orng_path)
print "READY"
sys.stdout.flush()
while 1:
try:
line = sys.stdin.readline()
except KeyboardInterrupt:
break
if not line:
break
tokens = line.rstrip().split()
if len(tokens) < 1:
raise Exception("No command")
(cmd, args) = (tokens[0], tokens[1:])
controller.execute_command(cmd, args)
if __name__ == '__main__':
main()