|
| 1 | +package com.openfin.desktop; |
| 2 | + |
| 3 | +import com.openfin.desktop.snapshot.SnapshotSource; |
| 4 | +import com.openfin.desktop.snapshot.SnapshotSourceProvider; |
| 5 | +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LocatorEx; |
| 6 | +import org.json.JSONArray; |
| 7 | +import org.json.JSONObject; |
| 8 | +import org.junit.AfterClass; |
| 9 | +import org.junit.BeforeClass; |
| 10 | +import org.junit.Ignore; |
| 11 | +import org.junit.Test; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +import java.util.concurrent.CountDownLatch; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.fail; |
| 20 | + |
| 21 | +public class SnapshotTest implements SnapshotSourceProvider { |
| 22 | + |
| 23 | + private static Logger logger = LoggerFactory.getLogger(SnapshotTest.class.getName()); |
| 24 | + |
| 25 | + private static final String DESKTOP_UUID = SnapshotTest.class.getName(); |
| 26 | + private static DesktopConnection desktopConnection; |
| 27 | + private static OpenFinRuntime runtime; |
| 28 | + private static final JSONObject SNAPSHOT_CONTENT = new JSONObject("{width: 123}"); |
| 29 | + |
| 30 | + private JSONObject randomSnapshot; |
| 31 | + |
| 32 | + @BeforeClass |
| 33 | + public static void setup() throws Exception { |
| 34 | + logger.debug("starting"); |
| 35 | + desktopConnection = TestUtils.setupConnection(DESKTOP_UUID); |
| 36 | + if (desktopConnection != null) { |
| 37 | + runtime = new OpenFinRuntime(desktopConnection); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @AfterClass |
| 42 | + public static void teardown() throws Exception { |
| 43 | + TestUtils.teardownDesktopConnection(desktopConnection); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void initProviderThenCreateClient() throws Exception { |
| 48 | + CountDownLatch latch = new CountDownLatch(2); |
| 49 | + final String appUuid = "initProviderThenCreateClient"; |
| 50 | + desktopConnection.getSnapshotSource().initSnapshotSourceProviderAsync(appUuid, this).thenAccept(provider -> { |
| 51 | + logger.debug("Snapshot provider created"); |
| 52 | + latch.countDown(); |
| 53 | + }); |
| 54 | + desktopConnection.getSnapshotSource().createSnapshotSourceClientAsync(appUuid).thenAccept(client -> { |
| 55 | + logger.debug("Snapshot client created"); |
| 56 | + latch.countDown(); |
| 57 | + }); |
| 58 | + |
| 59 | + latch.await(5, TimeUnit.SECONDS); |
| 60 | + |
| 61 | + assertEquals("initProviderThenCreateClient timeout", latch.getCount(), 0); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void initProviderThenCreateClientThenGetSnapshot() throws Exception { |
| 66 | + CountDownLatch latch = new CountDownLatch(2); |
| 67 | + final String appUuid = "initProviderThenCreateClientThenGetSnapshot"; |
| 68 | + desktopConnection.getSnapshotSource().initSnapshotSourceProviderAsync(appUuid, this).thenAccept(provider -> { |
| 69 | + logger.debug("Snapshot provider created"); |
| 70 | + latch.countDown(); |
| 71 | + }); |
| 72 | + |
| 73 | + desktopConnection.getSnapshotSource().createSnapshotSourceClientAsync(appUuid).thenAccept(client -> { |
| 74 | + client.getSnapshotAsync().thenAccept(snapshot -> { |
| 75 | + if (SNAPSHOT_CONTENT.toString().equals(snapshot.toString())) { |
| 76 | + latch.countDown(); |
| 77 | + } |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + latch.await(5, TimeUnit.SECONDS); |
| 82 | + |
| 83 | + assertEquals("initProviderThenCreateClientThenGetSnapshot timeout", latch.getCount(), 0); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void initProviderThenCreateClientThenApplySnapshot() throws Exception { |
| 88 | + CountDownLatch latch = new CountDownLatch(2); |
| 89 | + final JSONObject random = new JSONObject(String.format("{value: %f}", Math.random())); |
| 90 | + final String appUuid = "initProviderThenCreateClientThenApplySnapshot"; |
| 91 | + desktopConnection.getSnapshotSource().initSnapshotSourceProviderAsync(appUuid, this).thenAccept(provider -> { |
| 92 | + latch.countDown(); |
| 93 | + }); |
| 94 | + |
| 95 | + desktopConnection.getSnapshotSource().createSnapshotSourceClientAsync(appUuid).thenAccept(client -> { |
| 96 | + client.applySnapshotAsync(random).thenAccept(ack -> { |
| 97 | + client.getSnapshotAsync().thenAccept(snapshot -> { |
| 98 | + if (random.toString().equals(snapshot.toString())) { |
| 99 | + latch.countDown(); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + latch.await(5, TimeUnit.SECONDS); |
| 107 | + |
| 108 | + assertEquals("initProviderThenCreateClientThenGetSnapshot timeout", latch.getCount(), 0); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public JSONObject getSnapshot() { |
| 113 | + if (this.randomSnapshot != null) { |
| 114 | + return this.randomSnapshot; |
| 115 | + } else { |
| 116 | + return SNAPSHOT_CONTENT; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void applySnapshot(JSONObject snapshot) { |
| 122 | + this.randomSnapshot = snapshot; |
| 123 | + } |
| 124 | +} |
0 commit comments