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.OnOffType;
30 import org.openhab.core.library.types.PercentType;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.builder.ChannelBuilder;
36 import org.openhab.core.thing.binding.builder.ThingBuilder;
39 * Tests cases for {@link org.openhab.binding.dmx.internal.handler.DimmerThingHandler} in normal
42 * @author Jan N. Klug - Initial contribution
44 public class DimmerThingHandlerTest extends AbstractDmxThingTestParent {
45 private static final String TEST_CHANNEL_CONFIG = "100";
46 private static final int TEST_FADE_TIME = 1500;
48 private Map<String, Object> thingProperties;
49 private Thing dimmerThing;
50 private DimmerThingHandler dimmerThingHandler;
52 private final ThingUID THING_UID_DIMMER = new ThingUID(THING_TYPE_DIMMER, "testdimmer");
53 private final ChannelUID CHANNEL_UID_BRIGHTNESS = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS);
59 thingProperties = new HashMap<>();
60 thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL_CONFIG);
61 thingProperties.put(CONFIG_DIMMER_FADE_TIME, TEST_FADE_TIME);
62 thingProperties.put(CONFIG_DIMMER_DYNAMICTURNONVALUE, true);
64 dimmerThing = ThingBuilder
65 .create(THING_TYPE_DIMMER, "testdimmer").withLabel("Dimmer Thing").withBridge(bridge.getUID())
66 .withConfiguration(new Configuration(thingProperties)).withChannel(ChannelBuilder
67 .create(CHANNEL_UID_BRIGHTNESS, "Brightness").withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
70 dimmerThingHandler = new DimmerThingHandler(dimmerThing) {
72 protected @Nullable Bridge getBridge() {
76 initializeHandler(dimmerThingHandler);
80 public void testThingStatus() {
81 assertThingStatus(dimmerThing);
85 public void testThingStatus_noBridge() {
86 // check that thing is offline if no bridge found
87 DimmerThingHandler dimmerHandlerWithoutBridge = new DimmerThingHandler(dimmerThing) {
89 protected @Nullable Bridge getBridge() {
93 assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
97 public void testOnOffCommand() {
99 long currentTime = System.currentTimeMillis();
101 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
102 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
104 waitForAssert(() -> {
105 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
106 state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
110 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
111 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
113 waitForAssert(() -> {
114 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
115 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
120 public void testDynamicTurnOnValue() {
121 long currentTime = System.currentTimeMillis();
124 // turn on with arbitrary value
125 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(testValue));
126 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
128 waitForAssert(() -> {
129 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
130 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
133 // turn off and hopefully store last value
134 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
135 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
137 waitForAssert(() -> {
138 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
139 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
142 // turn on and restore value
143 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
144 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
146 waitForAssert(() -> {
147 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
148 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
153 public void testPercentTypeCommand() {
154 assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_BRIGHTNESS, TEST_FADE_TIME);