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