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.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.dmx.test.AbstractDmxThingTestParent;
29 import org.openhab.core.config.core.Configuration;
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.DimmerThingHandler} in normal
43 * @author Jan N. Klug - Initial contribution
46 public class DimmerThingHandlerTest extends AbstractDmxThingTestParent {
47 private static final String TEST_CHANNEL_CONFIG = "100";
48 private static final int TEST_FADE_TIME = 1500;
49 private static final ThingUID THING_UID_DIMMER = new ThingUID(THING_TYPE_DIMMER, "testdimmer");
50 private static final ChannelUID CHANNEL_UID_BRIGHTNESS = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS);
52 private @NonNullByDefault({}) Map<String, Object> thingProperties;
53 private @NonNullByDefault({}) Thing dimmerThing;
54 private @NonNullByDefault({}) DimmerThingHandler dimmerThingHandler;
60 thingProperties = new HashMap<>();
61 thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL_CONFIG);
62 thingProperties.put(CONFIG_DIMMER_FADE_TIME, TEST_FADE_TIME);
63 thingProperties.put(CONFIG_DIMMER_DYNAMICTURNONVALUE, true);
65 dimmerThing = ThingBuilder
66 .create(THING_TYPE_DIMMER, "testdimmer").withLabel("Dimmer Thing").withBridge(bridge.getUID())
67 .withConfiguration(new Configuration(thingProperties)).withChannel(ChannelBuilder
68 .create(CHANNEL_UID_BRIGHTNESS, "Brightness").withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
71 dimmerThingHandler = new DimmerThingHandler(dimmerThing) {
73 protected @Nullable Bridge getBridge() {
77 initializeHandler(dimmerThingHandler);
81 public void testThingStatus() {
82 assertThingStatus(dimmerThing);
86 public void testThingStatusNoBridge() {
87 // check that thing is offline if no bridge found
88 DimmerThingHandler dimmerHandlerWithoutBridge = new DimmerThingHandler(dimmerThing) {
90 protected @Nullable Bridge getBridge() {
94 assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
98 public void testOnOffCommand() {
100 long currentTime = System.currentTimeMillis();
102 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
103 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
105 waitForAssert(() -> {
106 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
107 state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
111 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
112 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
114 waitForAssert(() -> {
115 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
116 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
121 public void testDynamicTurnOnValue() {
122 long currentTime = System.currentTimeMillis();
125 // turn on with arbitrary value
126 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(testValue));
127 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
129 waitForAssert(() -> {
130 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
131 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
134 // turn off and hopefully store last value
135 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
136 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
138 waitForAssert(() -> {
139 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
140 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
143 // turn on and restore value
144 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
145 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
147 waitForAssert(() -> {
148 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
149 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
154 public void testPercentTypeCommand() {
155 assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_BRIGHTNESS, TEST_FADE_TIME);