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