Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Change way scrolling works to get around problem with monkeyrunner

Monkeyrunner's network mode doesn't add touch down times to the event
it sends, which causes problems when we're trying to scroll: Android's
gesture recognizer thinks we're doing a long tap, which can cause the
"save image" dialog to pop up.

Let's do the same thing we did with double tap events: upload a script
with touch down times to the device, and get the monkey tool to execute
it standalone.
  • Loading branch information...
commit 55d63960dc0a5090cee00fe917a851db082ee0fd 1 parent afa5289
William Lachance authored April 17, 2012
67  bin/devicecontroller.py
@@ -63,40 +63,55 @@ def _get_display_dimensions(self):
63 63
         height = int(self._send_cmd("getvar display.height"))
64 64
         return [width, height]
65 65
 
  66
+    def tap(self, x, y):
  67
+        self._send_cmd("touch down %s %s" % (x, y))
  68
+        self._send_cmd("touch up %s %s" % (x, y))
  69
+
  70
+    def _execute_script(self, script):
  71
+        '''Executes a set of monkey commands on the device'''
  72
+
  73
+        f = tempfile.NamedTemporaryFile()
  74
+        f.write("type= raw events\ncount= %s\nspeed= 0.0\nstart data >>\n" % len(script.split('\n')))
  75
+        f.write(script)
  76
+        f.flush()
  77
+        print "FLUSHED: %s" % time.time()
  78
+        remotefilename = '%s/%s' % ('/mnt/sdcard/tests',
  79
+                                    os.path.basename(f.name))
  80
+        print time.time()
  81
+        self._droid.pushFile(f.name, remotefilename)
  82
+        print "PUSHED: %s" % time.time()
  83
+        buf = StringIO.StringIO()
  84
+        self._droid.shell(["monkey", "-f", remotefilename, "1"], buf)
  85
+
66 86
     def drag(self, touch_start, touch_end, duration=1.0, num_steps=5):
67  
-        self._send_cmd("touch down %s %s" % touch_start)
  87
+        script = ""
  88
+        script += ("createDispatchPointer(0,0,0,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
  89
+                   "0)\n" % touch_start)
68 90
         delta = ((touch_end[0] - touch_start[0]) / num_steps,
69 91
                  (touch_end[1] - touch_start[1]) / num_steps)
70 92
         for i in range(num_steps):
71 93
             current = (touch_start[0] + delta[0]*i,
72 94
                        touch_start[1] + delta[1]*i)
73  
-            self._send_cmd("touch move %s %s" % current)
74  
-            time.sleep(duration/num_steps)
75  
-        self._send_cmd("touch up %s %s" % touch_end)
76  
-
77  
-    def tap(self, x, y):
78  
-        self._send_cmd("touch down %s %s" % (x, y))
79  
-        self._send_cmd("touch up %s %s" % (x, y))
  95
+            script += ("createDispatchPointer(0,0,2,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
  96
+                       "0)\n" % current)
  97
+            script += ("UserWait(%s)\n" % int((duration / num_steps) * 1000.0))
  98
+        script += ("createDispatchPointer(0,0,1,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
  99
+                   "0)\n" % touch_end)
  100
+        print time.time()
  101
+        self._execute_script(script)
  102
+        print time.time()
80 103
 
81 104
     def double_tap(self, x, y):
82 105
         # HACK: monkeyrunner doesn't set downTime correctly when in network
83 106
         # mode, which Android's gesture recognizer needs to detect a double
84 107
         # tap properly. as a workaround, use monkey's script functionality
85  
-        f = tempfile.NamedTemporaryFile()
86  
-        f.write("type= raw events\ncount= 4\nspeed= 1.0\nstart data >>\n")
  108
+        script = ""
87 109
         for i in range(0,2):
88  
-            f.write("createDispatchPointer(0,0,0,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
89  
-                    "0)\n" % (x,y))
90  
-            f.write("createDispatchPointer(0,0,1,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
91  
-                    "0)\n" % (x,y))
92  
-        f.flush()
93  
-        remotefilename = '%s/%s' % (self._droid.getDeviceRoot(),
94  
-                                    os.path.basename(f.name))
95  
-        self._droid.pushFile(f.name, remotefilename)
96  
-        buf = StringIO.StringIO()
97  
-        self._droid.shell(["monkey", "-f", remotefilename, "1"], buf)
98  
-        print remotefilename
99  
-        print "RESULT: %s" % buf.read()
  110
+            script += ("createDispatchPointer(0,0,0,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
  111
+                          "0)\n" % (x,y))
  112
+            script += ("createDispatchPointer(0,0,1,%s,%s,1.0,1.0,0,0.0,0.0,-1,"
  113
+                       "0)\n" % (x,y))
  114
+        self._execute_script(script)
100 115
 
101 116
     def scroll_down(self):
102 117
         x = int(self.dimensions[0] / 2)
@@ -104,6 +119,12 @@ def scroll_down(self):
104 119
         ytop = 120
105 120
         self.drag((x,ybottom), (x,ytop), 0.1, 5)
106 121
 
  122
+    def scroll_up(self):
  123
+        x = int(self.dimensions[0] / 2)
  124
+        ybottom = self.dimensions[1] - 100
  125
+        ytop = 120
  126
+        self.drag((x,ytop), (x,ybottom), 0.1, 5)
  127
+
107 128
 connection = MonkeyConnection()
108 129
 
109 130
 print "READY"
@@ -129,6 +150,8 @@ def scroll_down(self):
129 150
         break
130 151
     elif cmd == "scroll_down":
131 152
         connection.scroll_down()
  153
+    elif cmd == "scroll_up":
  154
+        connection.scroll_up()
132 155
     elif cmd == "tap":
133 156
         connection.tap(*params)
134 157
     elif cmd == "double_tap":
1  src/dashboard/index.html
@@ -109,6 +109,7 @@
109 109
           </form>
110 110
         </div>
111 111
         <p><b>Hint</b>: <i>Click on the datapoints to see a video of the testrun!</i></p>
  112
+        <p><b>Note</b>: Scrolling logic changed on April 17. All tests involving scrolling will post slightly different results after that date.</p>
112 113
       </div>
113 114
 
114 115
       <div class="span1">

0 notes on commit 55d6396

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