Пример #1
0
    def test_connect_succes(self):
        mock_adb = Mock()
        mock_adb.get_devices.return_value = {'a': 12, 'b': 13, 'c': 123}
        Adb.adb = mock_adb

        Adb.connect(123)

        mock_adb.get_devices.assert_called_once()
Пример #2
0
    def test_success_or_exception_succes(self, logger):
        input_string = 'Success'
        succes_msg = 'action succes'
        fail_msg = 'action fail'

        Adb.success_or_exception(input_string, succes_msg, fail_msg)

        logger.assert_called_once_with(succes_msg)
Пример #3
0
    def test_connect_device_missing(self):
        mock_adb = Mock()
        mock_adb.get_devices.return_value = {'a': 12, 'b': 13}
        Adb.adb = mock_adb

        with pytest.raises(Adb.ConnectionError):
            Adb.connect(123)

        mock_adb.get_devices.assert_called_once()
Пример #4
0
    def test_connect_no_devices(self):
        mock_adb = Mock()
        mock_adb.get_devices.return_value = {}
        Adb.adb = mock_adb

        with pytest.raises(Adb.ConnectionError):
            Adb.connect('123')

        mock_adb.get_devices.assert_called_once()
Пример #5
0
    def test_setup_succes_default_path(self, adb):
        adb_instance = MagicMock()
        adb_instance._ADB__error = None
        adb.return_value = adb_instance

        Adb.setup()

        assert isinstance(Adb.adb, MagicMock)
        adb.assert_called_once_with(adb_path='adb')
Пример #6
0
    def test_success_or_exception_exception(self, logger):
        input_string = 'fail'
        succes_msg = 'action succes'
        fail_msg = 'action fail'

        with pytest.raises(Adb.AdbError):
            Adb.success_or_exception(input_string, succes_msg, fail_msg)

        logger.assert_called_once_with(
            fail_msg + '\nMessage returned:\n{}'.format(input_string))
Пример #7
0
    def test_shell_su_error(self):
        mock_adb = Mock()
        mock_adb.shell_command.return_value = "su_error"
        Adb.adb = mock_adb

        with pytest.raises(Adb.AdbError):
            Adb.shell_su(123, "test_command_su")

        expected_calls = [
            call.set_target_by_name(123),
            call.shell_command('su -c \'test_command_su\'')
        ]
        assert mock_adb.mock_calls == expected_calls
Пример #8
0
    def test_list_apps(self, adb_shell):
        adb_shell.return_value = 'package:com.app.1\npackage:com.app.2\npackage:com.app.3'

        result = Adb.list_apps(123)

        adb_shell.assert_called_once_with(123, 'pm list packages')
        assert len(result) == 3
        assert 'com.app.1' in result
        assert 'com.app.2' in result
        assert 'com.app.3' in result
Пример #9
0
    def test_clear_app_data(self, s_or_e):
        mock_adb = Mock()
        mock_adb.shell_command.return_value = 'succes'
        Adb.adb = mock_adb
        device_id = 123
        name = 'app_name'

        manager = Mock()
        manager.attach_mock(s_or_e, "s_or_e_mock")
        manager.mock_adb = mock_adb

        Adb.clear_app_data(device_id, name)

        expected_calls = [
            call.mock_adb.set_target_by_name(123),
            call.mock_adb.shell_command('pm clear app_name'),
            call.s_or_e_mock(
                'succes', '{}: Data of "{}" cleared'.format(device_id, name),
                '{}: Failed to clear data for "{}"'.format(device_id, name))
        ]
        assert manager.mock_calls == expected_calls
Пример #10
0
    def test_uninstall_keep_data(self, s_or_e):
        mock_adb = Mock()
        mock_adb.uninstall.return_value = 'succes'
        Adb.adb = mock_adb
        device_id = 123
        name = 'app_name'

        manager = Mock()
        manager.attach_mock(s_or_e, "s_or_e_mock")
        manager.mock_adb = mock_adb

        Adb.uninstall(device_id, name, True)

        expected_calls = [
            call.mock_adb.set_target_by_name(123),
            call.mock_adb.uninstall(package=name, keepdata=False),
            call.s_or_e_mock(
                'succes', '{}: "{}" uninstalled'.format(device_id, name),
                '{}: Failed to uninstall "{}"'.format(device_id, name))
        ]
        assert manager.mock_calls == expected_calls
Пример #11
0
    def test_shell_su_succes(self):
        mock_adb = Mock()
        mock_adb.shell_command.return_value = "su_succes         "
        Adb.adb = mock_adb
        result = Adb.shell_su(123, "test_command_su")

        expected_calls = [
            call.set_target_by_name(123),
            call.shell_command('su -c \'test_command_su\'')
        ]
        assert mock_adb.mock_calls == expected_calls
        assert result == 'su_succes'
Пример #12
0
    def test_install_not_all_permissions(self):
        mock_adb = Mock()
        mock_adb._ADB__output = 'succes'
        Adb.adb = mock_adb
        device_id = 123
        apk = 'test_apk.apk'

        result = Adb.install(device_id, apk, all_permissions=False)

        assert result == 'succes'
        expected_calls = [
            call.set_target_by_name(device_id),
            call.run_cmd('install -r {}'.format(apk))
        ]
        assert mock_adb.mock_calls == expected_calls
Пример #13
0
    def test_logcat_no_regex(self):
        mock_adb = Mock()
        mock_adb.get_logcat.return_value = 'get_logcat output'
        Adb.adb = mock_adb

        device_id = 123

        result = Adb.logcat(device_id)

        assert result == 'get_logcat output'
        expected_calls = [
            call.set_target_by_name(device_id),
            call.get_logcat(lcfilter='-d')
        ]
        assert mock_adb.mock_calls == expected_calls
Пример #14
0
    def test_logcat_with_regex(self):
        mock_adb = Mock()
        mock_adb.get_logcat.return_value = 'get_logcat output'
        Adb.adb = mock_adb

        test_regex = '[a-zA-Z]+'
        device_id = 123

        result = Adb.logcat(device_id, test_regex)

        assert result == 'get_logcat output'
        expected_calls = [
            call.set_target_by_name(device_id),
            call.get_logcat(lcfilter='-d -e {}'.format(test_regex))
        ]
        assert mock_adb.mock_calls == expected_calls
Пример #15
0
    def test_push(self):
        mock_adb = Mock()
        mock_adb._ADB__output = 'push output'
        Adb.adb = mock_adb

        device_id = 123
        local_path = 'local/path'
        remote_path = 'remote/path'

        result = Adb.push(device_id, local_path, remote_path)

        assert result == 'push output'
        expected_calls = [
            call.set_target_by_name(device_id),
            call.run_cmd('push {} {}'.format(local_path, remote_path))
        ]
        assert mock_adb.mock_calls == expected_calls
Пример #16
0
    def test_pull_error_with_bytes_in(self):
        mock_adb = Mock()
        mock_adb._ADB__output = 'pull output'
        mock_adb._ADB__error = 'bytes in error'
        Adb.adb = mock_adb

        device_id = 123
        local_path = 'local/path'
        remote_path = 'remote/path'

        result = Adb.pull(device_id, remote_path, local_path)

        assert result == 'bytes in error'
        expected_calls = [
            call.set_target_by_name(device_id),
            call.run_cmd('pull {} {}'.format(remote_path, local_path))
        ]
        assert mock_adb.mock_calls == expected_calls
Пример #17
0
 def test_setup_error(self, adb):
     adb_instance = MagicMock()
     adb_instance._ADB__error = True
     adb.return_value = adb_instance
     with pytest.raises(Adb.AdbError):
         Adb.setup()