Skip to content

Commit

Permalink
Use a fixture for ADB testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartin16 committed Jul 11, 2023
1 parent b96cdc9 commit d1723b9
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 100 deletions.
13 changes: 4 additions & 9 deletions tests/integrations/android_sdk/ADB/test_avd_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import pytest

from briefcase.exceptions import BriefcaseCommandError, InvalidDeviceError
from briefcase.integrations.android_sdk import ADB


def test_emulator(mock_tools, capsys):
def test_emulator(adb, capsys):
"""Invoking `avd_name()` on an emulator returns the AVD."""
# Mock out the adb response for an emulator
adb = ADB(mock_tools, "deafbeefcafe")
adb.run = MagicMock(return_value="exampledevice\nOK\n")

# Invoke avd_name
Expand All @@ -20,10 +18,9 @@ def test_emulator(mock_tools, capsys):
adb.run.assert_called_once_with("emu", "avd", "name")


def test_device(mock_tools, capsys):
def test_device(adb, capsys):
"""Invoking `avd_name()` on a device returns None."""
# Mock out the adb response for a physical device
adb = ADB(mock_tools, "deafbeefcafe")
adb.run = MagicMock(
side_effect=subprocess.CalledProcessError(returncode=1, cmd="emu avd name")
)
Expand All @@ -35,10 +32,9 @@ def test_device(mock_tools, capsys):
adb.run.assert_called_once_with("emu", "avd", "name")


def test_adb_failure(mock_tools, capsys):
def test_adb_failure(adb, capsys):
"""If `adb()` fails for a miscellaneous reason, an error is raised."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")
adb.run = MagicMock(
side_effect=subprocess.CalledProcessError(returncode=69, cmd="emu avd name")
)
Expand All @@ -51,10 +47,9 @@ def test_adb_failure(mock_tools, capsys):
adb.run.assert_called_once_with("emu", "avd", "name")


def test_invalid_device(mock_tools, capsys):
def test_invalid_device(adb, capsys):
"""Invoking `avd_name()` on an invalid device raises an error."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")
adb.run = MagicMock(side_effect=InvalidDeviceError("device", "exampleDevice"))

# Invoke install
Expand Down
12 changes: 4 additions & 8 deletions tests/integrations/android_sdk/ADB/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@
import pytest

from briefcase.exceptions import BriefcaseCommandError
from briefcase.integrations.android_sdk import ADB


def test_datetime_success(mock_tools):
def test_datetime_success(adb):
"""adb.datetime() returns `datetime` for device."""
adb = ADB(mock_tools, "exampleDevice")
adb.run = Mock(return_value="1689098555\n")

expected_datetime = datetime(2023, 7, 11, 14, 2, 35)
assert adb.datetime() == expected_datetime
adb.run.assert_called_once_with("shell", "date", "+%s")


def test_datetime_failure_call(mock_tools):
def test_datetime_failure_call(adb):
"""adb.datetime() fails in subprocess call."""
adb = ADB(mock_tools, "exampleDevice")
adb.run = Mock(
side_effect=subprocess.CalledProcessError(returncode=1, cmd="adb shell ...")
)
Expand All @@ -32,10 +29,9 @@ def test_datetime_failure_call(mock_tools):
adb.datetime()


def test_datetime_failure_bad_value(mock_tools):
def test_datetime_failure_bad_value(adb):
"""adb.datetime() fails in output conversion."""
adb = ADB(mock_tools, "exampleDevice")
adb.run = Mock(return_value="this date is jan 1 1970")
adb.run = Mock(return_value="the date is jan 1 1970")

with pytest.raises(
BriefcaseCommandError,
Expand Down
10 changes: 3 additions & 7 deletions tests/integrations/android_sdk/ADB/test_force_stop_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import pytest

from briefcase.exceptions import BriefcaseCommandError, InvalidDeviceError
from briefcase.integrations.android_sdk import ADB


def test_force_stop_app(mock_tools, capsys):
def test_force_stop_app(adb, capsys):
"""Invoking `force_stop_app()` calls `run()` with the appropriate parameters."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")
adb.run = MagicMock(return_value="example normal adb output")

# Invoke force_stop_app
Expand All @@ -26,10 +24,9 @@ def test_force_stop_app(mock_tools, capsys):
assert "normal adb output" not in capsys.readouterr()


def test_force_top_fail(mock_tools, capsys):
def test_force_top_fail(adb, capsys):
"""If `force_stop_app()` fails, an error is raised."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")
adb.run = MagicMock(
side_effect=subprocess.CalledProcessError(returncode=69, cmd="force-stop")
)
Expand All @@ -44,10 +41,9 @@ def test_force_top_fail(mock_tools, capsys):
)


