def _check_app_installed(self): # Check that the Mobly Snippet app is installed. out = self._adb.shell('pm list package') if not utils.grep('^package:%s$' % self.package, out): raise jsonrpc_client_base.AppStartError( '%s is not installed on %s' % (self.package, self._adb.serial)) # Check that the app is instrumented. out = self._adb.shell('pm list instrumentation') matched_out = utils.grep( '^instrumentation:%s/%s' % (self.package, _INSTRUMENTATION_RUNNER_PACKAGE), out) if not matched_out: raise jsonrpc_client_base.AppStartError( '%s is installed on %s, but it is not instrumented.' % (self.package, self._adb.serial)) match = re.search('^instrumentation:(.*)\/(.*) \(target=(.*)\)$', matched_out[0]) target_name = match.group(3) # Check that the instrumentation target is installed if it's not the # same as the snippet package. if target_name != self.package: out = self._adb.shell('pm list package') if not utils.grep('^package:%s$' % target_name, out): raise jsonrpc_client_base.AppStartError( 'Instrumentation target %s is not installed on %s' % (target_name, self._adb.serial))
def _read_protocol_line(self): """Reads the next line of instrumentation output relevant to snippets. This method will skip over lines that don't start with 'SNIPPET' or 'INSTRUMENTATION_RESULT'. Returns: (str) Next line of snippet-related instrumentation output, stripped. Raises: jsonrpc_client_base.AppStartError: If EOF is reached without any protocol lines being read. """ while True: line = self._proc.stdout.readline().decode('utf-8') if not line: raise jsonrpc_client_base.AppStartError( self._ad, 'Unexpected EOF waiting for app to start') # readline() uses an empty string to mark EOF, and a single newline # to mark regular empty lines in the output. Don't move the strip() # call above the truthiness check, or this method will start # considering any blank output line to be EOF. line = line.strip() if (line.startswith('INSTRUMENTATION_RESULT:') or line.startswith('SNIPPET ')): self.log.debug( 'Accepted line from instrumentation output: "%s"', line) return line self.log.debug('Discarded line from instrumentation output: "%s"', line)
def start_app_and_connect(self): """Overrides superclass.""" # Check that sl4a is installed out = self._adb.shell('pm list package') if not utils.grep('com.googlecode.android_scripting', out): raise jsonrpc_client_base.AppStartError( '%s is not installed on %s' % (_APP_NAME, self._adb.serial)) # sl4a has problems connecting after disconnection, so kill the apk and # try connecting again. try: self.stop_app() except Exception as e: self.log.warning(e) # Launch the app self.device_port = _DEVICE_SIDE_PORT self._adb.shell(_LAUNCH_CMD % self.device_port) # Try to start the connection (not restore the connectivity). # The function name restore_app_connection is used here is for the # purpose of reusing the same code as it does when restoring the # connection. And we do not want to come up with another function # name to complicate the API. Change the name if necessary. self.restore_app_connection()
def check_app_installed(self): """Overrides superclass.""" out = self._adb.shell('pm list package') if not self._grep('com.googlecode.android_scripting', out): raise jsonrpc_client_base.AppStartError( '%s is not installed on %s' % (self.app_name, self._adb.serial))
def check_app_installed(self): """Overrides superclass.""" if not self._adb_grep_wrapper( "pm list package | grep com.googlecode.android_scripting"): raise jsonrpc_client_base.AppStartError( '%s is not installed on %s' % (self.app_name, self._adb.serial))
def start_app_and_connect(self): """Overrides superclass.""" # Check that sl4a is installed out = self._adb.shell('pm list package') if not utils.grep('com.googlecode.android_scripting', out): raise AppStartError('%s is not installed on %s' % (_APP_NAME, self._adb.serial)) # sl4a has problems connecting after disconnection, so kill the apk and # try connecting again. try: self.stop_app() except Exception as e: self.log.warning(e) # Launch the app self.host_port = utils.get_available_host_port() self.device_port = _DEVICE_SIDE_PORT self._adb.forward( ['tcp:%d' % self.host_port, 'tcp:%d' % self.device_port]) self._adb.shell(_LAUNCH_CMD % self.device_port) # Connect with retry start_time = time.time() expiration_time = start_time + _APP_START_WAIT_TIME started = False while time.time() < expiration_time: self.log.debug('Attempting to start %s.', self.app_name) try: self.connect() started = True break except: self.log.debug('%s is not yet running, retrying', self.app_name, exc_info=True) time.sleep(1) if not started: raise jsonrpc_client_base.AppStartError( '%s failed to start on %s.' % (self.app_name, self._adb.serial)) # Start an EventDispatcher for the current sl4a session event_client = Sl4aClient(self._adb, self.log) event_client.host_port = self.host_port event_client.connect(uid=self.uid, cmd=jsonrpc_client_base.JsonRpcCommand.CONTINUE) self.ed = event_dispatcher.EventDispatcher(event_client) self.ed.start()
def _connect_to_v0(self): self._adb.forward( ['tcp:%d' % self.host_port, 'tcp:%d' % self.device_port]) start_time = time.time() expiration_time = start_time + _APP_START_WAIT_TIME_V0 while time.time() < expiration_time: self.log.debug('Attempting to start %s.', self.package) try: self.connect() return except: self.log.debug( 'v0 snippet %s is not yet running, retrying', self.package, exc_info=True) time.sleep(1) raise jsonrpc_client_base.AppStartError( '%s failed to start on %s.' % (self.package, self._adb.serial))