2 * Copyright (c) 2010-2023 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.dmx.internal.handler;
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.core.Is.is;
17 import static org.hamcrest.number.IsCloseTo.closeTo;
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.openhab.binding.dmx.internal.DmxBindingConstants.*;
21 import java.util.HashMap;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.dmx.test.AbstractDmxThingTestParent;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.library.types.DecimalType;
30 import org.openhab.core.library.types.HSBType;
31 import org.openhab.core.library.types.OnOffType;
32 import org.openhab.core.library.types.PercentType;
33 import org.openhab.core.thing.Bridge;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingUID;
37 import org.openhab.core.thing.binding.builder.ChannelBuilder;
38 import org.openhab.core.thing.binding.builder.ThingBuilder;
41 * Tests cases for {@link org.openhab.binding.dmx.internal.handler.ColorThingHandler} in RGB mode.
43 * @author Jan N. Klug - Initial contribution
45 public class ColorThingHandlerTest extends AbstractDmxThingTestParent {
47 private static final String TEST_CHANNEL_CONFIG = "100/3";
48 private static final int TEST_FADE_TIME = 1500;
49 private static final HSBType TEST_COLOR = new HSBType(new DecimalType(280), new PercentType(100),
50 new PercentType(100));
52 private Map<String, Object> thingProperties;
53 private Thing dimmerThing;
54 private ColorThingHandler dimmerThingHandler;
56 private final ThingUID THING_UID_DIMMER = new ThingUID(THING_TYPE_COLOR, "testdimmer");
57 private final ChannelUID CHANNEL_UID_COLOR = new ChannelUID(THING_UID_DIMMER, CHANNEL_COLOR);
58 private final ChannelUID CHANNEL_UID_BRIGHTNESS_R = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_R);
59 private final ChannelUID CHANNEL_UID_BRIGHTNESS_G = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_G);
60 private final ChannelUID CHANNEL_UID_BRIGHTNESS_B = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_B);
65 thingProperties = new HashMap<>();
66 thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL_CONFIG);
67 thingProperties.put(CONFIG_DIMMER_FADE_TIME, TEST_FADE_TIME);
68 thingProperties.put(CONFIG_DIMMER_TURNONVALUE, "255,128,0");
69 thingProperties.put(CONFIG_DIMMER_DYNAMICTURNONVALUE, true);
70 dimmerThing = ThingBuilder.create(THING_TYPE_COLOR, "testdimmer").withLabel("Dimmer Thing")
71 .withBridge(bridge.getUID()).withConfiguration(new Configuration(thingProperties))
72 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_R, "Brightness R")
73 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
74 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_G, "Brightness G")
75 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
76 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_B, "Brightness B")
77 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
78 .withChannel(ChannelBuilder.create(CHANNEL_UID_COLOR, "Color").withType(COLOR_CHANNEL_TYPEUID).build())
80 dimmerThingHandler = new ColorThingHandler(dimmerThing) {
82 protected @Nullable Bridge getBridge() {
86 initializeHandler(dimmerThingHandler);
90 public void testThingStatus() {
91 assertThingStatus(dimmerThing);
95 public void testThingStatus_noBridge() {
96 // check that thing is offline if no bridge found
97 ColorThingHandler dimmerHandlerWithoutBridge = new ColorThingHandler(dimmerThing) {
99 protected @Nullable Bridge getBridge() {
103 assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
107 public void testOnOffCommand() {
109 long currentTime = System.currentTimeMillis();
111 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON);
112 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
114 waitForAssert(() -> {
115 assertChannelStateUpdate(CHANNEL_UID_COLOR, state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
116 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R, state -> assertEquals(PercentType.HUNDRED, state));
117 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G,
118 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50.0, 0.5))));
119 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.ZERO, state));
123 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.OFF);
124 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
126 waitForAssert(() -> {
127 assertChannelStateUpdate(CHANNEL_UID_COLOR,
128 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
129 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R, state -> assertEquals(PercentType.ZERO, state));
130 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
131 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.ZERO, state));
136 public void testDynamicTurnOnValue() {
137 long currentTime = System.currentTimeMillis();
139 // turn on with arbitrary value
140 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, TEST_COLOR);
141 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
143 waitForAssert(() -> {
144 assertChannelStateUpdate(CHANNEL_UID_COLOR,
145 state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
146 assertChannelStateUpdate(CHANNEL_UID_COLOR,
147 state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
148 assertChannelStateUpdate(CHANNEL_UID_COLOR,
149 state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 1))));
152 // turn off and hopefully store last value
153 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.OFF);
154 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
156 waitForAssert(() -> {
157 assertChannelStateUpdate(CHANNEL_UID_COLOR,
158 state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(0.0, 1))));
161 // turn on and restore value
162 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON);
163 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
165 waitForAssert(() -> {
166 assertChannelStateUpdate(CHANNEL_UID_COLOR,
167 state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
168 assertChannelStateUpdate(CHANNEL_UID_COLOR,
169 state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
170 assertChannelStateUpdate(CHANNEL_UID_COLOR,
171 state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 1))));
176 public void testPercentTypeCommand() {
177 assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_COLOR, TEST_FADE_TIME);
181 public void testColorCommand() {
183 long currentTime = System.currentTimeMillis();
185 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, TEST_COLOR);
186 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
188 waitForAssert(() -> {
189 assertChannelStateUpdate(CHANNEL_UID_COLOR,
190 state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 1))));
191 assertChannelStateUpdate(CHANNEL_UID_COLOR,
192 state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 0.5))));
193 assertChannelStateUpdate(CHANNEL_UID_COLOR,
194 state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 0.5))));
195 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R,
196 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(66.5, 0.5))));
197 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
198 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.HUNDRED, state));
202 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, new PercentType(30));
203 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
205 waitForAssert(() -> {
206 assertChannelStateUpdate(CHANNEL_UID_COLOR,
207 state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
208 assertChannelStateUpdate(CHANNEL_UID_COLOR,
209 state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
210 assertChannelStateUpdate(CHANNEL_UID_COLOR,
211 state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(30.0, 1))));
212 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R,
213 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(19.2, 0.5))));
214 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
215 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B,
216 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(29.8, 0.5))));