File: test_firetv_sync.py

package info (click to toggle)
python-androidtv 0.0.73-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 792 kB
  • sloc: python: 7,123; makefile: 188; sh: 105
file content (425 lines) | stat: -rw-r--r-- 18,320 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import sys
import unittest

try:
    # Python3
    from unittest.mock import patch
except ImportError:
    # Python2
    from mock import patch


sys.path.insert(0, "..")

from androidtv import constants, ha_state_detection_rules_validator
from androidtv.firetv.firetv_sync import FireTVSync
from . import patchers


UNKNOWN_APP = "unknown"

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"]

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"]}


class TestFireTVSyncPython(unittest.TestCase):
    ADB_ATTR = "_adb"
    PATCH_KEY = "python"

    def setUp(self):
        with patchers.PATCH_ADB_DEVICE_TCP, patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell("")[
            self.PATCH_KEY
        ]:
            self.ftv = FireTVSync("HOST", 5555)
            self.ftv.adb_connect()

    def test_turn_on_off(self):
        """Test that the ``FireTVSync.turn_on`` and ``FireTVSync.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_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._send_intent("TEST", constants.INTENT_LAUNCH_FIRETV)
            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._send_intent("TEST", constants.INTENT_LAUNCH_FIRETV)
            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 ``FireTVSync.launch_app`` and ``FireTVSync.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_FIRETV.format("TEST")
            )

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

    def test_running_apps(self):
        """Check that the ``running_apps`` property works correctly."""
        with patchers.patch_shell(None)[self.PATCH_KEY]:
            running_apps = self.ftv.running_apps()
            self.assertIsNone(running_apps, None)

        with patchers.patch_shell("")[self.PATCH_KEY]:
            running_apps = self.ftv.running_apps()
            self.assertIsNone(running_apps, None)

        with patchers.patch_shell(RUNNING_APPS_OUTPUT)[self.PATCH_KEY]:
            running_apps = self.ftv.running_apps()
            self.assertListEqual(running_apps, RUNNING_APPS_LIST)

    def test_get_properties(self):
        """Check that ``get_properties()`` works correctly."""
        with patchers.patch_shell(None)[self.PATCH_KEY]:
            with patchers.patch_calls(
                self.ftv, self.ftv.screen_on_awake_wake_lock_size
            ) as screen_on_awake_wake_lock_size, patchers.patch_calls(
                self.ftv, self.ftv.current_app_media_session_state
            ) as current_app_media_session_state, patchers.patch_calls(
                self.ftv, self.ftv.running_apps
            ) as running_apps, patchers.patch_calls(
                self.ftv, self.ftv.get_hdmi_input
            ) as get_hdmi_input:
                self.ftv.get_properties(lazy=True)
                assert screen_on_awake_wake_lock_size.called
                assert not current_app_media_session_state.called
                assert not running_apps.called
                assert not get_hdmi_input.called

            with patchers.patch_calls(
                self.ftv, self.ftv.screen_on_awake_wake_lock_size
            ) as screen_on_awake_wake_lock_size, patchers.patch_calls(
                self.ftv, self.ftv.current_app_media_session_state
            ) as current_app_media_session_state, patchers.patch_calls(
                self.ftv, self.ftv.running_apps
            ) as running_apps, patchers.patch_calls(
                self.ftv, self.ftv.get_hdmi_input
            ) as get_hdmi_input:
                self.ftv.get_properties(lazy=False, get_running_apps=True)
                assert screen_on_awake_wake_lock_size.called
                assert current_app_media_session_state.called
                assert running_apps.called
                assert get_hdmi_input.called

            with patchers.patch_calls(
                self.ftv, self.ftv.screen_on_awake_wake_lock_size
            ) as screen_on_awake_wake_lock_size, patchers.patch_calls(
                self.ftv, self.ftv.current_app_media_session_state
            ) as current_app_media_session_state, patchers.patch_calls(
                self.ftv, self.ftv.running_apps
            ) as running_apps, patchers.patch_calls(
                self.ftv, self.ftv.get_hdmi_input
            ) as get_hdmi_input:
                self.ftv.get_properties(lazy=False, get_running_apps=False)
                assert screen_on_awake_wake_lock_size.called
                assert current_app_media_session_state.called
                assert not running_apps.called
                assert get_hdmi_input.called

    def test_get_properties_dict(self):
        """Check that ``get_properties_dict()`` works correctly."""
        with patchers.patch_shell(None)[self.PATCH_KEY]:
            with patchers.patch_calls(self.ftv, self.ftv.get_properties) as get_properties:
                self.ftv.get_properties_dict()
                assert get_properties.called

    def test_update(self):
        """Check that the ``update`` method works correctly."""
        with patchers.patch_connect(False)[self.PATCH_KEY]:
            self.ftv.adb_connect()

        self.assertTupleEqual(self.ftv.update(), STATE_NONE)

    def assertUpdate(self, get_properties, update):
        """Check that the results of the `update` method are as expected."""
        with patch("androidtv.firetv.firetv_sync.FireTVSync.get_properties", return_value=get_properties):
            self.assertTupleEqual(self.ftv.update(), update)

    def test_state_detection(self):
        """Check that the state detection works as expected."""
        self.assertUpdate([False, None, -1, None, None, None, None], (constants.STATE_OFF, None, None, None))

        self.assertUpdate([True, False, -1, None, None, None, None], (constants.STATE_STANDBY, None, None, None))

        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
        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),
        )

        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),
        )

        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"]}

        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),
        )

        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),
        )

        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}}]}
        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
        self.assertUpdate(
            [True, True, 3, constants.APP_FIREFOX, 3, [constants.APP_FIREFOX], None],
            (constants.STATE_PLAYING, constants.APP_FIREFOX, [constants.APP_FIREFOX], None),
        )

        self.assertUpdate(
            [True, True, 1, constants.APP_FIREFOX, 3, [constants.APP_FIREFOX], None],
            (constants.STATE_IDLE, constants.APP_FIREFOX, [constants.APP_FIREFOX], None),
        )

        # Hulu
        self.assertUpdate(
            [True, True, 4, constants.APP_HULU, 3, [constants.APP_HULU], None],
            (constants.STATE_PLAYING, constants.APP_HULU, [constants.APP_HULU], None),
        )

        self.assertUpdate(
            [True, True, 2, constants.APP_HULU, 3, [constants.APP_HULU], None],
            (constants.STATE_PAUSED, constants.APP_HULU, [constants.APP_HULU], None),
        )

        self.assertUpdate(
            [True, True, 1, constants.APP_HULU, 3, [constants.APP_HULU], None],
            (constants.STATE_IDLE, constants.APP_HULU, [constants.APP_HULU], None),
        )

        # Jellyfin
        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),
        )

        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
        self.assertUpdate(
            [True, True, 1, constants.APP_NETFLIX, 3, [constants.APP_NETFLIX], None],
            (constants.STATE_PLAYING, constants.APP_NETFLIX, [constants.APP_NETFLIX], None),
        )

        self.assertUpdate(
            [True, True, 1, constants.APP_NETFLIX, 2, [constants.APP_NETFLIX], None],
            (constants.STATE_PAUSED, constants.APP_NETFLIX, [constants.APP_NETFLIX], None),
        )

        self.assertUpdate(
            [True, True, 1, constants.APP_NETFLIX, 1, [constants.APP_NETFLIX], None],
            (constants.STATE_IDLE, constants.APP_NETFLIX, [constants.APP_NETFLIX], None),
        )

        # Plex
        self.assertUpdate(
            [True, True, 1, constants.APP_PLEX, 3, [constants.APP_PLEX], None],
            (constants.STATE_PLAYING, constants.APP_PLEX, [constants.APP_PLEX], None),
        )

        self.assertUpdate(
            [True, True, 2, constants.APP_PLEX, 3, [constants.APP_PLEX], None],
            (constants.STATE_PAUSED, constants.APP_PLEX, [constants.APP_PLEX], None),
        )

        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
        self.assertUpdate(
            [True, True, 3, constants.APP_SPORT1, 3, [constants.APP_SPORT1], None],
            (constants.STATE_PLAYING, constants.APP_SPORT1, [constants.APP_SPORT1], None),
        )

        self.assertUpdate(
            [True, True, 2, constants.APP_SPORT1, 3, [constants.APP_SPORT1], None],
            (constants.STATE_PAUSED, constants.APP_SPORT1, [constants.APP_SPORT1], None),
        )

        self.assertUpdate(
            [True, True, 1, constants.APP_SPORT1, 3, [constants.APP_SPORT1], None],
            (constants.STATE_IDLE, constants.APP_SPORT1, [constants.APP_SPORT1], None),
        )

        # Spotify
        self.assertUpdate(
            [True, True, 1, constants.APP_SPOTIFY, 3, [constants.APP_SPOTIFY], None],
            (constants.STATE_PLAYING, constants.APP_SPOTIFY, [constants.APP_SPOTIFY], None),
        )

        self.assertUpdate(
            [True, True, 1, constants.APP_SPOTIFY, 2, [constants.APP_SPOTIFY], None],
            (constants.STATE_PAUSED, constants.APP_SPOTIFY, [constants.APP_SPOTIFY], None),
        )

        self.assertUpdate(
            [True, True, 1, constants.APP_SPOTIFY, 1, [constants.APP_SPOTIFY], None],
            (constants.STATE_IDLE, constants.APP_SPOTIFY, [constants.APP_SPOTIFY], None),
        )

        # RTL Plus (Germany)
        self.assertUpdate(
            [True, True, 3, constants.APP_TVNOW, 1, [constants.APP_TVNOW], None],
            (constants.STATE_PAUSED, constants.APP_TVNOW, [constants.APP_TVNOW], None),
        )

        self.assertUpdate(
            [True, True, 4, constants.APP_TVNOW, 1, [constants.APP_TVNOW], None],
            (constants.STATE_PLAYING, constants.APP_TVNOW, [constants.APP_TVNOW], None),
        )

        self.assertUpdate(
            [True, True, 5, constants.APP_TVNOW, 1, [constants.APP_TVNOW], None],
            (constants.STATE_PLAYING, constants.APP_TVNOW, [constants.APP_TVNOW], None),
        )

        self.assertUpdate(
            [True, True, 6, constants.APP_TVNOW, 1, [constants.APP_TVNOW], None],
            (constants.STATE_IDLE, constants.APP_TVNOW, [constants.APP_TVNOW], None),
        )

        # Twitch
        self.assertUpdate(
            [True, True, 2, constants.APP_TWITCH_FIRETV, 3, [constants.APP_TWITCH_FIRETV], None],
            (constants.STATE_PAUSED, constants.APP_TWITCH_FIRETV, [constants.APP_TWITCH_FIRETV], None),
        )

        self.assertUpdate(
            [True, True, 1, constants.APP_TWITCH_FIRETV, 3, [constants.APP_TWITCH_FIRETV], None],
            (constants.STATE_PLAYING, constants.APP_TWITCH_FIRETV, [constants.APP_TWITCH_FIRETV], None),
        )

        self.assertUpdate(
            [True, True, 1, constants.APP_TWITCH_FIRETV, 4, [constants.APP_TWITCH_FIRETV], None],
            (constants.STATE_PLAYING, constants.APP_TWITCH_FIRETV, [constants.APP_TWITCH_FIRETV], None),
        )

        self.assertUpdate(
            [True, True, 1, constants.APP_TWITCH_FIRETV, 1, [constants.APP_TWITCH_FIRETV], None],
            (constants.STATE_IDLE, constants.APP_TWITCH_FIRETV, [constants.APP_TWITCH_FIRETV], None),
        )

        # Waipu TV
        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),
        )

        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),
        )

        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
        self.assertUpdate(
            [True, True, 1, UNKNOWN_APP, 3, [UNKNOWN_APP], None],
            (constants.STATE_PLAYING, UNKNOWN_APP, [UNKNOWN_APP], None),
        )

        self.assertUpdate(
            [True, True, 1, UNKNOWN_APP, 2, [UNKNOWN_APP], None],
            (constants.STATE_PAUSED, UNKNOWN_APP, [UNKNOWN_APP], None),
        )

        self.assertUpdate(
            [True, True, 1, UNKNOWN_APP, 1, [UNKNOWN_APP], None],
            (constants.STATE_IDLE, UNKNOWN_APP, [UNKNOWN_APP], None),
        )

        self.assertUpdate(
            [True, True, 1, UNKNOWN_APP, None, [UNKNOWN_APP], None],
            (constants.STATE_PLAYING, UNKNOWN_APP, [UNKNOWN_APP], None),
        )

        self.assertUpdate(
            [True, True, 2, UNKNOWN_APP, None, [UNKNOWN_APP], None],
            (constants.STATE_PAUSED, UNKNOWN_APP, [UNKNOWN_APP], None),
        )


class TestFireTVSyncServer(TestFireTVSyncPython):
    ADB_ATTR = "_adb_device"
    PATCH_KEY = "server"

    def setUp(self):
        with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell("")[self.PATCH_KEY]:
            self.ftv = FireTVSync("HOST", 5555, adb_server_ip="ADB_SERVER_PORT")
            self.ftv.adb_connect()


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