def test_invalid_device(mock_tools, capsys):
def test_invalid_device(adb, capsys):
"""Invoking `force_stop_app()` on an invalid device raises an error."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")
adb.run = MagicMock(side_effect=InvalidDeviceError("device", "exampleDevice"))

# Invoke force_stop_app
Expand Down
13 changes: 4 additions & 9 deletions tests/integrations/android_sdk/ADB/test_has_booted.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import pytest

from briefcase.exceptions import BriefcaseCommandError, InvalidDeviceError
from briefcase.integrations.android_sdk import ADB


def test_booted(mock_tools, capsys):
def test_booted(adb, capsys):
"""A booted device returns true."""
# Mock out the adb response for an emulator
adb = ADB(mock_tools, "deafbeefcafe")
adb.run = MagicMock(return_value="1\n")

# Invoke avd_name
Expand All @@ -20,10 +18,9 @@ def test_booted(mock_tools, capsys):
adb.run.assert_called_once_with("shell", "getprop", "sys.boot_completed")


def test_not_booted(mock_tools, capsys):
def test_not_booted(adb, capsys):
"""A non-booted device returns False."""
# Mock out the adb response for an emulator
adb = ADB(mock_tools, "deafbeefcafe")
adb.run = MagicMock(return_value="\n")

# Invoke avd_name
Expand All @@ -33,10 +30,9 @@ def test_not_booted(mock_tools, capsys):
adb.run.assert_called_once_with("shell", "getprop", "sys.boot_completed")


def test_adb_failure(mock_tools, capsys):
def test_adb_failure(adb, capsys):
"""If ADB fails, an error is raised."""
# Mock out the adb response for an emulator
adb = ADB(mock_tools, "deafbeefcafe")
adb.run = MagicMock(
side_effect=subprocess.CalledProcessError(returncode=69, cmd="emu avd name")
)
Expand All @@ -49,10 +45,9 @@ def test_adb_failure(mock_tools, capsys):
adb.run.assert_called_once_with("shell", "getprop", "sys.boot_completed")


def test_invalid_device(mock_tools, capsys):
def test_invalid_device(adb, capsys):
"""If the device ID is invalid, an error is raised."""
# Mock out the adb response for an emulator
adb = ADB(mock_tools, "not-a-device")
adb.run = MagicMock(side_effect=InvalidDeviceError("device", "exampleDevice"))

# Invoke avd_name
Expand Down
10 changes: 3 additions & 7 deletions tests/integrations/android_sdk/ADB/test_install_apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import pytest

from briefcase.exceptions import BriefcaseCommandError, InvalidDeviceError
from briefcase.integrations.android_sdk import ADB


def test_install_apk(mock_tools, capsys):
def test_install_apk(adb, capsys):
"""Invoking `install_apk()` calls `run()` with the appropriate parameters."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")
adb.run = MagicMock(return_value="example normal adb output")

# Invoke install
Expand All @@ -24,10 +22,9 @@ def test_install_apk(mock_tools, capsys):
assert "normal adb output" not in capsys.readouterr()


def test_install_failure(mock_tools, capsys):
def test_install_failure(adb, capsys):
"""If `install_apk()` fails, an error is raised."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")
adb.run = MagicMock(
side_effect=subprocess.CalledProcessError(returncode=2, cmd="install")
)
Expand All @@ -40,10 +37,9 @@ def test_install_failure(mock_tools, capsys):
adb.run.assert_called_once_with("install", "-r", "example.apk")


def test_invalid_device(mock_tools, capsys):
def test_invalid_device(adb, capsys):
"""Invoking `install_apk()` on an invalid device raises an error."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")
adb.run = MagicMock(side_effect=InvalidDeviceError("device", "exampleDevice"))

# Invoke install
Expand Down
9 changes: 2 additions & 7 deletions tests/integrations/android_sdk/ADB/test_kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
import pytest

from briefcase.exceptions import BriefcaseCommandError
from briefcase.integrations.android_sdk import ADB


def test_kill(mock_tools):
def test_kill(mock_tools, adb):
"""An emulator can be killed."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")

# Invoke kill
adb.kill()

Expand All @@ -29,10 +25,9 @@ def test_kill(mock_tools):
)


def test_kill_failure(mock_tools):
def test_kill_failure(adb):
"""If emu kill fails, the error is caught."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")
adb.run = MagicMock(
side_effect=subprocess.CalledProcessError(returncode=1, cmd="adb emu kill")
)
Expand Down
7 changes: 1 addition & 6 deletions tests/integrations/android_sdk/ADB/test_logcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
import subprocess
from unittest import mock

from briefcase.integrations.android_sdk import ADB


