]> git.basschouten.com Git - openhab-addons.git/blob
d4c9baecc1d874ce63e3238d9be3a5f45358aa3d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.hue.internal;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
18 import static org.junit.jupiter.api.Assertions.assertTrue;
19
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.hue.internal.State.ColorMode;
22 import org.openhab.binding.hue.internal.dto.ColorTemperature;
23 import org.openhab.binding.hue.internal.handler.LightStateConverter;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.HSBType;
26 import org.openhab.core.library.types.PercentType;
27
28 /**
29  *
30  * @author Markus Bösling - Initial contribution
31  * @author Denis Dudnik - switched to internally integrated source of Jue library
32  * @author Markus Rathgeb - migrated to plain Java test
33  */
34 public class LightStateConverterTest {
35
36     @Test
37     public void colorTemperatureLightStateConverterConversionIsBijectiveDefaultColorTemperatureCapabilities() {
38         final State lightState = new State();
39         final ColorTemperature colorTemperature = new ColorTemperature();
40         for (int percent = 1; percent <= 100; ++percent) {
41             StateUpdate stateUpdate = LightStateConverter
42                     .toColorTemperatureLightStateFromPercentType(new PercentType(percent), colorTemperature);
43             assertThat(stateUpdate.commands, hasSize(1));
44             assertThat(stateUpdate.commands.get(0).key, is("ct"));
45             lightState.ct = Integer.parseInt(stateUpdate.commands.get(0).value.toString());
46             assertThat(LightStateConverter.toColorTemperaturePercentType(lightState, colorTemperature).intValue(),
47                     is(percent));
48         }
49     }
50
51     @Test
52     public void colorTemperatureLightStateConverterConversionIsBijectiveIndividualColorTemperatureCapabilities() {
53         final State lightState = new State();
54         final ColorTemperature colorTemperature = new ColorTemperature();
55         colorTemperature.min = 250;
56         colorTemperature.max = 454;
57         for (int percent = 1; percent <= 100; ++percent) {
58             StateUpdate stateUpdate = LightStateConverter
59                     .toColorTemperatureLightStateFromPercentType(new PercentType(percent), colorTemperature);
60             assertThat(stateUpdate.commands, hasSize(1));
61             assertThat(stateUpdate.commands.get(0).key, is("ct"));
62             lightState.ct = Integer.parseInt(stateUpdate.commands.get(0).value.toString());
63             assertThat(LightStateConverter.toColorTemperaturePercentType(lightState, colorTemperature).intValue(),
64                     is(percent));
65         }
66     }
67
68     @Test
69     public void brightnessOfZeroIsZero() {
70         final State lightState = new State();
71         // 0 percent should not be sent to the Hue interface
72         StateUpdate stateUpdate = LightStateConverter.toBrightnessLightState(PercentType.ZERO);
73         assertThat(stateUpdate.commands, hasSize(1));
74         // a brightness of 0 should result in 0 percent
75         lightState.bri = 0;
76         assertThat(LightStateConverter.toBrightnessPercentType(lightState), is(PercentType.ZERO));
77     }
78
79     @Test
80     public void brightnessLightStateConverterConversionIsBijective() {
81         final State lightState = new State();
82         for (int percent = 1; percent <= 100; ++percent) {
83             StateUpdate stateUpdate = LightStateConverter.toBrightnessLightState(new PercentType(percent));
84             assertThat(stateUpdate.commands, hasSize(2));
85             assertThat(stateUpdate.commands.get(1).key, is("bri"));
86             lightState.bri = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
87             assertThat(LightStateConverter.toBrightnessPercentType(lightState).intValue(), is(percent));
88         }
89     }
90
91     @Test
92     public void brightnessAlwaysGreaterThanZero() {
93         final State lightState = new State();
94         // a brightness greater than 1 should result in a percentage greater than 1
95         for (int brightness = 1; brightness <= 254; ++brightness) {
96             lightState.bri = brightness;
97             assertTrue(LightStateConverter.toBrightnessPercentType(lightState).intValue() > 0);
98         }
99     }
100
101     @Test
102     public void colorWithBightnessOfZeroIsZero() {
103         final State lightState = new State();
104         lightState.colormode = ColorMode.CT.toString();
105         // 0 percent should not be sent to the Hue interface
106         final HSBType hsbType = new HSBType(DecimalType.ZERO, PercentType.ZERO, PercentType.ZERO);
107         StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
108         assertThat(stateUpdate.commands, hasSize(1));
109         // a brightness of 0 should result in 0 percent
110         lightState.bri = 0;
111         assertThat(LightStateConverter.toHSBType(lightState).getBrightness(), is(PercentType.ZERO));
112     }
113
114     @Test
115     public void colorLightStateConverterForBrightnessConversionIsBijective() {
116         final State lightState = new State();
117         lightState.colormode = ColorMode.CT.toString();
118         for (int percent = 1; percent <= 100; ++percent) {
119             final HSBType hsbType = new HSBType(DecimalType.ZERO, PercentType.ZERO, new PercentType(percent));
120             StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
121             assertThat(stateUpdate.commands, hasSize(2));
122             assertThat(stateUpdate.commands.get(1).key, is("bri"));
123             lightState.bri = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
124             assertThat(LightStateConverter.toHSBType(lightState).getBrightness().intValue(), is(percent));
125         }
126     }
127
128     @Test
129     public void hsbBrightnessAlwaysGreaterThanZero() {
130         final State lightState = new State();
131         lightState.colormode = ColorMode.CT.toString();
132         // a brightness greater than 1 should result in a percentage greater than 1
133         for (int brightness = 1; brightness <= 254; ++brightness) {
134             lightState.bri = brightness;
135             assertTrue(LightStateConverter.toHSBType(lightState).getBrightness().intValue() > 0);
136         }
137     }
138
139     @Test
140     public void hsbHueAlwaysGreaterThanZeroAndLessThan360() {
141         final State lightState = new State();
142         for (int hue = 0; hue <= 65535; ++hue) {
143             lightState.hue = hue;
144             assertTrue(LightStateConverter.toHSBType(lightState).getHue().intValue() >= 0);
145             assertTrue(LightStateConverter.toHSBType(lightState).getHue().intValue() < 360);
146         }
147     }
148
149     @Test
150     public void colorLightStateConverterForSaturationConversionIsBijective() {
151         final State lightState = new State();
152         lightState.colormode = ColorMode.HS.toString();
153         for (int percent = 0; percent <= 100; ++percent) {
154             final HSBType hsbType = new HSBType(DecimalType.ZERO, new PercentType(percent), PercentType.HUNDRED);
155             StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
156             assertThat(stateUpdate.commands, hasSize(3));
157             assertThat(stateUpdate.commands.get(1).key, is("sat"));
158             lightState.sat = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
159             assertThat(LightStateConverter.toHSBType(lightState).getSaturation().intValue(), is(percent));
160         }
161     }
162
163     @Test
164     public void colorLightStateConverterForHueConversionIsBijective() {
165         final State lightState = new State();
166         lightState.colormode = ColorMode.HS.toString();
167         for (int hue = 0; hue < 360; ++hue) {
168             final HSBType hsbType = new HSBType(new DecimalType(hue), PercentType.HUNDRED, PercentType.HUNDRED);
169             StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
170             assertThat(stateUpdate.commands, hasSize(3));
171             assertThat(stateUpdate.commands.get(0).key, is("hue"));
172             lightState.hue = Integer.parseInt(stateUpdate.commands.get(0).value.toString());
173             assertThat(LightStateConverter.toHSBType(lightState).getHue().intValue(), is(hue));
174         }
175     }
176
177     @Test
178     public void colorLightStateConverterColorModeSelection() {
179         final State lightState = new State();
180         final HSBType hsbType = new HSBType(PercentType.HUNDRED, PercentType.HUNDRED, PercentType.HUNDRED);
181
182         lightState.colormode = null;
183         StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
184         assertThat(stateUpdate.commands, hasSize(2));
185         assertThat(stateUpdate.commands.get(0).key, is("xy"));
186
187         lightState.colormode = ColorMode.CT.toString();
188         stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
189         assertThat(stateUpdate.commands, hasSize(2));
190         assertThat(stateUpdate.commands.get(0).key, is("xy"));
191
192         lightState.colormode = ColorMode.HS.toString();
193         stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
194         assertThat(stateUpdate.commands, hasSize(3));
195         assertThat(stateUpdate.commands.get(0).key, is("hue"));
196
197         lightState.colormode = ColorMode.XY.toString();
198         stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
199         assertThat(stateUpdate.commands, hasSize(2));
200         assertThat(stateUpdate.commands.get(0).key, is("xy"));
201     }
202
203     @Test
204     public void hsbSaturationAlwaysGreaterThanZero() {
205         final State lightState = new State();
206         lightState.colormode = ColorMode.CT.toString();
207         // a saturation greater than 1 should result in a percentage greater than 1
208         for (int saturation = 1; saturation <= 254; ++saturation) {
209             lightState.sat = saturation;
210             assertTrue(LightStateConverter.toHSBType(lightState).getSaturation().intValue() > 0);
211         }
212     }
213 }