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