|
| 1 | +package com.openfin.desktop; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | + |
| 6 | +import java.util.concurrent.CompletionStage; |
| 7 | +import java.util.concurrent.CountDownLatch; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | + |
| 10 | +import org.json.JSONObject; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.BeforeClass; |
| 13 | +import org.junit.Test; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import com.openfin.desktop.snapshot.SnapshotSourceClient; |
| 18 | +import com.openfin.desktop.snapshot.SnapshotSourceProvider; |
| 19 | + |
| 20 | +public class SnapshotSourceTest { |
| 21 | + |
| 22 | + private static Logger logger = LoggerFactory.getLogger(SnapshotSourceTest.class.getName()); |
| 23 | + |
| 24 | + private static final String DESKTOP_UUID = SnapshotSourceTest.class.getName(); |
| 25 | + |
| 26 | + private DesktopConnection desktopConnection; |
| 27 | + |
| 28 | + @Before |
| 29 | + public void initDesktopConnection() throws Exception { |
| 30 | + this.desktopConnection = TestUtils.setupConnection(DESKTOP_UUID); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void initProviderThenCreateClient() throws Exception { |
| 35 | + String appUuid = "initProviderThenCreateClient"; |
| 36 | + |
| 37 | + SnapshotSourceClient snapshotSourceClient = this.desktopConnection.getSnapshotSource().initSnapshotSource(appUuid, new SnapshotSourceProvider() { |
| 38 | + @Override |
| 39 | + public JSONObject getSnapshot() { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void applySnapshot(JSONObject snapshot) { |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + }).thenCompose(snapshotSourceProvider-> { |
| 49 | + return this.desktopConnection.getSnapshotSource().createSnapshotSourceClient(appUuid); |
| 50 | + }).toCompletableFuture().get(10, TimeUnit.SECONDS); |
| 51 | + |
| 52 | + assertNotNull(snapshotSourceClient); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void initProviderThenCreateClientThenGetSnapshot() throws Exception { |
| 57 | + String appUuid = "initProviderThenCreateClientThenGetSnapshot"; |
| 58 | + String snapshotPropName = "whatever"; |
| 59 | + String snapshotPropValue = "hohoho"; |
| 60 | + |
| 61 | + SnapshotSourceClient snapshotSourceClient = this.desktopConnection.getSnapshotSource().initSnapshotSource(appUuid, new SnapshotSourceProvider() { |
| 62 | + @Override |
| 63 | + public JSONObject getSnapshot() { |
| 64 | + JSONObject snapshot = new JSONObject(); |
| 65 | + snapshot.put(snapshotPropName, snapshotPropValue); |
| 66 | + return snapshot; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void applySnapshot(JSONObject snapshot) { |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + }).thenCompose(snapshotSourceProvider-> { |
| 75 | + return this.desktopConnection.getSnapshotSource().createSnapshotSourceClient(appUuid); |
| 76 | + }).toCompletableFuture().get(10, TimeUnit.SECONDS); |
| 77 | + |
| 78 | + assertNotNull(snapshotSourceClient); |
| 79 | + |
| 80 | + JSONObject snapshot = snapshotSourceClient.getSnapshot().toCompletableFuture().get(10, TimeUnit.SECONDS); |
| 81 | + |
| 82 | + assertEquals(snapshot.get(snapshotPropName), snapshotPropValue); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void initProviderThenCreateClientThenApplySnapshot() throws Exception { |
| 87 | + String appUuid = "initProviderThenCreateClientThenApplySnapshot"; |
| 88 | + String snapshotPropName = "whatever"; |
| 89 | + String snapshotPropValue = "hohoho"; |
| 90 | + |
| 91 | + CountDownLatch latch = new CountDownLatch(1); |
| 92 | + |
| 93 | + SnapshotSourceClient snapshotSourceClient = this.desktopConnection.getSnapshotSource().initSnapshotSource(appUuid, new SnapshotSourceProvider() { |
| 94 | + @Override |
| 95 | + public JSONObject getSnapshot() { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void applySnapshot(JSONObject snapshot) { |
| 101 | + if (snapshotPropValue.equals(snapshot.getString(snapshotPropName))) { |
| 102 | + latch.countDown(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + }).thenCompose(snapshotSourceProvider-> { |
| 107 | + return this.desktopConnection.getSnapshotSource().createSnapshotSourceClient(appUuid); |
| 108 | + }).toCompletableFuture().get(10, TimeUnit.SECONDS); |
| 109 | + |
| 110 | + assertNotNull(snapshotSourceClient); |
| 111 | + |
| 112 | + JSONObject snapshot = new JSONObject(); |
| 113 | + snapshot.put(snapshotPropName, snapshotPropValue); |
| 114 | + |
| 115 | + snapshotSourceClient.applySnapshot(snapshot).toCompletableFuture().get(10, TimeUnit.SECONDS); |
| 116 | + |
| 117 | + latch.await(10, TimeUnit.SECONDS); |
| 118 | + |
| 119 | + assertEquals(0, latch.getCount()); |
| 120 | + } |
| 121 | +} |
0 commit comments