def test_logcat(mock_tools):
def test_logcat(mock_tools, adb):
"""Invoking `logcat()` calls `Popen()` with the appropriate parameters."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")

# Mock the result of calling Popen so we can compare against this return value
popen = mock.MagicMock()
mock_tools.subprocess.Popen.return_value = popen
Expand Down
9 changes: 2 additions & 7 deletions tests/integrations/android_sdk/ADB/test_logcat_tail.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
import pytest

from briefcase.exceptions import BriefcaseCommandError
from briefcase.integrations.android_sdk import ADB


def test_logcat_tail(mock_tools):
def test_logcat_tail(mock_tools, adb):
"""Invoking `logcat_tail()` calls `run()` with the appropriate parameters."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")

# Invoke logcat_tail with a specific timestamp
adb.logcat_tail(since=datetime(2022, 11, 10, 9, 8, 7))

Expand All @@ -38,10 +34,9 @@ def test_logcat_tail(mock_tools):
)


def test_adb_failure(mock_tools):
def test_adb_failure(mock_tools, adb):
"""If adb logcat fails, the error is caught."""
# Mock out the run command on an adb instance
adb = ADB(mock_tools, "exampleDevice")
mock_tools.subprocess.run = MagicMock(
side_effect=subprocess.CalledProcessError(returncode=1, cmd="adb logcat")
)
Expand Down
14 changes: 6 additions & 8 deletions tests/integrations/android_sdk/ADB/test_pid_exists.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import subprocess
from unittest.mock import Mock

from briefcase.integrations.android_sdk import ADB


def test_pid_exists_succeed(mock_tools):
def test_pid_exists_succeed(adb):
"""adb.pid_exists() can be called on a process that exists."""
adb = ADB(mock_tools, "exampleDevice")
adb.run = Mock(return_value="")

assert adb.pid_exists("1234")
adb.run.assert_called_once_with("shell", "test", "-e", "/proc/1234")


def test_pid_exists_quiet(mock_tools):
def test_pid_exists_quiet(adb):
"""adb.pid_exists() can be called in quiet mode on a process that exists."""
adb = ADB(mock_tools, "exampleDevice")
adb.run = Mock(return_value="")

assert adb.pid_exists("1234", quiet=True)
adb.run.assert_called_once_with("shell", "test", "-e", "/proc/1234", quiet=True)


def test_pid_does_not_exist(mock_tools):
def test_pid_does_not_exist(adb):
"""If adb.pid_exists() returns a status code of 1, it is interpreted as the process
not existing."""
adb = ADB(mock_tools, "exampleDevice")
adb.run = Mock(side_effect=subprocess.CalledProcessError(returncode=1, cmd="test"))

assert not adb.pid_exists("9999") is None
adb.run.assert_called_once_with("shell", "test", "-e", "/proc/9999")
18 changes: 8 additions & 10 deletions tests/integrations/android_sdk/ADB/test_pidof.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
import subprocess
from unittest.mock import Mock

from briefcase.integrations.android_sdk import ADB


def test_pidof_succeed(mock_tools):
def test_pidof_succeed(adb):
"""adb.pidof() can be called on a process that exists."""
adb = ADB(mock_tools, "exampleDevice")
adb.run = Mock(return_value="5678\n")

assert adb.pidof("com.example") == "5678"
adb.run.assert_called_once_with("shell", "pidof", "-s", "com.example")


def test_pidof_quiet(mock_tools):
def test_pidof_quiet(adb):
"""adb.pidof() can be called in quiet mode on a process that exists."""
adb = ADB(mock_tools, "exampleDevice")
adb.run = Mock(return_value="5678\n")

assert adb.pidof("com.example", quiet=True) == "5678"
adb.run.assert_called_once_with("shell", "pidof", "-s", "com.example", quiet=True)


def test_pidof_fail_exit_0(mock_tools):
def test_pidof_fail_exit_0(adb):
"""If adb.pidof() returns a PID of 0, it is interpreted as the process not
existing."""
adb = ADB(mock_tools, "exampleDevice")
adb.run = Mock(return_value="")

assert adb.pidof("com.example") is None
adb.run.assert_called_once_with("shell", "pidof", "-s", "com.example")


def test_pidof_fail_exit_1(mock_tools):
def test_pidof_fail_exit_1(adb):
"""If adb.pidof() fails, it is interpreted as the process not existing."""
adb = ADB(mock_tools, "exampleDevice")
adb.run = Mock(side_effect=subprocess.CalledProcessError(1, "adb shell"))

assert adb.pidof("com.example") is None
adb.run.assert_called_once_with("shell", "pidof", "-s", "com.example")
Loading

0 comments on commit d1723b9

Please sign in to comment.