Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Bug 749472 - Execute commands via one big monkey script

Before we were uploading and processing individual scripts. The problem with
this approach is that there was inevitably some latency involved in the process
of uploading files, etc. which could affect results.
  • Loading branch information...
commit 686882d32fb25a700afec35c39e53a73658688e3 1 parent 7158800
William Lachance authored May 24, 2012
9  bin/runtest.py
@@ -110,11 +110,10 @@ def input(self, request):
110 110
                 "'%s'" % (commandset, self.device.model)
111 111
             sys.exit(1)
112 112
 
113  
-        print "Executing commands '%s' for device '%s'" \
114  
-            " (time: %s, framenum: %s)" % (commandset, self.device.model,
115  
-                                           time.time(), self.start_frame)
116  
-        for cmdtokens in self.actions[commandset][self.device.model]:
117  
-            self.device.executeCommand(cmdtokens[0], cmdtokens[1:])
  113
+        print "Executing commands '%s' for device '%s' (time: %s, framenum: %s)" % (
  114
+            commandset, self.device.model, time.time(), self.start_frame)
  115
+
  116
+        self.device.executeCommands(self.actions[commandset][self.device.model])
118 117
 
119 118
         if self.capture_file:
120 119
             self.end_frame = self.capture_controller.capture_framenum()
90  src/eideticker/eideticker/device.py
@@ -37,7 +37,7 @@ def _init(self):
37 37
 
38 38
         # Hack: this gets rid of the "finished charging" modal dialog that the
39 39
         # LG G2X sometimes brings up
40  
-        self.tap(240, 617)
  40
+        self.executeCommand("tap", [240, 617])
41 41
 
42 42
     # FIXME: make this part of devicemanager
43 43
     def _shellCheckOutput(self, args):
@@ -47,11 +47,11 @@ def _shellCheckOutput(self, args):
47 47
             raise Exception("Non-zero return code for command: %s" % args)
48 48
         return str(buf.getvalue()[0:-1]).rstrip()
49 49
 
50  
-    def _executeScript(self, script):
  50
+    def _executeScript(self, events):
51 51
         '''Executes a set of monkey commands on the device'''
52 52
         f = tempfile.NamedTemporaryFile()
53  
-        f.write("type= raw events\ncount= %s\nspeed= 0.0\nstart data >>\n" % len(script.split('\n')))
54  
-        f.write(script)
  53
+        f.write("type= raw events\ncount= %s\nspeed= 1.0\nstart data >>\n" % len(events))
  54
+        f.write("\n".join(events) + "\n")
55 55
         f.flush()
56 56
         remotefilename = os.path.join(self.getDeviceRoot(),
57 57
                                       os.path.basename(f.name))
@@ -71,66 +71,78 @@ def clear_logcat(self):
71 71
     def get_logcat(self, args):
72 72
         return self._shellCheckOutput(["logcat", "-d"] + args)
73 73
 
74  
-    def drag(self, touch_start, touch_end, duration=1.0, num_steps=5):
75  
-        script = ""
76  
-        script += ("createDispatchPointer(0,0,0,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
77  
-                   "0)\n" % touch_start)
  74
+    def _getDragEvents(self, touch_start, touch_end, duration=1.0, num_steps=5):
  75
+        events = []
  76
+        events.append("createDispatchPointer(0,0,0,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
  77
+                      "0)" % touch_start)
  78
+
78 79
         delta = ((touch_end[0] - touch_start[0]) / num_steps,
79 80
                  (touch_end[1] - touch_start[1]) / num_steps)
80 81
         for i in range(num_steps):
81 82
             current = (touch_start[0] + delta[0]*i,
82 83
                        touch_start[1] + delta[1]*i)
83  
-            script += ("createDispatchPointer(0,0,2,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
84  
-                       "0)\n" % current)
85  
-            script += ("UserWait(%s)\n" % int((duration / num_steps) * 1000.0))
86  
-        script += ("createDispatchPointer(0,0,1,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
87  
-                   "0)\n" % touch_end)
88  
-        self._executeScript(script)
89  
-
90  
-    def tap(self, x, y, times=1):
91  
-        script = ""
  84
