File: OpenhomeDevicePlaylistTest.py

package info (click to toggle)
python-openhomedevice 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 300 kB
  • sloc: python: 1,973; xml: 765; makefile: 3
file content (174 lines) | stat: -rw-r--r-- 7,668 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
import unittest
import os
import asyncio

from openhomedevice.device import Device
from aioresponses import aioresponses


def async_test(coro):
    def wrapper(*args, **kwargs):
        loop = asyncio.new_event_loop()
        try:
            return loop.run_until_complete(coro(*args, **kwargs))
        finally:
            loop.close()

    return wrapper


class FakeAction:
    def __init__(self, response=None):
        self.was_called_times = 0
        self.response = response

    async def async_call(self, **kwargs):
        self.arguments = kwargs
        self.was_called_times += 1
        return self.response


def product_actions():
    return {
        "Product": FakeAction({"Name": b"My Friendly Name", "Room": b"Bathroom"}),
        "SetStandby": FakeAction(),
        "Standby": FakeAction({"Value": True}),
        "SourceIndex": FakeAction({"Value": 3}),
        "Source": FakeAction({"Type": "Playlist", "Name": "Playlist"}),
        "SetSourceIndex": FakeAction(),
        "SourceXml": FakeAction(
            {
                "Value": "<SourceList><Source><Name>Playlist</Name><Type>Playlist</Type><Visible>true</Visible><SystemName>Playlist</SystemName></Source><Source><Name>Radio</Name><Type>Radio</Type><Visible>false</Visible><SystemName>Radio</SystemName></Source><Source><Name>UPnP AV</Name><Type>UpnpAv</Type><Visible>false</Visible><SystemName>UPnP AV</SystemName></Source><Source><Name>Songcast</Name><Type>Receiver</Type><Visible>false</Visible><SystemName>Songcast</SystemName></Source><Source><Name>Net Aux</Name><Type>NetAux</Type><Visible>false</Visible><SystemName>Net Aux</SystemName></Source><Source><Name>Spotify</Name><Type>Spotify</Type><Visible>false</Visible><SystemName>Spotify</SystemName></Source><Source><Name>Roon</Name><Type>Scd</Type><Visible>true</Visible><SystemName>Roon</SystemName></Source><Source><Name>SpeakerTest</Name><Type>Private</Type><Visible>false</Visible><SystemName>SpeakerTest</SystemName></Source></SourceList>"
            }
        ),
    }


def playlist_actions():
    return {
        "TransportState": FakeAction({"Value": "Playing"}),
        "Play": FakeAction(),
        "Pause": FakeAction(),
        "Stop": FakeAction(),
        "Next": FakeAction(),
        "Previous": FakeAction(),
    }


class FakeService:
    def __init__(self, actions):
        self.actions = actions

    def action(self, action_called):
        return self.actions[action_called]


class OpenhomeDevicePlaylistTest(unittest.TestCase):
    @async_test
    @aioresponses()
    async def setUp(self, mocked):
        LOCATION = "http://mydevice:12345/desc.xml"
        with open(
            os.path.join(os.path.dirname(__file__), "data/softwaredescription.xml")
        ) as file:
            mocked.get(LOCATION, body=file.read())
            mocked.get(
                "http://mydevice:12345/softplayer.local/Upnp/av.openhome.org-Product-2/service.xml",
                body="<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\"><serviceStateTable/></scpd>",
            )
            mocked.get(
                "http://mydevice:12345/softplayer.local/Upnp/av.openhome.org-Volume-2/service.xml",
                body="<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\"><serviceStateTable/></scpd>",
            )
            mocked.get(
                "http://mydevice:12345/softplayer.local/Upnp/av.openhome.org-Credentials-1/service.xml",
                body="<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\"><serviceStateTable/></scpd>",
            )
            mocked.get(
                "http://mydevice:12345/softplayer.local/Upnp/av.openhome.org-Time-1/service.xml",
                body="<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\"><serviceStateTable/></scpd>",
            )
            mocked.get(
                "http://mydevice:12345/softplayer.local/Upnp/av.openhome.org-Info-1/service.xml",
                body="<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\"><serviceStateTable/></scpd>",
            )
            mocked.get(
                "http://mydevice:12345/softplayer.local/Upnp/av.openhome.org-Config-1/service.xml",
                body="<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\"><serviceStateTable/></scpd>",
            )
            mocked.get(
                "http://mydevice:12345/softplayer.local/Upnp/av.openhome.org-Playlist-1/service.xml",
                body="<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\"><serviceStateTable/></scpd>",
            )
            mocked.get(
                "http://mydevice:12345/softplayer.local/Upnp/av.openhome.org-Receiver-1/service.xml",
                body="<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\"><serviceStateTable/></scpd>",
            )
            mocked.get(
                "http://mydevice:12345/softplayer.local/Upnp/av.openhome.org-Sender-1/service.xml",
                body="<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\"><serviceStateTable/></scpd>",
            )
            mocked.get(
                "http://mydevice:12345/softplayer.local/Upnp/av.openhome.org-Radio-1/service.xml",
                body="<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\"><serviceStateTable/></scpd>",
            )
        self.sut = Device(LOCATION)
        await self.sut.init()
        soap_request_calls = []
        return super().setUp()

    def test_device_parses_uuid(self):
        self.assertEqual(self.sut.uuid(), "uuid:softplayer.local")

    @async_test
    async def test_transport_state(self):
        self.sut.product_service = FakeService(product_actions())
        self.sut.playlist_service = FakeService(playlist_actions())
        self.assertEqual(await self.sut.transport_state(), "Playing")

    @async_test
    async def test_play(self):
        self.sut.product_service = FakeService(product_actions())
        self.sut.playlist_service = FakeService(playlist_actions())
        await self.sut.play()
        self.assertEqual(self.sut.playlist_service.actions["Play"].was_called_times, 1)

    @async_test
    async def test_stop(self):
        self.sut.product_service = FakeService(product_actions())
        self.sut.playlist_service = FakeService(playlist_actions())
        await self.sut.stop()
        self.assertEqual(self.sut.playlist_service.actions["Stop"].was_called_times, 1)

    @async_test
    async def test_pause(self):
        self.sut.product_service = FakeService(product_actions())
        self.sut.playlist_service = FakeService(playlist_actions())
        await self.sut.pause()
        self.assertEqual(self.sut.playlist_service.actions["Pause"].was_called_times, 1)

    @async_test
    async def test_skip_forward(self):
        self.sut.product_service = FakeService(product_actions())
        self.sut.playlist_service = FakeService(playlist_actions())
        await self.sut.skip(10)
        self.assertEqual(self.sut.playlist_service.actions["Next"].was_called_times, 10)

    @async_test
    async def test_skip_backwards(self):
        self.sut.product_service = FakeService(product_actions())
        self.sut.playlist_service = FakeService(playlist_actions())
        await self.sut.skip(-10)
        self.assertEqual(
            self.sut.playlist_service.actions["Previous"].was_called_times, 10
        )

    @async_test
    async def test_skip_nowhere(self):
        self.sut.product_service = FakeService(product_actions())
        self.sut.playlist_service = FakeService(playlist_actions())
        await self.sut.skip(0)
        self.assertEqual(
            self.sut.playlist_service.actions["Previous"].was_called_times, 0
        )
        self.assertEqual(self.sut.playlist_service.actions["Next"].was_called_times, 0)