]> git.basschouten.com Git - openhab-addons.git/blob
140b36166a17296ad1678842b174a7f382076997
[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.junit.Assert.assertNotNull;
17 import static org.mockito.ArgumentMatchers.eq;
18 import static org.mockito.Mockito.verify;
19 import static org.openhab.binding.yamahareceiver.internal.TestModels.*;
20 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Zone.Main_Zone;
21
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.openhab.binding.yamahareceiver.internal.state.ZoneControlState;
25
26 /**
27  * Unit test for {@link ZoneControlXML}.
28  *
29  * @author Tomasz Maruszak - Initial contribution
30  */
31 public class ZoneControlXMLTest extends AbstractZoneControlXMLTest {
32
33     private ZoneControlXML subject;
34
35     private void given(String model) throws Exception {
36         ctx.prepareForModel(model);
37
38         DeviceInformationXML deviceInformation = new DeviceInformationXML(con, deviceInformationState);
39         deviceInformation.update();
40
41         subject = new ZoneControlXML(con, Main_Zone, zoneConfig, zoneControlStateListener, deviceInformationState,
42                 () -> inputConverter);
43     }
44
45     @Test
46     public void given_RX_S601D_when_power_then_sendsProperCommand() throws Exception {
47         when_power_then_sendsProperCommand(RX_S601D);
48     }
49
50     @Test
51     public void given_RX_V3900_when_power_then_sendsProperCommand() throws Exception {
52         when_power_then_sendsProperCommand(RX_V3900);
53     }
54
55     private void when_power_then_sendsProperCommand(String model) throws Exception {
56         given(model);
57
58         // when
59         subject.setPower(true);
60         subject.setPower(false);
61
62         // then
63         verify(con).send(eq("<Main_Zone><Power_Control><Power>On</Power></Power_Control></Main_Zone>"));
64         verify(con).send(eq("<Main_Zone><Power_Control><Power>Standby</Power></Power_Control></Main_Zone>"));
65     }
66
67     @Test
68     public void given_RX_S601D_when_mute_then_sendsProperCommand() throws Exception {
69         given(RX_S601D);
70
71         // when
72         subject.setMute(true);
73         subject.setMute(false);
74
75         // then
76         verify(con).send(eq("<Main_Zone><Volume><Mute>On</Mute></Volume></Main_Zone>"));
77         verify(con).send(eq("<Main_Zone><Volume><Mute>Off</Mute></Volume></Main_Zone>"));
78     }
79
80     @Test
81     public void given_RX_V3900_when_mute_then_sendsProperCommand() throws Exception {
82         given(RX_V3900);
83
84         // when
85         subject.setMute(true);
86         subject.setMute(false);
87
88         // then
89         verify(con).send(eq("<Main_Zone><Vol><Mute>On</Mute></Vol></Main_Zone>"));
90         verify(con).send(eq("<Main_Zone><Vol><Mute>Off</Mute></Vol></Main_Zone>"));
91     }
92
93     @Test
94     public void given_RX_S601D_when_volume_then_sendsProperCommand() throws Exception {
95         given(RX_S601D);
96
97         // when
98         subject.setVolumeDB(-2);
99
100         // then
101         verify(con).send(
102                 eq("<Main_Zone><Volume><Lvl><Val>-20</Val><Exp>1</Exp><Unit>dB</Unit></Lvl></Volume></Main_Zone>"));
103     }
104
105     @Test
106     public void given_RX_V3900_when_volume_then_sendsProperCommand() throws Exception {
107         given(RX_V3900);
108
109         // when
110         subject.setVolumeDB(-2);
111
112         // then
113         verify(con).send(eq("<Main_Zone><Vol><Lvl><Val>-20</Val><Exp>1</Exp><Unit>dB</Unit></Lvl></Vol></Main_Zone>"));
114     }
115
116     @Test
117     public void given_RX_S601D_when_input_then_sendsProperCommand() throws Exception {
118         when_input_then_sendsProperCommand(RX_S601D);
119     }
120
121     @Test
122     public void given_RX_V3900_when_input_then_sendsProperCommand() throws Exception {
123         when_input_then_sendsProperCommand(RX_V3900);
124     }
125
126     private void when_input_then_sendsProperCommand(String model) throws Exception {
127         given(model);
128
129         // when
130         subject.setInput("HDMI1");
131
132         // then
133         verify(con).send(eq("<Main_Zone><Input><Input_Sel>HDMI1</Input_Sel></Input></Main_Zone>"));
134     }
135
136     @Test
137     public void given_RX_S601D_when_surroundProgram_then_sendsProperCommand() throws Exception {
138         given(RX_S601D);
139
140         // when
141         subject.setSurroundProgram("Adventure");
142
143         // then
144         verify(con).send(eq(
145                 "<Main_Zone><Surround><Program_Sel><Current><Sound_Program>Adventure</Sound_Program></Current></Program_Sel></Surround></Main_Zone>"));
146     }
147
148     @Test
149     public void given_RX_V3900_when_surroundProgram_then_sendsProperCommand() throws Exception {
150         given(RX_V3900);
151
152         // when
153         subject.setSurroundProgram("Adventure");
154
155         // then
156         verify(con).send(eq(
157                 "<Main_Zone><Surr><Pgm_Sel><Straight>Off</Straight><Pgm>Adventure</Pgm></Pgm_Sel></Surr></Main_Zone>"));
158     }
159
160     @Test
161     public void given_RX_S601D_when_surroundProgramStraight_then_sendsProperCommand() throws Exception {
162         given(RX_S601D);
163
164         // when
165         subject.setSurroundProgram("Straight");
166
167         // then
168         verify(con).send(eq(
169                 "<Main_Zone><Surround><Program_Sel><Current><Straight>On</Straight></Current></Program_Sel></Surround></Main_Zone>"));
170     }
171
172     @Test
173     public void given_RX_V3900_when_surroundProgramStraight_then_sendsProperCommand() throws Exception {
174         given(RX_V3900);
175
176         // when
177         subject.setSurroundProgram("Straight");
178
179         // then
180         verify(con).send(eq("<Main_Zone><Surr><Pgm_Sel><Straight>On</Straight></Pgm_Sel></Surr></Main_Zone>"));
181     }
182
183     @Test
184     public void given_HTR_4069_when_dialogueLevel_then_sendsProperCommand() throws Exception {
185         given(HTR_4069);
186
187         // when
188         subject.setDialogueLevel(10);
189
190         // then
191         verify(con).send(eq(
192                 "<Main_Zone><Sound_Video><Dialogue_Adjust><Dialogue_Lvl>10</Dialogue_Lvl></Dialogue_Adjust></Sound_Video></Main_Zone>"));
193     }
194
195     @Test
196     public void given_RX_S601D_when_update_then_readsStateProperly() throws Exception {
197         given(RX_S601D);
198
199         // when
200         subject.update();
201
202         // then
203         ArgumentCaptor<ZoneControlState> stateArg = ArgumentCaptor.forClass(ZoneControlState.class);
204         verify(zoneControlStateListener).zoneStateChanged(stateArg.capture());
205
206         ZoneControlState state = stateArg.getValue();
207         assertNotNull(state);
208
209         assertEquals(false, state.power);
210         assertEquals(false, state.mute);
211         assertEquals(-43, state.volumeDB, 0);
212         assertEquals("Spotify", state.inputID);
213         assertEquals("5ch Stereo", state.surroundProgram);
214         // this model does not support dialogue level
215         assertEquals(0, state.dialogueLevel);
216     }
217
218     @Test
219     public void given_RX_V3900_when_update_then_readsStateProperly() throws Exception {
220         given(RX_V3900);
221
222         // when
223         subject.update();
224
225         // then
226         ArgumentCaptor<ZoneControlState> stateArg = ArgumentCaptor.forClass(ZoneControlState.class);
227         verify(zoneControlStateListener).zoneStateChanged(stateArg.capture());
228
229         ZoneControlState state = stateArg.getValue();
230         assertNotNull(state);
231
232         assertEquals(true, state.power);
233         assertEquals(false, state.mute);
234         assertEquals(-46, state.volumeDB, 0);
235         assertEquals("TV", state.inputID);
236         assertEquals("2ch Stereo", state.surroundProgram);
237         // this model does not support dialogue level
238         assertEquals(0, state.dialogueLevel);
239     }
240
241     @Test
242     public void given_HTR_4069_when_update_then_readsStateProperly() throws Exception {
243         given(HTR_4069);
244
245         // when
246         subject.update();
247
248         // then
249         ArgumentCaptor<ZoneControlState> stateArg = ArgumentCaptor.forClass(ZoneControlState.class);
250         verify(zoneControlStateListener).zoneStateChanged(stateArg.capture());
251
252         ZoneControlState state = stateArg.getValue();
253         assertNotNull(state);
254
255         assertEquals(false, state.power);
256         assertEquals(false, state.mute);
257         assertEquals(-46, state.volumeDB, 0);
258         assertEquals("TUNER", state.inputID);
259         assertEquals("5ch Stereo", state.surroundProgram);
260         assertEquals(1, state.dialogueLevel);
261     }
262 }