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