]> git.basschouten.com Git - openhab-addons.git/blob
01c9116b9e01fddcf0d525a6111c870d61490adb
[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.hue.internal;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.junit.Assert.*;
17
18 import org.junit.Test;
19 import org.openhab.binding.hue.internal.State.ColorMode;
20 import org.openhab.binding.hue.internal.handler.LightStateConverter;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.HSBType;
23 import org.openhab.core.library.types.PercentType;
24
25 /**
26  *
27  * @author Markus Bösling - Initial contribution
28  * @author Denis Dudnik - switched to internally integrated source of Jue library
29  * @author Markus Rathgeb - migrated to plain Java test
30  */
31 public class LightStateConverterTest {
32
33     @Test
34     public void brightnessOfZeroIsZero() {
35         final State lightState = new State();
36         // 0 percent should not be sent to the Hue interface
37         StateUpdate stateUpdate = LightStateConverter.toBrightnessLightState(PercentType.ZERO);
38         assertThat(stateUpdate.commands.size(), is(1));
39         // a brightness of 0 should result in 0 percent
40         lightState.bri = 0;
41         assertThat(LightStateConverter.toBrightnessPercentType(lightState), is(PercentType.ZERO));
42     }
43
44     @Test
45     public void brightnessLightStateConverterConversionIsBijective() {
46         final State lightState = new State();
47         for (int percent = 1; percent <= 100; ++percent) {
48             StateUpdate stateUpdate = LightStateConverter.toBrightnessLightState(new PercentType(percent));
49             assertThat(stateUpdate.commands.size(), is(2));
50             assertThat(stateUpdate.commands.get(1).key, is("bri"));
51             lightState.bri = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
52             assertThat(LightStateConverter.toBrightnessPercentType(lightState).intValue(), is(percent));
53         }
54     }
55
56     @Test
57     public void brightnessAlwaysGreaterThanZero() {
58         final State lightState = new State();
59         // a brightness greater than 1 should result in a percentage greater than 1
60         for (int brightness = 1; brightness <= 254; ++brightness) {
61             lightState.bri = brightness;
62             assertTrue(LightStateConverter.toBrightnessPercentType(lightState).intValue() > 0);
63         }
64     }
65
66     @Test
67     public void colorWithBightnessOfZeroIsZero() {
68         final State lightState = new State();
69         lightState.colormode = ColorMode.CT.toString();
70         // 0 percent should not be sent to the Hue interface
71         final HSBType hsbType = new HSBType(DecimalType.ZERO, PercentType.ZERO, PercentType.ZERO);
72         StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
73         assertThat(stateUpdate.commands.size(), is(2));
74         // a brightness of 0 should result in 0 percent
75         lightState.bri = 0;
76         assertThat(LightStateConverter.toHSBType(lightState).getBrightness(), is(PercentType.ZERO));
77     }
78
79     @Test
80     public void colorLightStateConverterForBrightnessConversionIsBijective() {
81         final State lightState = new State();
82         lightState.colormode = ColorMode.CT.toString();
83         for (int percent = 1; percent <= 100; ++percent) {
84             final HSBType hsbType = new HSBType(DecimalType.ZERO, PercentType.ZERO, new PercentType(percent));
85             StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
86             assertThat(stateUpdate.commands.size(), is(3));
87             assertThat(stateUpdate.commands.get(2).key, is("bri"));
88             lightState.bri = Integer.parseInt(stateUpdate.commands.get(2).value.toString());
89             assertThat(LightStateConverter.toHSBType(lightState).getBrightness().intValue(), is(percent));
90         }
91     }
92
93     @Test
94     public void hsbBrightnessAlwaysGreaterThanZero() {
95         final State lightState = new State();
96         lightState.colormode = ColorMode.CT.toString();
97         // a brightness greater than 1 should result in a percentage greater than 1
98         for (int brightness = 1; brightness <= 254; ++brightness) {
99             lightState.bri = brightness;
100             assertTrue(LightStateConverter.toHSBType(lightState).getBrightness().intValue() > 0);
101         }
102     }
103
104     @Test
105     public void hsbHueAlwaysGreaterThanZeroAndLessThan360() {
106         final State lightState = new State();
107         for (int hue = 0; hue <= 65535; ++hue) {
108             lightState.hue = hue;
109             assertTrue(LightStateConverter.toHSBType(lightState).getHue().intValue() >= 0);
110             assertTrue(LightStateConverter.toHSBType(lightState).getHue().intValue() < 360);
111         }
112     }
113
114     @Test
115     public void colorLightStateConverterForSaturationConversionIsBijective() {
116         final State lightState = new State();
117         lightState.colormode = ColorMode.CT.toString();
118         for (int percent = 0; percent <= 100; ++percent) {
119             final HSBType hsbType = new HSBType(DecimalType.ZERO, new PercentType(percent), PercentType.HUNDRED);
120             StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
121             assertThat(stateUpdate.commands.size(), is(3));
122             assertThat(stateUpdate.commands.get(1).key, is("sat"));
123             lightState.sat = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
124             assertThat(LightStateConverter.toHSBType(lightState).getSaturation().intValue(), is(percent));
125         }
126     }
127
128     @Test
129     public void colorLightStateConverterForHueConversionIsBijective() {
130         final State lightState = new State();
131         for (int hue = 0; hue < 360; ++hue) {
132             final HSBType hsbType = new HSBType(new DecimalType(hue), PercentType.HUNDRED, PercentType.HUNDRED);
133             StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
134             assertThat(stateUpdate.commands.size(), is(3));
135             assertThat(stateUpdate.commands.get(0).key, is("hue"));
136             lightState.hue = Integer.parseInt(stateUpdate.commands.get(0).value.toString());
137             assertThat(LightStateConverter.toHSBType(lightState).getHue().intValue(), is(hue));
138         }
139     }
140
141     @Test
142     public void hsbSaturationAlwaysGreaterThanZero() {
143         final State lightState = new State();
144         lightState.colormode = ColorMode.CT.toString();
145         // a saturation greater than 1 should result in a percentage greater than 1
146         for (int saturation = 1; saturation <= 254; ++saturation) {
147             lightState.sat = saturation;
148             assertTrue(LightStateConverter.toHSBType(lightState).getSaturation().intValue() > 0);
149         }
150     }
151 }