diff --git a/add-on/src/addon/javascript/background.js b/add-on/src/addon/javascript/background.js index f886f84..5e448af 100644 --- a/add-on/src/addon/javascript/background.js +++ b/add-on/src/addon/javascript/background.js @@ -89,6 +89,9 @@ var listeners = [function (message, returnValue) { } switch (type) { + case "NumberPing": + returnValue = message.number; + break; case "CommandLoadSong": returnValue = () => { if (message.song) diff --git a/browser/build.gradle b/browser/build.gradle index 81ef203..03dc21d 100644 --- a/browser/build.gradle +++ b/browser/build.gradle @@ -60,7 +60,8 @@ dependencies { implementation 'org.apache.logging.log4j:log4j-core:2.13.3' implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.13.3' implementation project(":browserCommands") - addon project(path: ":add-on", configuration: 'addonBuild') + testImplementation 'junit:junit:4.12' + addon project(path: ":add-on", configuration: 'addonBuild') } task downloadWindows_x86_64(type: Download) { diff --git a/browser/src/test/java/PingTest.java b/browser/src/test/java/PingTest.java new file mode 100644 index 0000000..4820f64 --- /dev/null +++ b/browser/src/test/java/PingTest.java @@ -0,0 +1,76 @@ +import static org.junit.Assert.assertEquals; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import edu.regis.universeplayer.NumberPing; +import edu.regis.universeplayer.browser.Browser; +import edu.regis.universeplayer.browserCommands.CommandReturn; + +public class PingTest +{ + @BeforeClass + public static void setupBrowser() throws IOException, InterruptedException + { + System.out.println("Creating browser"); + Browser.createBrowser(); + System.out.println("Browser created"); + if (Browser.getInstance() == null) + { + Browser.waitInstance(); + } + Thread thread = new Thread(Browser.getInstance()); + thread.start(); + System.out.println("Browser fully initialized"); + } + + @AfterClass + public static void closeBrowser() throws IOException, InterruptedException + { + Browser.getInstance().stop(); + System.out.println("Browser closed"); + } + + @Test + public void testPing() throws IOException, InterruptedException, ExecutionException + { + ArrayList> futures = new ArrayList<>(); + for (int i = 0; i < 3; i++) + { + futures.add(Browser.getInstance().sendObject("ping")); + } + System.out.println("Pings sent"); + for (int i = 0; i < futures.size(); i++) + { + assertEquals("pong", + ((CommandReturn) futures.get(i).get()) + .getReturnValue()); + } + System.out.println("Objects received"); + } + + @Test + public void testNumPing() throws IOException, InterruptedException, + ExecutionException + { + ArrayList> futures = new ArrayList<>(); + for (int i = 0; i < 3; i++) + { + futures.add(Browser.getInstance().sendObject(new NumberPing(i))); + } + System.out.println("Numbered pings sent"); + for (int i = 0; i < futures.size(); i++) + { + assertEquals(i, + ((CommandReturn) futures.get(i).get()) + .getReturnValue(), 0.001); + } + System.out.println("Objects received"); + } +} diff --git a/browserCommands/src/main/java/edu/regis/universeplayer/NumberPing.java b/browserCommands/src/main/java/edu/regis/universeplayer/NumberPing.java new file mode 100644 index 0000000..a089f95 --- /dev/null +++ b/browserCommands/src/main/java/edu/regis/universeplayer/NumberPing.java @@ -0,0 +1,21 @@ +package edu.regis.universeplayer; + +import java.io.Serializable; + +/** + * A test class for pinging the browser. + */ +public class NumberPing implements Serializable +{ + public int number; + + public NumberPing() + { + this(0); + } + + public NumberPing(int number) + { + this.number = number; + } +}