Skip to content

Commit

Permalink
Merge 5427521 into cdc291d
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Jan 4, 2020
2 parents cdc291d + 5427521 commit e9440a9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
7 changes: 1 addition & 6 deletions androidtv/basetv.py
Expand Up @@ -813,13 +813,8 @@ def launch_app(self, app):
app : str
The ID of the app that will be launched
Returns
-------
dict
A dictionary with keys ``'output'`` and ``'retcode'``, if they could be determined; otherwise, an empty dictionary
"""
return self._send_intent(app, constants.INTENT_LAUNCH)
self._adb.shell(constants.CMD_LAUNCH_APP.format(app))

def stop_app(self, app):
"""Stop an app.
Expand Down
13 changes: 8 additions & 5 deletions androidtv/constants.py
Expand Up @@ -11,6 +11,11 @@
import re


# Intents
INTENT_LAUNCH = "android.intent.category.LAUNCHER"
INTENT_HOME = "android.intent.category.HOME"


# echo '1' if the previous shell command was successful
CMD_SUCCESS1 = r" && echo -e '1\c'"

Expand All @@ -26,6 +31,9 @@
#: Get the current app
CMD_CURRENT_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP"

#: Launch an app if it is not already the current app
CMD_LAUNCH_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${{CURRENT_APP#*{{* * }} && CURRENT_APP=${{CURRENT_APP%%/*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " 1; fi"

#: Get the state from ``dumpsys media_session``; this assumes that the variable ``CURRENT_APP`` has been defined
CMD_MEDIA_SESSION_STATE = "dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {'"

Expand Down Expand Up @@ -82,11 +90,6 @@
CMD_MAC_ETH0 = "ip addr show eth0 | grep -m 1 ether"


# Intents
INTENT_LAUNCH = "android.intent.category.LAUNCHER"
INTENT_HOME = "android.intent.category.HOME"


# ADB key event codes
# https://developer.android.com/reference/android/view/KeyEvent
KEY_BACK = 4
Expand Down
18 changes: 13 additions & 5 deletions tests/test_firetv.py
Expand Up @@ -147,20 +147,28 @@ def test_turn_on_off(self):
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.
def test_send_intent(self):
"""Test that the ``_send_intent`` method works correctly.
"""
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell('output\r\nretcode')[self.PATCH_KEY]:
result = self.ftv.launch_app("TEST")
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell('output\r\nretcode')[self.PATCH_KEY]:
result = 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'})

with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell(None)[self.PATCH_KEY]:
result = self.ftv.launch_app("TEST")
result = 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, {})

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(None)[self.PATCH_KEY]:
self.ftv.launch_app("TEST")
self.assertEqual(getattr(self.ftv._adb, self.ADB_ATTR).shell_cmd, constants.CMD_LAUNCH_APP.format("TEST"))

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

Expand Down

0 comments on commit e9440a9

Please sign in to comment.