2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.homematic.internal.communicator.virtual;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
18 import java.io.IOException;
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;
36 * Tests for {@link ButtonVirtualDatapointHandler}.
38 * @author Michael Reitler - Initial Contribution
41 public class ButtonDatapointTest extends JavaTest {
43 private static final int DISABLE_DATAPOINT_DELAY = 50;
45 private MockEventReceiver mockEventReceiver;
46 private final ButtonVirtualDatapointHandler bvdpHandler = new ButtonVirtualDatapointHandler();
49 public void setup() throws IOException {
50 this.mockEventReceiver = new MockEventReceiver();
54 public void testShortPress() throws IOException, HomematicClientException {
55 HmDatapoint shortPressDp = createPressDatapoint("PRESS_SHORT", Boolean.TRUE);
56 HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(shortPressDp);
58 mockEventReceiver.eventReceived(shortPressDp);
60 assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
64 public void testLongPress() throws IOException, HomematicClientException {
65 HmDatapoint longPressDp = createPressDatapoint("PRESS_LONG", Boolean.TRUE);
66 HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(longPressDp);
68 mockEventReceiver.eventReceived(longPressDp);
70 assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.LONG_PRESSED));
74 public void testUnsupportedEvents() throws IOException, HomematicClientException {
75 HmDatapoint contPressDp = createPressDatapoint("PRESS_CONT", Boolean.TRUE);
76 HmDatapoint contButtonVirtualDatapoint = getButtonVirtualDatapoint(contPressDp);
78 mockEventReceiver.eventReceived(contPressDp);
80 HmDatapoint releaseDp = createPressDatapoint("PRESS_LONG_RELEASE", Boolean.TRUE);
81 HmDatapoint releaseButtonVirtualDatapoint = getButtonVirtualDatapoint(releaseDp);
83 mockEventReceiver.eventReceived(releaseDp);
85 HmDatapoint crapDp = createPressDatapoint("CRAP", Boolean.TRUE);
86 HmDatapoint crapButtonVirtualDatapoint = getButtonVirtualDatapoint(releaseDp);
88 mockEventReceiver.eventReceived(crapDp);
90 assertThat(contButtonVirtualDatapoint.getValue(), nullValue());
91 assertThat(releaseButtonVirtualDatapoint.getValue(), nullValue());
92 assertThat(crapButtonVirtualDatapoint.getValue(), nullValue());
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);
101 mockEventReceiver.eventReceived(shortPressDp);
102 assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
104 Thread.sleep(DISABLE_DATAPOINT_DELAY / 2);
106 shortPressDp.setValue(Boolean.TRUE);
107 mockEventReceiver.eventReceived(shortPressDp);
108 assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.DOUBLE_PRESSED));
110 Thread.sleep(DISABLE_DATAPOINT_DELAY * 2);
112 shortPressDp.setValue(Boolean.TRUE);
113 mockEventReceiver.eventReceived(shortPressDp);
114 assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
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);
130 private HmDatapoint getButtonVirtualDatapoint(HmDatapoint originalDatapoint) {
131 return originalDatapoint.getChannel().getDatapoints().stream()
132 .filter(dp -> HomematicConstants.VIRTUAL_DATAPOINT_NAME_BUTTON.equals(dp.getName())).findFirst()
137 * Mock parts of {@linkplain org.openhab.binding.homematic.internal.communicator.AbstractHomematicGateway}
139 private class MockEventReceiver {
141 public void eventReceived(HmDatapoint dp) throws IOException, HomematicClientException {
142 if (bvdpHandler.canHandleEvent(dp)) {
143 bvdpHandler.handleEvent(null, dp);
145 if (dp.isPressDatapoint() && MiscUtils.isTrueValue(dp.getValue())) {
146 disableDatapoint(dp);
150 private void disableDatapoint(HmDatapoint dp) {
153 Thread.sleep(DISABLE_DATAPOINT_DELAY);
154 dp.setValue(Boolean.FALSE);
155 } catch (InterruptedException e) {