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.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.homematic.internal.misc.HomematicClientException;
23 import org.openhab.binding.homematic.internal.misc.HomematicConstants;
24 import org.openhab.binding.homematic.internal.misc.MiscUtils;
25 import org.openhab.binding.homematic.internal.model.HmChannel;
26 import org.openhab.binding.homematic.internal.model.HmDatapoint;
27 import org.openhab.binding.homematic.internal.model.HmDevice;
28 import org.openhab.binding.homematic.internal.model.HmInterface;
29 import org.openhab.binding.homematic.internal.model.HmParamsetType;
30 import org.openhab.binding.homematic.internal.model.HmValueType;
31 import org.openhab.core.test.java.JavaTest;
32 import org.openhab.core.thing.CommonTriggerEvents;
35 * Tests for {@link ButtonVirtualDatapointHandler}.
37 * @author Michael Reitler - Initial Contribution
40 public class ButtonDatapointTest extends JavaTest {
42 private static final int DISABLE_DATAPOINT_DELAY = 50;
44 private MockEventReceiver mockEventReceiver;
45 private final ButtonVirtualDatapointHandler bvdpHandler = new ButtonVirtualDatapointHandler();
48 public void setup() throws IOException {
49 this.mockEventReceiver = new MockEventReceiver();
53 public void testShortPress() throws IOException, HomematicClientException {
54 HmDatapoint shortPressDp = createPressDatapoint("PRESS_SHORT", Boolean.TRUE);
55 HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(shortPressDp);
57 mockEventReceiver.eventReceived(shortPressDp);
59 assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
63 public void testLongPress() throws IOException, HomematicClientException {
64 HmDatapoint longPressDp = createPressDatapoint("PRESS_LONG", Boolean.TRUE);
65 HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(longPressDp);
67 mockEventReceiver.eventReceived(longPressDp);
69 assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.LONG_PRESSED));
73 public void testUnsupportedEvents() throws IOException, HomematicClientException {
74 HmDatapoint contPressDp = createPressDatapoint("PRESS_CONT", Boolean.TRUE);
75 HmDatapoint contButtonVirtualDatapoint = getButtonVirtualDatapoint(contPressDp);
77 mockEventReceiver.eventReceived(contPressDp);
79 HmDatapoint releaseDp = createPressDatapoint("PRESS_LONG_RELEASE", Boolean.TRUE);
80 HmDatapoint releaseButtonVirtualDatapoint = getButtonVirtualDatapoint(releaseDp);
82 mockEventReceiver.eventReceived(releaseDp);
84 HmDatapoint crapDp = createPressDatapoint("CRAP", Boolean.TRUE);
85 HmDatapoint crapButtonVirtualDatapoint = getButtonVirtualDatapoint(releaseDp);
87 mockEventReceiver.eventReceived(crapDp);
89 assertThat(contButtonVirtualDatapoint.getValue(), nullValue());
90 assertThat(releaseButtonVirtualDatapoint.getValue(), nullValue());
91 assertThat(crapButtonVirtualDatapoint.getValue(), nullValue());
95 public void testDoublePress() throws IOException, HomematicClientException, InterruptedException {
96 HmDatapoint shortPressDp = createPressDatapoint("PRESS_SHORT", Boolean.TRUE);
97 HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(shortPressDp);
99 mockEventReceiver.eventReceived(shortPressDp);
100 assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
102 Thread.sleep(DISABLE_DATAPOINT_DELAY / 2);
104 shortPressDp.setValue(Boolean.TRUE);
105 mockEventReceiver.eventReceived(shortPressDp);
106 assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.DOUBLE_PRESSED));
108 Thread.sleep(DISABLE_DATAPOINT_DELAY * 2);
110 shortPressDp.setValue(Boolean.TRUE);
111 mockEventReceiver.eventReceived(shortPressDp);
112 assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
115 private HmDatapoint createPressDatapoint(String channelName, Object value) {
116 HmDatapoint pressDp = new HmDatapoint(channelName, "", HmValueType.ACTION, value, true, HmParamsetType.VALUES);
117 HmChannel hmChannel = new HmChannel(channelName, 1);
118 HmDevice device = new HmDevice("ABC12345", HmInterface.RF, "HM-MOCK", "mockid", "mockid", "mockfw");
119 hmChannel.setDevice(device);
120 device.addChannel(hmChannel);
121 hmChannel.addDatapoint(pressDp);
122 pressDp.setChannel(hmChannel);
123 bvdpHandler.initialize(device);
128 private HmDatapoint getButtonVirtualDatapoint(HmDatapoint originalDatapoint) {
129 return originalDatapoint.getChannel().getDatapoints().stream()
130 .filter(dp -> HomematicConstants.VIRTUAL_DATAPOINT_NAME_BUTTON.equals(dp.getName())).findFirst()
135 * Mock parts of {@linkplain org.openhab.binding.homematic.internal.communicator.AbstractHomematicGateway}
137 private class MockEventReceiver {
139 public void eventReceived(HmDatapoint dp) throws IOException, HomematicClientException {
140 if (bvdpHandler.canHandleEvent(dp)) {
141 bvdpHandler.handleEvent(null, dp);
143 if (dp.isPressDatapoint() && MiscUtils.isTrueValue(dp.getValue())) {
144 disableDatapoint(dp);
148 private void disableDatapoint(HmDatapoint dp) {
151 Thread.sleep(DISABLE_DATAPOINT_DELAY);
152 dp.setValue(Boolean.FALSE);
153 } catch (InterruptedException e) {