2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.hue.internal;
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;
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;
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
34 public class LightStateConverterTest {
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(),
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(),
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
76 assertThat(LightStateConverter.toBrightnessPercentType(lightState), is(PercentType.ZERO));
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));
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);
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
111 assertThat(LightStateConverter.toHSBType(lightState).getBrightness(), is(PercentType.ZERO));
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));
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);
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);
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));
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));
178 public void colorLightStateConverterColorModeSelection() {
179 final State lightState = new State();
180 final HSBType hsbType = new HSBType(PercentType.HUNDRED, PercentType.HUNDRED, PercentType.HUNDRED);
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"));
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"));
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"));
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"));
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);