From fe59e6b6aee70770e6ea6e22acad327708e0281f Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Sun, 1 Nov 2020 08:08:50 -0800 Subject: [PATCH 1/5] Simplify 'get_device_properties()' test in test_basetv_async.py --- tests/test_basetv_async.py | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/tests/test_basetv_async.py b/tests/test_basetv_async.py index e3f529de..287431ed 100644 --- a/tests/test_basetv_async.py +++ b/tests/test_basetv_async.py @@ -320,28 +320,9 @@ async def test_get_device_properties(self): """ with async_patchers.patch_shell(DEVICE_PROPERTIES_OUTPUT1)[self.PATCH_KEY]: - device_properties = await self.btv.get_device_properties() - self.assertDictEqual(DEVICE_PROPERTIES_DICT1, device_properties) - - with async_patchers.patch_shell(DEVICE_PROPERTIES_OUTPUT2)[self.PATCH_KEY]: - device_properties = await self.btv.get_device_properties() - self.assertDictEqual(DEVICE_PROPERTIES_DICT2, device_properties) - - with async_patchers.patch_shell(DEVICE_PROPERTIES_OUTPUT3)[self.PATCH_KEY]: - device_properties = await self.btv.get_device_properties() - self.assertDictEqual(DEVICE_PROPERTIES_DICT3, device_properties) - - with async_patchers.patch_shell('manufacturer')[self.PATCH_KEY]: - device_properties = await self.btv.get_device_properties() - self.assertDictEqual({}, device_properties) - - with async_patchers.patch_shell('')[self.PATCH_KEY]: - device_properties = await self.btv.get_device_properties() - self.assertDictEqual({}, device_properties) - - with async_patchers.patch_shell(DEVICE_PROPERTIES_GOOGLE_TV)[self.PATCH_KEY]: - device_properties = await self.btv.get_device_properties() - self.assertTrue(self.btv._is_google_tv) + with patch("androidtv.basetv.basetv.BaseTV._parse_device_properties") as pdp: + await self.btv.get_device_properties() + assert pdp.called @awaiter async def test_awake(self): From 86a519d8c10303cd4eb7042981ffefcc269e682f Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Mon, 2 Nov 2020 22:10:48 -0800 Subject: [PATCH 2/5] Simplify tests for async modules --- tests/patchers.py | 20 ++ tests/test_androidtv_async.py | 648 ++-------------------------------- tests/test_basetv_async.py | 182 ++-------- tests/test_firetv_async.py | 415 +--------------------- 4 files changed, 78 insertions(+), 1187 deletions(-) diff --git a/tests/patchers.py b/tests/patchers.py index 76044120..dae67016 100644 --- a/tests/patchers.py +++ b/tests/patchers.py @@ -1,5 +1,7 @@ """Define patches used for androidtv tests.""" +import copy + try: # Python3 from unittest.mock import patch @@ -143,3 +145,21 @@ class CustomException(Exception): PATCH_CONNECT_FAIL_CUSTOM_EXCEPTION = {KEY_PYTHON: patch("{}.{}.connect".format(__name__, ADB_DEVICE_TCP_FAKE), side_effect=CustomException), KEY_SERVER: patch("{}.{}.device".format(__name__, CLIENT_FAKE_SUCCESS), side_effect=CustomException)} + + +def patch_calls(obj, wraps): + """Patch a method call without changing its behavior. + + Parameters + ---------- + obj + The object whose method will be patched (i.e., `self`) + wraps + The method that is being patched (i.e., `self.method`) + + Returns + ------- + The patched method + + """ + return patch.object(type(obj), wraps.__name__.split()[-1], wraps=wraps) diff --git a/tests/test_androidtv_async.py b/tests/test_androidtv_async.py index 85244ed5..1b4d062d 100644 --- a/tests/test_androidtv_async.py +++ b/tests/test_androidtv_async.py @@ -11,10 +11,9 @@ from . import async_patchers from .async_wrapper import awaiter +from .patchers import patch_calls -HDMI_INPUT_EMPTY = "\n" - STREAM_MUSIC_EMPTY = "- STREAM_MUSIC:\n \n- STREAM" STREAM_MUSIC_OFF = """- STREAM_MUSIC: @@ -57,247 +56,6 @@ Devices: speaker""" -RUNNING_APPS_OUTPUT = """ -u0_a18 316 197 1189204 115000 ffffffff 00000000 S com.netflix.ninja -u0_a2 15121 197 998628 24628 ffffffff 00000000 S com.amazon.device.controllermanager""" - -RUNNING_APPS_LIST = ['com.netflix.ninja', 'com.amazon.device.controllermanager'] - - -GET_PROPERTIES_OUTPUT1 = "" -GET_PROPERTIES_DICT1 = {'screen_on': False, - 'awake': False, - 'audio_state': None, - 'wake_lock_size': -1, - 'media_session_state': None, - 'current_app': None, - 'audio_output_device': None, - 'is_volume_muted': None, - 'volume': None, - 'running_apps': None, - 'hdmi_input': None} -STATE1 = (constants.STATE_OFF, None, None, None, None, None, None) - -GET_PROPERTIES_OUTPUT2 = "1" -GET_PROPERTIES_DICT2 = {'screen_on': True, - 'awake': False, - 'audio_state': None, - 'wake_lock_size': -1, - 'media_session_state': None, - 'current_app': None, - 'audio_output_device': None, - 'is_volume_muted': None, - 'volume': None, - 'running_apps': None, - 'hdmi_input': None} -STATE2 = (constants.STATE_STANDBY, None, None, None, None, None, None) - -GET_PROPERTIES_OUTPUT3 = """110Wake Locks: size=2 -com.amazon.tv.launcher - -""" + HDMI_INPUT_EMPTY + STREAM_MUSIC_ON -GET_PROPERTIES_DICT3 = {'screen_on': True, - 'awake': True, - 'audio_state': constants.STATE_IDLE, - 'wake_lock_size': 2, - 'current_app': 'com.amazon.tv.launcher', - 'media_session_state': None, - 'audio_output_device': 'hmdi_arc', - 'is_volume_muted': False, - 'volume': 22, - 'running_apps': None, - 'hdmi_input': None} -STATE3 = (constants.STATE_PLAYING, 'com.amazon.tv.launcher', ['com.amazon.tv.launcher'], 'hmdi_arc', False, (22 / 60.), None) - -GET_PROPERTIES_OUTPUT3A = GET_PROPERTIES_OUTPUT3[:1] -GET_PROPERTIES_OUTPUT3B = GET_PROPERTIES_OUTPUT3[:2] -GET_PROPERTIES_OUTPUT3C = GET_PROPERTIES_OUTPUT3[:3] -GET_PROPERTIES_OUTPUT3D = GET_PROPERTIES_OUTPUT3.splitlines()[0] -GET_PROPERTIES_OUTPUT3E = '\n'.join(GET_PROPERTIES_OUTPUT3.splitlines()[:2]) -GET_PROPERTIES_OUTPUT3F = '\n'.join(GET_PROPERTIES_OUTPUT3.splitlines()[:3]) -GET_PROPERTIES_OUTPUT3G = '\n'.join(GET_PROPERTIES_OUTPUT3.splitlines()[:4]) + "HDMI" - -GET_PROPERTIES_DICT3A = {'screen_on': True, - 'awake': False, - 'audio_state': None, - 'wake_lock_size': -1, - 'current_app': None, - 'media_session_state': None, - 'audio_output_device': None, - 'is_volume_muted': None, - 'volume': None, - 'running_apps': None, - 'hdmi_input': None} -GET_PROPERTIES_DICT3B = {'screen_on': True, - 'awake': True, - 'audio_state': None, - 'wake_lock_size': -1, - 'current_app': None, - 'media_session_state': None, - 'audio_output_device': None, - 'is_volume_muted': None, - 'volume': None, - 'running_apps': None, - 'hdmi_input': None} -GET_PROPERTIES_DICT3C = {'screen_on': True, - 'awake': True, - 'audio_state': constants.STATE_IDLE, - 'wake_lock_size': -1, - 'current_app': None, - 'media_session_state': None, - 'audio_output_device': None, - 'is_volume_muted': None, - 'volume': None, - 'running_apps': None, - 'hdmi_input': None} -GET_PROPERTIES_DICT3D = {'screen_on': True, - 'awake': True, - 'audio_state': constants.STATE_IDLE, - 'wake_lock_size': 2, - 'current_app': None, - 'media_session_state': None, - 'audio_output_device': None, - 'is_volume_muted': None, - 'volume': None, - 'running_apps': None, - 'hdmi_input': None} -GET_PROPERTIES_DICT3E = {'screen_on': True, - 'awake': True, - 'audio_state': constants.STATE_IDLE, - 'wake_lock_size': 2, - 'current_app': 'com.amazon.tv.launcher', - 'media_session_state': None, - 'audio_output_device': None, - 'is_volume_muted': None, - 'volume': None, - 'running_apps': None, - 'hdmi_input': None} -GET_PROPERTIES_DICT3F = {'screen_on': True, - 'awake': True, - 'audio_state': constants.STATE_IDLE, - 'wake_lock_size': 2, - 'current_app': 'com.amazon.tv.launcher', - 'media_session_state': None, - 'audio_output_device': None, - 'is_volume_muted': None, - 'volume': None, - 'running_apps': None, - 'hdmi_input': None} -GET_PROPERTIES_DICT3G = {'screen_on': True, - 'awake': True, - 'audio_state': constants.STATE_IDLE, - 'wake_lock_size': 2, - 'current_app': 'com.amazon.tv.launcher', - 'media_session_state': None, - 'audio_output_device': None, - 'is_volume_muted': None, - 'volume': None, - 'running_apps': None, - 'hdmi_input': 'HDMI'} - -GET_PROPERTIES_OUTPUT4 = """111Wake Locks: size=2 -com.amazon.tv.launcher -state=PlaybackState {state=1, position=0, buffered position=0, speed=0.0, updated=65749, actions=240640, custom actions=[], active item id=-1, error=null} -""" -GET_PROPERTIES_DICT4 = {'screen_on': True, - 'awake': True, - 'audio_state': constants.STATE_PAUSED, - 'wake_lock_size': 2, - 'current_app': 'com.amazon.tv.launcher', - 'media_session_state': 1, - 'audio_output_device': None, - 'is_volume_muted': None, - 'volume': None, - 'running_apps': None, - 'hdmi_input': None} - -GET_PROPERTIES_DICT_NONE = {'screen_on': None, - 'awake': None, - 'audio_state': None, - 'wake_lock_size': None, - 'media_session_state': None, - 'current_app': None, - 'audio_output_device': None, - 'is_volume_muted': None, - 'volume': None, - 'running_apps': None, - 'hdmi_input': None} -STATE_NONE = (None, None, None, None, None, None, None) - -# https://community.home-assistant.io/t/testers-needed-custom-state-detection-rules-for-android-tv-fire-tv/129493/6?u=jefflirion -STATE_DETECTION_RULES_PLEX = {'com.plexapp.android': [{'playing': {'media_session_state': 3, - 'wake_lock_size': 3}}, - {'paused': {'media_session_state': 3, - 'wake_lock_size': 1}}, - 'idle']} - -# Plex: idle -GET_PROPERTIES_OUTPUT_PLEX_IDLE = """110Wake Locks: size=1 -com.plexapp.android - -""" + HDMI_INPUT_EMPTY + STREAM_MUSIC_ON - -GET_PROPERTIES_DICT_PLEX_IDLE = {'screen_on': True, - 'awake': True, - 'audio_state': constants.STATE_IDLE, - 'wake_lock_size': 1, - 'media_session_state': None, - 'current_app': 'com.plexapp.android', - 'audio_output_device': 'hmdi_arc', - 'is_volume_muted': False, - 'volume': 22, - 'running_apps': None, - 'hdmi_input': None} - -STATE_PLEX_IDLE = (constants.STATE_PLAYING, 'com.plexapp.android', ['com.plexapp.android'], 'hmdi_arc', False, 22/60.) - -# Plex: playing -GET_PROPERTIES_OUTPUT_PLEX_PLAYING = """110Wake Locks: size=3 -com.plexapp.android -state=3 -""" + HDMI_INPUT_EMPTY + STREAM_MUSIC_ON - -GET_PROPERTIES_DICT_PLEX_PLAYING = {'screen_on': True, - 'awake': True, - 'audio_state': constants.STATE_IDLE, - 'wake_lock_size': 3, - 'media_session_state': 3, - 'current_app': 'com.plexapp.android', - 'audio_output_device': 'hmdi_arc', - 'is_volume_muted': False, - 'volume': 22, - 'running_apps': None, - 'hdmi_input': None} - -STATE_PLEX_PLAYING = (constants.STATE_PLAYING, 'com.plexapp.android', ['com.plexapp.android'], 'hmdi_arc', False, 22/60.) - -# Plex: paused -GET_PROPERTIES_OUTPUT_PLEX_PAUSED = """110Wake Locks: size=1 -com.plexapp.android -state=3 -""" + HDMI_INPUT_EMPTY + STREAM_MUSIC_ON - -GET_PROPERTIES_DICT_PLEX_PAUSED = {'screen_on': True, - 'awake': True, - 'audio_state': constants.STATE_IDLE, - 'wake_lock_size': 1, - 'media_session_state': 3, - 'current_app': 'com.plexapp.android', - 'audio_output_device': 'hmdi_arc', - 'is_volume_muted': False, - 'volume': 22, - 'running_apps': None, - 'hdmi_input': None} - -STATE_PLEX_PAUSED = (constants.STATE_PAUSED, 'com.plexapp.android', ['com.plexapp.android'], 'hmdi_arc', False, 22/60.) - -STATE_DETECTION_RULES1 = {'com.amazon.tv.launcher': ['off']} -STATE_DETECTION_RULES2 = {'com.amazon.tv.launcher': ['media_session_state', 'off']} -STATE_DETECTION_RULES3 = {'com.amazon.tv.launcher': [{'idle': {'wake_lock_size': 2}}]} -STATE_DETECTION_RULES4 = {'com.amazon.tv.launcher': [{'idle': {'wake_lock_size': 1}}, 'paused']} -STATE_DETECTION_RULES5 = {'com.amazon.tv.launcher': ['audio_state']} - - class TestAndroidTVAsyncPython(unittest.TestCase): PATCH_KEY = 'python' ADB_ATTR = '_adb' @@ -335,16 +93,9 @@ async def test_running_apps(self): """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - running_apps = await self.atv.running_apps() - self.assertIsNone(running_apps, None) - - with async_patchers.patch_shell('')[self.PATCH_KEY]: - running_apps = await self.atv.running_apps() - self.assertIsNone(running_apps, None) - - with async_patchers.patch_shell(RUNNING_APPS_OUTPUT)[self.PATCH_KEY]: - running_apps = await self.atv.running_apps() - self.assertListEqual(running_apps, RUNNING_APPS_LIST) + with patch_calls(self.atv, self.atv._running_apps) as patched: + await self.atv.running_apps() + assert patched.called @awaiter async def test_audio_output_device(self): @@ -352,28 +103,9 @@ async def test_audio_output_device(self): """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - audio_output_device = await self.atv.audio_output_device() - self.assertIsNone(audio_output_device) - - with async_patchers.patch_shell('')[self.PATCH_KEY]: - audio_output_device = await self.atv.audio_output_device() - self.assertIsNone(audio_output_device) - - with async_patchers.patch_shell(' ')[self.PATCH_KEY]: - audio_output_device = await self.atv.audio_output_device() - self.assertIsNone(audio_output_device) - - with async_patchers.patch_shell(STREAM_MUSIC_EMPTY)[self.PATCH_KEY]: - audio_output_device = await self.atv.audio_output_device() - self.assertIsNone(audio_output_device) - - with async_patchers.patch_shell(STREAM_MUSIC_OFF)[self.PATCH_KEY]: - audio_output_device = await self.atv.audio_output_device() - self.assertEqual('speaker', audio_output_device) - - with async_patchers.patch_shell(STREAM_MUSIC_ON)[self.PATCH_KEY]: - audio_output_device = await self.atv.audio_output_device() - self.assertEqual('hmdi_arc', audio_output_device) + with patch_calls(self.atv, self.atv._audio_output_device) as patched: + await self.atv.audio_output_device() + assert patched.called @awaiter async def test_volume(self): @@ -381,36 +113,9 @@ async def test_volume(self): """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - volume = await self.atv.volume() - self.assertIsNone(volume) - - with async_patchers.patch_shell('')[self.PATCH_KEY]: - volume = await self.atv.volume() - self.assertIsNone(volume) - - with async_patchers.patch_shell(' ')[self.PATCH_KEY]: - volume = await self.atv.volume() - self.assertIsNone(volume) - - with async_patchers.patch_shell(STREAM_MUSIC_EMPTY)[self.PATCH_KEY]: - volume = await self.atv.volume() - self.assertIsNone(volume) - - with async_patchers.patch_shell(STREAM_MUSIC_NO_VOLUME)[self.PATCH_KEY]: - volume = await self.atv.volume() - self.assertIsNone(volume) - - self.atv.max_volume = None - with async_patchers.patch_shell(STREAM_MUSIC_OFF)[self.PATCH_KEY]: - volume = await self.atv.volume() - self.assertEqual(volume, 20) - self.assertEqual(self.atv.max_volume, 60.) - - self.atv.max_volume = None - with async_patchers.patch_shell(STREAM_MUSIC_ON)[self.PATCH_KEY]: - volume = await self.atv.volume() - self.assertEqual(volume, 22) - self.assertEqual(self.atv.max_volume, 60.) + with patch_calls(self.atv, self.atv._volume) as patched: + await self.atv.volume() + assert patched.called @awaiter async def test_volume_level(self): @@ -418,36 +123,9 @@ async def test_volume_level(self): """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - volume_level = await self.atv.volume_level() - self.assertIsNone(volume_level) - - with async_patchers.patch_shell('')[self.PATCH_KEY]: - volume_level = await self.atv.volume_level() - self.assertIsNone(volume_level) - - with async_patchers.patch_shell(' ')[self.PATCH_KEY]: - volume_level = await self.atv.volume_level() - self.assertIsNone(volume_level) - - with async_patchers.patch_shell(STREAM_MUSIC_EMPTY)[self.PATCH_KEY]: - volume_level = await self.atv.volume_level() - self.assertIsNone(volume_level) - - with async_patchers.patch_shell(STREAM_MUSIC_NO_VOLUME)[self.PATCH_KEY]: - volume_level = await self.atv.volume_level() - self.assertIsNone(volume_level) - - self.atv.max_volume = None - with async_patchers.patch_shell(STREAM_MUSIC_OFF)[self.PATCH_KEY]: - volume_level = await self.atv.volume_level() - self.assertEqual(volume_level, 20./60) - self.assertEqual(self.atv.max_volume, 60.) - - self.atv.max_volume = None - with async_patchers.patch_shell(STREAM_MUSIC_ON)[self.PATCH_KEY]: - volume_level = await self.atv.volume_level() - self.assertEqual(volume_level, 22./60) - self.assertEqual(self.atv.max_volume, 60.) + with patch_calls(self.atv, self.atv._volume_level) as patched: + await self.atv.volume_level() + assert patched.called @awaiter async def test_is_volume_muted(self): @@ -455,24 +133,9 @@ async def test_is_volume_muted(self): """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - is_volume_muted = await self.atv.is_volume_muted() - self.assertIsNone(is_volume_muted) - - with async_patchers.patch_shell('')[self.PATCH_KEY]: - is_volume_muted = await self.atv.is_volume_muted() - self.assertIsNone(is_volume_muted) - - with async_patchers.patch_shell(' ')[self.PATCH_KEY]: - is_volume_muted = await self.atv.is_volume_muted() - self.assertIsNone(is_volume_muted) - - with async_patchers.patch_shell(STREAM_MUSIC_EMPTY)[self.PATCH_KEY]: - is_volume_muted = await self.atv.is_volume_muted() - self.assertIsNone(is_volume_muted) - - with async_patchers.patch_shell(STREAM_MUSIC_OFF)[self.PATCH_KEY]: - is_volume_muted = await self.atv.is_volume_muted() - self.assertFalse(is_volume_muted) + with patch_calls(self.atv, self.atv._is_volume_muted) as patched: + await self.atv.is_volume_muted() + assert patched.called @awaiter async def test_set_volume_level(self): @@ -570,270 +233,21 @@ async def test_get_properties(self): """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT_NONE) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT1)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT1) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT2)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT2) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT3) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3A)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT3A) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3B)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT3B) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3C)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT3C) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3D)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT3D) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3E)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT3E) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3F)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT3F) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3G)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT3G) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT4)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT4) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT4)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=False) - self.assertEqual(properties, GET_PROPERTIES_DICT4) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT4)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=False, lazy=False) - self.assertEqual(properties, GET_PROPERTIES_DICT4) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT_PLEX_IDLE)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT_PLEX_IDLE) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT_PLEX_PLAYING)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT_PLEX_PLAYING) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT_PLEX_PAUSED)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT_PLEX_PAUSED) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT_PLEX_PAUSED)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT_PLEX_PAUSED) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT_PLEX_PAUSED)[self.PATCH_KEY]: - properties = await self.atv.get_properties_dict(get_running_apps=False, lazy=True) - self.assertEqual(properties, GET_PROPERTIES_DICT_PLEX_PAUSED) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT_PLEX_PAUSED + RUNNING_APPS_OUTPUT)[self.PATCH_KEY]: - true_properties = GET_PROPERTIES_DICT_PLEX_PAUSED.copy() - true_properties['running_apps'] = RUNNING_APPS_LIST - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=True) - self.assertEqual(properties, true_properties) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT_PLEX_PAUSED + RUNNING_APPS_OUTPUT)[self.PATCH_KEY]: - true_properties = GET_PROPERTIES_DICT_PLEX_PAUSED.copy() - true_properties['running_apps'] = RUNNING_APPS_LIST - properties = await self.atv.get_properties_dict(get_running_apps=True, lazy=False) - self.assertEqual(properties, true_properties) + for get_running_apps in [True, False]: + for lazy in [True, False]: + with patch_calls(self.atv, self.atv._get_properties) as patched: + await self.atv.get_properties_dict(get_running_apps, lazy) + assert patched.called @awaiter async def test_update(self): """Check that the ``update`` method works correctly. """ - with async_patchers.patch_connect(False)[self.PATCH_KEY]: - await self.atv.adb_connect() - state = await self.atv.update() - self.assertTupleEqual(state, STATE_NONE) - - with async_patchers.patch_connect(True)[self.PATCH_KEY]: - await self.atv.adb_connect() - with async_patchers.patch_shell(None)[self.PATCH_KEY]: - state = await self.atv.update() - self.assertTupleEqual(state, STATE_NONE) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT1)[self.PATCH_KEY]: - state = await self.atv.update() - self.assertTupleEqual(state, STATE1) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT2)[self.PATCH_KEY]: - state = await self.atv.update() - self.assertTupleEqual(state, STATE2) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3)[self.PATCH_KEY]: - state = await self.atv.update() - self.assertTupleEqual(state, STATE3) - - self.atv._state_detection_rules = STATE_DETECTION_RULES1 - state = await self.atv.update() - self.assertEqual(state[0], constants.STATE_OFF) - - self.atv._state_detection_rules = STATE_DETECTION_RULES2 - state = await self.atv.update() - self.assertEqual(state[0], constants.STATE_OFF) - - self.atv._state_detection_rules = STATE_DETECTION_RULES3 - state = await self.atv.update() - self.assertEqual(state[0], constants.STATE_IDLE) - - self.atv._state_detection_rules = STATE_DETECTION_RULES4 - state = await self.atv.update() - self.assertEqual(state[0], constants.STATE_PAUSED) - - self.atv._state_detection_rules = STATE_DETECTION_RULES5 - state = await self.atv.update() - self.assertEqual(state[0], constants.STATE_IDLE) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3 + RUNNING_APPS_OUTPUT)[self.PATCH_KEY]: - self.atv._state_detection_rules = None - state = await self.atv.update(get_running_apps=True) - true_state = STATE3[:2] + (RUNNING_APPS_LIST,) + STATE3[3:] - self.assertTupleEqual(state, true_state) - - async def assertUpdate(self, get_properties, update): - """Check that the results of the `update` method are as expected. - - """ - with async_patchers.async_patch('androidtv.androidtv.androidtv_async.AndroidTVAsync.get_properties', return_value=get_properties): - self.assertTupleEqual(await self.atv.update(), update) - - @awaiter - async def test_state_detection(self): - """Check that the state detection works as expected. - - """ - self.atv.max_volume = 60. - await self.assertUpdate([False, False, None, -1, None, None, None, None, None, None, None], - (constants.STATE_OFF, None, None, None, None, None, None)) - - await self.assertUpdate([True, False, None, -1, None, None, None, None, None, None, None], - (constants.STATE_STANDBY, None, None, None, None, None, None)) - - # ATV Launcher - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, constants.APP_ATV_LAUNCHER, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, constants.APP_ATV_LAUNCHER, [constants.APP_ATV_LAUNCHER], 'hmdi_arc', False, 0.5, None)) - - # ATV Launcher with custom state detection - self.atv._state_detection_rules = {constants.APP_ATV_LAUNCHER: [{'idle': {'audio_state': 'idle'}}]} - await self.assertUpdate([True, True, constants.STATE_PAUSED, 2, constants.APP_ATV_LAUNCHER, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, constants.APP_ATV_LAUNCHER, [constants.APP_ATV_LAUNCHER], 'hmdi_arc', False, 0.5, None)) - - self.atv._state_detection_rules = {constants.APP_ATV_LAUNCHER: [{'idle': {'INVALID': 'idle'}}]} - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, constants.APP_ATV_LAUNCHER, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, constants.APP_ATV_LAUNCHER, [constants.APP_ATV_LAUNCHER], 'hmdi_arc', False, 0.5, None)) - - self.atv._state_detection_rules = None - - # Bell Fibe - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, constants.APP_BELL_FIBE, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, constants.APP_BELL_FIBE, [constants.APP_BELL_FIBE], 'hmdi_arc', False, 0.5, None)) - - # Netflix - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, constants.APP_NETFLIX, 2, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PAUSED, constants.APP_NETFLIX, [constants.APP_NETFLIX], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, constants.APP_NETFLIX, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PLAYING, constants.APP_NETFLIX, [constants.APP_NETFLIX], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, constants.APP_NETFLIX, 4, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, constants.APP_NETFLIX, [constants.APP_NETFLIX], 'hmdi_arc', False, 0.5, None)) - - # Plex - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, constants.APP_PLEX, 4, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, constants.APP_PLEX, [constants.APP_PLEX], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 3, constants.APP_PLEX, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PLAYING, constants.APP_PLEX, [constants.APP_PLEX], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 4, constants.APP_PLEX, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PLAYING, constants.APP_PLEX, [constants.APP_PLEX], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 5, constants.APP_PLEX, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PLAYING, constants.APP_PLEX, [constants.APP_PLEX], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 7, constants.APP_PLEX, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PLAYING, constants.APP_PLEX, [constants.APP_PLEX], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 1, constants.APP_PLEX, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PAUSED, constants.APP_PLEX, [constants.APP_PLEX], 'hmdi_arc', False, 0.5, None)) - - # TVheadend - await self.assertUpdate([True, True, constants.STATE_IDLE, 5, constants.APP_TVHEADEND, 4, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PAUSED, constants.APP_TVHEADEND, [constants.APP_TVHEADEND], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 6, constants.APP_TVHEADEND, 4, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PLAYING, constants.APP_TVHEADEND, [constants.APP_TVHEADEND], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 1, constants.APP_TVHEADEND, 4, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, constants.APP_TVHEADEND, [constants.APP_TVHEADEND], 'hmdi_arc', False, 0.5, None)) - - # VLC - await self.assertUpdate([True, True, constants.STATE_IDLE, 6, constants.APP_VLC, 2, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PAUSED, constants.APP_VLC, [constants.APP_VLC], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 6, constants.APP_VLC, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PLAYING, constants.APP_VLC, [constants.APP_VLC], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 6, constants.APP_VLC, 4, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, constants.APP_VLC, [constants.APP_VLC], 'hmdi_arc', False, 0.5, None)) - - # VRV - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, constants.APP_VRV, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, constants.APP_VRV, [constants.APP_VRV], 'hmdi_arc', False, 0.5, None)) - - # YouTube - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, constants.APP_YOUTUBE, 2, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PAUSED, constants.APP_YOUTUBE, [constants.APP_YOUTUBE], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, constants.APP_YOUTUBE, 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PLAYING, constants.APP_YOUTUBE, [constants.APP_YOUTUBE], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, constants.APP_YOUTUBE, 4, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, constants.APP_YOUTUBE, [constants.APP_YOUTUBE], 'hmdi_arc', False, 0.5, None)) - - # Unknown app - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, 'unknown', 2, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PAUSED, 'unknown', ['unknown'], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, 'unknown', 3, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PLAYING, 'unknown', ['unknown'], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, 'unknown', 4, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, 'unknown', ['unknown'], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_PLAYING, 2, 'unknown', None, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PLAYING, 'unknown', ['unknown'], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 1, 'unknown', None, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PAUSED, 'unknown', ['unknown'], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 2, 'unknown', None, 'hmdi_arc', False, 30, None, None], - (constants.STATE_PLAYING, 'unknown', ['unknown'], 'hmdi_arc', False, 0.5, None)) - - await self.assertUpdate([True, True, constants.STATE_IDLE, 3, 'unknown', None, 'hmdi_arc', False, 30, None, None], - (constants.STATE_IDLE, 'unknown', ['unknown'], 'hmdi_arc', False, 0.5, None)) + with patch_calls(self.atv, self.atv._update) as patched: + await self.atv.update() + assert patched.called class TestAndroidTVAsyncServer(TestAndroidTVAsyncPython): @@ -847,19 +261,5 @@ async def setUp(self): await self.atv.adb_connect() -class TestStateDetectionRulesValidator(unittest.TestCase): - def test_state_detection_rules_validator(self): - """Check that the ``state_detection_rules_validator`` function works correctly. - - """ - with async_patchers.patch_connect(True)['python'], async_patchers.patch_shell('')['python']: - # Make sure that no error is raised when the state detection rules are valid - AndroidTVAsync('HOST', 5555, state_detection_rules=STATE_DETECTION_RULES1) - AndroidTVAsync('HOST', 5555, state_detection_rules=STATE_DETECTION_RULES2) - AndroidTVAsync('HOST', 5555, state_detection_rules=STATE_DETECTION_RULES3) - AndroidTVAsync('HOST', 5555, state_detection_rules=STATE_DETECTION_RULES4) - AndroidTVAsync('HOST', 5555, state_detection_rules=STATE_DETECTION_RULES5) - - if __name__ == "__main__": unittest.main() diff --git a/tests/test_basetv_async.py b/tests/test_basetv_async.py index 287431ed..fe35a69a 100644 --- a/tests/test_basetv_async.py +++ b/tests/test_basetv_async.py @@ -6,66 +6,13 @@ sys.path.insert(0, '..') +import androidtv from androidtv import constants, ha_state_detection_rules_validator from androidtv.basetv.basetv_async import BaseTVAsync from . import async_patchers from .async_wrapper import awaiter - - -DEVICE_PROPERTIES_OUTPUT1 = """Amazon -AFTT -SERIALNO -5.1.1 - link/ether ab:cd:ef:gh:ij:kl brd ff:ff:ff:ff:ff:ff -Device "eth0" does not exist. -""" - -DEVICE_PROPERTIES_DICT1 = {'manufacturer': 'Amazon', - 'model': 'AFTT', - 'serialno': 'SERIALNO', - 'sw_version': '5.1.1', - 'wifimac': 'ab:cd:ef:gh:ij:kl', - 'ethmac': None} - -DEVICE_PROPERTIES_OUTPUT2 = """Amazon -AFTT - -5.1.1 - link/ether ab:cd:ef:gh:ij:kl brd ff:ff:ff:ff:ff:ff -Device "eth0" does not exist. -""" - -DEVICE_PROPERTIES_DICT2 = {'manufacturer': 'Amazon', - 'model': 'AFTT', - 'serialno': None, - 'sw_version': '5.1.1', - 'wifimac': 'ab:cd:ef:gh:ij:kl', - 'ethmac': None} - -DEVICE_PROPERTIES_OUTPUT3 = """Not Amazon -AFTT -SERIALNO -5.1.1 -Device "wlan0" does not exist. - link/ether ab:cd:ef:gh:ij:kl brd ff:ff:ff:ff:ff:ff -""" - -DEVICE_PROPERTIES_DICT3 = {'manufacturer': 'Not Amazon', - 'model': 'AFTT', - 'serialno': 'SERIALNO', - 'sw_version': '5.1.1', - 'wifimac': None, - 'ethmac': 'ab:cd:ef:gh:ij:kl'} - -# Source: https://community.home-assistant.io/t/new-chromecast-w-android-tv-integration-only-showing-as-off-or-idle/234424/15 -DEVICE_PROPERTIES_GOOGLE_TV = """Google -Chromecast -SERIALNO -10 - link/ether ab:cd:ef:gh:ij:kl brd ff:ff:ff:ff:ff:ff -Device "eth0" does not exist. -""" +from .patchers import patch_calls MEDIA_SESSION_STATE_OUTPUT = "com.amazon.tv.launcher\nstate=PlaybackState {state=2, position=0, buffered position=0, speed=0.0, updated=65749, actions=240640, custom actions=[], active item id=-1, error=null}" @@ -319,10 +266,10 @@ async def test_get_device_properties(self): """Check that ``get_device_properties`` works correctly. """ - with async_patchers.patch_shell(DEVICE_PROPERTIES_OUTPUT1)[self.PATCH_KEY]: - with patch("androidtv.basetv.basetv.BaseTV._parse_device_properties") as pdp: + with async_patchers.patch_shell("")[self.PATCH_KEY]: + with patch_calls(self.btv, self.btv._parse_device_properties) as patched: await self.btv.get_device_properties() - assert pdp.called + assert patched.called @awaiter async def test_awake(self): @@ -344,20 +291,9 @@ async def test_audio_state(self): """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - audio_state = await self.btv.audio_state() - self.assertIsNone(audio_state, None) - - with async_patchers.patch_shell('0')[self.PATCH_KEY]: - audio_state = await self.btv.audio_state() - self.assertEqual(audio_state, constants.STATE_IDLE) - - with async_patchers.patch_shell('1')[self.PATCH_KEY]: - audio_state = await self.btv.audio_state() - self.assertEqual(audio_state, constants.STATE_PAUSED) - - with async_patchers.patch_shell('2')[self.PATCH_KEY]: - audio_state = await self.btv.audio_state() - self.assertEqual(audio_state, constants.STATE_PLAYING) + with patch_calls(self.btv, self.btv._audio_state) as patched: + await self.btv.audio_state() + assert patched.called @awaiter async def test_current_app(self): @@ -365,16 +301,9 @@ async def test_current_app(self): """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - current_app = await self.btv.current_app() - self.assertIsNone(current_app, None) - - with async_patchers.patch_shell('')[self.PATCH_KEY]: - current_app = await self.btv.current_app() - self.assertIsNone(current_app, None) - - with async_patchers.patch_shell('com.amazon.tv.launcher')[self.PATCH_KEY]: - current_app = await self.btv.current_app() - self.assertEqual(current_app, "com.amazon.tv.launcher") + with patch_calls(self.btv, self.btv._current_app) as patched: + await self.btv.current_app() + assert patched.called @awaiter async def test_media_session_state(self): @@ -382,24 +311,9 @@ async def test_media_session_state(self): """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - media_session_state = await self.btv.media_session_state() - self.assertIsNone(media_session_state, None) - - with async_patchers.patch_shell('')[self.PATCH_KEY]: - media_session_state = await self.btv.media_session_state() - self.assertIsNone(media_session_state, None) - - with async_patchers.patch_shell('unknown.app\n ')[self.PATCH_KEY]: - media_session_state = await self.btv.media_session_state() - self.assertIsNone(media_session_state, None) - - with async_patchers.patch_shell('2')[self.PATCH_KEY]: - media_session_state = await self.btv.media_session_state() - self.assertIsNone(media_session_state, None) - - with async_patchers.patch_shell(MEDIA_SESSION_STATE_OUTPUT)[self.PATCH_KEY]: - media_session_state = await self.btv.media_session_state() - self.assertEqual(media_session_state, 2) + with patch_calls(self.btv, self.btv._current_app_media_session_state) as patched: + await self.btv.media_session_state() + assert patched.called @awaiter async def test_screen_on(self): @@ -415,48 +329,15 @@ async def test_screen_on(self): with async_patchers.patch_shell('1')[self.PATCH_KEY]: self.assertTrue(await self.btv.screen_on()) - @awaiter - async def test_state_detection_rules_validator(self): - """Check that the ``state_detection_rules_validator`` function works correctly. - - """ - with async_patchers.patch_connect(True)['python'], async_patchers.patch_shell('')['python']: - # Make sure that no error is raised when the state detection rules are valid - BaseTVAsync('HOST', 5555, state_detection_rules=STATE_DETECTION_RULES1) - BaseTVAsync('HOST', 5555, state_detection_rules=STATE_DETECTION_RULES2) - BaseTVAsync('HOST', 5555, state_detection_rules=STATE_DETECTION_RULES3) - BaseTVAsync('HOST', 5555, state_detection_rules=STATE_DETECTION_RULES4) - BaseTVAsync('HOST', 5555, state_detection_rules=STATE_DETECTION_RULES5) - - # Make sure that an error is raised when the state detection rules are invalid - self.assertRaises(TypeError, BaseTVAsync, 'HOST', 5555, '', state_detection_rules=STATE_DETECTION_RULES_INVALID1) - self.assertRaises(KeyError, BaseTVAsync, 'HOST', 5555, '', state_detection_rules=STATE_DETECTION_RULES_INVALID2) - self.assertRaises(KeyError, BaseTVAsync, 'HOST', 5555, '', state_detection_rules=STATE_DETECTION_RULES_INVALID3) - self.assertRaises(KeyError, BaseTVAsync, 'HOST', 5555, '', state_detection_rules=STATE_DETECTION_RULES_INVALID4) - self.assertRaises(KeyError, BaseTVAsync, 'HOST', 5555, '', state_detection_rules=STATE_DETECTION_RULES_INVALID5) - self.assertRaises(KeyError, BaseTVAsync, 'HOST', 5555, '', state_detection_rules=STATE_DETECTION_RULES_INVALID6) - self.assertRaises(KeyError, BaseTVAsync, 'HOST', 5555, '', state_detection_rules=STATE_DETECTION_RULES_INVALID7) - self.assertRaises(KeyError, BaseTVAsync, 'HOST', 5555, '', state_detection_rules=STATE_DETECTION_RULES_INVALID8) - self.assertRaises(KeyError, BaseTVAsync, 'HOST', 5555, '', state_detection_rules=STATE_DETECTION_RULES_INVALID9) - self.assertRaises(KeyError, BaseTVAsync, 'HOST', 5555, '', state_detection_rules=STATE_DETECTION_RULES_INVALID10) - self.assertRaises(KeyError, BaseTVAsync, 'HOST', 5555, '', state_detection_rules=STATE_DETECTION_RULES_INVALID11) - @awaiter async def test_wake_lock_size(self): """Check that the ``wake_lock_size`` property works correctly. """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - self.assertIsNone(await self.btv.wake_lock_size()) - - with async_patchers.patch_shell('')[self.PATCH_KEY]: - self.assertIsNone(await self.btv.wake_lock_size()) - - with async_patchers.patch_shell('Wake Locks: size=2')[self.PATCH_KEY]: - self.assertEqual(await self.btv.wake_lock_size(), 2) - - with async_patchers.patch_shell('INVALID')[self.PATCH_KEY]: - self.assertIsNone(await self.btv.wake_lock_size()) + with patch_calls(self.btv, self.btv._wake_lock_size) as patched: + await self.btv.wake_lock_size() + assert patched.called @awaiter async def test_get_hdmi_input(self): @@ -464,22 +345,9 @@ async def test_get_hdmi_input(self): """ with async_patchers.patch_shell("HDMI2")[self.PATCH_KEY]: - self.assertEqual(await self.btv.get_hdmi_input(), "HDMI2") - - with async_patchers.patch_shell("HDMI2\n")[self.PATCH_KEY]: - self.assertEqual(await self.btv.get_hdmi_input(), "HDMI2") - - with async_patchers.patch_shell("HDMI2\r\n")[self.PATCH_KEY]: - self.assertEqual(await self.btv.get_hdmi_input(), "HDMI2") - - with async_patchers.patch_shell("")[self.PATCH_KEY]: - self.assertIsNone(await self.btv.get_hdmi_input()) - - with async_patchers.patch_shell("\r\n")[self.PATCH_KEY]: - self.assertIsNone(await self.btv.get_hdmi_input()) - - with async_patchers.patch_shell(None)[self.PATCH_KEY]: - self.assertIsNone(await self.btv.get_hdmi_input()) + with patch_calls(self.btv, self.btv._get_hdmi_input) as patched: + await self.btv.get_hdmi_input() + assert patched.called @awaiter async def test_learn_sendevent(self): @@ -493,15 +361,5 @@ async def test_learn_sendevent(self): self.assertEqual(await self.btv.learn_sendevent(), "") -class TestHAStateDetectionRulesValidator(unittest.TestCase): - def test_ha_state_detection_rules_validator(self): - """Check that ``ha_state_detection_rules_validator()`` works correctly. - - """ - with self.assertRaises(AssertionError): - for app_id, rules in STATE_DETECTION_RULES_INVALID2.items(): - ha_state_detection_rules_validator(AssertionError)(rules) - - if __name__ == "__main__": unittest.main() diff --git a/tests/test_firetv_async.py b/tests/test_firetv_async.py index 82997d78..51a5c303 100644 --- a/tests/test_firetv_async.py +++ b/tests/test_firetv_async.py @@ -10,141 +10,7 @@ from . import async_patchers from .async_wrapper import awaiter - - -HDMI_INPUT_EMPTY = "\n" - -CURRENT_APP_OUTPUT = "com.amazon.tv.launcher" - -RUNNING_APPS_OUTPUT = """u0_a18 316 197 1189204 115000 ffffffff 00000000 S com.netflix.ninja -u0_a2 15121 197 998628 24628 ffffffff 00000000 S com.amazon.device.controllermanager""" - -RUNNING_APPS_LIST = ['com.netflix.ninja', 'com.amazon.device.controllermanager'] - -GET_PROPERTIES_OUTPUT1 = "" -GET_PROPERTIES_DICT1 = {'screen_on': False, - 'awake': False, - 'wake_lock_size': -1, - 'current_app': None, - 'media_session_state': None, - 'running_apps': None, - 'hdmi_input': None} -STATE1 = (constants.STATE_OFF, None, None, None) - -GET_PROPERTIES_OUTPUT2 = "1" -GET_PROPERTIES_DICT2 = {'screen_on': True, - 'awake': False, - 'wake_lock_size': -1, - 'current_app': None, - 'media_session_state': None, - 'running_apps': None, - 'hdmi_input': None} -STATE2 = (constants.STATE_STANDBY, None, None, None) - -GET_PROPERTIES_OUTPUT3 = """11Wake Locks: size=2 -com.amazon.tv.launcher - - -u0_a2 17243 197 998628 24932 ffffffff 00000000 S com.amazon.device.controllermanager -u0_a2 17374 197 995368 20764 ffffffff 00000000 S com.amazon.device.controllermanager:BluetoothReceiver""" -GET_PROPERTIES_DICT3 = {'screen_on': True, - 'awake': True, - 'wake_lock_size': 2, - 'current_app': 'com.amazon.tv.launcher', - 'media_session_state': None, - 'running_apps': ['com.amazon.device.controllermanager', 'com.amazon.device.controllermanager:BluetoothReceiver'], - 'hdmi_input': None} -STATE3 = (constants.STATE_IDLE, 'com.amazon.tv.launcher', ['com.amazon.device.controllermanager', 'com.amazon.device.controllermanager:BluetoothReceiver'], None) - -GET_PROPERTIES_OUTPUT3A = GET_PROPERTIES_OUTPUT3[0] -GET_PROPERTIES_OUTPUT3B = GET_PROPERTIES_OUTPUT3[:2] -GET_PROPERTIES_OUTPUT3C = GET_PROPERTIES_OUTPUT3.splitlines()[0] -GET_PROPERTIES_OUTPUT3D = '\n'.join(GET_PROPERTIES_OUTPUT3.splitlines()[:2]) -GET_PROPERTIES_OUTPUT3E = '\n'.join(GET_PROPERTIES_OUTPUT3.splitlines()[:3]) -GET_PROPERTIES_OUTPUT3F = '\n'.join(GET_PROPERTIES_OUTPUT3.splitlines()[:4]) + "HDMI" - -GET_PROPERTIES_DICT3A = {'screen_on': True, - 'awake': False, - 'wake_lock_size': -1, - 'current_app': None, - 'media_session_state': None, - 'running_apps': None, - 'hdmi_input': None} -GET_PROPERTIES_DICT3B = {'screen_on': True, - 'awake': True, - 'wake_lock_size': -1, - 'current_app': None, - 'media_session_state': None, - 'running_apps': None, - 'hdmi_input': None} -GET_PROPERTIES_DICT3C = {'screen_on': True, - 'awake': True, - 'wake_lock_size': 2, - 'current_app': None, - 'media_session_state': None, - 'running_apps': None, - 'hdmi_input': None} -GET_PROPERTIES_DICT3D = {'screen_on': True, - 'awake': True, - 'wake_lock_size': 2, - 'current_app': 'com.amazon.tv.launcher', - 'media_session_state': None, - 'running_apps': None, - 'hdmi_input': None} -GET_PROPERTIES_DICT3E = {'screen_on': True, - 'awake': True, - 'wake_lock_size': 2, - 'current_app': 'com.amazon.tv.launcher', - 'media_session_state': None, - 'running_apps': None, - 'hdmi_input': None} -GET_PROPERTIES_DICT3F = {'screen_on': True, - 'awake': True, - 'wake_lock_size': 2, - 'current_app': 'com.amazon.tv.launcher', - 'media_session_state': None, - 'running_apps': None, - 'hdmi_input': 'HDMI'} - -GET_PROPERTIES_OUTPUT4 = """11Wake Locks: size=2 -com.amazon.tv.launcher -state=PlaybackState {state=2, position=0, buffered position=0, speed=0.0, updated=65749, actions=240640, custom actions=[], active item id=-1, error=null}""" -GET_PROPERTIES_DICT4 = {'screen_on': True, - 'awake': True, - 'wake_lock_size': 2, - 'current_app': 'com.amazon.tv.launcher', - 'media_session_state': 2, - 'running_apps': None, - 'hdmi_input': None} - -GET_PROPERTIES_OUTPUT5 = """11Wake Locks: size=2 -com.amazon.tv.launcher -state=PlaybackState {state=2, position=0, buffered position=0, speed=0.0, updated=65749, actions=240640, custom actions=[], active item id=-1, error=null} - -u0_a2 17243 197 998628 24932 ffffffff 00000000 S com.amazon.device.controllermanager -u0_a2 17374 197 995368 20764 ffffffff 00000000 S com.amazon.device.controllermanager:BluetoothReceiver""" -GET_PROPERTIES_DICT5 = {'screen_on': True, - 'awake': True, - 'wake_lock_size': 2, - 'current_app': 'com.amazon.tv.launcher', - 'media_session_state': 2, - 'running_apps': ['com.amazon.device.controllermanager', 'com.amazon.device.controllermanager:BluetoothReceiver'], - 'hdmi_input': None} - -GET_PROPERTIES_DICT_NONE = {'screen_on': None, - 'awake': None, - 'wake_lock_size': None, - 'media_session_state': None, - 'current_app': None, - 'running_apps': None, - 'hdmi_input': None} -STATE_NONE = (None, None, None, None) - -STATE_DETECTION_RULES1 = {'com.amazon.tv.launcher': ['off']} -STATE_DETECTION_RULES2 = {'com.amazon.tv.launcher': ['media_session_state', 'off']} -STATE_DETECTION_RULES3 = {'com.amazon.tv.launcher': [{'standby': {'wake_lock_size': 2}}]} -STATE_DETECTION_RULES4 = {'com.amazon.tv.launcher': [{'standby': {'wake_lock_size': 1}}, 'paused']} -STATE_DETECTION_RULES5 = {'com.amazon.tv.launcher': ['audio_state']} +from .patchers import patch_calls class TestFireTVAsyncPython(unittest.TestCase): @@ -174,7 +40,7 @@ async def test_send_intent(self): """Test that the ``_send_intent`` method works correctly. """ - with async_patchers.patch_connect(True)[self.PATCH_KEY], async_patchers.patch_shell('output\r\nretcode')[self.PATCH_KEY]: + with async_patchers.patch_shell('output\r\nretcode')[self.PATCH_KEY]: result = await self.ftv._send_intent("TEST", constants.INTENT_LAUNCH) self.assertEqual(getattr(self.ftv._adb, self.ADB_ATTR).shell_cmd, "monkey -p TEST -c android.intent.category.LAUNCHER 1; echo $?") self.assertDictEqual(result, {'output': 'output', 'retcode': 'retcode'}) @@ -189,7 +55,7 @@ async def test_launch_app_stop_app(self): """Test that the ``FireTVAsync.launch_app`` and ``FireTVAsync.stop_app`` methods work correctly. """ - with async_patchers.patch_connect(True)[self.PATCH_KEY], async_patchers.patch_shell('')[self.PATCH_KEY]: + with async_patchers.patch_shell('')[self.PATCH_KEY]: await self.ftv.launch_app("TEST") self.assertEqual(getattr(self.ftv._adb, self.ADB_ATTR).shell_cmd, constants.CMD_LAUNCH_APP.format("TEST")) @@ -202,16 +68,9 @@ async def test_running_apps(self): """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - running_apps = await self.ftv.running_apps() - self.assertIsNone(running_apps, None) - - with async_patchers.patch_shell('')[self.PATCH_KEY]: - running_apps = await self.ftv.running_apps() - self.assertIsNone(running_apps, None) - - with async_patchers.patch_shell(RUNNING_APPS_OUTPUT)[self.PATCH_KEY]: - running_apps = await self.ftv.running_apps() - self.assertListEqual(running_apps, RUNNING_APPS_LIST) + with patch_calls(self.ftv, self.ftv._running_apps) as patched: + await self.ftv.running_apps() + assert patched.called @awaiter async def test_get_properties(self): @@ -219,267 +78,21 @@ async def test_get_properties(self): """ with async_patchers.patch_shell(None)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT_NONE) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT1)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT1) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT2)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT2) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3A)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3A) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3B)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3B) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3C)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3C) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3D)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3D) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3E)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3E) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3E)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True, get_running_apps=False) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3E) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3E)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=False, get_running_apps=False) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3E) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3F)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3F) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT4)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT4) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT4)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True, get_running_apps=False) - self.assertDictEqual(properties, GET_PROPERTIES_DICT4) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT5)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT5) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT5)[self.PATCH_KEY]: - properties = await self.ftv.get_properties_dict(lazy=False) - self.assertDictEqual(properties, GET_PROPERTIES_DICT5) + for get_running_apps in [True, False]: + for lazy in [True, False]: + with patch_calls(self.ftv, self.ftv._get_properties) as patched: + await self.ftv.get_properties_dict(get_running_apps, lazy) + assert patched.called @awaiter async def test_update(self): """Check that the ``update`` method works correctly. """ - with async_patchers.patch_connect(False)[self.PATCH_KEY]: - await self.ftv.adb_connect() - state = await self.ftv.update() - self.assertTupleEqual(state, STATE_NONE) - - with async_patchers.patch_connect(True)[self.PATCH_KEY]: - self.assertTrue(await self.ftv.adb_connect()) - with async_patchers.patch_shell(None)[self.PATCH_KEY]: - state = await self.ftv.update() - self.assertTupleEqual(state, STATE_NONE) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT1)[self.PATCH_KEY]: - state = await self.ftv.update() - self.assertTupleEqual(state, STATE1) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT2)[self.PATCH_KEY]: - state = await self.ftv.update() - self.assertTupleEqual(state, STATE2) - - with async_patchers.patch_shell(GET_PROPERTIES_OUTPUT3)[self.PATCH_KEY]: - state = await self.ftv.update() - self.assertTupleEqual(state, STATE3) - - self.ftv._state_detection_rules = STATE_DETECTION_RULES1 - state = await self.ftv.update() - self.assertEqual(state[0], constants.STATE_OFF) - - self.ftv._state_detection_rules = STATE_DETECTION_RULES2 - state = await self.ftv.update() - self.assertEqual(state[0], constants.STATE_OFF) - - self.ftv._state_detection_rules = STATE_DETECTION_RULES3 - state = await self.ftv.update() - self.assertEqual(state[0], constants.STATE_STANDBY) - - self.ftv._state_detection_rules = STATE_DETECTION_RULES4 - state = await self.ftv.update() - self.assertEqual(state[0], constants.STATE_PAUSED) - - self.ftv._state_detection_rules = STATE_DETECTION_RULES5 - state = await self.ftv.update() - self.assertEqual(state[0], constants.STATE_IDLE) - - async def assertUpdate(self, get_properties, update): - """Check that the results of the `update` method are as expected. - - """ - with async_patchers.async_patch('androidtv.firetv.firetv_async.FireTVAsync.get_properties', return_value=get_properties): - self.assertTupleEqual(await self.ftv.update(), update) - - @awaiter - async def test_state_detection(self): - """Check that the state detection works as expected. - - """ - await self.assertUpdate([False, None, -1, None, None, None, None], - (constants.STATE_OFF, None, None, None)) - - await self.assertUpdate([True, False, -1, None, None, None, None], - (constants.STATE_STANDBY, None, None, None)) - - await self.assertUpdate([True, True, 1, "com.amazon.tv.launcher", None, None, None], - (constants.STATE_IDLE, "com.amazon.tv.launcher", ["com.amazon.tv.launcher"], None)) - - # Amazon Video - await self.assertUpdate([True, True, 1, constants.APP_AMAZON_VIDEO, 3, [constants.APP_AMAZON_VIDEO], None], - (constants.STATE_PLAYING, constants.APP_AMAZON_VIDEO, [constants.APP_AMAZON_VIDEO], None)) - - await self.assertUpdate([True, True, 1, constants.APP_AMAZON_VIDEO, 2, [constants.APP_AMAZON_VIDEO], None], - (constants.STATE_PAUSED, constants.APP_AMAZON_VIDEO, [constants.APP_AMAZON_VIDEO], None)) - - await self.assertUpdate([True, True, 1, constants.APP_AMAZON_VIDEO, 1, [constants.APP_AMAZON_VIDEO], None], - (constants.STATE_IDLE, constants.APP_AMAZON_VIDEO, [constants.APP_AMAZON_VIDEO], None)) - - # Amazon Video with custom state detection rules - self.ftv._state_detection_rules = {constants.APP_AMAZON_VIDEO: ['media_session_state']} - - await self.assertUpdate([True, True, 2, constants.APP_AMAZON_VIDEO, 2, [constants.APP_AMAZON_VIDEO], None], - (constants.STATE_PAUSED, constants.APP_AMAZON_VIDEO, [constants.APP_AMAZON_VIDEO], None)) - - await self.assertUpdate([True, True, 5, constants.APP_AMAZON_VIDEO, 3, [constants.APP_AMAZON_VIDEO], None], - (constants.STATE_PLAYING, constants.APP_AMAZON_VIDEO, [constants.APP_AMAZON_VIDEO], None)) - - await self.assertUpdate([True, True, 5, constants.APP_AMAZON_VIDEO, 1, [constants.APP_AMAZON_VIDEO], None], - (constants.STATE_IDLE, constants.APP_AMAZON_VIDEO, [constants.APP_AMAZON_VIDEO], None)) - - self.ftv._state_detection_rules = {constants.APP_AMAZON_VIDEO: [{'standby': {'media_session_state': 2}}]} - await self.assertUpdate([True, True, 2, constants.APP_AMAZON_VIDEO, None, [constants.APP_AMAZON_VIDEO], None], - (constants.STATE_IDLE, constants.APP_AMAZON_VIDEO, [constants.APP_AMAZON_VIDEO], None)) - - # Firefox - await self.assertUpdate([True, True, 3, constants.APP_FIREFOX, 3, [constants.APP_FIREFOX], None], - (constants.STATE_PLAYING, constants.APP_FIREFOX, [constants.APP_FIREFOX], None)) - - await self.assertUpdate([True, True, 1, constants.APP_FIREFOX, 3, [constants.APP_FIREFOX], None], - (constants.STATE_IDLE, constants.APP_FIREFOX, [constants.APP_FIREFOX], None)) - - # Hulu - await self.assertUpdate([True, True, 4, constants.APP_HULU, 3, [constants.APP_HULU], None], - (constants.STATE_PLAYING, constants.APP_HULU, [constants.APP_HULU], None)) - - await self.assertUpdate([True, True, 2, constants.APP_HULU, 3, [constants.APP_HULU], None], - (constants.STATE_PAUSED, constants.APP_HULU, [constants.APP_HULU], None)) - - await self.assertUpdate([True, True, 1, constants.APP_HULU, 3, [constants.APP_HULU], None], - (constants.STATE_IDLE, constants.APP_HULU, [constants.APP_HULU], None)) - - # Jellyfin - await self.assertUpdate([True, True, 2, constants.APP_JELLYFIN_TV, 3, [constants.APP_JELLYFIN_TV], None], - (constants.STATE_PLAYING, constants.APP_JELLYFIN_TV, [constants.APP_JELLYFIN_TV], None)) - - await self.assertUpdate([True, True, 4, constants.APP_JELLYFIN_TV, 3, [constants.APP_JELLYFIN_TV], None], - (constants.STATE_PAUSED, constants.APP_JELLYFIN_TV, [constants.APP_JELLYFIN_TV], None)) - - # Netfilx - await self.assertUpdate([True, True, 1, constants.APP_NETFLIX, 3, [constants.APP_NETFLIX], None], - (constants.STATE_PLAYING, constants.APP_NETFLIX, [constants.APP_NETFLIX], None)) - - await self.assertUpdate([True, True, 1, constants.APP_NETFLIX, 2, [constants.APP_NETFLIX], None], - (constants.STATE_PAUSED, constants.APP_NETFLIX, [constants.APP_NETFLIX], None)) - - await self.assertUpdate([True, True, 1, constants.APP_NETFLIX, 1, [constants.APP_NETFLIX], None], - (constants.STATE_IDLE, constants.APP_NETFLIX, [constants.APP_NETFLIX], None)) - - # Plex - await self.assertUpdate([True, True, 1, constants.APP_PLEX, 3, [constants.APP_PLEX], None], - (constants.STATE_PLAYING, constants.APP_PLEX, [constants.APP_PLEX], None)) - - await self.assertUpdate([True, True, 2, constants.APP_PLEX, 3, [constants.APP_PLEX], None], - (constants.STATE_PAUSED, constants.APP_PLEX, [constants.APP_PLEX], None)) - - await self.assertUpdate([True, True, 1, constants.APP_PLEX, 1, [constants.APP_PLEX], None], - (constants.STATE_IDLE, constants.APP_PLEX, [constants.APP_PLEX], None)) - - # Sport 1 - await self.assertUpdate([True, True, 3, constants.APP_SPORT1, 3, [constants.APP_SPORT1], None], - (constants.STATE_PLAYING, constants.APP_SPORT1, [constants.APP_SPORT1], None)) - - await self.assertUpdate([True, True, 2, constants.APP_SPORT1, 3, [constants.APP_SPORT1], None], - (constants.STATE_PAUSED, constants.APP_SPORT1, [constants.APP_SPORT1], None)) - - await self.assertUpdate([True, True, 1, constants.APP_SPORT1, 3, [constants.APP_SPORT1], None], - (constants.STATE_IDLE, constants.APP_SPORT1, [constants.APP_SPORT1], None)) - - # Spotify - await self.assertUpdate([True, True, 1, constants.APP_SPOTIFY, 3, [constants.APP_SPOTIFY], None], - (constants.STATE_PLAYING, constants.APP_SPOTIFY, [constants.APP_SPOTIFY], None)) - - await self.assertUpdate([True, True, 1, constants.APP_SPOTIFY, 2, [constants.APP_SPOTIFY], None], - (constants.STATE_PAUSED, constants.APP_SPOTIFY, [constants.APP_SPOTIFY], None)) - - await self.assertUpdate([True, True, 1, constants.APP_SPOTIFY, 1, [constants.APP_SPOTIFY], None], - (constants.STATE_IDLE, constants.APP_SPOTIFY, [constants.APP_SPOTIFY], None)) - - # Twitch - await self.assertUpdate([True, True, 2, constants.APP_TWITCH, 3, [constants.APP_TWITCH], None], - (constants.STATE_PAUSED, constants.APP_TWITCH, [constants.APP_TWITCH], None)) - - await self.assertUpdate([True, True, 1, constants.APP_TWITCH, 3, [constants.APP_TWITCH], None], - (constants.STATE_PLAYING, constants.APP_TWITCH, [constants.APP_TWITCH], None)) - - await self.assertUpdate([True, True, 1, constants.APP_TWITCH, 4, [constants.APP_TWITCH], None], - (constants.STATE_PLAYING, constants.APP_TWITCH, [constants.APP_TWITCH], None)) - - await self.assertUpdate([True, True, 1, constants.APP_TWITCH, 1, [constants.APP_TWITCH], None], - (constants.STATE_IDLE, constants.APP_TWITCH, [constants.APP_TWITCH], None)) - - # Waipu TV - await self.assertUpdate([True, True, 3, constants.APP_WAIPU_TV, 1, [constants.APP_WAIPU_TV], None], - (constants.STATE_PLAYING, constants.APP_WAIPU_TV, [constants.APP_WAIPU_TV], None)) - - await self.assertUpdate([True, True, 2, constants.APP_WAIPU_TV, 1, [constants.APP_WAIPU_TV], None], - (constants.STATE_PAUSED, constants.APP_WAIPU_TV, [constants.APP_WAIPU_TV], None)) - - await self.assertUpdate([True, True, 1, constants.APP_WAIPU_TV, 1, [constants.APP_WAIPU_TV], None], - (constants.STATE_IDLE, constants.APP_WAIPU_TV, [constants.APP_WAIPU_TV], None)) - - # Unknown app - await self.assertUpdate([True, True, 1, 'unknown', 3, ['unknown'], None], - (constants.STATE_PLAYING, 'unknown', ['unknown'], None)) - - await self.assertUpdate([True, True, 1, 'unknown', 2, ['unknown'], None], - (constants.STATE_PAUSED, 'unknown', ['unknown'], None)) - - await self.assertUpdate([True, True, 1, 'unknown', 1, ['unknown'], None], - (constants.STATE_IDLE, 'unknown', ['unknown'], None)) - - await self.assertUpdate([True, True, 1, 'unknown', None, ['unknown'], None], - (constants.STATE_PLAYING, 'unknown', ['unknown'], None)) - - await self.assertUpdate([True, True, 2, 'unknown', None, ['unknown'], None], - (constants.STATE_PAUSED, 'unknown', ['unknown'], None)) + with patch_calls(self.ftv, self.ftv._get_properties) as patched: + await self.ftv.update() + assert patched.called class TestFireTVAsyncServer(TestFireTVAsyncPython): From bdbaed6d05ba271af7986b2554fdd775bf65a1a1 Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Tue, 3 Nov 2020 07:56:27 -0800 Subject: [PATCH 3/5] Remove unused import --- tests/patchers.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/patchers.py b/tests/patchers.py index dae67016..dd2649fc 100644 --- a/tests/patchers.py +++ b/tests/patchers.py @@ -1,7 +1,5 @@ """Define patches used for androidtv tests.""" -import copy - try: # Python3 from unittest.mock import patch From b3ad291e74cafc6cb82462d8402a0e1711215007 Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Thu, 5 Nov 2020 07:49:15 -0800 Subject: [PATCH 4/5] Remove unnecessary import amd constants --- tests/test_basetv_async.py | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/tests/test_basetv_async.py b/tests/test_basetv_async.py index fe35a69a..91d8c057 100644 --- a/tests/test_basetv_async.py +++ b/tests/test_basetv_async.py @@ -7,33 +7,13 @@ sys.path.insert(0, '..') import androidtv -from androidtv import constants, ha_state_detection_rules_validator +from androidtv import constants from androidtv.basetv.basetv_async import BaseTVAsync from . import async_patchers from .async_wrapper import awaiter from .patchers import patch_calls -MEDIA_SESSION_STATE_OUTPUT = "com.amazon.tv.launcher\nstate=PlaybackState {state=2, position=0, buffered position=0, speed=0.0, updated=65749, actions=240640, custom actions=[], active item id=-1, error=null}" - -STATE_DETECTION_RULES1 = {'com.amazon.tv.launcher': ['off']} -STATE_DETECTION_RULES2 = {'com.amazon.tv.launcher': ['media_session_state', 'off']} -STATE_DETECTION_RULES3 = {'com.amazon.tv.launcher': [{'standby': {'wake_lock_size': 2}}]} -STATE_DETECTION_RULES4 = {'com.amazon.tv.launcher': [{'standby': {'wake_lock_size': 1}}, 'paused']} -STATE_DETECTION_RULES5 = {'com.amazon.tv.launcher': ['audio_state']} - -STATE_DETECTION_RULES_INVALID1 = {123: ['media_session_state']} -STATE_DETECTION_RULES_INVALID2 = {'com.amazon.tv.launcher': [123]} -STATE_DETECTION_RULES_INVALID3 = {'com.amazon.tv.launcher': ['INVALID']} -STATE_DETECTION_RULES_INVALID4 = {'com.amazon.tv.launcher': [{'INVALID': {'wake_lock_size': 2}}]} -STATE_DETECTION_RULES_INVALID5 = {'com.amazon.tv.launcher': [{'standby': 'INVALID'}]} -STATE_DETECTION_RULES_INVALID6 = {'com.amazon.tv.launcher': [{'standby': {'INVALID': 2}}]} -STATE_DETECTION_RULES_INVALID7 = {'com.amazon.tv.launcher': [{'standby': {'wake_lock_size': 'INVALID'}}]} -STATE_DETECTION_RULES_INVALID8 = {'com.amazon.tv.launcher': [{'standby': {'media_session_state': 'INVALID'}}]} -STATE_DETECTION_RULES_INVALID9 = {'com.amazon.tv.launcher': [{'standby': {'audio_state': 123}}]} -STATE_DETECTION_RULES_INVALID10 = {'com.amazon.tv.launcher': [{'standby': {'media_session_state': 'INVALID'}}]} -STATE_DETECTION_RULES_INVALID11 = {'com.amazon.tv.launcher': [{'standby': {'audio_state': 123}}]} - PNG_IMAGE = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\n\x00\x00\x00\n\x08\x06\x00\x00\x00\x8d2\xcf\xbd\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\x00\x00\tpHYs\x00\x00\x0fa\x00\x00\x0fa\x01\xa8?\xa7i\x00\x00\x00\x0eIDAT\x18\x95c`\x18\x05\x83\x13\x00\x00\x01\x9a\x00\x01\x16\xca\xd3i\x00\x00\x00\x00IEND\xaeB`\x82' From 8de619cb3893f43113d8c32935bbfc1ab3a0a814 Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Thu, 5 Nov 2020 07:55:52 -0800 Subject: [PATCH 5/5] Remove unused import --- tests/test_firetv_async.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_firetv_async.py b/tests/test_firetv_async.py index 51a5c303..c54b9905 100644 --- a/tests/test_firetv_async.py +++ b/tests/test_firetv_async.py @@ -5,7 +5,7 @@ sys.path.insert(0, '..') -from androidtv import constants, ha_state_detection_rules_validator +from androidtv import constants from androidtv.firetv.firetv_async import FireTVAsync from . import async_patchers