+            events.append("createDispatchPointer(0,0,2,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
  85
+                          "0)" % current)
  86
+            events.append("UserWait(%s)" % int((duration / num_steps) * 1000.0))
  87
+        events.append("createDispatchPointer(0,0,1,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
  88
+                      "0)" % touch_end)
  89
+        events.append("UserWait(250)")
  90
+        return events
  91
+
  92
+    def _getSleepEvent(self, duration=1.0):
  93
+        return "UserWait(%s)" % int(float(duration) * 1000.0)
  94
+
  95
+    def _getTapEvents(self, x, y, times=1):
  96
+        events = []
92 97
         for i in range(0, times):
93  
-            script += ("createDispatchPointer(0,0,0,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
94  
-                          "0)\n" % (x,y))
95  
-            script += ("createDispatchPointer(0,0,1,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
96  
-                       "0)\n" % (x,y))
97  
-        self._executeScript(script)
  98
+            events.append("createDispatchPointer(0,0,0,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
  99
+                          "0)" % (x,y))
  100
+            events.append("createDispatchPointer(0,0,1,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
  101
+                          "0)" % (x,y))
  102
+        return events
98 103
 
99  
-    def scrollDown(self):
  104
+    def _getScrollDownEvents(self):
100 105
         x = int(self.dimensions[0] / 2)
101 106
         ybottom = self.dimensions[1] - 100
102 107
         ytop = 200
103  
-        self.drag((x,ybottom), (x,ytop), 0.1, 10)
  108
+        return self._getDragEvents((x,ybottom), (x,ytop), 0.1, 10)
104 109
 
105  
-    def scrollUp(self):
  110
+    def _getScrollUpEvents(self):
106 111
         x = int(self.dimensions[0] / 2)
107 112
         ybottom = self.dimensions[1] - 100
108 113
         ytop = 200
109  
-        self.drag((x,ytop), (x,ybottom), 0.1, 10)
  114
+        return self._getDragEvents((x,ytop), (x,ybottom), 0.1, 10)
110 115
 
111  
-    def executeCommand(self, cmd, args):
112  
-        if cmd == "quit":
113  
-            pass # for backwards compatibility only
114  
-        elif cmd == "scroll_down":
115  
-            self.scrollDown()
  116
+    def _getCmdEvents(self, cmd, args):
  117
+        if cmd == "scroll_down":
  118
+            cmdevents = self._getScrollDownEvents()
116 119
         elif cmd == "scroll_up":
117  
-            self.scrollUp()
  120
+            cmdevents = self._getScrollUpEvents()
118 121
         elif cmd == "tap":
119  
-            self.tap(*args)
  122
+            cmdevents = self._getTapEvents(*args)
120 123
         elif cmd == "double_tap":
121  
-            self.tap(*args, times=2)
  124
+            cmdevents = self._getTapEvents(*args, times=2)
122 125
         elif cmd == "sleep":
123  
-            sleeptime = 1
124  
-            if len(args) > 0:
125  
-                sleeptime = int(args[0])
126  
-            time.sleep(sleeptime)
  126
+            if len(args):
  127
+                cmdevents = [self._getSleepEvent(duration=args[0])]
  128
+            else:
  129
+                cmdevents = [self._getSleepEvent()]
127 130
         else:
128 131
             raise Exception("Unknown command")
129 132
 
  133
+        return cmdevents
  134
+
  135
+    def executeCommand(self, cmd, args):
  136
+        cmdevents = self._getCmdEvents(cmd, args)
  137
+        if cmdevents:
  138
+            self._executeScript(cmdevents)
  139
+
130 140
     def executeCommands(self, cmds):
  141
+        cmdevents = []
131 142
         for cmd in cmds:
132 143
             (cmd, args) = (cmd[0], cmd[1:])
133  
-            self.executeCommand(cmd, args)
  144
+            cmdevents.extend(self._getCmdEvents(cmd, args))
  145
+        self._executeScript(cmdevents)
134 146
 
135 147
 
136 148
 class DroidADB(mozdevice.DroidADB, EidetickerMixin):

0 notes on commit 686882d

Please sign in to comment.
Something went wrong with that request. Please try again.