Skip to content

Commit

Permalink
Merge abe39de into dfb353c
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Feb 7, 2021
2 parents dfb353c + abe39de commit 979ef67
Show file tree
Hide file tree
Showing 6 changed files with 464 additions and 35 deletions.
26 changes: 19 additions & 7 deletions androidtv/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,32 @@
#: Determine whether the device is awake
CMD_AWAKE = "dumpsys power | grep mWakefulness | grep -q Awake"

#: 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"
#: Parse the current app from the dumpsys outputand store it in the variable ``CURRENT_APP``
CMD_PARSE_CURRENT_APP = 'CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\\}*}'

#: Get the current app (but don't echo it)
CMD_GET_CURRENT_APP = "CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && " + CMD_PARSE_CURRENT_APP

#: Get the current app and echo it
CMD_CURRENT_APP = CMD_GET_CURRENT_APP + ' && echo $CURRENT_APP'

#: Define current app for a Google TV device
CMD_GET_CURRENT_APP_GOOGLE_TV = 'CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && ' + CMD_PARSE_CURRENT_APP

#: Get the current app for a Google TV device
CMD_CURRENT_APP_GOOGLE_TV = "CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP"
CMD_CURRENT_APP_GOOGLE_TV = CMD_GET_CURRENT_APP_GOOGLE_TV + ' && echo $CURRENT_APP'

#: Get the HDMI input
CMD_HDMI_INPUT = "dumpsys activity starter | grep -o 'HDMIInputService\\/HW[0-9]' -m 1 | grep -o 'HW[0-9]'"
CMD_HDMI_INPUT = "dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' | grep -o 'HW[0-9]'"

#: Launch an app if it is not already the current app (assumes the variable ``CURRENT_APP`` has already been set)
CMD_LAUNCH_APP_CONDITION = "if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi"

#: 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 + " --pct-syskeys 0 1; fi"
CMD_LAUNCH_APP = CMD_GET_CURRENT_APP.replace('{', '{{').replace('}', '}}') + ' && ' + CMD_LAUNCH_APP_CONDITION

#: Launch an app if it is not already the current app (for Google TV devices)
CMD_LAUNCH_APP_GOOGLE_TV = "CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${{CURRENT_APP%%/*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi"
#: Launch an app on a Google TV device
CMD_LAUNCH_APP_GOOGLE_TV = CMD_GET_CURRENT_APP_GOOGLE_TV.replace('{', '{{').replace('}', '}}') + ' && ' + CMD_LAUNCH_APP_CONDITION

#: 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
4 changes: 2 additions & 2 deletions tests/test_androidtv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
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_OUTPUT3G = '\n'.join(GET_PROPERTIES_OUTPUT3.splitlines()[:4]) + "HW2"

GET_PROPERTIES_DICT3A = {'screen_on': True,
'awake': False,
Expand Down Expand Up @@ -196,7 +196,7 @@
'is_volume_muted': None,
'volume': None,
'running_apps': None,
'hdmi_input': 'HDMI'}
'hdmi_input': 'HW2'}

GET_PROPERTIES_OUTPUT4 = """111Wake Locks: size=2
com.amazon.tv.launcher
Expand Down
2 changes: 1 addition & 1 deletion tests/test_basetv_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ async def test_get_hdmi_input(self):
"""Check that the ``get_hdmi_input`` function works correctly.
"""
with async_patchers.patch_shell("HDMI2")[self.PATCH_KEY]:
with async_patchers.patch_shell("HW2")[self.PATCH_KEY]:
with patch_calls(self.btv, self.btv._get_hdmi_input) as patched:
await self.btv.get_hdmi_input()
assert patched.called
Expand Down
33 changes: 27 additions & 6 deletions tests/test_basetv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@
Device "eth0" does not exist.
"""

DEVICE_PROPERTIES_OUTPUT_SONY_TV = """Sony
BRAVIA 4K GB
SERIALNO
8.0.0
link/ether 11:22:33:44:55:66 brd ff:ff:ff:ff:ff:ff
link/ether ab:cd:ef:gh:ij:kl brd ff:ff:ff:ff:ff:ff
"""

DEVICE_PROPERTIES_DICT_SONY_TV = {
'manufacturer': 'Sony',
'model': 'BRAVIA 4K GB',
'serialno': 'SERIALNO',
'sw_version': '8.0.0',
'wifimac': '11:22:33:44:55:66',
'ethmac': 'ab:cd:ef:gh:ij:kl'
}

INSTALLED_APPS_OUTPUT_1 = """package:org.example.app
package:org.example.launcher
"""
Expand Down Expand Up @@ -350,6 +367,10 @@ def test_get_device_properties(self):
self.assertEqual(self.btv.device_properties["manufacturer"], "Google")
self.assertEqual(self.btv._cmd_get_properties_lazy_no_running_apps, constants.CMD_GOOGLE_TV_PROPERTIES_LAZY_NO_RUNNING_APPS)

with patchers.patch_shell(DEVICE_PROPERTIES_OUTPUT_SONY_TV)[self.PATCH_KEY]:
device_properties = self.btv.get_device_properties()
self.assertDictEqual(DEVICE_PROPERTIES_DICT_SONY_TV, device_properties)

def test_get_installed_apps(self):
""""Check that `get_installed_apps` works correctly.
Expand Down Expand Up @@ -493,14 +514,14 @@ def test_get_hdmi_input(self):
"""Check that the ``get_hdmi_input`` function works correctly.
"""
with patchers.patch_shell("HDMI2")[self.PATCH_KEY]:
self.assertEqual(self.btv.get_hdmi_input(), "HDMI2")
with patchers.patch_shell("HW2")[self.PATCH_KEY]:
self.assertEqual(self.btv.get_hdmi_input(), "HW2")

with patchers.patch_shell("HDMI2\n")[self.PATCH_KEY]:
self.assertEqual(self.btv.get_hdmi_input(), "HDMI2")
with patchers.patch_shell("HW2\n")[self.PATCH_KEY]:
self.assertEqual(self.btv.get_hdmi_input(), "HW2")

with patchers.patch_shell("HDMI2\r\n")[self.PATCH_KEY]:
self.assertEqual(self.btv.get_hdmi_input(), "HDMI2")
with patchers.patch_shell("HW2\r\n")[self.PATCH_KEY]:
self.assertEqual(self.btv.get_hdmi_input(), "HW2")

with patchers.patch_shell("")[self.PATCH_KEY]:
self.assertIsNone(self.btv.get_hdmi_input())
Expand Down

0 comments on commit 979ef67

Please sign in to comment.