def testMethodDecoratorTranslatesOldExceptions(self):
        test_obj = self._MethodDecoratorTestObject(self)

        exception_desc = 'Old response timeout error'
        with self.assertRaises(device_errors.CommandTimeoutError) as e:
            test_obj.alwaysRaisesProvidedException(
                old_errors.WaitForResponseTimedOutError(exception_desc))
        self.assertEquals(exception_desc, str(e.exception))

        exception_desc = 'Old device error'
        with self.assertRaises(device_errors.DeviceUnreachableError) as e:
            test_obj.alwaysRaisesProvidedException(
                old_errors.DeviceUnresponsiveError(exception_desc))
        self.assertEquals(exception_desc, str(e.exception))
    def testExplicitDecoratorTranslatesOldExceptions(self):
        """Tests that the explicit decorator translates old exceptions."""
        @decorators.WithExplicitTimeoutAndRetries(30, 10)
        def alwaysRaisesProvidedException(exception):
            raise exception

        exception_desc = 'Old response timeout error'
        with self.assertRaises(device_errors.CommandTimeoutError) as e:
            alwaysRaisesProvidedException(
                old_errors.WaitForResponseTimedOutError(exception_desc))
        self.assertEquals(exception_desc, str(e.exception))

        exception_desc = 'Old device error'
        with self.assertRaises(device_errors.DeviceUnreachableError) as e:
            alwaysRaisesProvidedException(
                old_errors.DeviceUnresponsiveError(exception_desc))
        self.assertEquals(exception_desc, str(e.exception))
    def RunShellCommand(self, command, timeout_time=20, log_result=False):
        """Send a command to the adb shell and return the result.

    Args:
      command: String containing the shell command to send. Must not include
               the single quotes as we use them to escape the whole command.
      timeout_time: Number of seconds to wait for command to respond before
        retrying, used by AdbInterface.SendShellCommand.
      log_result: Boolean to indicate whether we should log the result of the
                  shell command.

    Returns:
      list containing the lines of output received from running the command
    """
        logging.info('>>> $' + command)
        if "'" in command: logging.warning(command + " contains ' quotes")
        result = self._adb.SendShellCommand("'%s'" % command,
                                            timeout_time).splitlines()
        if ['error: device not found'] == result:
            raise errors.DeviceUnresponsiveError('device not found')
        if log_result:
            logging.info('\n>>> '.join(result))
        return result
    def FileExistsOnDevice(self, file_name):
        """Checks whether the given file exists on the device.

    Args:
      file_name: Full path of file to check.

    Returns:
      True if the file exists, False otherwise.
    """
        assert '"' not in file_name, 'file_name cannot contain double quotes'
        try:
            status = self._adb.SendShellCommand('\'test -e "%s"; echo $?\'' %
                                                (file_name))
            if 'test: not found' not in status:
                return int(status) == 0

            status = self._adb.SendShellCommand(
                '\'ls "%s" >/dev/null 2>&1; echo $?\'' % (file_name))
            return int(status) == 0
        except ValueError:
            if IsDeviceAttached(self._device):
                raise errors.DeviceUnresponsiveError('Device may be offline.')

            return False