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.OnOffType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.builder.ChannelBuilder;
35 import org.openhab.core.thing.binding.builder.ThingBuilder;
38 * Tests cases for {@link org.openhab.binding.dmx.internal.handler.DimmerThingHandler} in normal
41 * @author Jan N. Klug - Initial contribution
43 public class DimmerThingHandlerTest extends AbstractDmxThingTestParent {
44 private static final String TEST_CHANNEL_CONFIG = "100";
45 private static final int TEST_FADE_TIME = 1500;
47 private Map<String, Object> thingProperties;
48 private Thing dimmerThing;
49 private DimmerThingHandler dimmerThingHandler;
51 private final ThingUID THING_UID_DIMMER = new ThingUID(THING_TYPE_DIMMER, "testdimmer");
52 private final ChannelUID CHANNEL_UID_BRIGHTNESS = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS);
58 thingProperties = new HashMap<>();
59 thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL_CONFIG);
60 thingProperties.put(CONFIG_DIMMER_FADE_TIME, TEST_FADE_TIME);
61 thingProperties.put(CONFIG_DIMMER_DYNAMICTURNONVALUE, true);
63 dimmerThing = ThingBuilder
64 .create(THING_TYPE_DIMMER, "testdimmer").withLabel("Dimmer Thing").withBridge(bridge.getUID())
65 .withConfiguration(new Configuration(thingProperties)).withChannel(ChannelBuilder
66 .create(CHANNEL_UID_BRIGHTNESS, "Brightness").withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
69 dimmerThingHandler = new DimmerThingHandler(dimmerThing) {
71 protected @Nullable Bridge getBridge() {
75 initializeHandler(dimmerThingHandler);
79 public void testThingStatus() {
80 assertThingStatus(dimmerThing);
84 public void testThingStatus_noBridge() {
85 // check that thing is offline if no bridge found
86 DimmerThingHandler dimmerHandlerWithoutBridge = new DimmerThingHandler(dimmerThing) {
88 protected @Nullable Bridge getBridge() {
92 assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
96 public void testOnOffCommand() {
98 long currentTime = System.currentTimeMillis();
100 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
101 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
103 waitForAssert(() -> {
104 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
105 state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
109 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
110 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
112 waitForAssert(() -> {
113 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
114 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
119 public void testDynamicTurnOnValue() {
120 long currentTime = System.currentTimeMillis();
123 // turn on with arbitrary value
124 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(testValue));
125 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
127 waitForAssert(() -> {
128 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
129 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
132 // turn off and hopefully store last value
133 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
134 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
136 waitForAssert(() -> {
137 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
138 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
141 // turn on and restore value
142 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
143 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
145 waitForAssert(() -> {
146 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
147 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
152 public void testPercentTypeCommand() {
153 assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_BRIGHTNESS, TEST_FADE_TIME);