2 * Copyright (c) 2010-2020 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.junit.Assert.*;
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;
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
31 public class LightStateConverterTest {
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
41 assertThat(LightStateConverter.toBrightnessPercentType(lightState), is(PercentType.ZERO));
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));
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);
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
76 assertThat(LightStateConverter.toHSBType(lightState).getBrightness(), is(PercentType.ZERO));
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));
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);
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);
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));
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));
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);