]> git.basschouten.com Git - openhab-addons.git/blob
df58dd117b187b675803256e022f1de4f0ca4529
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.dmx.internal.handler;
14
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.*;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
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;
37
38 /**
39  * Tests cases for {@link org.openhab.binding.dmx.internal.handler.DimmerThingHandler} in normal
40  * mode.
41  *
42  * @author Jan N. Klug - Initial contribution
43  */
44 public class DimmerThingHandlerTest extends AbstractDmxThingTestParent {
45     private static final String TEST_CHANNEL_CONFIG = "100";
46     private static final int TEST_FADE_TIME = 1500;
47
48     private Map<String, Object> thingProperties;
49     private Thing dimmerThing;
50     private DimmerThingHandler dimmerThingHandler;
51
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);
54
55     @BeforeEach
56     public void setUp() {
57         super.setup();
58
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);
63
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())
68                 .build();
69
70         dimmerThingHandler = new DimmerThingHandler(dimmerThing) {
71             @Override
72             protected @Nullable Bridge getBridge() {
73                 return bridge;
74             }
75         };
76         initializeHandler(dimmerThingHandler);
77     }
78
79     @Test
80     public void testThingStatus() {
81         assertThingStatus(dimmerThing);
82     }
83
84     @Test
85     public void testThingStatus_noBridge() {
86         // check that thing is offline if no bridge found
87         DimmerThingHandler dimmerHandlerWithoutBridge = new DimmerThingHandler(dimmerThing) {
88             @Override
89             protected @Nullable Bridge getBridge() {
90                 return null;
91             }
92         };
93         assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
94     }
95
96     @Test
97     public void testOnOffCommand() {
98         // on
99         long currentTime = System.currentTimeMillis();
100
101         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
102         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
103
104         waitForAssert(() -> {
105             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
106                     state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
107         });
108
109         // off
110         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
111         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
112
113         waitForAssert(() -> {
114             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
115                     state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
116         });
117     }
118
119     @Test
120     public void testDynamicTurnOnValue() {
121         long currentTime = System.currentTimeMillis();
122         int testValue = 75;
123
124         // turn on with arbitrary value
125         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(testValue));
126         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
127
128         waitForAssert(() -> {
129             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
130                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
131         });
132
133         // turn off and hopefully store last value
134         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
135         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
136
137         waitForAssert(() -> {
138             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
139                     state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
140         });
141
142         // turn on and restore value
143         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
144         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
145
146         waitForAssert(() -> {
147             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
148                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
149         });
150     }
151
152     @Test
153     public void testPercentTypeCommand() {
154         assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_BRIGHTNESS, TEST_FADE_TIME);
155     }
156 }