Ejemplo n.º 1
0
def test_error_handling(mock_sdk, tmp_path, name, exception):
    """ADB.command() can parse errors returned by adb."""
    # Set up a mock command with a subprocess module that has with sample data loaded.
    adb_samples = Path(__file__).parent / "adb_errors"
    with (adb_samples / (name + ".out")).open("r") as adb_output_file:
        with (adb_samples / (name + ".returncode")).open(
                encoding="utf-8") as returncode_file:
            mock_sdk.command.subprocess.check_output.side_effect = (
                subprocess.CalledProcessError(
                    returncode=int(returncode_file.read().strip()),
                    cmd=["ignored"],
                    output=adb_output_file.read(),
                ))

    # Create an ADB instance and invoke run()
    adb = ADB(mock_sdk, "exampleDevice")
    with pytest.raises(exception):
        adb.run("example", "command")

    # Check that adb was invoked as expected
    mock_sdk.command.subprocess.check_output.assert_called_once_with(
        [
            os.fsdecode(tmp_path / "sdk" / "platform-tools" / "adb"),
            "-s",
            "exampleDevice",
            "example",
            "command",
        ],
        stderr=subprocess.STDOUT,
    )
Ejemplo n.º 2
0
def test_adb_failure(mock_sdk):
    """If adb logcat fails, the error is caught."""
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(side_effect=subprocess.CalledProcessError(
        returncode=1, cmd="adb logcat"))

    with pytest.raises(BriefcaseCommandError):
        adb.clear_log()
Ejemplo n.º 3
0
def test_invalid_device(mock_sdk):
    """If the device doesn't exist, the error is caught."""
    # Use real `adb` output from launching an activity that does not exist.
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(
        side_effect=InvalidDeviceError("device", "exampleDevice"))

    with pytest.raises(InvalidDeviceError):
        adb.clear_log()
Ejemplo n.º 4
0
def test_booted(mock_sdk, capsys):
    "A booted device returns true"
    # Mock out the adb response for an emulator
    adb = ADB(mock_sdk, "deafbeefcafe")
    adb.run = MagicMock(return_value="1\n")

    # Invoke avd_name
    assert adb.has_booted()

    # Validate call parameters.
    adb.run.assert_called_once_with("shell", "getprop", "sys.boot_completed")
Ejemplo n.º 5
0
def test_emulator(mock_sdk, capsys):
    "Invoking `avd_name()` on an emulator returns the AVD."
    # Mock out the adb response for an emulator
    adb = ADB(mock_sdk, "deafbeefcafe")
    adb.run = MagicMock(return_value="exampledevice\nOK\n")

    # Invoke avd_name
    assert adb.avd_name() == 'exampledevice'

    # Validate call parameters.
    adb.run.assert_called_once_with("emu", "avd", "name")
Ejemplo n.º 6
0
def test_invalid_device(mock_sdk):
    "If the device doesn't exist, the error is caught."
    # Use real `adb` output from launching an activity that does not exist.
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(
        side_effect=InvalidDeviceError('device', 'exampleDevice'))

    with pytest.raises(InvalidDeviceError):
        adb.start_app("com.example.sample.package",
                      "com.example.sample.activity")
Ejemplo n.º 7
0
def test_device(mock_sdk, capsys):
    "Invoking `avd_name()` on a device returns None."
    # Mock out the adb response for a physical device
    adb = ADB(mock_sdk, "deafbeefcafe")
    adb.run = MagicMock(side_effect=subprocess.CalledProcessError(
        returncode=1, cmd='emu avd name'))

    # Invoke avd_name
    assert adb.avd_name() is None

    # Validate call parameters.
    adb.run.assert_called_once_with("emu", "avd", "name")
Ejemplo n.º 8
0
def test_invalid_device(mock_sdk, capsys):
    """If the device ID is invalid, an error is raised."""
    # Mock out the adb response for an emulator
    adb = ADB(mock_sdk, "not-a-device")
    adb.run = MagicMock(side_effect=InvalidDeviceError("device", "exampleDevice"))

    # Invoke avd_name
    with pytest.raises(BriefcaseCommandError):
        adb.has_booted()

    # Validate call parameters.
    adb.run.assert_called_once_with("shell", "getprop", "sys.boot_completed")
Ejemplo n.º 9
0
def test_adb_failure(mock_sdk, capsys):
    "If `adb()` fails for a miscellaneous reason, an error is raised."
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(side_effect=subprocess.CalledProcessError(
        returncode=69, cmd='emu avd name'))

    # Invoke install
    with pytest.raises(BriefcaseCommandError):
        adb.avd_name()

    # Validate call parameters.
    adb.run.assert_called_once_with("emu", "avd", "name")
Ejemplo n.º 10
0
def test_adb_failure(mock_sdk, capsys):
    "If ADB fails, an error is raised"
    # Mock out the adb response for an emulator
    adb = ADB(mock_sdk, "deafbeefcafe")
    adb.run = MagicMock(side_effect=subprocess.CalledProcessError(
        returncode=69, cmd='emu avd name'))

    # Invoke avd_name
    with pytest.raises(BriefcaseCommandError):
        adb.has_booted()

    # Validate call parameters.
    adb.run.assert_called_once_with("shell", "getprop", "sys.boot_completed")
Ejemplo n.º 11
0
def test_install_failure(mock_sdk, capsys):
    "If `install_apk()` fails, an error is raised."
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(
        side_effect=subprocess.CalledProcessError(returncode=2, cmd='install'))

    # Invoke install
    with pytest.raises(BriefcaseCommandError):
        adb.install_apk("example.apk")

    # Validate call parameters.
    adb.run.assert_called_once_with("install", "example.apk")
