Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Instead of monkeyrunner, create our own client which uses the protocol

Seems to be some socket issues with the upstream monkeyrunner, sigh...
  • Loading branch information...
commit 8d034212bc38c1fc80b9fe792c0b06919c74141a 1 parent 1f4e4ee
William Lachance authored March 22, 2012
78  bin/devicecontroller.py
@@ -6,24 +6,78 @@
6 6
 # part of the tools provided with the Android SDK (see:
7 7
 # http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html)
8 8
 
  9
+import select
  10
+import socket
  11
+import subprocess
9 12
 import sys
10 13
 import time
11  
-from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
12 14
 
13  
-class Device(object):
  15
+import mozdevice
14 16
 
15  
-    def __init__(self):
16  
-        self.device = MonkeyRunner.waitForConnection()
17  
-        self.width = int(self.device.getProperty('display.width'))
18  
-        self.height = int(self.device.getProperty('display.height'))
  17
+class MonkeyConnection(object):
  18
+
  19
+    def __init__(self, port=9999):
  20
+        # kill any existing instances of the monkey process
  21
+        droid = mozdevice.DroidADB()
  22
+        droid.killProcess('app_process')
  23
+
  24
+        subprocess.check_call(["adb", "forward", "tcp:%s" % port, "tcp:%s" % port])
  25
+        p = subprocess.Popen(["adb", "shell", "monkey", "--port", str(port)])
  26
+
  27
+        connected = False
  28
+        tries = 0
  29
+        while not connected and tries < 20:
  30
+            time.sleep(0.5)
  31
+            try:
  32
+                self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  33
+                self._sock.connect(('localhost', port))
  34
+                self.dimensions = self._get_display_dimensions()
  35
+                connected = True
  36
+            except Exception, e:
  37
+                print "Can't connect to localhost:%s, retrying..." % port
  38
+            tries+=1
  39
+
  40
+        if not connected:
  41
+            raise Exception("Could not open monkey connection!")
  42
+        print "Connected!"
  43
+        time.sleep(1)
  44
+
  45
+    def _send_cmd(self, cmd):
  46
+        select.select([], [self._sock], [])
  47
+        sent = self._sock.send("%s\r\n" % cmd)
  48
+        ret = None
  49
+        while not ret:
  50
+            select.select([self._sock], [], [])
  51
+            ret = self._sock.recv(1024)
  52
+        if not ret.startswith("OK"):
  53
+            raise Exception("Exception running command '%s'. Got: %s" % (cmd, ret))
  54
+        if len(ret) > 3:
  55
+            return ret[3:]
  56
+        return ""
  57
+
  58
+    def _get_display_dimensions(self):
  59
+        width = int(self._send_cmd("getvar display.width"))
  60
+        height = int(self._send_cmd("getvar display.height"))
  61
+        return [width, height]
  62
+
  63
+    def drag(self, touch_start, touch_end, duration=1.0, num_steps=5):
  64
+        self._send_cmd("touch down %s %s" % touch_start)
  65
+        delta = ((touch_end[0] - touch_start[0]) / num_steps,
  66
+                 (touch_end[1] - touch_start[1]) / num_steps)
  67
+        for i in range(num_steps):
  68
+            current = (touch_start[0] + delta[0]*i,
  69
+                       touch_start[1] + delta[1]*i)
  70
+            self._send_cmd("touch move %s %s" % current)
  71
+            time.sleep(duration/num_steps)
  72
+        self._send_cmd("touch up %s %s" % touch_end)
19 73
 
20 74
     def scroll_down(self):
21  
-        x = int(self.width / 2)
22  
-        ybottom = self.height - 100
23  
-        ytop = 200
24  
-        self.device.drag((x,ybottom), (x, ytop), 0.1, 5)
  75
+        x = int(self.dimensions[0] / 2)
  76
+        ybottom = self.dimensions[1] - 100
  77
+        ytop = 120
  78
+        self.drag((x,ybottom), (x,ytop), 0.1, 5)
25 79
 
26  
-device = Device()
  80
+connection = MonkeyConnection()
27 81
 
28 82
 print "READY"
29 83
 sys.stdout.flush()
@@ -47,7 +101,7 @@ def scroll_down(self):
47 101
         print "QUIT: %s" % time.time()
48 102
         break
49 103
     elif cmd == "scroll_down":
50  
-        device.scroll_down()
  104
+        connection.scroll_down()
51 105
     elif cmd == "sleep":
52 106
         sleeptime = 1
53 107
         if len(params) > 0:
2  bin/runtest.py
@@ -74,7 +74,7 @@ def __init__(self, capture_metadata, capture_file, controller):
74 74
         # open up a monkeyrunner process on startup, wait for it to give a
75 75
         # line so we don't have to wait
76 76
         # for it later (note that not all tests use monkeyrunner)
77  
-        args = ['monkeyrunner', os.path.join(BINDIR, 'devicecontroller.py')]
  77
+        args = ['python', os.path.join(BINDIR, 'devicecontroller.py')]
78 78
         self.monkey_proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
79 79
         line = self.monkey_proc.stdout.readline()
80 80
 

0 notes on commit 8d03421

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