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.junit.jupiter.api.Assertions.assertTrue;
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;
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
32 public class LightStateConverterTest {
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
42 assertThat(LightStateConverter.toBrightnessPercentType(lightState), is(PercentType.ZERO));
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));
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);
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
77 assertThat(LightStateConverter.toHSBType(lightState).getBrightness(), is(PercentType.ZERO));
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));
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);
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);
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));
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));
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);