Skip to content

Commit

Permalink
Get firetv coverage to 100% (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Sep 4, 2019
1 parent 3e285ca commit 7c7c4c4
Show file tree
Hide file tree
Showing 3 changed files with 349 additions and 126 deletions.
194 changes: 112 additions & 82 deletions tests/test_androidtv.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,25 @@ def test_device(self):
device = self.atv.device
self.assertEqual('hmdi_arc', device)

def test_turn_on_off(self):
"""Test that the ``AndroidTV.turn_on`` and ``AndroidTV.turn_off`` methods work correctly.
"""
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell('')[self.PATCH_KEY]:
self.atv.turn_on()
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, constants.CMD_SCREEN_ON + " || input keyevent {0}".format(constants.KEY_POWER))

self.atv.turn_off()
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, constants.CMD_SCREEN_ON + " && input keyevent {0}".format(constants.KEY_POWER))

def test_start_intent(self):
"""Test that the ``AndroidTV.start_intent`` method works correctly.
"""
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell('')[self.PATCH_KEY]:
self.atv.start_intent("TEST")
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "am start -a android.intent.action.VIEW -d TEST")

def test_volume(self):
"""Check that the ``volume`` property works correctly.
Expand Down Expand Up @@ -525,6 +544,88 @@ def test_is_volume_muted(self):
is_volume_muted = self.atv.is_volume_muted
self.assertFalse(is_volume_muted)

def test_set_volume_level(self):
"""Check that the ``set_volume_level`` method works correctly.
"""
with patchers.patch_shell(None)[self.PATCH_KEY]:
new_volume_level = self.atv.set_volume_level(0.5)
self.assertIsNone(new_volume_level)

with patchers.patch_shell('')[self.PATCH_KEY]:
new_volume_level = self.atv.set_volume_level(0.5)
self.assertIsNone(new_volume_level)

with patchers.patch_shell(DUMPSYS_AUDIO_ON)[self.PATCH_KEY]:
new_volume_level = self.atv.set_volume_level(0.5)
self.assertEqual(new_volume_level, 0.5)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "(input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24) &")

with patchers.patch_shell('')[self.PATCH_KEY]:
new_volume_level = self.atv.set_volume_level(0.5, 22./60)
self.assertEqual(new_volume_level, 0.5)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "(input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24) &")

def test_volume_up(self):
"""Check that the ``volume_up`` method works correctly.
"""
with patchers.patch_shell(None)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_up()
self.assertIsNone(new_volume_level)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")

with patchers.patch_shell('')[self.PATCH_KEY]:
new_volume_level = self.atv.volume_up()
self.assertIsNone(new_volume_level)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")

with patchers.patch_shell(DUMPSYS_AUDIO_ON)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_up()
self.assertEqual(new_volume_level, 23./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")
new_volume_level = self.atv.volume_up(23./60)
self.assertEqual(new_volume_level, 24./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")

with patchers.patch_shell(DUMPSYS_AUDIO_OFF)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_up()
self.assertEqual(new_volume_level, 21./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")
new_volume_level = self.atv.volume_up(21./60)
self.assertEqual(new_volume_level, 22./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")

def test_volume_down(self):
"""Check that the ``volume_down`` method works correctly.
"""
with patchers.patch_shell(None)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_down()
self.assertIsNone(new_volume_level)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")

with patchers.patch_shell('')[self.PATCH_KEY]:
new_volume_level = self.atv.volume_down()
self.assertIsNone(new_volume_level)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")

with patchers.patch_shell(DUMPSYS_AUDIO_ON)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_down()
self.assertEqual(new_volume_level, 21./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")
new_volume_level = self.atv.volume_down(21./60)
self.assertEqual(new_volume_level, 20./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")

with patchers.patch_shell(DUMPSYS_AUDIO_OFF)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_down()
self.assertEqual(new_volume_level, 19./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")
new_volume_level = self.atv.volume_down(19./60)
self.assertEqual(new_volume_level, 18./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")

def test_get_properties(self):
"""Check that ``get_properties()`` works correctly.
Expand Down Expand Up @@ -633,88 +734,6 @@ def test_update(self):
state = self.atv.update()
self.assertEqual(state[0], constants.STATE_IDLE)

def test_set_volume_level(self):
"""Check that the ``set_volume_level`` method works correctly.
"""
with patchers.patch_shell(None)[self.PATCH_KEY]:
new_volume_level = self.atv.set_volume_level(0.5)
self.assertIsNone(new_volume_level)

with patchers.patch_shell('')[self.PATCH_KEY]:
new_volume_level = self.atv.set_volume_level(0.5)
self.assertIsNone(new_volume_level)

with patchers.patch_shell(DUMPSYS_AUDIO_ON)[self.PATCH_KEY]:
new_volume_level = self.atv.set_volume_level(0.5)
self.assertEqual(new_volume_level, 0.5)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "(input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24) &")

with patchers.patch_shell('')[self.PATCH_KEY]:
new_volume_level = self.atv.set_volume_level(0.5, 22./60)
self.assertEqual(new_volume_level, 0.5)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "(input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24 && sleep 1 && input keyevent 24) &")

def test_volume_up(self):
"""Check that the ``volume_up`` method works correctly.
"""
with patchers.patch_shell(None)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_up()
self.assertIsNone(new_volume_level)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")

with patchers.patch_shell('')[self.PATCH_KEY]:
new_volume_level = self.atv.volume_up()
self.assertIsNone(new_volume_level)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")

