]> git.basschouten.com Git - openhab-addons.git/blob
be58072c3b202de4d2e360b69f21852e8ef01905
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.homematic.internal.communicator.virtual;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.io.IOException;
19
20 import org.junit.Ignore;
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.homematic.internal.misc.HomematicClientException;
24 import org.openhab.binding.homematic.internal.misc.HomematicConstants;
25 import org.openhab.binding.homematic.internal.misc.MiscUtils;
26 import org.openhab.binding.homematic.internal.model.HmChannel;
27 import org.openhab.binding.homematic.internal.model.HmDatapoint;
28 import org.openhab.binding.homematic.internal.model.HmDevice;
29 import org.openhab.binding.homematic.internal.model.HmInterface;
30 import org.openhab.binding.homematic.internal.model.HmParamsetType;
31 import org.openhab.binding.homematic.internal.model.HmValueType;
32 import org.openhab.core.test.java.JavaTest;
33 import org.openhab.core.thing.CommonTriggerEvents;
34
35 /**
36  * Tests for {@link ButtonVirtualDatapointHandler}.
37  *
38  * @author Michael Reitler - Initial Contribution
39  *
40  */
41 public class ButtonDatapointTest extends JavaTest {
42
43     private static final int DISABLE_DATAPOINT_DELAY = 50;
44
45     private MockEventReceiver mockEventReceiver;
46     private final ButtonVirtualDatapointHandler bvdpHandler = new ButtonVirtualDatapointHandler();
47
48     @BeforeEach
49     public void setup() throws IOException {
50         this.mockEventReceiver = new MockEventReceiver();
51     }
52
53     @Test
54     public void testShortPress() throws IOException, HomematicClientException {
55         HmDatapoint shortPressDp = createPressDatapoint("PRESS_SHORT", Boolean.TRUE);
56         HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(shortPressDp);
57
58         mockEventReceiver.eventReceived(shortPressDp);
59
60         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
61     }
62
63     @Test
64     public void testLongPress() throws IOException, HomematicClientException {
65         HmDatapoint longPressDp = createPressDatapoint("PRESS_LONG", Boolean.TRUE);
66         HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(longPressDp);
67
68         mockEventReceiver.eventReceived(longPressDp);
69
70         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.LONG_PRESSED));
71     }
72
73     @Test
74     public void testUnsupportedEvents() throws IOException, HomematicClientException {
75         HmDatapoint contPressDp = createPressDatapoint("PRESS_CONT", Boolean.TRUE);
76         HmDatapoint contButtonVirtualDatapoint = getButtonVirtualDatapoint(contPressDp);
77
78         mockEventReceiver.eventReceived(contPressDp);
79
80         HmDatapoint releaseDp = createPressDatapoint("PRESS_LONG_RELEASE", Boolean.TRUE);
81         HmDatapoint releaseButtonVirtualDatapoint = getButtonVirtualDatapoint(releaseDp);
82
83         mockEventReceiver.eventReceived(releaseDp);
84
85         HmDatapoint crapDp = createPressDatapoint("CRAP", Boolean.TRUE);
86         HmDatapoint crapButtonVirtualDatapoint = getButtonVirtualDatapoint(releaseDp);
87
88         mockEventReceiver.eventReceived(crapDp);
89
90         assertThat(contButtonVirtualDatapoint.getValue(), nullValue());
91         assertThat(releaseButtonVirtualDatapoint.getValue(), nullValue());
92         assertThat(crapButtonVirtualDatapoint.getValue(), nullValue());
93     }
94
95     @Test
96     @Ignore(value = "Test is unstable see #10753")
97     public void testDoublePress() throws IOException, HomematicClientException, InterruptedException {
98         HmDatapoint shortPressDp = createPressDatapoint("PRESS_SHORT", Boolean.TRUE);
99         HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(shortPressDp);
100
101         mockEventReceiver.eventReceived(shortPressDp);
102         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
103
104         Thread.sleep(DISABLE_DATAPOINT_DELAY / 2);
105
106         shortPressDp.setValue(Boolean.TRUE);
107         mockEventReceiver.eventReceived(shortPressDp);
108         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.DOUBLE_PRESSED));
109
110         Thread.sleep(DISABLE_DATAPOINT_DELAY * 2);
111
112         shortPressDp.setValue(Boolean.TRUE);
113         mockEventReceiver.eventReceived(shortPressDp);
114         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
115     }
116
117     private HmDatapoint createPressDatapoint(String channelName, Object value) {
118         HmDatapoint pressDp = new HmDatapoint(channelName, "", HmValueType.ACTION, value, true, HmParamsetType.VALUES);
119         HmChannel hmChannel = new HmChannel(channelName, 1);
120         HmDevice device = new HmDevice("ABC12345", HmInterface.RF, "HM-MOCK", "mockid", "mockid", "mockfw");
121         hmChannel.setDevice(device);
122         device.addChannel(hmChannel);
123         hmChannel.addDatapoint(pressDp);
124         pressDp.setChannel(hmChannel);
125         bvdpHandler.initialize(device);
126
127         return pressDp;
128     }
129
130     private HmDatapoint getButtonVirtualDatapoint(HmDatapoint originalDatapoint) {
131         return originalDatapoint.getChannel().getDatapoints().stream()
132                 .filter(dp -> HomematicConstants.VIRTUAL_DATAPOINT_NAME_BUTTON.equals(dp.getName())).findFirst()
133                 .orElse(null);
134     }
135
136     /**
137      * Mock parts of {@linkplain org.openhab.binding.homematic.internal.communicator.AbstractHomematicGateway}
138      */
139     private class MockEventReceiver {
140
141         public void eventReceived(HmDatapoint dp) throws IOException, HomematicClientException {
142             if (bvdpHandler.canHandleEvent(dp)) {
143                 bvdpHandler.handleEvent(null, dp);
144             }
145             if (dp.isPressDatapoint() && MiscUtils.isTrueValue(dp.getValue())) {
146                 disableDatapoint(dp);
147             }
148         }
149
150         private void disableDatapoint(HmDatapoint dp) {
151             new Thread(() -> {
152                 try {
153                     Thread.sleep(DISABLE_DATAPOINT_DELAY);
154                     dp.setValue(Boolean.FALSE);
155                 } catch (InterruptedException e) {
156                 }
157             }).start();
158         }
159     }
160 }