]> git.basschouten.com Git - openhab-addons.git/blob
9c75321af9473b979abe7b934089d6551c5f9df3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.yamahareceiver.internal.protocol.xml;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.yamahareceiver.internal.TestModels.*;
19 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Inputs.*;
20
21 import java.util.function.Consumer;
22
23 import org.junit.jupiter.api.Test;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Captor;
26 import org.mockito.Mock;
27 import org.openhab.binding.yamahareceiver.internal.config.YamahaBridgeConfig;
28 import org.openhab.binding.yamahareceiver.internal.state.PlayInfoState;
29 import org.openhab.binding.yamahareceiver.internal.state.PlayInfoStateListener;
30
31 /**
32  * Unit test for {@link InputWithPlayControlXML}.
33  *
34  * @author Tomasz Maruszak - Initial contribution
35  */
36 public class InputWithPlayControlXMLTest extends AbstractZoneControlXMLTest {
37
38     private InputWithPlayControlXML subject;
39
40     private @Mock PlayInfoStateListener playInfoStateListener;
41     private @Captor ArgumentCaptor<PlayInfoState> playInfoStateArg;
42     private @Mock YamahaBridgeConfig bridgeConfig;
43
44     private String albumUrl;
45
46     private void given(String model, String input, Consumer<ModelContext> setup) throws Exception {
47         ctx.prepareForModel(model);
48
49         DeviceInformationXML deviceInformation = new DeviceInformationXML(con, deviceInformationState);
50         deviceInformation.update();
51
52         setup.accept(ctx);
53
54         albumUrl = "http://some/url.jpg";
55         when(bridgeConfig.getAlbumUrl()).thenReturn(albumUrl);
56
57         subject = new InputWithPlayControlXML(input, con, playInfoStateListener, bridgeConfig, deviceInformationState);
58     }
59
60     @Test
61     public void given_RX_S601D_and_Spotify_when_playStopPause_then_sendsProperCommand() throws Exception {
62         given(RX_S601D, INPUT_SPOTIFY, ctx -> {
63             ctx.respondWith("<Spotify><Play_Info>GetParam</Play_Info></Spotify>", "Spotify_Play_Info.xml");
64         });
65
66         // when
67         subject.play();
68         subject.stop();
69         subject.pause();
70
71         // then
72         verify(con).send(eq("<Spotify><Play_Control><Playback>Play</Playback></Play_Control></Spotify>"));
73         verify(con).send(eq("<Spotify><Play_Control><Playback>Stop</Playback></Play_Control></Spotify>"));
74         verify(con).send(eq("<Spotify><Play_Control><Playback>Pause</Playback></Play_Control></Spotify>"));
75     }
76
77     @Test
78     public void given_RX_S601D_and_Spotify_when_nextPrevious_then_sendsProperCommand() throws Exception {
79         given(RX_S601D, INPUT_SPOTIFY, ctx -> {
80             ctx.respondWith("<Spotify><Play_Info>GetParam</Play_Info></Spotify>", "Spotify_Play_Info.xml");
81         });
82
83         // when
84         subject.nextTrack();
85         subject.previousTrack();
86
87         // then
88         verify(con).send(eq("<Spotify><Play_Control><Playback>Skip Fwd</Playback></Play_Control></Spotify>"));
89         verify(con).send(eq("<Spotify><Play_Control><Playback>Skip Rev</Playback></Play_Control></Spotify>"));
90     }
91
92     @Test
93     public void given_RX_S601D_and_Bluetooth_when_playStopPause_then_sendsProperCommand() throws Exception {
94         given(RX_S601D, INPUT_BLUETOOTH, ctx -> {
95             ctx.respondWith("<Bluetooth><Play_Info>GetParam</Play_Info></Bluetooth>", "Bluetooth_Play_Info.xml");
96         });
97
98         // when
99         subject.play();
100         subject.stop();
101         subject.pause();
102
103         // then
104         verify(con).send(eq("<Bluetooth><Play_Control><Playback>Play</Playback></Play_Control></Bluetooth>"));
105         verify(con).send(eq("<Bluetooth><Play_Control><Playback>Stop</Playback></Play_Control></Bluetooth>"));
106         verify(con).send(eq("<Bluetooth><Play_Control><Playback>Pause</Playback></Play_Control></Bluetooth>"));
107     }
108
109     @Test
110     public void given_RX_S601D_and_Bluetooth_when_nextPrevious_then_sendsProperCommand() throws Exception {
111         given(RX_S601D, INPUT_BLUETOOTH, ctx -> {
112             ctx.respondWith("<Bluetooth><Play_Info>GetParam</Play_Info></Bluetooth>", "Bluetooth_Play_Info.xml");
113         });
114
115         // when
116         subject.nextTrack();
117         subject.previousTrack();
118
119         // then
120         verify(con).send(eq("<Bluetooth><Play_Control><Playback>Skip Fwd</Playback></Play_Control></Bluetooth>"));
121         verify(con).send(eq("<Bluetooth><Play_Control><Playback>Skip Rev</Playback></Play_Control></Bluetooth>"));
122     }
123
124     @Test
125     public void given_RX_S601D_and_NET_RADIO_when_nextPrevious_then_sendsProperCommand() throws Exception {
126         given(RX_S601D, INPUT_NET_RADIO, ctx -> {
127             ctx.respondWith("<NET_RADIO><Play_Info>GetParam</Play_Info></NET_RADIO>", "NET_RADIO_Play_Info.xml");
128         });
129
130         // when
131         subject.nextTrack();
132         subject.previousTrack();
133
134         // then
135         verify(con).send(eq("<NET_RADIO><Play_Control><Playback>Skip Fwd</Playback></Play_Control></NET_RADIO>"));
136         verify(con).send(eq("<NET_RADIO><Play_Control><Playback>Skip Rev</Playback></Play_Control></NET_RADIO>"));
137     }
138
139     @Test
140     public void given_RX_S601D_and_Spotify_when_update_then_stateIsProperlyRead() throws Exception {
141         given(RX_S601D, INPUT_SPOTIFY, ctx -> {
142             ctx.respondWith("<Spotify><Play_Info>GetParam</Play_Info></Spotify>", "Spotify_Play_Info.xml");
143         });
144
145         ArgumentCaptor<PlayInfoState> playInfoStateArg = ArgumentCaptor.forClass(PlayInfoState.class);
146
147         // when
148         subject.update();
149
150         // then
151         verify(playInfoStateListener).playInfoUpdated(playInfoStateArg.capture());
152         PlayInfoState state = playInfoStateArg.getValue();
153
154         assertEquals("Play", state.playbackMode);
155         assertEquals("Above & Beyond", state.artist);
156         assertEquals("Acoustic - Live At The Hollywood Bowl", state.album);
157         assertEquals("No One On Earth - Live At The Hollywood Bowl", state.song);
158         assertEquals("N/A", state.station);
159         assertEquals("http://localhost/YamahaRemoteControl/AlbumART/AlbumART6585.jpg", state.songImageUrl);
160     }
161
162     @Test
163     public void given_RX_S601D_and_NET_RADIO_when_update_then_stateIsProperlyRead() throws Exception {
164         given(RX_S601D, INPUT_NET_RADIO, ctx -> {
165             ctx.respondWith("<NET_RADIO><Play_Info>GetParam</Play_Info></NET_RADIO>", "NET_RADIO_Play_Info.xml");
166         });
167
168         // when
169         subject.update();
170
171         // then
172         verify(playInfoStateListener).playInfoUpdated(playInfoStateArg.capture());
173         PlayInfoState state = playInfoStateArg.getValue();
174
175         assertEquals("Play", state.playbackMode);
176         assertEquals("N/A", state.artist);
177         assertEquals("Chilli ZET PL", state.station);
178         assertEquals("", state.album);
179         assertEquals("LESZEK MOZDZER - ZDROWY KOLATAJ", state.song);
180         assertEquals("http://localhost/YamahaRemoteControl/AlbumART/AlbumART4626.jpg", state.songImageUrl);
181     }
182
183     @Test
184     public void given_RX_S601D_and_Bluetooth_when_update_then_stateIsProperlyRead() throws Exception {
185         given(RX_S601D, INPUT_BLUETOOTH, ctx -> {
186             ctx.respondWith("<Bluetooth><Play_Info>GetParam</Play_Info></Bluetooth>", "Bluetooth_Play_Info.xml");
187         });
188
189         ArgumentCaptor<PlayInfoState> playInfoStateArg = ArgumentCaptor.forClass(PlayInfoState.class);
190
191         // when
192         subject.update();
193
194         // then
195         verify(playInfoStateListener).playInfoUpdated(playInfoStateArg.capture());
196         PlayInfoState state = playInfoStateArg.getValue();
197
198         assertEquals("Play", state.playbackMode);
199         assertEquals("M.I.K.E.", state.artist);
200         assertEquals("A State Of Trance Classics, Vol. 12 (The Full Unmixed Versions)", state.album);
201         assertEquals("Voices From The Inside", state.song);
202         assertEquals("N/A", state.station);
203         assertEquals(albumUrl, state.songImageUrl);
204     }
205
206     @Test
207     public void given_RX_V3900_and_NET_RADIO_when_playStopPause_then_sendsProperCommand() throws Exception {
208         given(RX_V3900, INPUT_NET_RADIO, ctx -> {
209             ctx.respondWith("<NET_USB><Play_Info>GetParam</Play_Info></NET_USB>", "NET_USB_Play_Info.xml");
210         });
211
212         // when
213         subject.play();
214         subject.stop();
215         subject.pause();
216
217         // then
218         verify(con).send(eq("<NET_USB><Play_Control><Play>Play</Play></Play_Control></NET_USB>"));
219         verify(con).send(eq("<NET_USB><Play_Control><Play>Stop</Play></Play_Control></NET_USB>"));
220         verify(con).send(eq("<NET_USB><Play_Control><Play>Pause</Play></Play_Control></NET_USB>"));
221     }
222
223     @Test
224     public void given_RX_V3900_and_NET_RADIO_when_nextPrevious_then_sendsProperCommand() throws Exception {
225         given(RX_V3900, INPUT_NET_RADIO, ctx -> {
226             ctx.respondWith("<NET_USB><Play_Info>GetParam</Play_Info></NET_USB>", "NET_USB_Play_Info.xml");
227         });
228
229         // when
230         subject.nextTrack();
231         subject.previousTrack();
232
233         // then
234         verify(con).send(eq("<NET_USB><Play_Control><Skip>Fwd</Skip></Play_Control></NET_USB>"));
235         verify(con).send(eq("<NET_USB><Play_Control><Skip>Rev</Skip></Play_Control></NET_USB>"));
236     }
237
238     @Test
239     public void given_RX_V3900_and_NET_RADIO_when_update_then_stateIsProperlyRead() throws Exception {
240         given(RX_V3900, INPUT_NET_RADIO, ctx -> {
241             ctx.respondWith("<NET_USB><Play_Info>GetParam</Play_Info></NET_USB>", "NET_USB_Play_Info.xml");
242         });
243
244         // when
245         subject.update();
246
247         // then
248         verify(playInfoStateListener).playInfoUpdated(playInfoStateArg.capture());
249         PlayInfoState state = playInfoStateArg.getValue();
250
251         assertEquals("Play", state.playbackMode);
252         assertEquals("Some Artist", state.artist);
253         assertEquals("Some Album", state.album);
254         assertEquals("SuomiPOP 98.1", state.song);
255         assertEquals("N/A", state.station);
256         assertEquals(albumUrl, state.songImageUrl);
257     }
258
259     @Test
260     public void given_RX_V3900_and_TUNER_when_update_then_stateIsProperlyRead() throws Exception {
261         given(RX_V3900, INPUT_TUNER, ctx -> {
262             ctx.respondWith("<Tuner><Play_Info>GetParam</Play_Info></Tuner>", "Tuner_Play_Info.xml");
263         });
264
265         // when
266         subject.update();
267
268         // then
269         verify(playInfoStateListener).playInfoUpdated(playInfoStateArg.capture());
270         PlayInfoState state = playInfoStateArg.getValue();
271
272         assertEquals("Stop", state.playbackMode);
273         assertEquals("", state.artist);
274         assertEquals("POP_M", state.album);
275         assertEquals("", state.song);
276         assertEquals("SUOMIPOP", state.station);
277         assertEquals(albumUrl, state.songImageUrl);
278     }
279 }