Ejemplo n.º 1
0
 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 AppStartPreCheckError(self._ad,
                                     '%s is not installed.' % self.package)
     # 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 AppStartPreCheckError(
             self._ad,
             '%s is installed, but it is not instrumented.' % self.package)
     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 AppStartPreCheckError(
                 self._ad, 'Instrumentation target %s is not installed.' %
                 target_name)
Ejemplo n.º 2
0
 def _check_app_installed(self):
     # Check that the Mobly Snippet app is installed for the current user.
     out = self._adb.shell(f'pm list package --user {self.user_id}')
     if not utils.grep('^package:%s$' % self.package, out):
         raise AppStartPreCheckError(
             self._ad,
             f'{self.package} is not installed for user {self.user_id}.')
     # Check that the app is instrumented.
     out = self._adb.shell('pm list instrumentation')
     matched_out = utils.grep(
         f'^instrumentation:{self.package}/{_INSTRUMENTATION_RUNNER_PACKAGE}',
         out)
     if not matched_out:
         raise AppStartPreCheckError(
             self._ad,
             f'{self.package} is installed, but it is not instrumented.')
     match = re.search(r'^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(f'pm list package --user {self.user_id}')
         if not utils.grep('^package:%s$' % target_name, out):
             raise AppStartPreCheckError(
                 self._ad,
                 f'Instrumentation target {target_name} is not installed for user '
                 f'{self.user_id}.')
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
    def _validate_snippet_app_on_device(self):
        """Validates the Mobly Snippet app is available on the device.

    To run as an instrumentation test, the Mobly Snippet app must already be
    installed and instrumented on the Android device.

    Raises:
      errors.ServerStartPreCheckError: if the server app is not installed
        for the current user.
    """
        # Validate that the Mobly Snippet app is installed for the current user.
        out = self._adb.shell(f'pm list package --user {self.user_id}')
        if not utils.grep(f'^package:{self.package}$', out):
            raise errors.ServerStartPreCheckError(
                self._device,
                f'{self.package} is not installed for user {self.user_id}.')

        # Validate that the app is instrumented.
        out = self._adb.shell('pm list instrumentation')
        matched_out = utils.grep(
            f'^instrumentation:{self.package}/{_INSTRUMENTATION_RUNNER_PACKAGE}',
            out)
        if not matched_out:
            raise errors.ServerStartPreCheckError(
                self._device,
                f'{self.package} is installed, but it is not instrumented.')
        match = re.search(r'^instrumentation:(.*)\/(.*) \(target=(.*)\)$',
                          matched_out[0])
        target_name = match.group(3)
        # Validate 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(f'pm list package --user {self.user_id}')
            if not utils.grep(f'^package:{target_name}$', out):
                raise errors.ServerStartPreCheckError(
                    self._device,
                    f'Instrumentation target {target_name} is not installed for user '
                    f'{self.user_id}.')
Ejemplo n.º 5
0
    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()