From 7e9eef516063fe32579d65b399c73ec1f487dd8f Mon Sep 17 00:00:00 2001 From: Stephan van Rooij <1292510+svrooij@users.noreply.github.com> Date: Thu, 7 Dec 2023 22:55:31 +0100 Subject: [PATCH] chore: Tests for Device Description --- tests/Sonos.Base.Tests/FakeData.cs | 429 +++++++++++++++++++++ tests/Sonos.Base.Tests/SonosDeviceTests.cs | 15 + tests/Sonos.Base.Tests/TestHelpers.cs | 17 + 3 files changed, 461 insertions(+) create mode 100644 tests/Sonos.Base.Tests/FakeData.cs diff --git a/tests/Sonos.Base.Tests/FakeData.cs b/tests/Sonos.Base.Tests/FakeData.cs new file mode 100644 index 0000000..0541830 --- /dev/null +++ b/tests/Sonos.Base.Tests/FakeData.cs @@ -0,0 +1,429 @@ +namespace Sonos.Base.Tests; +internal class FakeData +{ + public const string S1DeviceDescription = @" + + + 1 + 0 + + + urn:schemas-upnp-org:device:ZonePlayer:1 + 192.168.x.x - Sonos Play:5 + Sonos, Inc. + http://www.sonos.com + S5 + Sonos Play:5 + Sonos Play:5 + http://www.sonos.com/products/zoneplayers/S5 + 57.19-46310 + 1 + 1.16.4.1-2.0 + 00-0E-58-00-00-00:A + 00:0E:58:00:00:00 + uuid:RINCON_000E5800000001400 + + + 0 + image/png + 48 + 48 + 24 + /img/icon-S5.png + + + 56.0-00000 + 36.0-00000 + 1.18.10 + 1.1.0 + 11.12 + OTP: 1.1.1(1-16-4-zp5s-0.5) + Werkkamer Stephan + Play:5 + 5 + 0x02000002 + 0x00006172 + 0x0003102a + P100 + 0 + 3 + 0.000 + 0.000 + 32 + 32 + #DEACTIVATION_STATE_TAG_AND_VALUE# + #DEACTIVATION_TTL_TAG_AND_VALUE# + #DEACTIVATION_DATE_TIME_TAG_AND_VALUE# + 1 + 425 + 0 + + + urn:schemas-upnp-org:service:AlarmClock:1 + urn:upnp-org:serviceId:AlarmClock + /AlarmClock/Control + /AlarmClock/Event + /xml/AlarmClock1.xml + + + urn:schemas-upnp-org:service:MusicServices:1 + urn:upnp-org:serviceId:MusicServices + /MusicServices/Control + /MusicServices/Event + /xml/MusicServices1.xml + + + urn:schemas-upnp-org:service:AudioIn:1 + urn:upnp-org:serviceId:AudioIn + /AudioIn/Control + /AudioIn/Event + /xml/AudioIn1.xml + + + urn:schemas-upnp-org:service:DeviceProperties:1 + urn:upnp-org:serviceId:DeviceProperties + /DeviceProperties/Control + /DeviceProperties/Event + /xml/DeviceProperties1.xml + + + urn:schemas-upnp-org:service:SystemProperties:1 + urn:upnp-org:serviceId:SystemProperties + /SystemProperties/Control + /SystemProperties/Event + /xml/SystemProperties1.xml + + + urn:schemas-upnp-org:service:ZoneGroupTopology:1 + urn:upnp-org:serviceId:ZoneGroupTopology + /ZoneGroupTopology/Control + /ZoneGroupTopology/Event + /xml/ZoneGroupTopology1.xml + + + urn:schemas-upnp-org:service:GroupManagement:1 + urn:upnp-org:serviceId:GroupManagement + /GroupManagement/Control + /GroupManagement/Event + /xml/GroupManagement1.xml + + + urn:schemas-tencent-com:service:QPlay:1 + urn:tencent-com:serviceId:QPlay + /QPlay/Control + /QPlay/Event + /xml/QPlay1.xml + + + + + urn:schemas-upnp-org:device:MediaServer:1 + 192.168.x.x - Sonos Play:5 Media Server + Sonos, Inc. + http://www.sonos.com + S5 + Sonos Play:5 Media Server + Sonos Play:5 + http://www.sonos.com/products/zoneplayers/S5 + uuid:RINCON_000E5800000001400_MS + + + urn:schemas-upnp-org:service:ContentDirectory:1 + urn:upnp-org:serviceId:ContentDirectory + /MediaServer/ContentDirectory/Control + /MediaServer/ContentDirectory/Event + /xml/ContentDirectory1.xml + + + urn:schemas-upnp-org:service:ConnectionManager:1 + urn:upnp-org:serviceId:ConnectionManager + /MediaServer/ConnectionManager/Control + /MediaServer/ConnectionManager/Event + /xml/ConnectionManager1.xml + + + + + urn:schemas-upnp-org:device:MediaRenderer:1 + Werkkamer Stephan - Sonos Play:5 Media Renderer + Sonos, Inc. + http://www.sonos.com + S5 + Sonos Play:5 Media Renderer + Sonos Play:5 + http://www.sonos.com/products/zoneplayers/S5 + uuid:RINCON_000E5800000001400_MR + + + urn:schemas-upnp-org:service:RenderingControl:1 + urn:upnp-org:serviceId:RenderingControl + /MediaRenderer/RenderingControl/Control + /MediaRenderer/RenderingControl/Event + /xml/RenderingControl1.xml + + + urn:schemas-upnp-org:service:ConnectionManager:1 + urn:upnp-org:serviceId:ConnectionManager + /MediaRenderer/ConnectionManager/Control + /MediaRenderer/ConnectionManager/Event + /xml/ConnectionManager1.xml + + + urn:schemas-upnp-org:service:AVTransport:1 + urn:upnp-org:serviceId:AVTransport + /MediaRenderer/AVTransport/Control + /MediaRenderer/AVTransport/Event + /xml/AVTransport1.xml + + + urn:schemas-sonos-com:service:Queue:1 + urn:sonos-com:serviceId:Queue + /MediaRenderer/Queue/Control + /MediaRenderer/Queue/Event + /xml/Queue1.xml + + + urn:schemas-upnp-org:service:GroupRenderingControl:1 + urn:upnp-org:serviceId:GroupRenderingControl + /MediaRenderer/GroupRenderingControl/Control + /MediaRenderer/GroupRenderingControl/Event + /xml/GroupRenderingControl1.xml + + + urn:schemas-upnp-org:service:VirtualLineIn:1 + urn:upnp-org:serviceId:VirtualLineIn + /MediaRenderer/VirtualLineIn/Control + /MediaRenderer/VirtualLineIn/Event + /xml/VirtualLineIn1.xml + + + + urn:rhapsody-real-com:device-id-1-0:sonos_1:RINCON_000E5800000001400 + + + + + QPlay:2 + + + image/png + 48 + 48 + 24 + /img/icon-S5.png + + + + + + +"; + + public const string S2DeviceDescription = @" + + + 1 + 0 + + + urn:schemas-upnp-org:device:ZonePlayer:1 + 192.168.1.10 - Sonos One + Sonos, Inc. + http://www.sonos.com + S18 + Sonos One + Sonos One + http://www.sonos.com/products/zoneplayers/S18 + 76.2-47142 + 2 + 1.26.1.7-2.1 + 54-2A-1B-AA-AA-AA:C + 54:2A:1B:AA:AA:AA + uuid:RINCON_000E58000BB001400 + + + 0 + image/png + 48 + 48 + 24 + /img/icon-S18.png + + + 75.0-00000 + 58.0-00000 + 1.37.0 + 1.1.0 + 15.10 + + 35 + Badkamer + One + 20 + 0x00000000 + 0x05c18332 + 0x0541580e + A201 + 1 + 5 + 1024 + 1024 + 20 + 0 + 1443 + 1843 + + + urn:schemas-upnp-org:service:AlarmClock:1 + urn:upnp-org:serviceId:AlarmClock + /AlarmClock/Control + /AlarmClock/Event + /xml/AlarmClock1.xml + + + urn:schemas-upnp-org:service:MusicServices:1 + urn:upnp-org:serviceId:MusicServices + /MusicServices/Control + /MusicServices/Event + /xml/MusicServices1.xml + + + urn:schemas-upnp-org:service:DeviceProperties:1 + urn:upnp-org:serviceId:DeviceProperties + /DeviceProperties/Control + /DeviceProperties/Event + /xml/DeviceProperties1.xml + + + urn:schemas-upnp-org:service:SystemProperties:1 + urn:upnp-org:serviceId:SystemProperties + /SystemProperties/Control + /SystemProperties/Event + /xml/SystemProperties1.xml + + + urn:schemas-upnp-org:service:ZoneGroupTopology:1 + urn:upnp-org:serviceId:ZoneGroupTopology + /ZoneGroupTopology/Control + /ZoneGroupTopology/Event + /xml/ZoneGroupTopology1.xml + + + urn:schemas-upnp-org:service:GroupManagement:1 + urn:upnp-org:serviceId:GroupManagement + /GroupManagement/Control + /GroupManagement/Event + /xml/GroupManagement1.xml + + + urn:schemas-tencent-com:service:QPlay:1 + urn:tencent-com:serviceId:QPlay + /QPlay/Control + /QPlay/Event + /xml/QPlay1.xml + + + + + urn:schemas-upnp-org:device:MediaServer:1 + 192.168.1.10 - Sonos One Media Server + Sonos, Inc. + http://www.sonos.com + S18 + Sonos One Media Server + Sonos One + http://www.sonos.com/products/zoneplayers/S18 + uuid:RINCON_000E58000BB001400_MS + + + urn:schemas-upnp-org:service:ContentDirectory:1 + urn:upnp-org:serviceId:ContentDirectory + /MediaServer/ContentDirectory/Control + /MediaServer/ContentDirectory/Event + /xml/ContentDirectory1.xml + + + urn:schemas-upnp-org:service:ConnectionManager:1 + urn:upnp-org:serviceId:ConnectionManager + /MediaServer/ConnectionManager/Control + /MediaServer/ConnectionManager/Event + /xml/ConnectionManager1.xml + + + + + urn:schemas-upnp-org:device:MediaRenderer:1 + Badkamer - Sonos One Media Renderer + Sonos, Inc. + http://www.sonos.com + S18 + Sonos One Media Renderer + Sonos One + http://www.sonos.com/products/zoneplayers/S18 + uuid:RINCON_000E58000BB001400_MR + + + urn:schemas-upnp-org:service:RenderingControl:1 + urn:upnp-org:serviceId:RenderingControl + /MediaRenderer/RenderingControl/Control + /MediaRenderer/RenderingControl/Event + /xml/RenderingControl1.xml + + + urn:schemas-upnp-org:service:ConnectionManager:1 + urn:upnp-org:serviceId:ConnectionManager + /MediaRenderer/ConnectionManager/Control + /MediaRenderer/ConnectionManager/Event + /xml/ConnectionManager1.xml + + + urn:schemas-upnp-org:service:AVTransport:1 + urn:upnp-org:serviceId:AVTransport + /MediaRenderer/AVTransport/Control + /MediaRenderer/AVTransport/Event + /xml/AVTransport1.xml + + + urn:schemas-sonos-com:service:Queue:1 + urn:sonos-com:serviceId:Queue + /MediaRenderer/Queue/Control + /MediaRenderer/Queue/Event + /xml/Queue1.xml + + + urn:schemas-upnp-org:service:GroupRenderingControl:1 + urn:upnp-org:serviceId:GroupRenderingControl + /MediaRenderer/GroupRenderingControl/Control + /MediaRenderer/GroupRenderingControl/Event + /xml/GroupRenderingControl1.xml + + + urn:schemas-upnp-org:service:VirtualLineIn:1 + urn:upnp-org:serviceId:VirtualLineIn + /MediaRenderer/VirtualLineIn/Control + /MediaRenderer/VirtualLineIn/Event + /xml/VirtualLineIn1.xml + + + + urn:rhapsody-real-com:device-id-1-0:sonos_1:RINCON_000E58000BB001400 + + + + + QPlay:2 + + + image/png + 48 + 48 + 24 + /img/icon-S18.png + + + + + + +"; +} \ No newline at end of file diff --git a/tests/Sonos.Base.Tests/SonosDeviceTests.cs b/tests/Sonos.Base.Tests/SonosDeviceTests.cs index d534a14..d211d64 100644 --- a/tests/Sonos.Base.Tests/SonosDeviceTests.cs +++ b/tests/Sonos.Base.Tests/SonosDeviceTests.cs @@ -80,5 +80,20 @@ public async Task Stop_ExecutesCorrectCommand() var result = await sonos.StopAsync(); Assert.True(result); } + + + [Theory] + [InlineData(FakeData.S1DeviceDescription, "192.168.x.x - Sonos Play:5")] + [InlineData(FakeData.S2DeviceDescription, "192.168.1.10 - Sonos One")] + + public async Task GetDeviceDescriptionAsync_ParsesDeviceDescription(string xml, string name) + { + var mockedHelper = new Mock(); + mockedHelper.MockDeviceDescription(deviceDescription: xml); + var sonos = new SonosDevice(new SonosDeviceOptions(TestHelpers.DefaultUri, new StaticSonosServiceProvider(mockedHelper.Object))); + var description = await sonos.GetDeviceDescriptionAsync(); + Assert.NotNull(description); + Assert.Equal(name, description.device.friendlyName); + } } } \ No newline at end of file diff --git a/tests/Sonos.Base.Tests/TestHelpers.cs b/tests/Sonos.Base.Tests/TestHelpers.cs index dd176f3..bca6f6e 100644 --- a/tests/Sonos.Base.Tests/TestHelpers.cs +++ b/tests/Sonos.Base.Tests/TestHelpers.cs @@ -19,6 +19,7 @@ using Microsoft.Extensions.DependencyInjection; using Moq; using Moq.Protected; +using Sonos.Base.Tests; using System; using System.Linq; using System.Net.Http; @@ -34,6 +35,22 @@ public static class TestHelpers private const string SoapActionHeader = "soapaction"; private const string SoapResponseFormat = @"{2}"; + public static Mock MockDeviceDescription(this Mock mock, string deviceDescription, string baseUrl = defaultUri) + { + mock + .Protected() + .Setup>("SendAsync", + ItExpr.Is(m => m.RequestUri == new Uri(new Uri(baseUrl), "/xml/device_description.xml")), + ItExpr.IsAny() + ).ReturnsAsync(new HttpResponseMessage + { + StatusCode = System.Net.HttpStatusCode.OK, + Content = new StringContent(deviceDescription) + }); + + return mock; + } + /// /// Mock a specific request to sonos speakers. ///