]> git.basschouten.com Git - openhab-addons.git/blob
b495509cb16d59a726fa2e9a9bf9ab63646e67a3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.emotiva.internal;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.CHANNEL_MAIN_VOLUME;
18 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.CHANNEL_MUTE;
19 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.CHANNEL_STANDBY;
20 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.CHANNEL_SURROUND;
21 import static org.openhab.binding.emotiva.internal.EmotivaCommandHelper.volumeDecibelToPercentage;
22 import static org.openhab.binding.emotiva.internal.EmotivaCommandHelper.volumePercentageToDecibel;
23 import static org.openhab.binding.emotiva.internal.protocol.EmotivaControlCommands.mute;
24 import static org.openhab.binding.emotiva.internal.protocol.EmotivaControlCommands.mute_off;
25 import static org.openhab.binding.emotiva.internal.protocol.EmotivaControlCommands.mute_on;
26 import static org.openhab.binding.emotiva.internal.protocol.EmotivaControlCommands.standby;
27 import static org.openhab.binding.emotiva.internal.protocol.EmotivaControlCommands.surround;
28 import static org.openhab.binding.emotiva.internal.protocol.EmotivaControlCommands.surround_trim_set;
29 import static org.openhab.binding.emotiva.internal.protocol.EmotivaControlCommands.volume;
30 import static org.openhab.binding.emotiva.internal.protocol.EmotivaDataType.DIMENSIONLESS_DECIBEL;
31 import static org.openhab.binding.emotiva.internal.protocol.EmotivaDataType.ON_OFF;
32 import static org.openhab.binding.emotiva.internal.protocol.EmotivaProtocolVersion.PROTOCOL_V2;
33 import static org.openhab.binding.emotiva.internal.protocol.EmotivaProtocolVersion.PROTOCOL_V3;
34
35 import java.util.Map;
36 import java.util.concurrent.ConcurrentHashMap;
37 import java.util.stream.Stream;
38
39 import org.eclipse.jdt.annotation.NonNullByDefault;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.params.ParameterizedTest;
42 import org.junit.jupiter.params.provider.Arguments;
43 import org.junit.jupiter.params.provider.MethodSource;
44 import org.openhab.binding.emotiva.internal.protocol.EmotivaControlCommands;
45 import org.openhab.binding.emotiva.internal.protocol.EmotivaControlRequest;
46 import org.openhab.binding.emotiva.internal.protocol.EmotivaDataType;
47 import org.openhab.binding.emotiva.internal.protocol.EmotivaProtocolVersion;
48 import org.openhab.core.library.types.PercentType;
49
50 /**
51  * Unit tests for the EmotivaCommandHelper.
52  *
53  * @author Espen Fossen - Initial contribution
54  */
55 @NonNullByDefault
56 class EmotivaCommandHelperTest {
57
58     @Test
59     void volumeToPercentage() {
60         assertThat(volumeDecibelToPercentage("-100 dB"), is(PercentType.valueOf("0")));
61         assertThat(volumeDecibelToPercentage(" -96"), is(PercentType.valueOf("0")));
62         assertThat(volumeDecibelToPercentage("-41 dB "), is(PercentType.valueOf("50")));
63         assertThat(volumeDecibelToPercentage("15"), is(PercentType.valueOf("100")));
64         assertThat(volumeDecibelToPercentage("20"), is(PercentType.valueOf("100")));
65     }
66
67     @Test
68     void volumeToDecibel() {
69         assertThat(volumePercentageToDecibel("-10"), is(-96));
70         assertThat(volumePercentageToDecibel("0%"), is(-96));
71         assertThat(volumePercentageToDecibel("50 %"), is(-41));
72         assertThat(volumePercentageToDecibel("100 % "), is(15));
73         assertThat(volumePercentageToDecibel("110"), is(15));
74     }
75
76     private static Stream<Arguments> channelToControlRequest() {
77         return Stream.of(
78                 Arguments.of(CHANNEL_SURROUND, "surround", DIMENSIONLESS_DECIBEL, surround, surround, surround,
79                         surround_trim_set, PROTOCOL_V2, -24.0, 24.0),
80                 Arguments.of(CHANNEL_SURROUND, "surround", DIMENSIONLESS_DECIBEL, surround, surround, surround,
81                         surround_trim_set, PROTOCOL_V3, -24.0, 24.0),
82                 Arguments.of(CHANNEL_MUTE, "mute", ON_OFF, mute, mute_on, mute_off, mute, PROTOCOL_V2, 0, 0),
83                 Arguments.of(CHANNEL_STANDBY, "standby", ON_OFF, standby, standby, standby, standby, PROTOCOL_V2, 0, 0),
84                 Arguments.of(CHANNEL_MAIN_VOLUME, "volume", DIMENSIONLESS_DECIBEL, volume, volume, volume, volume,
85                         PROTOCOL_V2, -96, 15));
86     }
87
88     @ParameterizedTest
89     @MethodSource("channelToControlRequest")
90     void testChannelToControlRequest(String channel, String name, EmotivaDataType emotivaDataType,
91             EmotivaControlCommands defaultCommand, EmotivaControlCommands onCommand, EmotivaControlCommands offCommand,
92             EmotivaControlCommands setCommand, EmotivaProtocolVersion version, double min, double max) {
93         final Map<String, Map<EmotivaControlCommands, String>> commandMaps = new ConcurrentHashMap<>();
94
95         EmotivaControlRequest surround = EmotivaCommandHelper.channelToControlRequest(channel, commandMaps, version);
96         assertThat(surround.getName(), is(name));
97         assertThat(surround.getChannel(), is(channel));
98         assertThat(surround.getDataType(), is(emotivaDataType));
99         assertThat(surround.getDefaultCommand(), is(defaultCommand));
100         assertThat(surround.getOnCommand(), is(onCommand));
101         assertThat(surround.getOffCommand(), is(offCommand));
102         assertThat(surround.getSetCommand(), is(setCommand));
103         assertThat(surround.getMinValue(), is(min));
104         assertThat(surround.getMaxValue(), is(max));
105     }
106 }