Ejemplo n.º 12
0
def test_invalid_device(mock_sdk, capsys):
    """Invoking `install_apk()` on an invalid device raises an error."""
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(
        side_effect=InvalidDeviceError("device", "exampleDevice"))

    # Invoke install
    with pytest.raises(InvalidDeviceError):
        adb.install_apk("example.apk")

    # Validate call parameters.
    adb.run.assert_called_once_with("install", "example.apk")
Ejemplo n.º 13
0
def test_invalid_device(mock_sdk, capsys):
    "Invoking `avd_name()` on an invalid device raises an error."
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(
        side_effect=InvalidDeviceError('device', 'exampleDevice'))

    # Invoke install
    with pytest.raises(InvalidDeviceError):
        adb.avd_name()

    # Validate call parameters.
    adb.run.assert_called_once_with("emu", "avd", "name")
Ejemplo n.º 14
0
def test_invalid_device(mock_sdk, capsys):
    "Invoking `force_stop_app()` on an invalid device raises an error."
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(side_effect=InvalidDeviceError('device', 'exampleDevice'))

    # Invoke force_stop_app
    with pytest.raises(InvalidDeviceError):
        adb.force_stop_app("com.example.sample.package")

    # Validate call parameters.
    adb.run.assert_called_once_with(
        "shell", "am", "force-stop", "com.example.sample.package"
    )
Ejemplo n.º 15
0
def test_force_top_fail(mock_sdk, capsys):
    """If `force_stop_app()` fails, an error is raised."""
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(side_effect=subprocess.CalledProcessError(
        returncode=69, cmd="force-stop"))

    # Invoke force_stop_app
    with pytest.raises(BriefcaseCommandError):
        adb.force_stop_app("com.example.sample.package")

    # Validate call parameters.
    adb.run.assert_called_once_with("shell", "am", "force-stop",
                                    "com.example.sample.package")
Ejemplo n.º 16
0
def test_clear_log(mock_sdk, capsys):
    """Invoking `clear_log()` calls `run()` with the appropriate parameters."""
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(return_value="example normal adb output")

    # Invoke clear_log
    adb.clear_log()

    # Validate call parameters.
    adb.run.assert_called_once_with("logcat", "-c")

    # Validate that the normal output of the command was not printed (since there
    # was no error).
    assert "normal adb output" not in capsys.readouterr()
Ejemplo n.º 17
0
def test_force_stop_app(mock_sdk, capsys):
    "Invoking `force_stop_app()` calls `run()` with the appropriate parameters."
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(return_value="example normal adb output")

    # Invoke force_stop_app
    adb.force_stop_app("com.example.sample.package")

    # Validate call parameters.
    adb.run.assert_called_once_with(
        "shell", "am", "force-stop", "com.example.sample.package"
    )

    # Validate that the normal output of the command was not printed (since there
    # was no error).
    assert "normal adb output" not in capsys.readouterr()
Ejemplo n.º 18
0
def test_simple_command(mock_sdk, tmp_path):
    """ADB.command() invokes adb with the provided arguments."""
    # Create an ADB instance and invoke command()
    adb = ADB(mock_sdk, "exampleDevice")

    adb.run("example", "command")

    # Check that adb was invoked with the expected commands
    mock_sdk.command.subprocess.check_output.assert_called_once_with(
        [
            os.fsdecode(tmp_path / "sdk" / "platform-tools" / "adb"),
            "-s",
            "exampleDevice",
            "example",
            "command",
        ],
        stderr=subprocess.STDOUT,
    )
Ejemplo n.º 19
0
def test_missing_activity(mock_sdk):
    """If the activity doesn't exist, the error is caught."""
    # Use real `adb` output from launching an activity that does not exist.
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(return_value="""\
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.\
LAUNCHER] cmp=com.example.sample.package/.MainActivity }
Error type 3
Error: Activity class {com.example.sample.package/com.example.sample.package.\
MainActivity} does not exist.
""")

    with pytest.raises(BriefcaseCommandError) as exc_info:
        adb.start_app("com.example.sample.package",
                      "com.example.sample.activity")

    assert "Activity class not found" in str(exc_info.value)
Ejemplo n.º 20
0
def test_logcat(mock_sdk):
    """Invoking `logcat()` calls `run()` with the appropriate parameters."""
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")

    # Invoke logcat
    adb.logcat()

    # Validate call parameters.
    mock_sdk.command.subprocess.run.assert_called_once_with(
        [
            os.fsdecode(mock_sdk.adb_path),
            "-s",
            "exampleDevice",
            "logcat",
            "-s",
            "MainActivity:*",
            "stdio:*",
            "Python:*",
        ],
        env=mock_sdk.env,
        check=True,
    )
Ejemplo n.º 21
0
def test_start_app_launches_app(mock_sdk, capsys):
    """Invoking `start_app()` calls `run()` with the appropriate parameters."""
    # Mock out the run command on an adb instance
    adb = ADB(mock_sdk, "exampleDevice")
    adb.run = MagicMock(return_value="example normal adb output")

    # Invoke start_app
    adb.start_app("com.example.sample.package", "com.example.sample.activity")

    # Validate call parameters.
    adb.run.assert_called_once_with(
        "shell",
        "am",
        "start",
        "com.example.sample.package/com.example.sample.activity",
        "-a",
        "android.intent.action.MAIN",
        "-c",
        "android.intent.category.LAUNCHER",
    )

    # Validate that the normal output of the command was not printed (since there
    # was no error).
    assert "normal adb output" not in capsys.readouterr()
Ejemplo n.º 22
0
 def mock_adb(device):
     adb = ADB(sdk, device)
     adb.run = sdk.mock_run
     return adb