|
| 1 | +package io.github.hapjava.characteristics.impl; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import io.github.hapjava.characteristics.Characteristic; |
| 6 | +import io.github.hapjava.characteristics.impl.windowcovering.CurrentHorizontalTiltAngleCharacteristic; |
| 7 | +import io.github.hapjava.characteristics.impl.windowcovering.CurrentVerticalTiltAngleCharacteristic; |
| 8 | +import io.github.hapjava.characteristics.impl.windowcovering.TargetHorizontalTiltAngleCharacteristic; |
| 9 | +import io.github.hapjava.characteristics.impl.windowcovering.TargetVerticalTiltAngleCharacteristic; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.concurrent.CompletableFuture; |
| 12 | +import javax.json.JsonObject; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +/** Custom tilt angle ranges, see https://github.com/hap-java/HAP-Java/issues/162 */ |
| 16 | +public class CustomTiltAngleRangeTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void defaultConstructorsKeepTheirSerializedOutput() { |
| 20 | + // Serialized output captured before custom ranges were added. |
| 21 | + for (Case c : |
| 22 | + Arrays.asList( |
| 23 | + Case.of( |
| 24 | + new CurrentHorizontalTiltAngleCharacteristic( |
| 25 | + () -> CompletableFuture.completedFuture(50), cb -> {}, () -> {}), |
| 26 | + "{\"iid\":7,\"type\":\"6C\",\"perms\":[\"pr\",\"ev\"],\"format\":\"int\",\"value\":50,\"minValue\":-90,\"maxValue\":90,\"minStep\":1,\"unit\":\"arcdegrees\"}"), |
| 27 | + Case.of( |
| 28 | + new TargetHorizontalTiltAngleCharacteristic( |
| 29 | + () -> CompletableFuture.completedFuture(50), v -> {}, cb -> {}, () -> {}), |
| 30 | + "{\"iid\":7,\"type\":\"7B\",\"perms\":[\"pr\",\"pw\",\"ev\"],\"format\":\"int\",\"value\":50,\"minValue\":-90,\"maxValue\":90,\"minStep\":1,\"unit\":\"arcdegrees\"}"), |
| 31 | + Case.of( |
| 32 | + new CurrentVerticalTiltAngleCharacteristic( |
| 33 | + () -> CompletableFuture.completedFuture(50), cb -> {}, () -> {}), |
| 34 | + "{\"iid\":7,\"type\":\"6E\",\"perms\":[\"pr\",\"ev\"],\"format\":\"int\",\"value\":50,\"minValue\":-90,\"maxValue\":90,\"minStep\":1,\"unit\":\"arcdegrees\"}"), |
| 35 | + Case.of( |
| 36 | + new TargetVerticalTiltAngleCharacteristic( |
| 37 | + () -> CompletableFuture.completedFuture(50), v -> {}, cb -> {}, () -> {}), |
| 38 | + "{\"iid\":7,\"type\":\"7D\",\"perms\":[\"pr\",\"pw\",\"ev\"],\"format\":\"int\",\"value\":50,\"minValue\":-90,\"maxValue\":90,\"minStep\":1,\"unit\":\"arcdegrees\"}"))) { |
| 39 | + assertThat(c.characteristic.toJson(7).join().toString()) |
| 40 | + .as(c.characteristic.getClass().getSimpleName()) |
| 41 | + .isEqualTo(c.expected); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void customRangeIsAdvertised() { |
| 47 | + for (Characteristic c : |
| 48 | + Arrays.<Characteristic>asList( |
| 49 | + new CurrentHorizontalTiltAngleCharacteristic( |
| 50 | + 0, 90, () -> CompletableFuture.completedFuture(50), cb -> {}, () -> {}), |
| 51 | + new TargetHorizontalTiltAngleCharacteristic( |
| 52 | + 0, 90, () -> CompletableFuture.completedFuture(50), v -> {}, cb -> {}, () -> {}), |
| 53 | + new CurrentVerticalTiltAngleCharacteristic( |
| 54 | + 0, 90, () -> CompletableFuture.completedFuture(50), cb -> {}, () -> {}), |
| 55 | + new TargetVerticalTiltAngleCharacteristic( |
| 56 | + 0, 90, () -> CompletableFuture.completedFuture(50), v -> {}, cb -> {}, () -> {}))) { |
| 57 | + JsonObject json = c.toJson(7).join(); |
| 58 | + String name = c.getClass().getSimpleName(); |
| 59 | + assertThat(json.getInt("minValue")).as(name).isEqualTo(0); |
| 60 | + assertThat(json.getInt("maxValue")).as(name).isEqualTo(90); |
| 61 | + assertThat(json.getInt("minStep")).as(name).isEqualTo(1); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static class Case { |
| 66 | + final Characteristic characteristic; |
| 67 | + final String expected; |
| 68 | + |
| 69 | + private Case(Characteristic characteristic, String expected) { |
| 70 | + this.characteristic = characteristic; |
| 71 | + this.expected = expected; |
| 72 | + } |
| 73 | + |
| 74 | + static Case of(Characteristic characteristic, String expected) { |
| 75 | + return new Case(characteristic, expected); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments