diff --git a/buttplug4j.connectors.javax.websocket.client/build.gradle b/buttplug4j.connectors.javax.websocket.client/build.gradle index cc3ff7d..24a8840 100644 --- a/buttplug4j.connectors.javax.websocket.client/build.gradle +++ b/buttplug4j.connectors.javax.websocket.client/build.gradle @@ -15,7 +15,7 @@ dependencies { api 'org.eclipse.jetty.websocket:websocket-javax-client:10.0.26' testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4' testImplementation project(':buttplug4j.utils.test') - testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.13.4' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher:6.0.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.13.4' } diff --git a/buttplug4j.connectors.javax.websocket.client/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/client/ButtplugWSClientMockTest.java b/buttplug4j.connectors.javax.websocket.client/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/client/ButtplugWSClientMockTest.java index fb1e0b6..32782d6 100644 --- a/buttplug4j.connectors.javax.websocket.client/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/client/ButtplugWSClientMockTest.java +++ b/buttplug4j.connectors.javax.websocket.client/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/client/ButtplugWSClientMockTest.java @@ -5,12 +5,13 @@ import io.github.blackspherefollower.buttplug4j.client.ButtplugDeviceFeatureException; import io.github.blackspherefollower.buttplug4j.protocol.ButtplugMessage; import io.github.blackspherefollower.buttplug4j.protocol.messages.InputReading; -import io.github.blackspherefollower.buttplug4j.utils.test.WSDMClient; import io.github.blackspherefollower.buttplug4j.utils.test.IntifaceEngineWrapper; +import io.github.blackspherefollower.buttplug4j.utils.test.WSDMClient; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import java.net.URI; +import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.*; @@ -18,7 +19,7 @@ public class ButtplugWSClientMockTest { @Test public void TestConnect() throws Exception { - try(IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper() ) { + try (IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper()) { Thread.sleep(500); WSDMClient wsdev = new WSDMClient(new URI("ws://localhost:" + wrapper.dport), "LVS-Fake", "A9816725B"); Thread.sleep(500); @@ -49,9 +50,8 @@ public void TestConnect() throws Exception { } @Test - @Disabled("See https://github.com/buttplugio/buttplug/issues/801") public void TestBattery() throws Exception { - try(IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper() ) { + try (IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper()) { Thread.sleep(500); WSDMClient wsdev = new WSDMClient(new URI("ws://localhost:" + wrapper.dport), "LVS-Fake", "A9816725B"); Thread.sleep(500); @@ -65,9 +65,9 @@ public void TestBattery() throws Exception { for (ButtplugClientDevice dev : client.getDevices()) { for (ButtplugClientDeviceFeature feat : dev.getDeviceFeatures().values()) { if (feat.HasBattery()) { - ButtplugMessage res = feat.ReadBattery().get(); - if (res instanceof InputReading && ((InputReading) res).getData() instanceof InputReading.BatteryData) { - InputReading.BatteryData reading = (InputReading.BatteryData) ((InputReading) res).getData(); + ButtplugMessage res = feat.ReadBattery().get(2, TimeUnit.SECONDS); + if (res instanceof InputReading && ((InputReading) res).getData() instanceof InputReading.Battery) { + InputReading.Battery reading = (InputReading.Battery) ((InputReading) res).getData(); int battery = reading.getValue(); System.out.println("Battery is " + battery); assertTrue(battery >= 0); diff --git a/buttplug4j.connectors.javax.websocket.server/build.gradle b/buttplug4j.connectors.javax.websocket.server/build.gradle index 5a07e6b..cd573b2 100644 --- a/buttplug4j.connectors.javax.websocket.server/build.gradle +++ b/buttplug4j.connectors.javax.websocket.server/build.gradle @@ -15,7 +15,7 @@ dependencies { testImplementation 'org.eclipse.jetty.websocket:websocket-javax-server:10.0.26' testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4' testImplementation project(':buttplug4j.utils.test') - testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.13.4' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher:6.0.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.13.4' } diff --git a/buttplug4j.connectors.javax.websocket.server/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/server/ButtplugClientWSServerMockTest.java b/buttplug4j.connectors.javax.websocket.server/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/server/ButtplugClientWSServerMockTest.java index 5ecc96b..5d00578 100644 --- a/buttplug4j.connectors.javax.websocket.server/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/server/ButtplugClientWSServerMockTest.java +++ b/buttplug4j.connectors.javax.websocket.server/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/server/ButtplugClientWSServerMockTest.java @@ -11,14 +11,13 @@ import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.websocket.javax.server.config.JavaxWebSocketServletContainerInitializer; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import javax.websocket.Session; import javax.websocket.server.ServerEndpointConfig; -import java.io.IOException; import java.net.URI; -import java.util.concurrent.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -32,7 +31,7 @@ public void TestConnect() throws Exception { ButtplugClientWSServerExample server = new ButtplugClientWSServerExample(lport, testDone); Thread.sleep(500); - try (IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper(lport) ) { + try (IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper(lport)) { Thread.sleep(500); WSDMClient wsdev = new WSDMClient(new URI("ws://localhost:" + wrapper.dport), "LVS-Fake", "A9816725B"); testDone.get(10, TimeUnit.SECONDS); @@ -90,8 +89,8 @@ public void onConnected(ButtplugClient client) { firstDevice.get(5, TimeUnit.SECONDS); for (ButtplugClientDevice dev : client.getDevices()) { - for(ButtplugClientDeviceFeature feat : dev.getDeviceFeatures().values()) { - if( feat.HasVibrate() ) { + for (ButtplugClientDeviceFeature feat : dev.getDeviceFeatures().values()) { + if (feat.HasVibrate()) { feat.VibrateFloat(0.5F).get(); } } diff --git a/buttplug4j.connectors.jetty.websocket.client/build.gradle b/buttplug4j.connectors.jetty.websocket.client/build.gradle index 61af4e9..3c48204 100644 --- a/buttplug4j.connectors.jetty.websocket.client/build.gradle +++ b/buttplug4j.connectors.jetty.websocket.client/build.gradle @@ -16,7 +16,7 @@ dependencies { api 'org.eclipse.jetty.websocket:websocket-api:9.4.58.v20250814' testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4' testImplementation project(':buttplug4j.utils.test') - testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.13.4' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher:6.0.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.13.4' } diff --git a/buttplug4j.connectors.jetty.websocket.client/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/jetty/websocket/client/ButtplugClientWSJettyClientTest.java b/buttplug4j.connectors.jetty.websocket.client/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/jetty/websocket/client/ButtplugClientWSJettyClientTest.java index 1bf6c6a..13b0841 100644 --- a/buttplug4j.connectors.jetty.websocket.client/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/jetty/websocket/client/ButtplugClientWSJettyClientTest.java +++ b/buttplug4j.connectors.jetty.websocket.client/src/test/java/io/github/blackspherefollower/buttplug4j/connectors/jetty/websocket/client/ButtplugClientWSJettyClientTest.java @@ -3,17 +3,15 @@ import io.github.blackspherefollower.buttplug4j.client.ButtplugClientDevice; import io.github.blackspherefollower.buttplug4j.client.ButtplugClientDeviceFeature; import io.github.blackspherefollower.buttplug4j.client.ButtplugDeviceFeatureException; -import io.github.blackspherefollower.buttplug4j.utils.test.WSDMClient; -import io.github.blackspherefollower.buttplug4j.utils.test.IntifaceEngineWrapper; import io.github.blackspherefollower.buttplug4j.protocol.ButtplugMessage; import io.github.blackspherefollower.buttplug4j.protocol.messages.InputReading; - +import io.github.blackspherefollower.buttplug4j.utils.test.IntifaceEngineWrapper; +import io.github.blackspherefollower.buttplug4j.utils.test.WSDMClient; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import java.io.File; -import java.io.IOException; import java.net.URI; +import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.*; @@ -22,7 +20,7 @@ public class ButtplugClientWSJettyClientTest { @Test public void TestConnect() throws Exception { - try(IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper() ) { + try (IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper()) { Thread.sleep(500); WSDMClient wsdev = new WSDMClient(new URI("ws://localhost:" + wrapper.dport), "LVS-Fake", "A9816725B"); Thread.sleep(500); @@ -55,9 +53,8 @@ public void TestConnect() throws Exception { } @Test - @Disabled("See https://github.com/buttplugio/buttplug/issues/801") public void TestBattery() throws Exception { - try(IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper() ) { + try (IntifaceEngineWrapper wrapper = new IntifaceEngineWrapper()) { Thread.sleep(500); WSDMClient wsdev = new WSDMClient(new URI("ws://localhost:" + wrapper.dport), "LVS-Fake", "A9816725B"); Thread.sleep(500); @@ -71,9 +68,9 @@ public void TestBattery() throws Exception { for (ButtplugClientDevice dev : client.getDevices()) { for (ButtplugClientDeviceFeature feat : dev.getDeviceFeatures().values()) { if (feat.HasBattery()) { - ButtplugMessage res = feat.ReadBattery().get(); - if (res instanceof InputReading && ((InputReading) res).getData() instanceof InputReading.BatteryData) { - InputReading.BatteryData reading = (InputReading.BatteryData) ((InputReading) res).getData(); + ButtplugMessage res = feat.ReadBattery().get(2, TimeUnit.SECONDS); + if (res instanceof InputReading && ((InputReading) res).getData() instanceof InputReading.Battery) { + InputReading.Battery reading = (InputReading.Battery) ((InputReading) res).getData(); int battery = reading.getValue(); System.out.println("Battery is " + battery); assertTrue(battery >= 0); diff --git a/buttplug4j.utils.mdns/build.gradle b/buttplug4j.utils.mdns/build.gradle index f3116dd..835b3de 100644 --- a/buttplug4j.utils.mdns/build.gradle +++ b/buttplug4j.utils.mdns/build.gradle @@ -11,10 +11,11 @@ repositories { } dependencies { - api 'org.jmdns:jmdns:3.6.1' + api 'org.jmdns:jmdns:3.6.3' api 'org.slf4j:slf4j-simple:2.0.17' testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4' - testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.13.4' + testImplementation project(':buttplug4j.utils.test') + testRuntimeOnly 'org.junit.platform:junit-platform-launcher:6.0.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.13.4' } diff --git a/buttplug4j.utils.mdns/src/main/java/io/github/blackspherefollower/buttplug4j/utils/mdns/ButtplugDiscover.java b/buttplug4j.utils.mdns/src/main/java/io/github/blackspherefollower/buttplug4j/utils/mdns/ButtplugDiscover.java index 0dbbec5..2e9c255 100644 --- a/buttplug4j.utils.mdns/src/main/java/io/github/blackspherefollower/buttplug4j/utils/mdns/ButtplugDiscover.java +++ b/buttplug4j.utils.mdns/src/main/java/io/github/blackspherefollower/buttplug4j/utils/mdns/ButtplugDiscover.java @@ -3,6 +3,9 @@ import javax.jmdns.JmmDNS; import javax.jmdns.ServiceEvent; import javax.jmdns.ServiceListener; +import javax.jmdns.ServiceTypeListener; +import java.io.Closeable; +import java.io.IOException; import java.net.Inet4Address; import java.net.URI; import java.net.URISyntaxException; @@ -10,31 +13,41 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentSkipListSet; -public final class ButtplugDiscover implements ServiceListener { +public final class ButtplugDiscover implements ServiceListener, Closeable { private final ConcurrentHashMap> servers = new ConcurrentHashMap<>(); + private final JmmDNS jmmdns; private DiscovereyEventHandler discovereyEventHandler = null; public ButtplugDiscover(DiscovereyEventHandler discovereyEventHandler) { this.discovereyEventHandler = discovereyEventHandler; - JmmDNS jmmdns = JmmDNS.Factory.getInstance(); + jmmdns = JmmDNS.Factory.getInstance(); jmmdns.addServiceListener("_intiface_engine._tcp.local.", this); - } + try { + jmmdns.addServiceTypeListener(new ServiceTypeListener() { + @Override + public void serviceTypeAdded(ServiceEvent serviceEvent) { + System.err.println("Added service type " + serviceEvent.getType()); + } - public ButtplugDiscover() { - JmmDNS jmmdns = JmmDNS.Factory.getInstance(); - jmmdns.addServiceListener("_intiface_engine._tcp.local.", this); - } + @Override + public void subTypeForServiceTypeAdded(ServiceEvent serviceEvent) { + System.err.println("Added sub type " + serviceEvent.getType()); - public void setDiscovereyEventHandler(DiscovereyEventHandler discovereyEventHandler) { - this.discovereyEventHandler = discovereyEventHandler; + } + }); + } catch (IOException e) { + e.printStackTrace(); + } } @Override public void serviceAdded(ServiceEvent event) { + System.err.println("Added " + event.getName() + " " + event.getInfo().toString() + " " + event.getType()); } @Override public void serviceRemoved(ServiceEvent event) { + System.err.println("Removed " + event.getName() + " " + event.getInfo().toString() + " " + event.getType()); servers.remove(event.getInfo().getName()); if (discovereyEventHandler != null) { discovereyEventHandler.LostButtplug(event.getInfo().getName()); @@ -43,6 +56,7 @@ public void serviceRemoved(ServiceEvent event) { @Override public void serviceResolved(ServiceEvent event) { + System.err.println("Resolved " + event.getName() + " " + event.getInfo().toString() + " " + event.getType()); ConcurrentSkipListSet set = new ConcurrentSkipListSet<>(); for (Inet4Address addr : event.getInfo().getInet4Addresses()) { @@ -68,6 +82,11 @@ public ConcurrentHashMap> GetServers() { return servers; } + @Override + public void close() throws IOException { + jmmdns.close(); + } + public interface DiscovereyEventHandler { void FoundButtplug(String name, Set addresses); diff --git a/buttplug4j.utils.mdns/src/test/java/io/github/blackspherefollower/buttplug4j/utils/mdns/ButtplugDiscoverTest.java b/buttplug4j.utils.mdns/src/test/java/io/github/blackspherefollower/buttplug4j/utils/mdns/ButtplugDiscoverTest.java index 8c9420f..a36f14a 100644 --- a/buttplug4j.utils.mdns/src/test/java/io/github/blackspherefollower/buttplug4j/utils/mdns/ButtplugDiscoverTest.java +++ b/buttplug4j.utils.mdns/src/test/java/io/github/blackspherefollower/buttplug4j/utils/mdns/ButtplugDiscoverTest.java @@ -1,30 +1,57 @@ package io.github.blackspherefollower.buttplug4j.utils.mdns; +import io.github.blackspherefollower.buttplug4j.utils.test.IntifaceEngineWrapper; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class ButtplugDiscoverTest { - @Disabled + @Disabled("Not able to get mDNS this working right now") @Test public void TestConnect() throws Exception { - ButtplugDiscover discover = new ButtplugDiscover(new ButtplugDiscover.DiscovereyEventHandler() { - @Override - public void FoundButtplug(String name, Set addresses) { - System.out.println("WOO, a buttplug appeared : " + name + " : " + String.join(", ", addresses.stream().map(uri -> uri.toString()).collect(Collectors.toList()))); - } + CompletableFuture seen1 = new CompletableFuture<>(); + CompletableFuture seen2 = new CompletableFuture<>(); + CompletableFuture gone1 = new CompletableFuture<>(); + CompletableFuture gone2 = new CompletableFuture<>(); + try ( + IntifaceEngineWrapper wrapper1 = new IntifaceEngineWrapper(new ArrayList<>(Arrays.asList("--broadcast-server-mdns"))); + IntifaceEngineWrapper wrapper2 = new IntifaceEngineWrapper(new ArrayList<>(Arrays.asList("--broadcast-server-mdns", "--mdns-suffix", "test-intiface-2"))); + + ButtplugDiscover discover = new ButtplugDiscover(new ButtplugDiscover.DiscovereyEventHandler() { + @Override + public void FoundButtplug(String name, Set addresses) { + System.out.println("WOO, a buttplug appeared : " + name + " : " + String.join(", ", addresses.stream().map(uri -> uri.toString()).collect(Collectors.toList()))); + } + + @Override + public void LostButtplug(String name) { + System.out.println("Oh... a buttplug disappeared : " + name); + } + }); + ) { + + assertTrue(seen1.get(2, TimeUnit.MINUTES)); + assertTrue(seen2.get(15, TimeUnit.SECONDS)); - @Override - public void LostButtplug(String name) { - System.out.println("Oh... a buttplug disappeared : " + name); - } - }); + assertFalse(gone1.isDone()); + wrapper1.close(); + assertTrue(gone1.get(15, TimeUnit.SECONDS)); - Thread.sleep(60000); + assertFalse(gone2.isDone()); + wrapper2.close(); + assertTrue(gone2.get(15, TimeUnit.SECONDS)); + } } } \ No newline at end of file diff --git a/buttplug4j.utils.test/src/main/java/io/github/blackspherefollower/buttplug4j/utils/test/IntifaceEngineWrapper.java b/buttplug4j.utils.test/src/main/java/io/github/blackspherefollower/buttplug4j/utils/test/IntifaceEngineWrapper.java index 3172aa2..81c4644 100644 --- a/buttplug4j.utils.test/src/main/java/io/github/blackspherefollower/buttplug4j/utils/test/IntifaceEngineWrapper.java +++ b/buttplug4j.utils.test/src/main/java/io/github/blackspherefollower/buttplug4j/utils/test/IntifaceEngineWrapper.java @@ -3,6 +3,8 @@ import java.io.*; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; public class IntifaceEngineWrapper implements Closeable { @@ -15,20 +17,28 @@ public IntifaceEngineWrapper() throws Exception { cport = (int) (Math.random() * 63000) + 1025; dport = (int) (Math.random() * 63000) + 1025; - setup(true); + setup(true, new ArrayList()); } + public IntifaceEngineWrapper(int lport) throws Exception { cport = lport; dport = (int) (Math.random() * 63000) + 1025; - setup(false); + setup(false, new ArrayList()); + } + + public IntifaceEngineWrapper(ArrayList extraArgs) throws Exception { + cport = (int) (Math.random() * 63000) + 1025; + dport = (int) (Math.random() * 63000) + 1025; + + setup(true, extraArgs); } - public void setup(boolean listen) throws Exception { - try{ + public void setup(boolean listen, ArrayList extraArgs) throws Exception { + try { Runtime.getRuntime().exec("intiface-engine --version").waitFor(); } catch (IOException e) { - org.junit.jupiter.api.Assumptions.abort( "intiface-engine not found, skipping tests"); + org.junit.jupiter.api.Assumptions.abort("intiface-engine not found, skipping tests"); } Path userConfig = Files.createTempFile("user-config_", ".json"); @@ -46,8 +56,9 @@ public void setup(boolean listen) throws Exception { } } - ProcessBuilder pb = - new ProcessBuilder("intiface-engine", listen ? "--websocket-port" : "--websocket-client-address", listen ? String.valueOf(cport) : ("ws://127.0.0.1:" + String.valueOf(cport) + "/bob"), "--use-device-websocket-server", "--user-device-config-file", userConfig.toAbsolutePath().toString(), "--device-websocket-server-port", String.valueOf(dport), "--log", "TRACE"); + ArrayList args = new ArrayList<>(Arrays.asList("intiface-engine", listen ? "--websocket-port" : "--websocket-client-address", listen ? String.valueOf(cport) : ("ws://127.0.0.1:" + String.valueOf(cport) + "/bob"), "--use-device-websocket-server", "--user-device-config-file", userConfig.toAbsolutePath().toString(), "--device-websocket-server-port", String.valueOf(dport), "--log", "TRACE", "--allow-v4-spec")); + args.addAll(extraArgs); + ProcessBuilder pb = new ProcessBuilder(args); Path log = Files.createTempFile("intiface_", ".log"); pb.redirectErrorStream(true); @@ -55,18 +66,18 @@ public void setup(boolean listen) throws Exception { proc = pb.start(); Thread.sleep(500); - if( !proc.isAlive()) { + if (!proc.isAlive()) { close(); - org.junit.jupiter.api.Assumptions.abort( "intiface-engine not started, skipping tests"); + org.junit.jupiter.api.Assumptions.abort("intiface-engine not started, skipping tests"); } } @Override public void close() { - if(proc != null) { + if (proc != null) { proc.destroyForcibly(); } - if(userConfig != null) { + if (userConfig != null) { try { Files.deleteIfExists(userConfig); } catch (IOException ignore) { diff --git a/buttplug4j/build.gradle b/buttplug4j/build.gradle index ba9ea31..0199f69 100644 --- a/buttplug4j/build.gradle +++ b/buttplug4j/build.gradle @@ -16,7 +16,7 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4' testImplementation 'dev.harrel:json-schema:1.8.2' testImplementation 'org.mockito:mockito-core:5.20.0' - testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.13.4' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher:6.0.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.13.4' } diff --git a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceFeature.java b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceFeature.java index 119e414..1038e97 100644 --- a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceFeature.java +++ b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceFeature.java @@ -2,7 +2,6 @@ import io.github.blackspherefollower.buttplug4j.protocol.ButtplugMessage; import io.github.blackspherefollower.buttplug4j.protocol.messages.DeviceFeature; -import io.github.blackspherefollower.buttplug4j.protocol.messages.InputCmd; import io.github.blackspherefollower.buttplug4j.protocol.messages.InputCommandType; import io.github.blackspherefollower.buttplug4j.protocol.messages.OutputCmd; @@ -44,7 +43,7 @@ private int GetStepFromFloat(final String type, final float value) throws Buttpl steps *= value; return (int) Math.floor(steps); } else if (desc instanceof DeviceFeature.PositionWithDuration) { - double steps = ((DeviceFeature.PositionWithDuration) desc).getPosition()[1]; + double steps = ((DeviceFeature.PositionWithDuration) desc).getValue()[1]; steps *= value; return (int) Math.floor(steps); } else { @@ -62,11 +61,6 @@ private void CheckStepRange(final String type, final float value) throws Buttplu if (value > steps || value < 0) { throw new ButtplugDeviceFeatureException("Range error"); } - } else if (desc instanceof DeviceFeature.PositionWithDuration) { - int steps = ((DeviceFeature.PositionWithDuration) desc).getPosition()[1]; - if (value > steps || value < 0) { - throw new ButtplugDeviceFeatureException("Range error"); - } } else { throw new ButtplugDeviceFeatureException(type); } diff --git a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/DeviceFeature.java b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/DeviceFeature.java index 7ee4176..7d0019a 100644 --- a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/DeviceFeature.java +++ b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/DeviceFeature.java @@ -92,7 +92,7 @@ public int[] getValue() { return value; } - public void setStepCount(int[] value) { + public void setValue(int[] value) { this.value = value; } @@ -189,30 +189,20 @@ public Position() { } } - public static class PositionWithDuration implements OutputDescriptor { - @JsonProperty(value = "Position", required = true) - private int[] position; + public static class PositionWithDuration extends SteppedOutputDescriptor { @JsonProperty(value = "Duration", required = true) private int[] duration; - public PositionWithDuration(int[] position, int[] duration) { - this.position = position; + public PositionWithDuration(int[] value, int[] duration) { + super(value); this.duration = duration; } public PositionWithDuration() { - this.position = new int[]{0, 0}; + super(new int[]{0, 0}); this.duration = new int[]{0, 0}; } - public int[] getPosition() { - return position; - } - - public void setPosition(int[] position) { - this.position = position; - } - public int[] getDuration() { return duration; } @@ -230,20 +220,20 @@ public boolean equals(Object o) { return false; } PositionWithDuration that = (PositionWithDuration) o; - return java.util.Arrays.equals(position, that.position) && java.util.Arrays.equals(duration, that.duration); + return java.util.Arrays.equals(getValue(), that.getValue()) && java.util.Arrays.equals(duration, that.duration); } } @JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME) @JsonSubTypes({ @JsonSubTypes.Type(value = DeviceFeature.Battery.class, name = "Battery"), - @JsonSubTypes.Type(value = DeviceFeature.Rssi.class, name = "RSSI"), + @JsonSubTypes.Type(value = DeviceFeature.Rssi.class, name = "Rssi"), @JsonSubTypes.Type(value = DeviceFeature.Button.class, name = "Button"), @JsonSubTypes.Type(value = DeviceFeature.Pressure.class, name = "Pressure"), @JsonSubTypes.Type(value = DeviceFeature.PositionInput.class, name = "Position") }) public static class InputDescriptor { - @JsonProperty(value = "InputCommands", required = true) + @JsonProperty(value = "Command", required = true) private ArrayList input; public InputDescriptor(ArrayList input) { @@ -260,7 +250,7 @@ public void setInput(ArrayList input) { } public static class RangedInputDescriptor extends InputDescriptor { - @JsonProperty(value = "ValueRange", required = true) + @JsonProperty(value = "Value", required = true) private int[][] valueRange; public RangedInputDescriptor(ArrayList input, int[][] valueRange) { diff --git a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputCmd.java b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputCmd.java index b6ffba8..573dd31 100644 --- a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputCmd.java +++ b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputCmd.java @@ -8,10 +8,10 @@ public class InputCmd extends ButtplugDeviceMessage { @JsonProperty(value = "FeatureIndex", required = true) private int featureIndex; - @JsonProperty(value = "InputType", required = true) + @JsonProperty(value = "Type", required = true) private String inputType; - @JsonProperty(value = "InputCommand", required = true) + @JsonProperty(value = "Command", required = true) private InputCommandType inputCommand; public InputCmd(int id, final long deviceIndex, final int featureIndex, final String inputType, final InputCommandType inputCommand) { diff --git a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputReading.java b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputReading.java index ad0846e..a818323 100644 --- a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputReading.java +++ b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputReading.java @@ -1,19 +1,25 @@ package io.github.blackspherefollower.buttplug4j.protocol.messages; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; import io.github.blackspherefollower.buttplug4j.protocol.ButtplugDeviceMessage; public class InputReading extends ButtplugDeviceMessage { @JsonProperty(value = "FeatureIndex", required = true) private int featureIndex; - @JsonProperty(value = "Data", required = true) + @JsonProperty(value = "Reading", required = true) private InputData data; public InputReading(int id, long deviceIndex, int featureIndex) { super(id, deviceIndex); this.featureIndex = featureIndex; } + public InputReading() { + super(-1, -1); + this.featureIndex = -1; + } public InputData getData() { return data; @@ -31,11 +37,19 @@ public void setFeatureIndex(int featureIndex) { this.featureIndex = featureIndex; } - public interface InputData { + @JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME) + @JsonSubTypes({ + @JsonSubTypes.Type(value = Battery.class, name = "Battery"), + @JsonSubTypes.Type(value = Rssi.class, name = "Rssi"), + @JsonSubTypes.Type(value = Button.class, name = "Button"), + @JsonSubTypes.Type(value = Presure.class, name = "Pressure"), + @JsonSubTypes.Type(value = Position.class, name = "Position"), + }) + public static class InputData { } - static public class InputIntegerData { - @JsonProperty(value = "Data", required = true) + static public class InputIntegerData extends InputData { + @JsonProperty(value = "Value", required = true) int value; public int getValue() { @@ -47,15 +61,18 @@ public void setValue(int value) { } } - static public class BatteryData extends InputIntegerData { + static public class Battery extends InputIntegerData { + } + + static public class Rssi extends InputIntegerData { } - static public class RssiData extends InputIntegerData { + static public class Button extends InputIntegerData { } - static public class ButtonData extends InputIntegerData { + static public class Presure extends InputIntegerData { } - static public class PresureData extends InputIntegerData { + static public class Position extends InputIntegerData { } } diff --git a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/OutputCmd.java b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/OutputCmd.java index 10f1e00..239ebd3 100644 --- a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/OutputCmd.java +++ b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/OutputCmd.java @@ -61,6 +61,9 @@ public abstract static class ValueCommand implements IOutputCommand { protected ValueCommand(int value) { this.value = value; } + protected ValueCommand() { + this.value = 0; + } public int getValue() { return value; @@ -151,30 +154,20 @@ public Position() { } } - public static class PositionWithDuration implements IOutputCommand { - @JsonProperty(value = "Position", required = true) - private int position; + public static class PositionWithDuration extends ValueCommand { @JsonProperty(value = "Duration", required = true) private int duration; - public PositionWithDuration(int position, int duration) { - this.position = position; + public PositionWithDuration(int value, int duration) { + super(value); this.duration = duration; } public PositionWithDuration() { - this.position = 0; + super(); this.duration = 0; } - public int getPosition() { - return position; - } - - public void setPosition(int position) { - this.position = position; - } - public int getDuration() { return duration; } diff --git a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/util/Pair.java b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/util/Pair.java index c2e3b8d..d528247 100644 --- a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/util/Pair.java +++ b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/util/Pair.java @@ -43,7 +43,7 @@ public boolean equals(Object o) { } return ((Pair) o).a.equals(a) && ((Pair) o).b.equals(b); } - + @Override public int hashCode() { return a.hashCode() ^ b.hashCode(); diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/ButtplugExceptionTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/ButtplugExceptionTest.java index fac4ec2..2343ba3 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/ButtplugExceptionTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/ButtplugExceptionTest.java @@ -1,8 +1,8 @@ - package io.github.blackspherefollower.buttplug4j; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class ButtplugExceptionTest { @@ -11,7 +11,7 @@ public void testExceptionWithMessage() { String errorMessage = "Test error message"; ButtplugException exception = new ButtplugException(); exception.setMessage(errorMessage); - + assertEquals(errorMessage, exception.getMessage()); } } diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceFeatureTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceFeatureTest.java index 6625a2a..e438d02 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceFeatureTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceFeatureTest.java @@ -1,4 +1,3 @@ - package io.github.blackspherefollower.buttplug4j.client; import io.github.blackspherefollower.buttplug4j.protocol.ButtplugMessage; @@ -25,12 +24,12 @@ class ButtplugClientDeviceFeatureTest { @BeforeEach void setup() { mockDevice = mock(ButtplugClientDevice.class); - + // Create a test feature with various output types testFeature = new DeviceFeature(); testFeature.setFeatureIndex(0); testFeature.setFeatureDescription("Test Feature"); - + ArrayList outputs = new ArrayList<>(); outputs.add(new DeviceFeature.Vibrate(new int[]{0, 100})); outputs.add(new DeviceFeature.Rotate(new int[]{0, 50})); @@ -41,10 +40,10 @@ void setup() { outputs.add(new DeviceFeature.Led(new int[]{0, 255})); outputs.add(new DeviceFeature.Temperature(new int[]{0, 30})); testFeature.setOutput(outputs); - + ArrayList inputs = new ArrayList<>(); testFeature.setInput(inputs); - + clientFeature = new ButtplugClientDeviceFeature(mockDevice, testFeature); } @@ -57,9 +56,9 @@ void testConstructor() { void testVibrateWithValidStep() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.Vibrate(50); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -77,9 +76,9 @@ void testVibrateWithInvalidStepThrowsException() { void testVibrateFloatWithValidValue() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.VibrateFloat(0.5f); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -91,13 +90,13 @@ void testVibrateFloatWithValidValue() throws Exception { void testVibrateFloatWithBoundaryValues() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + // Test minimum value clientFeature.VibrateFloat(0.0f); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); assertEquals(0, ((OutputCmd.Vibrate) captor.getValue()).getValue()); - + // Test maximum value reset(mockDevice); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); @@ -116,9 +115,9 @@ void testVibrateFloatWithInvalidValueThrowsException() { void testRotateWithValidStep() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.Rotate(25); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -130,9 +129,9 @@ void testRotateWithValidStep() throws Exception { void testRotateFloatWithValidValue() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.RotateFloat(0.5f); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -144,9 +143,9 @@ void testRotateFloatWithValidValue() throws Exception { void testConstrictWithValidStep() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.Constrict(5); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -158,9 +157,9 @@ void testConstrictWithValidStep() throws Exception { void testConstrictFloatWithValidValue() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.ConstrictFloat(0.5f); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -172,9 +171,9 @@ void testConstrictFloatWithValidValue() throws Exception { void testSprayWithValidStep() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.Spray(3); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -186,9 +185,9 @@ void testSprayWithValidStep() throws Exception { void testSprayFloatWithValidValue() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.SprayFloat(0.6f); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -200,9 +199,9 @@ void testSprayFloatWithValidValue() throws Exception { void testPositionWithValidStep() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.Position(15); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -214,9 +213,9 @@ void testPositionWithValidStep() throws Exception { void testPositionFloatWithValidValue() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.PositionFloat(0.8f); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -228,18 +227,18 @@ void testPositionFloatWithValidValue() throws Exception { void testPositionWithDurationWithValidStep() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + // Add PositionWithDuration to the feature testFeature.getOutput().add(new DeviceFeature.PositionWithDuration(new int[]{0, 25}, new int[]{0, 1000})); clientFeature = new ButtplugClientDeviceFeature(mockDevice, testFeature); - + Future result = clientFeature.PositionWithDuration(15, 500); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); assertInstanceOf(OutputCmd.PositionWithDuration.class, captor.getValue()); - assertEquals(15, ((OutputCmd.PositionWithDuration) captor.getValue()).getPosition()); + assertEquals(15, ((OutputCmd.PositionWithDuration) captor.getValue()).getValue()); assertEquals(500, ((OutputCmd.PositionWithDuration) captor.getValue()).getDuration()); } @@ -247,18 +246,18 @@ void testPositionWithDurationWithValidStep() throws Exception { void testPositionWithDurationFloatWithValidValue() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + // Add PositionWithDuration to the feature testFeature.getOutput().add(new DeviceFeature.PositionWithDuration(new int[]{0, 25}, new int[]{0, 1000})); clientFeature = new ButtplugClientDeviceFeature(mockDevice, testFeature); - + Future result = clientFeature.PositionWithDurationFloat(0.8f, 500); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); assertInstanceOf(OutputCmd.PositionWithDuration.class, captor.getValue()); - assertEquals(20, ((OutputCmd.PositionWithDuration) captor.getValue()).getPosition()); + assertEquals(20, ((OutputCmd.PositionWithDuration) captor.getValue()).getValue()); assertEquals(500, ((OutputCmd.PositionWithDuration) captor.getValue()).getDuration()); } @@ -266,9 +265,9 @@ void testPositionWithDurationFloatWithValidValue() throws Exception { void testLedWithValidStep() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.Led(128); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -280,9 +279,9 @@ void testLedWithValidStep() throws Exception { void testLedFloatWithValidValue() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.LedFloat(0.5f); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -294,9 +293,9 @@ void testLedFloatWithValidValue() throws Exception { void testOscillateWithValidStep() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.Oscillate(10); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -308,9 +307,9 @@ void testOscillateWithValidStep() throws Exception { void testOscillateFloatWithValidValue() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.OscillateFloat(0.5f); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -322,9 +321,9 @@ void testOscillateFloatWithValidValue() throws Exception { void testTemperatureWithValidStep() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.Temperature(15); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -336,9 +335,9 @@ void testTemperatureWithValidStep() throws Exception { void testTemperatureFloatWithValidValue() throws Exception { CompletableFuture future = CompletableFuture.completedFuture(mock(ButtplugMessage.class)); when(mockDevice.sendOutputCommand(anyInt(), any(OutputCmd.IOutputCommand.class))).thenReturn(future); - + Future result = clientFeature.TemperatureFloat(0.5f); - + assertNotNull(result); ArgumentCaptor captor = ArgumentCaptor.forClass(OutputCmd.IOutputCommand.class); verify(mockDevice).sendOutputCommand(eq(0), captor.capture()); @@ -354,9 +353,9 @@ void testUnsupportedOutputTypeThrowsException() { limitedFeature.setFeatureDescription("Limited Feature"); limitedFeature.setOutput(new ArrayList<>()); limitedFeature.setInput(new ArrayList<>()); - + ButtplugClientDeviceFeature limited = new ButtplugClientDeviceFeature(mockDevice, limitedFeature); - + assertThrows(ButtplugDeviceFeatureException.class, () -> limited.Vibrate(50)); } diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceTest.java index 46f0f52..0856f47 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientDeviceTest.java @@ -1,4 +1,3 @@ - package io.github.blackspherefollower.buttplug4j.client; import io.github.blackspherefollower.buttplug4j.protocol.ButtplugMessage; @@ -30,7 +29,7 @@ void setup() { mockClient = mock(ButtplugClient.class); HashMap features = new HashMap<>(); - + // Feature 0: Vibrator DeviceFeature vibratorFeature = new DeviceFeature(); vibratorFeature.setFeatureIndex(0); @@ -39,7 +38,7 @@ void setup() { vibratorOutputs.add(new DeviceFeature.Vibrate(new int[]{0, 100})); vibratorFeature.setOutput(vibratorOutputs); features.put(0, vibratorFeature); - + // Feature 1: Battery sensor DeviceFeature batteryFeature = new DeviceFeature(); batteryFeature.setFeatureIndex(1); @@ -52,14 +51,14 @@ void setup() { features.put(1, batteryFeature); // Create a test device with various features - testDevice = new Device(5, "Test Device",features, 100, "Display Name"); + testDevice = new Device(5, "Test Device", features, 100, "Display Name"); testDevice.setDeviceFeatures(features); - + when(mockClient.getNextMsgId()).thenReturn(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); when(mockClient.sendMessage(any(ButtplugMessage.class))) .thenReturn(CompletableFuture.completedFuture(mock(ButtplugMessage.class))); - + clientDevice = new ButtplugClientDevice(mockClient, testDevice); } @@ -75,7 +74,7 @@ void testConstructorInitializesProperties() { void testConstructorWithNullDisplayName() { testDevice.setDeviceDisplayName(null); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + assertEquals("Test Device", device.getDisplayName()); } @@ -83,14 +82,14 @@ void testConstructorWithNullDisplayName() { void testConstructorWithEmptyDisplayName() { testDevice.setDeviceDisplayName(""); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + assertEquals("Test Device", device.getDisplayName()); } @Test void testGetDeviceFeatures() { Map features = clientDevice.getDeviceFeatures(); - + assertNotNull(features); assertEquals(2, features.size()); assertTrue(features.containsKey(0)); @@ -102,7 +101,7 @@ void testGetDeviceFeatures() { @Test void testSendStopDeviceCmd() { Future result = clientDevice.sendStopDeviceCmd(); - + assertNotNull(result); verify(mockClient).getNextMsgId(); verify(mockClient).sendMessage(any(ButtplugMessage.class)); @@ -111,9 +110,9 @@ void testSendStopDeviceCmd() { @Test void testSendOutputCommand() { OutputCmd.Vibrate vibrateCommand = new OutputCmd.Vibrate(50); - + Future result = clientDevice.sendOutputCommand(0, vibrateCommand); - + assertNotNull(result); verify(mockClient).getNextMsgId(); verify(mockClient).sendMessage(any(OutputCmd.class)); @@ -122,9 +121,9 @@ void testSendOutputCommand() { @Test void testSendOutputCommandWithDifferentFeatureIndex() { OutputCmd.Vibrate vibrateCommand = new OutputCmd.Vibrate(75); - + Future result = clientDevice.sendOutputCommand(1, vibrateCommand); - + assertNotNull(result); verify(mockClient).getNextMsgId(); verify(mockClient).sendMessage(any(OutputCmd.class)); @@ -134,7 +133,7 @@ void testSendOutputCommandWithDifferentFeatureIndex() { void testDeviceWithNoMessageTimingGap() { testDevice.setDeviceMessageTimingGap(null); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + assertNull(device.getMessageTimingGap()); } @@ -142,7 +141,7 @@ void testDeviceWithNoMessageTimingGap() { void testDeviceWithZeroMessageTimingGap() { testDevice.setDeviceMessageTimingGap(0); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + assertEquals(Integer.valueOf(0), device.getMessageTimingGap()); } @@ -150,7 +149,7 @@ void testDeviceWithZeroMessageTimingGap() { void testDeviceWithNoFeatures() { testDevice.setDeviceFeatures(new HashMap<>()); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + Map features = device.getDeviceFeatures(); assertNotNull(features); assertTrue(features.isEmpty()); @@ -160,7 +159,7 @@ void testDeviceWithNoFeatures() { void testDeviceWithNullFeatures() { testDevice.setDeviceFeatures(null); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + Map features = device.getDeviceFeatures(); assertNotNull(features); assertTrue(features.isEmpty()); @@ -169,7 +168,7 @@ void testDeviceWithNullFeatures() { @Test void testDeviceWithMultipleOutputFeatures() { HashMap multiFeatures = new HashMap<>(); - + // Add multiple output features for (int i = 0; i < 5; i++) { DeviceFeature feature = new DeviceFeature(); @@ -180,13 +179,13 @@ void testDeviceWithMultipleOutputFeatures() { feature.setOutput(outputs); multiFeatures.put(i, feature); } - + testDevice.setDeviceFeatures(multiFeatures); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + Map features = device.getDeviceFeatures(); assertEquals(5, features.size()); - + for (int i = 0; i < 5; i++) { assertTrue(features.containsKey(i)); assertEquals("Feature " + i, features.get(i).getDescription()); @@ -196,7 +195,7 @@ void testDeviceWithMultipleOutputFeatures() { @Test void testDeviceWithMixedInputOutputFeatures() { HashMap mixedFeatures = new HashMap<>(); - + // Output feature DeviceFeature outputFeature = new DeviceFeature(); outputFeature.setFeatureIndex(0); @@ -205,7 +204,7 @@ void testDeviceWithMixedInputOutputFeatures() { outputs.add(new DeviceFeature.Rotate(new int[]{0, 50})); outputFeature.setOutput(outputs); mixedFeatures.put(0, outputFeature); - + // Input feature DeviceFeature inputFeature = new DeviceFeature(); inputFeature.setFeatureIndex(1); @@ -216,7 +215,7 @@ void testDeviceWithMixedInputOutputFeatures() { inputs.add(new DeviceFeature.Pressure(commands, new int[][]{{0, 0}, {0, 100}})); inputFeature.setInput(inputs); mixedFeatures.put(1, inputFeature); - + // Both input and output feature DeviceFeature mixedFeature = new DeviceFeature(); mixedFeature.setFeatureIndex(2); @@ -224,10 +223,10 @@ void testDeviceWithMixedInputOutputFeatures() { mixedFeature.setOutput(outputs); mixedFeature.setInput(inputs); mixedFeatures.put(2, mixedFeature); - + testDevice.setDeviceFeatures(mixedFeatures); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + Map features = device.getDeviceFeatures(); assertEquals(3, features.size()); assertEquals("Output Feature", features.get(0).getDescription()); @@ -261,7 +260,7 @@ void testDeviceWithSpecialCharactersInName() { testDevice.setDeviceName(specialName); testDevice.setDeviceDisplayName(specialName); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + assertEquals(specialName, device.getName()); assertEquals(specialName, device.getDisplayName()); } @@ -270,7 +269,7 @@ void testDeviceWithSpecialCharactersInName() { void testDeviceWithNegativeIndex() { testDevice.setDeviceIndex(-1); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + assertEquals(-1, device.getDeviceIndex()); } @@ -278,7 +277,7 @@ void testDeviceWithNegativeIndex() { void testDeviceWithLargeIndex() { testDevice.setDeviceIndex(Integer.MAX_VALUE); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + assertEquals(Integer.MAX_VALUE, device.getDeviceIndex()); } @@ -286,7 +285,7 @@ void testDeviceWithLargeIndex() { void testDeviceWithLargeMessageTimingGap() { testDevice.setDeviceMessageTimingGap(Integer.MAX_VALUE); ButtplugClientDevice device = new ButtplugClientDevice(mockClient, testDevice); - + assertEquals(Integer.valueOf(Integer.MAX_VALUE), device.getMessageTimingGap()); } @@ -295,11 +294,11 @@ void testMultipleSendOutputCommandCalls() { OutputCmd.Vibrate command1 = new OutputCmd.Vibrate(25); OutputCmd.Vibrate command2 = new OutputCmd.Vibrate(50); OutputCmd.Vibrate command3 = new OutputCmd.Vibrate(75); - + Future result1 = clientDevice.sendOutputCommand(0, command1); Future result2 = clientDevice.sendOutputCommand(0, command2); Future result3 = clientDevice.sendOutputCommand(0, command3); - + assertNotNull(result1); assertNotNull(result2); assertNotNull(result3); diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientExceptionTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientExceptionTest.java index 6676d42..49eff10 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientExceptionTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientExceptionTest.java @@ -2,7 +2,9 @@ import io.github.blackspherefollower.buttplug4j.ButtplugException; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; public class ButtplugClientExceptionTest { @@ -10,7 +12,7 @@ public class ButtplugClientExceptionTest { public void testExceptionWithMessage() { String errorMessage = "Client error occurred"; ButtplugClientException exception = new ButtplugClientException(errorMessage); - + assertEquals(errorMessage, exception.getMessage()); } diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientTest.java index b43aed4..a7faf2a 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClientTest.java @@ -171,7 +171,7 @@ void testOnMessageWithInputReading() { @Test void testStartScanningAsync() { CompletableFuture future = (CompletableFuture) client.startScanningAsync(); - + assertNotNull(future); assertInstanceOf(StartScanning.class, client.lastSentMessage); } @@ -179,7 +179,7 @@ void testStartScanningAsync() { @Test void testStopScanningAsync() { CompletableFuture future = (CompletableFuture) client.stopScanningAsync(); - + assertNotNull(future); assertInstanceOf(StopScanning.class, client.lastSentMessage); } @@ -187,7 +187,7 @@ void testStopScanningAsync() { @Test void testStopAllDevicesAsync() { CompletableFuture future = (CompletableFuture) client.stopAllDevicesAsync(); - + assertNotNull(future); assertInstanceOf(StopAllDevices.class, client.lastSentMessage); } @@ -196,7 +196,7 @@ void testStopAllDevicesAsync() { void testStartScanning() throws ExecutionException, InterruptedException, IOException { client.setNextResponse(new Ok(1)); boolean result = client.startScanning(); - + assertTrue(result); assertInstanceOf(StartScanning.class, client.lastSentMessage); } @@ -205,7 +205,7 @@ void testStartScanning() throws ExecutionException, InterruptedException, IOExce void testStopScanning() throws ExecutionException, InterruptedException { client.setNextResponse(new Ok(1)); boolean result = client.stopScanning(); - + assertTrue(result); assertInstanceOf(StopScanning.class, client.lastSentMessage); } @@ -214,7 +214,7 @@ void testStopScanning() throws ExecutionException, InterruptedException { void testStopAllDevices() throws ExecutionException, InterruptedException, IOException { client.setNextResponse(new Ok(1)); boolean result = client.stopAllDevices(); - + assertTrue(result); assertInstanceOf(StopAllDevices.class, client.lastSentMessage); } @@ -222,7 +222,7 @@ void testStopAllDevices() throws ExecutionException, InterruptedException, IOExc @Test void testGetDevicesReturnsEmptyListInitially() { List devices = client.getDevices(); - + assertNotNull(devices); assertTrue(devices.isEmpty()); } @@ -231,7 +231,7 @@ void testGetDevicesReturnsEmptyListInitially() { void testRequestDeviceList() throws ButtplugClientException, ExecutionException, InterruptedException { // Create test device - Device device = new Device(0,"Test Device",new HashMap<>(),100, "Display Name" ); + Device device = new Device(0, "Test Device", new HashMap<>(), 100, "Display Name"); HashMap devices = new HashMap<>(); devices.put(0, device); @@ -255,7 +255,7 @@ void testRequestDeviceList() throws ButtplugClientException, ExecutionException, assertEquals(1, client.getDevices().size()); addedDevice.set(null); - Device device2 = new Device(1,"Test Device 2",new HashMap<>(),100, "Other" ); + Device device2 = new Device(1, "Test Device 2", new HashMap<>(), 100, "Other"); devices.put(1, device2); client.onMessage(Collections.singletonList(deviceList)); @@ -288,7 +288,7 @@ void testRequestDeviceListWithError() { @Test void testSendDeviceMessageWithValidDevice() throws Exception { // Add a device first - Device device = new Device(5,"Test Device",new HashMap<>(),100, "Display Name" ); + Device device = new Device(5, "Test Device", new HashMap<>(), 100, "Display Name"); HashMap devices = new HashMap<>(); devices.put(5, device); @@ -311,7 +311,7 @@ void testSendDeviceMessageWithValidDevice() throws Exception { @Test void testSendDeviceMessageWithInvalidDevice() { - Device device = new Device(999,"Invalid Device",new HashMap<>(),100, "" ); + Device device = new Device(999, "Invalid Device", new HashMap<>(), 100, ""); ButtplugClientDevice clientDevice = new ButtplugClientDevice(client, device); @@ -328,7 +328,7 @@ void testSendDeviceMessageWithInvalidDevice() { @Test void testDisconnect() { client.setConnectionState(ButtplugClient.ConnectionState.CONNECTED); - + CompletableFuture future = new CompletableFuture<>(); client.scheduleWait(10, future); @@ -364,10 +364,10 @@ void testDoHandshakeWithPing() throws InterruptedException { client.doHandshake(); assertEquals(ButtplugClient.ConnectionState.CONNECTED, client.getConnectionState()); - + // Wait a bit to see if ping timer fires Thread.sleep(300); - + // Verify ping was sent boolean foundPing = false; for (ButtplugMessage msg : client.sentMessages) { @@ -391,8 +391,8 @@ void testDoHandshakeWithError() { @Test void testMultipleDevicesInDeviceList() throws Exception { - Device device1 = new Device(0,"Device 1",new HashMap<>(),100, "" ); - Device device2 = new Device(1,"Device 2",new HashMap<>(),100, "" ); + Device device1 = new Device(0, "Device 1", new HashMap<>(), 100, ""); + Device device2 = new Device(1, "Device 2", new HashMap<>(), 100, ""); HashMap devices = new HashMap<>(); devices.put(0, device1); @@ -437,7 +437,7 @@ void testWaitForOk() throws ExecutionException, InterruptedException { assertTrue(client.waitForOk(okFuture)); CompletableFuture errorFuture = CompletableFuture.completedFuture( - new Error("Error", Error.ErrorClass.ERROR_UNKNOWN, 1) + new Error("Error", Error.ErrorClass.ERROR_UNKNOWN, 1) ); assertFalse(client.waitForOk(errorFuture)); } @@ -447,10 +447,10 @@ void testWaitForOk() throws ExecutionException, InterruptedException { */ private static class TestButtplugClient extends ButtplugClient { private final List messageQueue = new ArrayList<>(); - private int queueIndex = 0; ButtplugMessage lastSentMessage; List sentMessages = new ArrayList<>(); boolean cleanupCalled = false; + private int queueIndex = 0; public TestButtplugClient(String clientName) { super(clientName); @@ -464,7 +464,7 @@ public void setNextResponse(ButtplugMessage message) { protected CompletableFuture sendMessage(ButtplugMessage msg) { lastSentMessage = msg; sentMessages.add(msg); - + CompletableFuture future = new CompletableFuture<>(); scheduleWait(msg.getId(), future); @@ -475,7 +475,7 @@ protected CompletableFuture sendMessage(ButtplugMessage msg) { } else { onMessage(Collections.singletonList(new Ok(msg.getId()))); } - + return future; } diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugDeviceExceptionTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugDeviceExceptionTest.java index a56fb4a..9534073 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugDeviceExceptionTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugDeviceExceptionTest.java @@ -2,7 +2,9 @@ import io.github.blackspherefollower.buttplug4j.ButtplugException; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; public class ButtplugDeviceExceptionTest { @@ -10,7 +12,7 @@ public class ButtplugDeviceExceptionTest { public void testExceptionWithMessage() { String errorMessage = "Device not found"; ButtplugDeviceException exception = new ButtplugDeviceException(errorMessage); - + assertEquals(errorMessage, exception.getMessage()); } diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugDeviceFeatureExceptionTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugDeviceFeatureExceptionTest.java index a86283d..37ec1e2 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugDeviceFeatureExceptionTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugDeviceFeatureExceptionTest.java @@ -2,14 +2,16 @@ import io.github.blackspherefollower.buttplug4j.ButtplugException; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; public class ButtplugDeviceFeatureExceptionTest { @Test public void testExceptionWithMessage() { ButtplugDeviceFeatureException exception = new ButtplugDeviceFeatureException("slap"); - + assertEquals("Buttplug Device Feature does not support slap", exception.getMessage()); } diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugProtocolExceptionTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugProtocolExceptionTest.java index b358461..db87aee 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugProtocolExceptionTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/ButtplugProtocolExceptionTest.java @@ -3,7 +3,9 @@ import com.fasterxml.jackson.core.JsonParseException; import io.github.blackspherefollower.buttplug4j.ButtplugException; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; public class ButtplugProtocolExceptionTest { @@ -12,7 +14,7 @@ public void testExceptionWithMessage() { String errorMessage = "Invalid protocol message"; JsonParseException cause = new JsonParseException(errorMessage); ButtplugProtocolException exception = new ButtplugProtocolException(cause); - + assertEquals("Buttplug JSON message exception", exception.getMessage()); assertEquals(errorMessage, exception.getCause().getMessage()); } diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/DeviceFeatureTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/DeviceFeatureTest.java index 6393074..afcf7f0 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/DeviceFeatureTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/client/DeviceFeatureTest.java @@ -2,7 +2,7 @@ import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class DeviceFeatureTest { @@ -15,7 +15,7 @@ public void testDeviceFeatureCreation() { @Test public void testDeviceFeatureWithProperties() { DeviceFeature feature = new DeviceFeature(); - + // Test that the feature can be created and has expected behavior // The exact implementation depends on the DeviceFeature class structure assertNotNull(feature); diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/ButtplugMessageTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/ButtplugMessageTest.java index aaf0b58..19aff69 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/ButtplugMessageTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/ButtplugMessageTest.java @@ -4,7 +4,8 @@ import io.github.blackspherefollower.buttplug4j.protocol.messages.Ping; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; public class ButtplugMessageTest { @@ -31,7 +32,7 @@ public void testSystemMessageId() { public void testDifferentMessageTypes() { ButtplugMessage okMsg = new Ok(1); ButtplugMessage pingMsg = new Ping(2); - + assertNotEquals(okMsg.getClass(), pingMsg.getClass()); assertNotEquals(okMsg.getId(), pingMsg.getId()); } diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/DeviceListTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/DeviceListTest.java index f42a29f..07b3de6 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/DeviceListTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/DeviceListTest.java @@ -33,7 +33,7 @@ public static void setup() throws IOException { @Test public void test() throws IOException, ButtplugProtocolException { - String testStr = "[{\"DeviceList\":{\"Id\":5,\"Devices\":{\"0\":{\"DeviceIndex\":0,\"DeviceName\":\"Test Vibrator\",\"DeviceMessageTimingGap\":100,\"DeviceFeatures\":{\"0\":{\"FeatureIndex\":0,\"FeatureDescription\":\"Clitoral Stimulator\",\"Output\":{\"Vibrate\":{\"Value\":[0,20]}}},\"1\":{\"FeatureIndex\":1,\"FeatureDescription\":\"Insertable Stimulator\",\"Output\":{\"Vibrate\":{\"Value\":[0,20]}}},\"2\":{\"FeatureIndex\":2,\"FeatureDescription\":\"Battery\",\"Input\":{\"Battery\":{\"InputCommands\":[\"Read\"],\"ValueRange\":[[0,0],[0,100]]}}}}},\"2\":{\"DeviceIndex\":2,\"DeviceName\":\"Test Stroker\",\"DeviceMessageTimingGap\":100,\"DeviceDisplayName\":\"User set name\",\"DeviceFeatures\":{\"0\":{\"FeatureIndex\":0,\"FeatureDescription\":\"Stroker\",\"Output\":{\"Oscillate\":{\"Value\":[0,20]},\"PositionWithDuration\":{\"Position\":[0,100],\"Duration\":[0,2000]}},\"Input\":{\"Position\":{\"InputCommands\":[\"Subscribe\",\"Read\"],\"ValueRange\":[[0,0],[0,100]]}}},\"1\":{\"FeatureIndex\":1,\"FeatureDescription\":\"Bluetooth Radio RSSI\",\"Input\":{\"RSSI\":{\"InputCommands\":[\"Read\"],\"ValueRange\":[[-10,0],[-100,0]]}}}}}}}}]"; + String testStr = "[{\"DeviceList\":{\"Id\":5,\"Devices\":{\"0\":{\"DeviceIndex\":0,\"DeviceName\":\"Test Vibrator\",\"DeviceMessageTimingGap\":100,\"DeviceFeatures\":{\"0\":{\"FeatureIndex\":0,\"FeatureDescription\":\"Clitoral Stimulator\",\"Output\":{\"Vibrate\":{\"Value\":[0,20]}}},\"1\":{\"FeatureIndex\":1,\"FeatureDescription\":\"Insertable Stimulator\",\"Output\":{\"Vibrate\":{\"Value\":[0,20]}}},\"2\":{\"FeatureIndex\":2,\"FeatureDescription\":\"Battery\",\"Input\":{\"Battery\":{\"Command\":[\"Read\"],\"Value\":[[0,0],[0,100]]}}}}},\"2\":{\"DeviceIndex\":2,\"DeviceName\":\"Test Stroker\",\"DeviceMessageTimingGap\":100,\"DeviceDisplayName\":\"User set name\",\"DeviceFeatures\":{\"0\":{\"FeatureIndex\":0,\"FeatureDescription\":\"Stroker\",\"Output\":{\"Oscillate\":{\"Value\":[0,20]},\"PositionWithDuration\":{\"Value\":[0,100],\"Duration\":[0,2000]}},\"Input\":{\"Position\":{\"Command\":[\"Subscribe\",\"Read\"],\"Value\":[[0,0],[0,100]]}}},\"1\":{\"FeatureIndex\":1,\"FeatureDescription\":\"Bluetooth Radio RSSI\",\"Input\":{\"Rssi\":{\"Command\":[\"Read\"],\"Value\":[[-10,0],[-100,0]]}}}}}}}}]"; Validator.Result result = new ValidatorFactory().validate(schema, testStr); assertTrue(result.isValid(), result.getErrors().stream().map(error -> error.getError() + " - " + error.getInstanceLocation()).collect(Collectors.joining("\n"))); @@ -82,7 +82,7 @@ public void test() throws IOException, ButtplugProtocolException { assertEquals(0, dev2Features.get(0).getFeatureIndex()); assertEquals("Stroker", dev2Features.get(0).getFeatureDescription()); assertEquals(2, dev2Features.get(0).getOutput().size()); - assertArrayEquals(new String[]{"Oscillate","PositionWithDuration"}, dev2Features.get(0).getOutput().stream().map(outputDescriptor -> outputDescriptor.getClass().getSimpleName()).toArray()); + assertArrayEquals(new String[]{"Oscillate", "PositionWithDuration"}, dev2Features.get(0).getOutput().stream().map(outputDescriptor -> outputDescriptor.getClass().getSimpleName()).toArray()); assertEquals(1, dev2Features.get(0).getInput().size()); assertEquals(1, dev2Features.get(1).getFeatureIndex()); assertArrayEquals(new String[]{"PositionInput"}, dev2Features.get(0).getInput().stream().map(inputDescriptor -> inputDescriptor.getClass().getSimpleName()).toArray()); @@ -99,4 +99,67 @@ public void test() throws IOException, ButtplugProtocolException { assertEquals(testStr, jsonOut); } + @Test + public void testObsure() throws IOException, ButtplugProtocolException { + String testStr = "[{\"DeviceList\":{\"Id\":5,\"Devices\":{\"0\":{\"DeviceIndex\":0,\"DeviceName\":\"Test Everything\",\"DeviceMessageTimingGap\":100,\"DeviceDisplayName\":\"Everything\",\"DeviceFeatures\":{" + + "\"0\":{\"FeatureIndex\":0,\"FeatureDescription\":\"Example Vibrate\",\"Output\":{\"Vibrate\":{\"Value\":[0,20]}}}," + + "\"1\":{\"FeatureIndex\":1,\"FeatureDescription\":\"Example Rotate\",\"Output\":{\"Rotate\":{\"Value\":[-20,20]}}}," + + "\"2\":{\"FeatureIndex\":2,\"FeatureDescription\":\"Example Oscillate\",\"Output\":{\"Oscillate\":{\"Value\":[0,20]}}}," + + "\"3\":{\"FeatureIndex\":3,\"FeatureDescription\":\"Example Constrict\",\"Output\":{\"Constrict\":{\"Value\":[0,20]}}}," + + "\"4\":{\"FeatureIndex\":4,\"FeatureDescription\":\"Example Temperature\",\"Output\":{\"Temperature\":{\"Value\":[0,20]}}}," + + "\"5\":{\"FeatureIndex\":5,\"FeatureDescription\":\"Example Spray\",\"Output\":{\"Spray\":{\"Value\":[0,1]}}}," + + "\"6\":{\"FeatureIndex\":6,\"FeatureDescription\":\"Example Led\",\"Output\":{\"Led\":{\"Value\":[0,255]}}}," + + "\"7\":{\"FeatureIndex\":7,\"FeatureDescription\":\"Example Position\",\"Output\":{\"Position\":{\"Value\":[0,255]}}}," + + "\"8\":{\"FeatureIndex\":8,\"FeatureDescription\":\"Example PositionWithDuration\",\"Output\":{\"PositionWithDuration\":{\"Value\":[0,255],\"Duration\":[0,2000]}}}," + + "\"9\":{\"FeatureIndex\":9,\"FeatureDescription\":\"Example Button\",\"Input\":{\"Button\":{\"Command\":[\"Read\"],\"Value\":[[0,0],[0,1]]}}}," + + "\"10\":{\"FeatureIndex\":10,\"FeatureDescription\":\"Example Pressure\",\"Input\":{\"Pressure\":{\"Command\":[\"Read\"],\"Value\":[[0,0],[0,100]]}}}," + + "\"11\":{\"FeatureIndex\":11,\"FeatureDescription\":\"Example Rssi\",\"Input\":{\"Rssi\":{\"Command\":[\"Read\"],\"Value\":[[0,0],[0,100]]}}}," + + "\"12\":{\"FeatureIndex\":12,\"FeatureDescription\":\"Example Battery\",\"Input\":{\"Battery\":{\"Command\":[\"Read\"],\"Value\":[[0,0],[0,100]]}}}" + + "}}}}}]"; + + Validator.Result result = new ValidatorFactory().validate(schema, testStr); + assertTrue(result.isValid(), result.getErrors().stream().map(error -> error.getError() + " - " + error.getInstanceLocation()).collect(Collectors.joining("\n"))); + + ButtplugJsonMessageParser parser = new ButtplugJsonMessageParser(); + List msgs = parser.parseJson(testStr); + + assertEquals(1, msgs.size()); + assertEquals(DeviceList.class, msgs.get(0).getClass()); + assertEquals(5, msgs.get(0).getId()); + assertEquals(1, ((DeviceList) msgs.get(0)).getDevices().size()); + + HashMap devs = ((DeviceList) msgs.get(0)).getDevices(); + assertNotNull(devs.get(0)); + assertEquals(0, devs.get(0).getDeviceIndex()); + assertEquals("Test Everything", devs.get(0).getDeviceName()); + assertEquals("Everything", devs.get(0).getDeviceDisplayName()); + assertEquals(100, devs.get(0).getDeviceMessageTimingGap()); + HashMap dev0Features = devs.get(0).getDeviceFeatures(); + assertEquals(13, dev0Features.size()); + + String[] outputNames = new String[]{"Vibrate", "Rotate", "Oscillate", "Constrict", "Temperature", "Spray", "Led", "Position", "PositionWithDuration"}; + for (int i = 0; i < outputNames.length; i++) { + assertEquals(i, dev0Features.get(i).getFeatureIndex()); + assertEquals("Example "+outputNames[i], dev0Features.get(i).getFeatureDescription()); + assertEquals(1, dev0Features.get(i).getOutput().size()); + assertArrayEquals(new String[]{outputNames[i]}, dev0Features.get(i).getOutput().stream().map(outputDescriptor -> outputDescriptor.getClass().getSimpleName()).toArray()); + assertNull(dev0Features.get(0).getInput()); + } + + String[] inputNames = new String[]{"Button", "Pressure", "Rssi", "Battery"}; + for (int i = 0; i < inputNames.length; i++) { + assertEquals(i+outputNames.length, dev0Features.get(i+outputNames.length).getFeatureIndex()); + assertEquals("Example "+inputNames[i], dev0Features.get(i+outputNames.length).getFeatureDescription()); + assertNull(dev0Features.get(i+outputNames.length).getOutput()); + assertEquals(1, dev0Features.get(i+outputNames.length).getInput().size()); + assertArrayEquals(new String[]{inputNames[i]}, dev0Features.get(i+outputNames.length).getInput().stream().map(inputDescriptor -> inputDescriptor.getClass().getSimpleName()).toArray()); + } + + String jsonOut = parser.formatJson(msgs); + assertEquals(testStr, jsonOut); + + jsonOut = parser.formatJson(msgs.get(0)); + assertEquals(testStr, jsonOut); + } + } diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/ErrorTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/ErrorTest.java index 3c4d501..be2e79b 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/ErrorTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/ErrorTest.java @@ -41,16 +41,16 @@ public void test() throws IOException, ButtplugProtocolException { ButtplugJsonMessageParser parser = new ButtplugJsonMessageParser(); List msgs = parser.parseJson(testStr); - assertEquals(msgs.size(), 1); - assertEquals(msgs.get(0).getClass(), - Error.class); - assertEquals(msgs.get(0).getId(), 7); + assertEquals(1, msgs.size()); + assertEquals(Error.class, + msgs.get(0).getClass()); + assertEquals(7, msgs.get(0).getId()); assertEquals( - ((Error) msgs.get(0)).getErrorMessage(), - "TestError"); + "TestError", + ((Error) msgs.get(0)).getErrorMessage()); assertEquals( - ((Error) msgs.get(0)).getErrorCode(), - Error.ErrorClass.ERROR_DEVICE); + Error.ErrorClass.ERROR_DEVICE, + ((Error) msgs.get(0)).getErrorCode()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); @@ -59,4 +59,17 @@ public void test() throws IOException, ButtplugProtocolException { assertEquals(testStr, jsonOut); } + @Test + public void testException() throws IOException, ButtplugProtocolException { + Error e = new Error(new Exception("TestError"), 7); + + assertEquals(7, e.getId()); + assertEquals( + "TestError", + e.getException().getMessage()); + assertEquals( + Error.ErrorClass.ERROR_UNKNOWN, + e.getErrorCode()); + } + } diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputCmdTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputCmdTest.java index 5375670..0068324 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputCmdTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputCmdTest.java @@ -17,7 +17,8 @@ import java.util.List; import java.util.stream.Collectors; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class InputCmdTest { @@ -33,7 +34,7 @@ public static void setup() throws IOException { @Test public void testInputCmdSubscribe() throws IOException, ButtplugProtocolException { - String testStr = "[{\"InputCmd\":{\"Id\":1,\"DeviceIndex\":0,\"FeatureIndex\":0,\"InputType\":\"Battery\",\"InputCommand\":\"Subscribe\"}}]"; + String testStr = "[{\"InputCmd\":{\"Id\":1,\"DeviceIndex\":0,\"FeatureIndex\":0,\"Type\":\"Battery\",\"Command\":\"Subscribe\"}}]"; Validator.Result result = new ValidatorFactory().validate(schema, testStr); assertTrue(result.isValid(), result.getErrors().stream().map(Error::getError).collect(Collectors.joining("\n"))); @@ -58,7 +59,7 @@ public void testInputCmdSubscribe() throws IOException, ButtplugProtocolExceptio @Test public void testInputCmdUnsubscribe() throws IOException, ButtplugProtocolException { - String testStr = "[{\"InputCmd\":{\"Id\":2,\"DeviceIndex\":1,\"FeatureIndex\":1,\"InputType\":\"RSSI\",\"InputCommand\":\"Unsubscribe\"}}]"; + String testStr = "[{\"InputCmd\":{\"Id\":2,\"DeviceIndex\":1,\"FeatureIndex\":1,\"Type\":\"RSSI\",\"Command\":\"Unsubscribe\"}}]"; Validator.Result result = new ValidatorFactory().validate(schema, testStr); assertTrue(result.isValid(), result.getErrors().stream().map(Error::getError).collect(Collectors.joining("\n"))); @@ -83,7 +84,7 @@ public void testInputCmdUnsubscribe() throws IOException, ButtplugProtocolExcept @Test public void testInputCmdRead() throws IOException, ButtplugProtocolException { - String testStr = "[{\"InputCmd\":{\"Id\":3,\"DeviceIndex\":0,\"FeatureIndex\":0,\"InputType\":\"Button\",\"InputCommand\":\"Read\"}}]"; + String testStr = "[{\"InputCmd\":{\"Id\":3,\"DeviceIndex\":0,\"FeatureIndex\":0,\"Type\":\"Button\",\"Command\":\"Read\"}}]"; Validator.Result result = new ValidatorFactory().validate(schema, testStr); assertTrue(result.isValid(), result.getErrors().stream().map(Error::getError).collect(Collectors.joining("\n"))); @@ -101,4 +102,12 @@ public void testInputCmdRead() throws IOException, ButtplugProtocolException { jsonOut = parser.formatJson(msgs.get(0)); assertEquals(testStr, jsonOut); } + + @Test + public void testInputCmdReadInternal() throws IOException, ButtplugProtocolException { + InputCmd cmd = new InputCmd(1, 0, 0, "Battery", InputCommandType.READ); + + assertEquals(InputCommandType.READ, cmd.getInputCommand()); + assertEquals("Battery", cmd.getInputType()); + } } diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputReadingTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputReadingTest.java index 960c73c..a5678dd 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputReadingTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/InputReadingTest.java @@ -35,7 +35,7 @@ public static void setup() throws IOException { @Test public void testInputReadingCreation() { InputReading reading = new InputReading(1, 0, 0); - + assertEquals(1, reading.getId()); assertEquals(0, reading.getDeviceIndex()); assertNotNull(reading); @@ -51,21 +51,122 @@ public void testInputReadingInheritance() { @Test public void testInputDataInterfaces() { // Test that InputData interface classes exist - InputReading.BatteryData batteryData = new InputReading.BatteryData(); - InputReading.RssiData rssiData = new InputReading.RssiData(); - InputReading.ButtonData buttonData = new InputReading.ButtonData(); - InputReading.PresureData presureData = new InputReading.PresureData(); - - assertInstanceOf(InputReading.BatteryData.class, batteryData); - assertInstanceOf(InputReading.RssiData.class, rssiData); - assertInstanceOf(InputReading.ButtonData.class, buttonData); - assertInstanceOf(InputReading.PresureData.class, presureData); + InputReading.Battery batteryData = new InputReading.Battery(); + InputReading.Rssi rssiData = new InputReading.Rssi(); + InputReading.Button buttonData = new InputReading.Button(); + InputReading.Presure presureData = new InputReading.Presure(); + InputReading.Position positionData = new InputReading.Position(); + + assertInstanceOf(InputReading.Battery.class, batteryData); + assertInstanceOf(InputReading.Rssi.class, rssiData); + assertInstanceOf(InputReading.Button.class, buttonData); + assertInstanceOf(InputReading.Presure.class, presureData); + assertInstanceOf(InputReading.Position.class, positionData); } @Test - @Disabled("See https://github.com/buttplugio/buttplug/issues/801") public void testBatteryReading() throws ButtplugProtocolException { - String testStr = "[{\"InputReading\":{\"Id\":1,\"DeviceIndex\":0,\"FeatureIndex\":1,\"Data\":{\"Battery\":{\"Data\":100}}}}]"; + String testStr = "[{\"InputReading\":{\"Id\":10,\"DeviceIndex\":0,\"FeatureIndex\":1,\"Reading\":{\"Battery\":{\"Value\":100}}}}]"; + + Validator.Result result = new ValidatorFactory().validate(schema, testStr); + assertTrue(result.isValid(), result.getErrors().stream().map(error -> error.getError() + " - " + error.getInstanceLocation()).collect(Collectors.joining("\n"))); + + ButtplugJsonMessageParser parser = new ButtplugJsonMessageParser(); + List msgs = parser.parseJson(testStr); + + assertEquals(1, msgs.size()); + assertEquals(InputReading.class, msgs.get(0).getClass()); + assertEquals(10, msgs.get(0).getId()); + assertEquals(0, ((InputReading) msgs.get(0)).getDeviceIndex()); + assertEquals(1, ((InputReading) msgs.get(0)).getFeatureIndex()); + assertEquals(InputReading.Battery.class, ((InputReading) msgs.get(0)).getData().getClass()); + assertEquals(100, ((InputReading.Battery)((InputReading) msgs.get(0)).getData()).getValue()); + + String jsonOut = parser.formatJson(msgs); + assertEquals(testStr, jsonOut); + + jsonOut = parser.formatJson(msgs.get(0)); + assertEquals(testStr, jsonOut); + } + + @Test + public void testRSSIReading() throws ButtplugProtocolException { + String testStr = "[{\"InputReading\":{\"Id\":10,\"DeviceIndex\":0,\"FeatureIndex\":1,\"Reading\":{\"Rssi\":{\"Value\":-10}}}}]"; + + Validator.Result result = new ValidatorFactory().validate(schema, testStr); + assertTrue(result.isValid(), result.getErrors().stream().map(error -> error.getError() + " - " + error.getInstanceLocation()).collect(Collectors.joining("\n"))); + + ButtplugJsonMessageParser parser = new ButtplugJsonMessageParser(); + List msgs = parser.parseJson(testStr); + + assertEquals(1, msgs.size()); + assertEquals(InputReading.class, msgs.get(0).getClass()); + assertEquals(10, msgs.get(0).getId()); + assertEquals(0, ((InputReading) msgs.get(0)).getDeviceIndex()); + assertEquals(1, ((InputReading) msgs.get(0)).getFeatureIndex()); + assertEquals(InputReading.Rssi.class, ((InputReading) msgs.get(0)).getData().getClass()); + assertEquals(-10, ((InputReading.Rssi)((InputReading) msgs.get(0)).getData()).getValue()); + + String jsonOut = parser.formatJson(msgs); + assertEquals(testStr, jsonOut); + + jsonOut = parser.formatJson(msgs.get(0)); + assertEquals(testStr, jsonOut); + } + + @Test + public void testPressureReading() throws ButtplugProtocolException { + String testStr = "[{\"InputReading\":{\"Id\":10,\"DeviceIndex\":0,\"FeatureIndex\":1,\"Reading\":{\"Pressure\":{\"Value\":50}}}}]"; + + Validator.Result result = new ValidatorFactory().validate(schema, testStr); + assertTrue(result.isValid(), result.getErrors().stream().map(error -> error.getError() + " - " + error.getInstanceLocation()).collect(Collectors.joining("\n"))); + + ButtplugJsonMessageParser parser = new ButtplugJsonMessageParser(); + List msgs = parser.parseJson(testStr); + + assertEquals(1, msgs.size()); + assertEquals(InputReading.class, msgs.get(0).getClass()); + assertEquals(10, msgs.get(0).getId()); + assertEquals(0, ((InputReading) msgs.get(0)).getDeviceIndex()); + assertEquals(1, ((InputReading) msgs.get(0)).getFeatureIndex()); + assertEquals(InputReading.Presure.class, ((InputReading) msgs.get(0)).getData().getClass()); + assertEquals(50, ((InputReading.Presure)((InputReading) msgs.get(0)).getData()).getValue()); + + String jsonOut = parser.formatJson(msgs); + assertEquals(testStr, jsonOut); + + jsonOut = parser.formatJson(msgs.get(0)); + assertEquals(testStr, jsonOut); + } + + @Test + public void testButtonReading() throws ButtplugProtocolException { + String testStr = "[{\"InputReading\":{\"Id\":10,\"DeviceIndex\":0,\"FeatureIndex\":1,\"Reading\":{\"Button\":{\"Value\":1}}}}]"; + + Validator.Result result = new ValidatorFactory().validate(schema, testStr); + assertTrue(result.isValid(), result.getErrors().stream().map(error -> error.getError() + " - " + error.getInstanceLocation()).collect(Collectors.joining("\n"))); + + ButtplugJsonMessageParser parser = new ButtplugJsonMessageParser(); + List msgs = parser.parseJson(testStr); + + assertEquals(1, msgs.size()); + assertEquals(InputReading.class, msgs.get(0).getClass()); + assertEquals(10, msgs.get(0).getId()); + assertEquals(0, ((InputReading) msgs.get(0)).getDeviceIndex()); + assertEquals(1, ((InputReading) msgs.get(0)).getFeatureIndex()); + assertEquals(InputReading.Button.class, ((InputReading) msgs.get(0)).getData().getClass()); + assertEquals(1, ((InputReading.Button)((InputReading) msgs.get(0)).getData()).getValue()); + + String jsonOut = parser.formatJson(msgs); + assertEquals(testStr, jsonOut); + + jsonOut = parser.formatJson(msgs.get(0)); + assertEquals(testStr, jsonOut); + } + + @Test + public void testPositionReading() throws ButtplugProtocolException { + String testStr = "[{\"InputReading\":{\"Id\":10,\"DeviceIndex\":0,\"FeatureIndex\":1,\"Reading\":{\"Position\":{\"Value\":25}}}}]"; Validator.Result result = new ValidatorFactory().validate(schema, testStr); assertTrue(result.isValid(), result.getErrors().stream().map(error -> error.getError() + " - " + error.getInstanceLocation()).collect(Collectors.joining("\n"))); @@ -74,12 +175,12 @@ public void testBatteryReading() throws ButtplugProtocolException { List msgs = parser.parseJson(testStr); assertEquals(1, msgs.size()); - assertEquals(InputReading.BatteryData.class, msgs.get(0).getClass()); - assertEquals(1, msgs.get(0).getId(), 1); - assertEquals(4, ((ServerInfo) msgs.get(0)).getProtocolVersionMajor()); - assertEquals(0, ((ServerInfo) msgs.get(0)).getProtocolVersionMinor()); - assertEquals(500, ((ServerInfo) msgs.get(0)).getMaxPingTime()); - assertEquals("Websocket Server", ((ServerInfo) msgs.get(0)).getServerName()); + assertEquals(InputReading.class, msgs.get(0).getClass()); + assertEquals(10, msgs.get(0).getId()); + assertEquals(0, ((InputReading) msgs.get(0)).getDeviceIndex()); + assertEquals(1, ((InputReading) msgs.get(0)).getFeatureIndex()); + assertEquals(InputReading.Position.class, ((InputReading) msgs.get(0)).getData().getClass()); + assertEquals(25, ((InputReading.Position)((InputReading) msgs.get(0)).getData()).getValue()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/OutputCmdTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/OutputCmdTest.java index 33a9fcc..2bf70fe 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/OutputCmdTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/protocol/messages/OutputCmdTest.java @@ -47,7 +47,7 @@ public void testVibrate() throws IOException, ButtplugProtocolException { assertEquals(0, ((OutputCmd) msgs.get(0)).getDeviceIndex()); assertEquals(0, ((OutputCmd) msgs.get(0)).getFeatureIndex()); assertInstanceOf(OutputCmd.Vibrate.class, ((OutputCmd) msgs.get(0)).getCommand()); - assertEquals(5, ((OutputCmd.Vibrate)((OutputCmd) msgs.get(0)).getCommand()).getValue()); + assertEquals(5, ((OutputCmd.Vibrate) ((OutputCmd) msgs.get(0)).getCommand()).getValue()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); @@ -72,7 +72,7 @@ public void testRotate() throws IOException, ButtplugProtocolException { assertEquals(0, ((OutputCmd) msgs.get(0)).getDeviceIndex()); assertEquals(0, ((OutputCmd) msgs.get(0)).getFeatureIndex()); assertInstanceOf(OutputCmd.Rotate.class, ((OutputCmd) msgs.get(0)).getCommand()); - assertEquals(5, ((OutputCmd.Rotate)((OutputCmd) msgs.get(0)).getCommand()).getValue()); + assertEquals(5, ((OutputCmd.Rotate) ((OutputCmd) msgs.get(0)).getCommand()).getValue()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); @@ -97,7 +97,7 @@ public void testRotateBackwards() throws IOException, ButtplugProtocolException assertEquals(0, ((OutputCmd) msgs.get(0)).getDeviceIndex()); assertEquals(0, ((OutputCmd) msgs.get(0)).getFeatureIndex()); assertInstanceOf(OutputCmd.Rotate.class, ((OutputCmd) msgs.get(0)).getCommand()); - assertEquals(-5, ((OutputCmd.Rotate)((OutputCmd) msgs.get(0)).getCommand()).getValue()); + assertEquals(-5, ((OutputCmd.Rotate) ((OutputCmd) msgs.get(0)).getCommand()).getValue()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); @@ -122,7 +122,7 @@ public void testPosition() throws IOException, ButtplugProtocolException { assertEquals(0, ((OutputCmd) msgs.get(0)).getDeviceIndex()); assertEquals(0, ((OutputCmd) msgs.get(0)).getFeatureIndex()); assertInstanceOf(OutputCmd.Position.class, ((OutputCmd) msgs.get(0)).getCommand()); - assertEquals(5, ((OutputCmd.Position)((OutputCmd) msgs.get(0)).getCommand()).getValue()); + assertEquals(5, ((OutputCmd.Position) ((OutputCmd) msgs.get(0)).getCommand()).getValue()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); @@ -147,7 +147,7 @@ public void testSpray() throws IOException, ButtplugProtocolException { assertEquals(0, ((OutputCmd) msgs.get(0)).getDeviceIndex()); assertEquals(0, ((OutputCmd) msgs.get(0)).getFeatureIndex()); assertInstanceOf(OutputCmd.Spray.class, ((OutputCmd) msgs.get(0)).getCommand()); - assertEquals(5, ((OutputCmd.Spray)((OutputCmd) msgs.get(0)).getCommand()).getValue()); + assertEquals(5, ((OutputCmd.Spray) ((OutputCmd) msgs.get(0)).getCommand()).getValue()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); @@ -172,7 +172,7 @@ public void testConstrict() throws IOException, ButtplugProtocolException { assertEquals(0, ((OutputCmd) msgs.get(0)).getDeviceIndex()); assertEquals(0, ((OutputCmd) msgs.get(0)).getFeatureIndex()); assertInstanceOf(OutputCmd.Constrict.class, ((OutputCmd) msgs.get(0)).getCommand()); - assertEquals(5, ((OutputCmd.Constrict)((OutputCmd) msgs.get(0)).getCommand()).getValue()); + assertEquals(5, ((OutputCmd.Constrict) ((OutputCmd) msgs.get(0)).getCommand()).getValue()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); @@ -197,7 +197,7 @@ public void testOscillate() throws IOException, ButtplugProtocolException { assertEquals(0, ((OutputCmd) msgs.get(0)).getDeviceIndex()); assertEquals(0, ((OutputCmd) msgs.get(0)).getFeatureIndex()); assertInstanceOf(OutputCmd.Oscillate.class, ((OutputCmd) msgs.get(0)).getCommand()); - assertEquals(5, ((OutputCmd.Oscillate)((OutputCmd) msgs.get(0)).getCommand()).getValue()); + assertEquals(5, ((OutputCmd.Oscillate) ((OutputCmd) msgs.get(0)).getCommand()).getValue()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); @@ -222,7 +222,7 @@ public void testTemperature() throws IOException, ButtplugProtocolException { assertEquals(0, ((OutputCmd) msgs.get(0)).getDeviceIndex()); assertEquals(0, ((OutputCmd) msgs.get(0)).getFeatureIndex()); assertInstanceOf(OutputCmd.Temperature.class, ((OutputCmd) msgs.get(0)).getCommand()); - assertEquals(5, ((OutputCmd.Temperature)((OutputCmd) msgs.get(0)).getCommand()).getValue()); + assertEquals(5, ((OutputCmd.Temperature) ((OutputCmd) msgs.get(0)).getCommand()).getValue()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); @@ -247,7 +247,7 @@ public void testLed() throws IOException, ButtplugProtocolException { assertEquals(0, ((OutputCmd) msgs.get(0)).getDeviceIndex()); assertEquals(0, ((OutputCmd) msgs.get(0)).getFeatureIndex()); assertInstanceOf(OutputCmd.Led.class, ((OutputCmd) msgs.get(0)).getCommand()); - assertEquals(5, ((OutputCmd.Led)((OutputCmd) msgs.get(0)).getCommand()).getValue()); + assertEquals(5, ((OutputCmd.Led) ((OutputCmd) msgs.get(0)).getCommand()).getValue()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); @@ -258,7 +258,7 @@ public void testLed() throws IOException, ButtplugProtocolException { @Test public void testPositionWithDuration() throws IOException, ButtplugProtocolException { - String testStr = "[{\"OutputCmd\":{\"Id\":1,\"DeviceIndex\":0,\"FeatureIndex\":0,\"Command\":{\"PositionWithDuration\":{\"Position\":5,\"Duration\":10}}}}]"; + String testStr = "[{\"OutputCmd\":{\"Id\":1,\"DeviceIndex\":0,\"FeatureIndex\":0,\"Command\":{\"PositionWithDuration\":{\"Value\":5,\"Duration\":10}}}}]"; Validator.Result result = new ValidatorFactory().validate(schema, testStr); assertTrue(result.isValid(), result.getErrors().stream().map(Error::getError).collect(Collectors.joining("\n"))); @@ -272,8 +272,8 @@ public void testPositionWithDuration() throws IOException, ButtplugProtocolExcep assertEquals(0, ((OutputCmd) msgs.get(0)).getDeviceIndex()); assertEquals(0, ((OutputCmd) msgs.get(0)).getFeatureIndex()); assertInstanceOf(OutputCmd.PositionWithDuration.class, ((OutputCmd) msgs.get(0)).getCommand()); - assertEquals(5, ((OutputCmd.PositionWithDuration)((OutputCmd) msgs.get(0)).getCommand()).getPosition()); - assertEquals(10, ((OutputCmd.PositionWithDuration)((OutputCmd) msgs.get(0)).getCommand()).getDuration()); + assertEquals(5, ((OutputCmd.PositionWithDuration) ((OutputCmd) msgs.get(0)).getCommand()).getValue()); + assertEquals(10, ((OutputCmd.PositionWithDuration) ((OutputCmd) msgs.get(0)).getCommand()).getDuration()); String jsonOut = parser.formatJson(msgs); assertEquals(testStr, jsonOut); diff --git a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/util/PairTest.java b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/util/PairTest.java index 9096083..023d73d 100644 --- a/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/util/PairTest.java +++ b/buttplug4j/src/test/java/io/github/blackspherefollower/buttplug4j/util/PairTest.java @@ -1,6 +1,7 @@ package io.github.blackspherefollower.buttplug4j.util; import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.*; public class PairTest { @@ -8,7 +9,7 @@ public class PairTest { @Test public void testPairCreation() { Pair pair = new Pair<>("test", 42); - + assertEquals("test", pair.getLeft()); assertEquals(42, pair.getRight()); } @@ -16,7 +17,7 @@ public void testPairCreation() { @Test public void testPairWithNullValues() { Pair pair = new Pair<>(null, null); - + assertNull(pair.getLeft()); assertNull(pair.getRight()); } @@ -24,10 +25,10 @@ public void testPairWithNullValues() { @Test public void testPairSetters() { Pair pair = new Pair<>("initial", 1); - + pair.setLeft("updated"); pair.setRight(2); - + assertEquals("updated", pair.getLeft()); assertEquals(2, pair.getRight()); } @@ -37,7 +38,7 @@ public void testPairEquality() { Pair pair1 = new Pair<>("test", 42); Pair pair2 = new Pair<>("test", 42); Pair pair3 = new Pair<>("different", 42); - + assertEquals(pair1, pair2); assertNotEquals(pair1, pair3); } @@ -46,7 +47,7 @@ public void testPairEquality() { public void testPairHashCode() { Pair pair1 = new Pair<>("test", 42); Pair pair2 = new Pair<>("test", 42); - + assertEquals(pair1.hashCode(), pair2.hashCode()); } @@ -54,7 +55,7 @@ public void testPairHashCode() { public void testPairToString() { Pair pair = new Pair<>("test", 42); String str = pair.toString(); - + assertNotNull(str); assertTrue(str.contains("test")); assertTrue(str.contains("42")); @@ -63,7 +64,7 @@ public void testPairToString() { @Test public void testPairWithDifferentTypes() { Pair pair = new Pair<>(3.14, true); - + assertEquals(3.14, pair.getLeft()); assertEquals(true, pair.getRight()); }