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.dmx.internal.handler;
15 import static org.hamcrest.core.Is.is;
16 import static org.hamcrest.number.IsCloseTo.closeTo;
17 import static org.junit.Assert.*;
18 import static org.openhab.binding.dmx.internal.DmxBindingConstants.*;
20 import java.util.HashMap;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.openhab.binding.dmx.test.AbstractDmxThingTestParent;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.HSBType;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.PercentType;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingUID;
36 import org.openhab.core.thing.binding.builder.ChannelBuilder;
37 import org.openhab.core.thing.binding.builder.ThingBuilder;
40 * Tests cases for {@link org.openhab.binding.dmx.internal.handler.ColorThingHandler} in RGB mode.
42 * @author Jan N. Klug - Initial contribution
44 public class ColorThingHandlerTest extends AbstractDmxThingTestParent {
46 private static final String TEST_CHANNEL_CONFIG = "100/3";
47 private static final int TEST_FADE_TIME = 1500;
48 private static final HSBType TEST_COLOR = new HSBType(new DecimalType(280), new PercentType(100),
49 new PercentType(100));
51 private Map<String, Object> thingProperties;
52 private Thing dimmerThing;
53 private ColorThingHandler dimmerThingHandler;
55 private final ThingUID THING_UID_DIMMER = new ThingUID(THING_TYPE_COLOR, "testdimmer");
56 private final ChannelUID CHANNEL_UID_COLOR = new ChannelUID(THING_UID_DIMMER, CHANNEL_COLOR);
57 private final ChannelUID CHANNEL_UID_BRIGHTNESS_R = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_R);
58 private final ChannelUID CHANNEL_UID_BRIGHTNESS_G = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_G);
59 private final ChannelUID CHANNEL_UID_BRIGHTNESS_B = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_B);
64 thingProperties = new HashMap<>();
65 thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL_CONFIG);
66 thingProperties.put(CONFIG_DIMMER_FADE_TIME, TEST_FADE_TIME);
67 thingProperties.put(CONFIG_DIMMER_TURNONVALUE, "255,128,0");
68 thingProperties.put(CONFIG_DIMMER_DYNAMICTURNONVALUE, true);
69 dimmerThing = ThingBuilder.create(THING_TYPE_COLOR, "testdimmer").withLabel("Dimmer Thing")
70 .withBridge(bridge.getUID()).withConfiguration(new Configuration(thingProperties))
71 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_R, "Brightness R")
72 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
73 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_G, "Brightness G")
74 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
75 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_B, "Brightness B")
76 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
77 .withChannel(ChannelBuilder.create(CHANNEL_UID_COLOR, "Color").withType(COLOR_CHANNEL_TYPEUID).build())
79 dimmerThingHandler = new ColorThingHandler(dimmerThing) {
81 protected @Nullable Bridge getBridge() {
85 initializeHandler(dimmerThingHandler);
89 public void testThingStatus() {
90 assertThingStatus(dimmerThing);
94 public void testThingStatus_noBridge() {
95 // check that thing is offline if no bridge found
96 ColorThingHandler dimmerHandlerWithoutBridge = new ColorThingHandler(dimmerThing) {
98 protected @Nullable Bridge getBridge() {
102 assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
106 public void testOnOffCommand() {
108 long currentTime = System.currentTimeMillis();
110 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON);
111 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
113 waitForAssert(() -> {
114 assertChannelStateUpdate(CHANNEL_UID_COLOR, state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
115 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R, state -> assertEquals(PercentType.HUNDRED, state));
116 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G,
117 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50.0, 0.5))));
118 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.ZERO, state));
122 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.OFF);
123 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
125 waitForAssert(() -> {
126 assertChannelStateUpdate(CHANNEL_UID_COLOR,
127 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
128 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R, state -> assertEquals(PercentType.ZERO, state));
129 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
130 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.ZERO, state));
135 public void testDynamicTurnOnValue() {
136 long currentTime = System.currentTimeMillis();
138 // turn on with arbitrary value
139 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, TEST_COLOR);
140 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
142 waitForAssert(() -> {
143 assertChannelStateUpdate(CHANNEL_UID_COLOR,
144 state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
145 assertChannelStateUpdate(CHANNEL_UID_COLOR,
146 state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
147 assertChannelStateUpdate(CHANNEL_UID_COLOR,
148 state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 1))));
151 // turn off and hopefully store last value
152 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.OFF);
153 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
155 waitForAssert(() -> {
156 assertChannelStateUpdate(CHANNEL_UID_COLOR,
157 state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(0.0, 1))));
160 // turn on and restore value
161 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON);
162 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
164 waitForAssert(() -> {
165 assertChannelStateUpdate(CHANNEL_UID_COLOR,
166 state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
167 assertChannelStateUpdate(CHANNEL_UID_COLOR,
168 state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
169 assertChannelStateUpdate(CHANNEL_UID_COLOR,
170 state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 1))));
175 public void testPercentTypeCommand() {
176 assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_COLOR, TEST_FADE_TIME);
180 public void testColorCommand() {
182 long currentTime = System.currentTimeMillis();
184 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, TEST_COLOR);
185 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
187 waitForAssert(() -> {
188 assertChannelStateUpdate(CHANNEL_UID_COLOR,
189 state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 1))));
190 assertChannelStateUpdate(CHANNEL_UID_COLOR,
191 state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 0.5))));
192 assertChannelStateUpdate(CHANNEL_UID_COLOR,
193 state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 0.5))));
194 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R,
195 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(66.5, 0.5))));
196 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
197 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.HUNDRED, state));
201 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, new PercentType(30));
202 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
204 waitForAssert(() -> {
205 assertChannelStateUpdate(CHANNEL_UID_COLOR,
206 state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
207 assertChannelStateUpdate(CHANNEL_UID_COLOR,
208 state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
209 assertChannelStateUpdate(CHANNEL_UID_COLOR,
210 state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(30.0, 1))));
211 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R,
212 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(19.2, 0.5))));
213 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
214 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B,
215 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(29.8, 0.5))));