]> git.basschouten.com Git - openhab-addons.git/blob
1f7946571ef81e832fcabe5afd7395d549f79ed2
[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.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;
38
39 /**
40  * Tests cases for {@link org.openhab.binding.dmx.internal.handler.DimmerThingHandler} in normal
41  * mode.
42  *
43  * @author Jan N. Klug - Initial contribution
44  */
45 @NonNullByDefault
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);
51
52     private @NonNullByDefault({}) Map<String, Object> thingProperties;
53     private @NonNullByDefault({}) Thing dimmerThing;
54     private @NonNullByDefault({}) DimmerThingHandler dimmerThingHandler;
55
56     @BeforeEach
57     public void setUp() {
58         super.setup();
59
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);
64
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())
69                 .build();
70
71         dimmerThingHandler = new DimmerThingHandler(dimmerThing) {
72             @Override
73             protected @Nullable Bridge getBridge() {
74                 return bridge;
75             }
76         };
77         initializeHandler(dimmerThingHandler);
78     }
79
80     @Test
81     public void testThingStatus() {
82         assertThingStatus(dimmerThing);
83     }
84
85     @Test
86     public void testThingStatusNoBridge() {
87         // check that thing is offline if no bridge found
88         DimmerThingHandler dimmerHandlerWithoutBridge = new DimmerThingHandler(dimmerThing) {
89             @Override
90             protected @Nullable Bridge getBridge() {
91                 return null;
92             }
93         };
94         assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
95     }
96
97     @Test
98     public void testOnOffCommand() {
99         // on
100         long currentTime = System.currentTimeMillis();
101
102         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
103         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
104
105         waitForAssert(() -> {
106             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
107                     state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
108         });
109
110         // off
111         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
112         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
113
114         waitForAssert(() -> {
115             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
116                     state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
117         });
118     }
119
120     @Test
121     public void testDynamicTurnOnValue() {
122         long currentTime = System.currentTimeMillis();
123         int testValue = 75;
124
125         // turn on with arbitrary value
126         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(testValue));
127         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
128
129         waitForAssert(() -> {
130             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
131                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
132         });
133
134         // turn off and hopefully store last value
135         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
136         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
137
138         waitForAssert(() -> {
139             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
140                     state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
141         });
142
143         // turn on and restore value
144         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
145         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
146
147         waitForAssert(() -> {
148             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
149                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
150         });
151     }
152
153     @Test
154     public void testPercentTypeCommand() {
155         assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_BRIGHTNESS, TEST_FADE_TIME);
156     }
157 }