with patchers.patch_shell(DUMPSYS_AUDIO_ON)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_up()
self.assertEqual(new_volume_level, 23./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")
new_volume_level = self.atv.volume_up(23./60)
self.assertEqual(new_volume_level, 24./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")

with patchers.patch_shell(DUMPSYS_AUDIO_OFF)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_up()
self.assertEqual(new_volume_level, 21./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")
new_volume_level = self.atv.volume_up(21./60)
self.assertEqual(new_volume_level, 22./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 24")

def test_volume_down(self):
"""Check that the ``volume_down`` method works correctly.
"""
with patchers.patch_shell(None)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_down()
self.assertIsNone(new_volume_level)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")

with patchers.patch_shell('')[self.PATCH_KEY]:
new_volume_level = self.atv.volume_down()
self.assertIsNone(new_volume_level)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")

with patchers.patch_shell(DUMPSYS_AUDIO_ON)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_down()
self.assertEqual(new_volume_level, 21./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")
new_volume_level = self.atv.volume_down(21./60)
self.assertEqual(new_volume_level, 20./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")

with patchers.patch_shell(DUMPSYS_AUDIO_OFF)[self.PATCH_KEY]:
new_volume_level = self.atv.volume_down()
self.assertEqual(new_volume_level, 19./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")
new_volume_level = self.atv.volume_down(19./60)
self.assertEqual(new_volume_level, 18./60)
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent 25")

def assertUpdate(self, get_properties, update):
"""Check that the results of the `update` method are as expected.
Expand All @@ -737,6 +756,17 @@ def test_state_detection(self):
self.assertUpdate([True, True, 2, constants.APP_ATV_LAUNCHER, 3, constants.STATE_IDLE, 'hmdi_arc', False, 30],
(constants.STATE_STANDBY, constants.APP_ATV_LAUNCHER, 'hmdi_arc', False, 0.5))

# ATV Launcher with custom state detection
self.atv._state_detection_rules = {constants.APP_ATV_LAUNCHER: [{'idle': {'audio_state': 'idle'}}]}
self.assertUpdate([True, True, 2, constants.APP_ATV_LAUNCHER, 3, None, 'hmdi_arc', False, 30],
(constants.STATE_STANDBY, constants.APP_ATV_LAUNCHER, 'hmdi_arc', False, 0.5))

self.atv._state_detection_rules = {constants.APP_ATV_LAUNCHER: [{'idle': {'INVALID': 'idle'}}]}
self.assertUpdate([True, True, 2, constants.APP_ATV_LAUNCHER, 3, None, 'hmdi_arc', False, 30],
(constants.STATE_STANDBY, constants.APP_ATV_LAUNCHER, 'hmdi_arc', False, 0.5))

self.atv._state_detection_rules = None

# Bell Fibe
self.assertUpdate([True, True, 2, constants.APP_BELL_FIBE, 3, constants.STATE_IDLE, 'hmdi_arc', False, 30],
(constants.STATE_IDLE, constants.APP_BELL_FIBE, 'hmdi_arc', False, 0.5))
Expand Down
41 changes: 0 additions & 41 deletions tests/test_basetv.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,25 +188,6 @@ def test_keys(self):
self.atv.media_previous_track()
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_PREVIOUS))

def test_turn_on_off(self):
"""Test that the ``AndroidTV.turn_on`` and ``AndroidTV.turn_off`` methods work correctly.
"""
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell('')[self.PATCH_KEY]:
self.atv.turn_on()
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, constants.CMD_SCREEN_ON + " || input keyevent {0}".format(constants.KEY_POWER))

self.atv.turn_off()
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, constants.CMD_SCREEN_ON + " && input keyevent {0}".format(constants.KEY_POWER))

def test_start_intent(self):
"""Test that the ``AndroidTV.start_intent`` method works correctly.
"""
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell('')[self.PATCH_KEY]:
self.atv.start_intent("TEST")
self.assertEqual(getattr(self.atv.adb, self.ADB_ATTR).shell_cmd, "am start -a android.intent.action.VIEW -d TEST")


class TestFireTVSimplePython(unittest.TestCase):
PATCH_KEY = 'python'
Expand All @@ -216,28 +197,6 @@ def setUp(self):
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell('')[self.PATCH_KEY]:
self.ftv = FireTV('IP:PORT')

def test_turn_on_off(self):
"""Test that the ``FireTV.turn_on`` and ``FireTV.turn_off`` methods work correctly.
"""
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell('')[self.PATCH_KEY]:
self.ftv.turn_on()
self.assertEqual(getattr(self.ftv.adb, self.ADB_ATTR).shell_cmd, constants.CMD_SCREEN_ON + " || (input keyevent {0} && input keyevent {1})".format(constants.KEY_POWER, constants.KEY_HOME))

self.ftv.turn_off()
self.assertEqual(getattr(self.ftv.adb, self.ADB_ATTR).shell_cmd, constants.CMD_SCREEN_ON + " && input keyevent {0}".format(constants.KEY_SLEEP))

def test_launch_app_stop_app(self):
"""Test that the ``FireTV.launch_app`` and ``FireTV.stop_app`` methods work correctly.
"""
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell('')[self.PATCH_KEY]:
self.ftv.launch_app("TEST")
self.assertEqual(getattr(self.ftv.adb, self.ADB_ATTR).shell_cmd, "monkey -p TEST -c android.intent.category.LAUNCHER 1; echo $?")

self.ftv.stop_app("TEST")
self.assertEqual(getattr(self.ftv.adb, self.ADB_ATTR).shell_cmd, "am force-stop TEST")


if __name__ == "__main__":
unittest.main()
Loading

0 comments on commit 7c7c4c4

Please sign in to comment.