]> git.basschouten.com Git - openhab-addons.git/blob
1c6bf28c55d17a370d395255b42110bb9d16c1c8
[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.DecimalType;
29 import org.openhab.core.library.types.HSBType;
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.ColorThingHandler} in RGB mode.
41  *
42  * @author Jan N. Klug - Initial contribution
43  */
44 public class ColorThingHandlerTest extends AbstractDmxThingTestParent {
45
46     private static final String TEST_CHANNEL_CONFIG = "100/3";
47     private static final int TEST_FADE_TIME = 1500;
48     private static final HSBType TEST_COLOR = new HSBType(new DecimalType(280), new PercentType(100),
49             new PercentType(100));
50
51     private Map<String, Object> thingProperties;
52     private Thing dimmerThing;
53     private ColorThingHandler dimmerThingHandler;
54
55     private final ThingUID THING_UID_DIMMER = new ThingUID(THING_TYPE_COLOR, "testdimmer");
56     private final ChannelUID CHANNEL_UID_COLOR = new ChannelUID(THING_UID_DIMMER, CHANNEL_COLOR);
57     private final ChannelUID CHANNEL_UID_BRIGHTNESS_R = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_R);
58     private final ChannelUID CHANNEL_UID_BRIGHTNESS_G = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_G);
59     private final ChannelUID CHANNEL_UID_BRIGHTNESS_B = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_B);
60
61     @Before
62     public void setUp() {
63         super.setup();
64         thingProperties = new HashMap<>();
65         thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL_CONFIG);
66         thingProperties.put(CONFIG_DIMMER_FADE_TIME, TEST_FADE_TIME);
67         thingProperties.put(CONFIG_DIMMER_TURNONVALUE, "255,128,0");
68         thingProperties.put(CONFIG_DIMMER_DYNAMICTURNONVALUE, true);
69         dimmerThing = ThingBuilder.create(THING_TYPE_COLOR, "testdimmer").withLabel("Dimmer Thing")
70                 .withBridge(bridge.getUID()).withConfiguration(new Configuration(thingProperties))
71                 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_R, "Brightness R")
72                         .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
73                 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_G, "Brightness G")
74                         .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
75                 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_B, "Brightness B")
76                         .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
77                 .withChannel(ChannelBuilder.create(CHANNEL_UID_COLOR, "Color").withType(COLOR_CHANNEL_TYPEUID).build())
78                 .build();
79         dimmerThingHandler = new ColorThingHandler(dimmerThing) {
80             @Override
81             protected @Nullable Bridge getBridge() {
82                 return bridge;
83             }
84         };
85         initializeHandler(dimmerThingHandler);
86     }
87
88     @Test
89     public void testThingStatus() {
90         assertThingStatus(dimmerThing);
91     }
92
93     @Test
94     public void testThingStatus_noBridge() {
95         // check that thing is offline if no bridge found
96         ColorThingHandler dimmerHandlerWithoutBridge = new ColorThingHandler(dimmerThing) {
97             @Override
98             protected @Nullable Bridge getBridge() {
99                 return null;
100             }
101         };
102         assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
103     }
104
105     @Test
106     public void testOnOffCommand() {
107         // on
108         long currentTime = System.currentTimeMillis();
109
110         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON);
111         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
112
113         waitForAssert(() -> {
114             assertChannelStateUpdate(CHANNEL_UID_COLOR, state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
115             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R, state -> assertEquals(PercentType.HUNDRED, state));
116             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G,
117                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50.0, 0.5))));
118             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.ZERO, state));
119         });
120
121         // off
122         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.OFF);
123         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
124
125         waitForAssert(() -> {
126             assertChannelStateUpdate(CHANNEL_UID_COLOR,
127                     state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
128             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R, state -> assertEquals(PercentType.ZERO, state));
129             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
130             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.ZERO, state));
131         });
132     }
133
134     @Test
135     public void testDynamicTurnOnValue() {
136         long currentTime = System.currentTimeMillis();
137
138         // turn on with arbitrary value
139         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, TEST_COLOR);
140         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
141
142         waitForAssert(() -> {
143             assertChannelStateUpdate(CHANNEL_UID_COLOR,
144                     state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
145             assertChannelStateUpdate(CHANNEL_UID_COLOR,
146                     state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
147             assertChannelStateUpdate(CHANNEL_UID_COLOR,
148                     state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 1))));
149         });
150
151         // turn off and hopefully store last value
152         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.OFF);
153         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
154
155         waitForAssert(() -> {
156             assertChannelStateUpdate(CHANNEL_UID_COLOR,
157                     state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(0.0, 1))));
158         });
159
160         // turn on and restore value
161         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON);
162         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
163
164         waitForAssert(() -> {
165             assertChannelStateUpdate(CHANNEL_UID_COLOR,
166                     state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
167             assertChannelStateUpdate(CHANNEL_UID_COLOR,
168                     state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
169             assertChannelStateUpdate(CHANNEL_UID_COLOR,
170                     state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 1))));
171         });
172     }
173
174     @Test
175     public void testPercentTypeCommand() {
176         assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_COLOR, TEST_FADE_TIME);
177     }
178
179     @Test
180     public void testColorCommand() {
181         // setting of color
182         long currentTime = System.currentTimeMillis();
183
184         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, TEST_COLOR);
185         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
186
187         waitForAssert(() -> {
188             assertChannelStateUpdate(CHANNEL_UID_COLOR,
189                     state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 1))));
190             assertChannelStateUpdate(CHANNEL_UID_COLOR,
191                     state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 0.5))));
192             assertChannelStateUpdate(CHANNEL_UID_COLOR,
193                     state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 0.5))));
194             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R,
195                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(66.5, 0.5))));
196             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
197             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.HUNDRED, state));
198         });
199
200         // color dimming
201         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, new PercentType(30));
202         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
203
204         waitForAssert(() -> {
205             assertChannelStateUpdate(CHANNEL_UID_COLOR,
206                     state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
207             assertChannelStateUpdate(CHANNEL_UID_COLOR,
208                     state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
209             assertChannelStateUpdate(CHANNEL_UID_COLOR,
210                     state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(30.0, 1))));
211             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R,
212                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(19.2, 0.5))));
213             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
214             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B,
215                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(29.8, 0.5))));
216         });
217     }
218 }