2019-09-02 20:08:01 +00:00
|
|
|
"""Define patches used for androidtv tests."""
|
|
|
|
|
|
|
|
from socket import error as socket_error
|
2019-10-17 13:33:20 +00:00
|
|
|
from unittest.mock import mock_open, patch
|
2019-09-02 20:08:01 +00:00
|
|
|
|
|
|
|
|
2019-09-27 05:53:26 +00:00
|
|
|
class AdbDeviceFake:
|
|
|
|
"""A fake of the `adb_shell.adb_device.AdbDevice` class."""
|
2019-09-02 20:08:01 +00:00
|
|
|
|
2019-09-27 05:53:26 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""Initialize a fake `adb_shell.adb_device.AdbDevice` instance."""
|
|
|
|
self.available = False
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""Close the socket connection."""
|
|
|
|
self.available = False
|
|
|
|
|
|
|
|
def connect(self, *args, **kwargs):
|
2019-09-02 20:08:01 +00:00
|
|
|
"""Try to connect to a device."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2019-09-27 05:53:26 +00:00
|
|
|
def shell(self, cmd):
|
2019-09-02 20:08:01 +00:00
|
|
|
"""Send an ADB shell command."""
|
2019-09-27 05:53:26 +00:00
|
|
|
return None
|
2019-09-02 20:08:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ClientFakeSuccess:
|
|
|
|
"""A fake of the `adb_messenger.client.Client` class when the connection and shell commands succeed."""
|
|
|
|
|
|
|
|
def __init__(self, host="127.0.0.1", port=5037):
|
|
|
|
"""Initialize a `ClientFakeSuccess` instance."""
|
|
|
|
self._devices = []
|
|
|
|
|
|
|
|
def devices(self):
|
|
|
|
"""Get a list of the connected devices."""
|
|
|
|
return self._devices
|
|
|
|
|
|
|
|
def device(self, serial):
|
|
|
|
"""Mock the `Client.device` method when the device is connected via ADB."""
|
|
|
|
device = DeviceFake(serial)
|
|
|
|
self._devices.append(device)
|
|
|
|
return device
|
|
|
|
|
|
|
|
|
|
|
|
class ClientFakeFail:
|
|
|
|
"""A fake of the `adb_messenger.client.Client` class when the connection and shell commands fail."""
|
|
|
|
|
|
|
|
def __init__(self, host="127.0.0.1", port=5037):
|
|
|
|
"""Initialize a `ClientFakeFail` instance."""
|
|
|
|
self._devices = []
|
|
|
|
|
|
|
|
def devices(self):
|
|
|
|
"""Get a list of the connected devices."""
|
|
|
|
return self._devices
|
|
|
|
|
|
|
|
def device(self, serial):
|
|
|
|
"""Mock the `Client.device` method when the device is not connected via ADB."""
|
|
|
|
self._devices = []
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceFake:
|
|
|
|
"""A fake of the `adb_messenger.device.Device` class."""
|
|
|
|
|
|
|
|
def __init__(self, host):
|
|
|
|
"""Initialize a `DeviceFake` instance."""
|
|
|
|
self.host = host
|
|
|
|
|
|
|
|
def get_serial_no(self):
|
|
|
|
"""Get the serial number for the device (IP:PORT)."""
|
|
|
|
return self.host
|
|
|
|
|
|
|
|
def shell(self, cmd):
|
|
|
|
"""Send an ADB shell command."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
def patch_connect(success):
|
2019-09-27 05:53:26 +00:00
|
|
|
"""Mock the `adb_shell.adb_device.AdbDevice` and `adb_messenger.client.Client` classes."""
|
|
|
|
|
|
|
|
def connect_success_python(self, *args, **kwargs):
|
|
|
|
"""Mock the `AdbDeviceFake.connect` method when it succeeds."""
|
|
|
|
self.available = True
|
|
|
|
|
|
|
|
def connect_fail_python(self, *args, **kwargs):
|
|
|
|
"""Mock the `AdbDeviceFake.connect` method when it fails."""
|
|
|
|
raise socket_error
|
2019-09-02 20:08:01 +00:00
|
|
|
|
|
|
|
if success:
|
|
|
|
return {
|
|
|
|
"python": patch(
|
2019-09-27 05:53:26 +00:00
|
|
|
f"{__name__}.AdbDeviceFake.connect", connect_success_python
|
2019-09-02 20:08:01 +00:00
|
|
|
),
|
|
|
|
"server": patch("androidtv.adb_manager.Client", ClientFakeSuccess),
|
|
|
|
}
|
|
|
|
return {
|
2019-09-27 05:53:26 +00:00
|
|
|
"python": patch(f"{__name__}.AdbDeviceFake.connect", connect_fail_python),
|
2019-09-02 20:08:01 +00:00
|
|
|
"server": patch("androidtv.adb_manager.Client", ClientFakeFail),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def patch_shell(response=None, error=False):
|
2019-09-27 05:53:26 +00:00
|
|
|
"""Mock the `AdbDeviceFake.shell` and `DeviceFake.shell` methods."""
|
2019-09-02 20:08:01 +00:00
|
|
|
|
|
|
|
def shell_success(self, cmd):
|
2019-09-27 05:53:26 +00:00
|
|
|
"""Mock the `AdbDeviceFake.shell` and `DeviceFake.shell` methods when they are successful."""
|
2019-09-02 20:08:01 +00:00
|
|
|
self.shell_cmd = cmd
|
|
|
|
return response
|
|
|
|
|
|
|
|
def shell_fail_python(self, cmd):
|
2019-09-27 05:53:26 +00:00
|
|
|
"""Mock the `AdbDeviceFake.shell` method when it fails."""
|
2019-09-02 20:08:01 +00:00
|
|
|
self.shell_cmd = cmd
|
|
|
|
raise AttributeError
|
|
|
|
|
|
|
|
def shell_fail_server(self, cmd):
|
|
|
|
"""Mock the `DeviceFake.shell` method when it fails."""
|
|
|
|
self.shell_cmd = cmd
|
|
|
|
raise ConnectionResetError
|
|
|
|
|
|
|
|
if not error:
|
|
|
|
return {
|
2019-09-27 05:53:26 +00:00
|
|
|
"python": patch(f"{__name__}.AdbDeviceFake.shell", shell_success),
|
2019-09-02 20:08:01 +00:00
|
|
|
"server": patch(f"{__name__}.DeviceFake.shell", shell_success),
|
|
|
|
}
|
|
|
|
return {
|
2019-09-27 05:53:26 +00:00
|
|
|
"python": patch(f"{__name__}.AdbDeviceFake.shell", shell_fail_python),
|
2019-09-02 20:08:01 +00:00
|
|
|
"server": patch(f"{__name__}.DeviceFake.shell", shell_fail_server),
|
|
|
|
}
|
2019-09-27 05:53:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
PATCH_ADB_DEVICE = patch("androidtv.adb_manager.AdbDevice", AdbDeviceFake)
|
2019-10-17 13:33:20 +00:00
|
|
|
PATCH_ANDROIDTV_OPEN = patch("androidtv.adb_manager.open", mock_open())
|
|
|
|
PATCH_KEYGEN = patch("homeassistant.components.androidtv.media_player.keygen")
|
|
|
|
PATCH_SIGNER = patch("androidtv.adb_manager.PythonRSASigner")
|
|
|
|
|
|
|
|
|
|
|
|
def isfile(filepath):
|
|
|
|
"""Mock `os.path.isfile`."""
|
|
|
|
return filepath.endswith("adbkey")
|
|
|
|
|
|
|
|
|
|
|
|
PATCH_ISFILE = patch("os.path.isfile", isfile)
|
|
|
|
PATCH_ACCESS = patch("os.access", return_value=True)
|