|
| 1 | +package com.openfin.desktop; |
| 2 | + |
| 3 | +import org.json.JSONArray; |
| 4 | +import org.json.JSONObject; |
| 5 | +import org.junit.AfterClass; |
| 6 | +import org.junit.BeforeClass; |
| 7 | +import org.junit.Test; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.util.UUID; |
| 12 | +import java.util.concurrent.CountDownLatch; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import java.util.concurrent.atomic.AtomicReference; |
| 15 | + |
| 16 | +import static junit.framework.Assert.assertEquals; |
| 17 | + |
| 18 | +/** |
| 19 | + * Created by richard on 4/1/2016. |
| 20 | + */ |
| 21 | +public class RuntimeConfigTest { |
| 22 | + private static Logger logger = LoggerFactory.getLogger(RuntimeConfigTest.class.getName()); |
| 23 | + |
| 24 | + private static final String DESKTOP_UUID = RuntimeConfigTest.class.getName(); |
| 25 | + private static DesktopConnection desktopConnection; |
| 26 | + private static RuntimeConfiguration configuration; |
| 27 | + private static String appUUID = UUID.randomUUID().toString(); |
| 28 | + |
| 29 | + @BeforeClass |
| 30 | + public static void setup() throws Exception { |
| 31 | + logger.debug("starting"); |
| 32 | + configuration = getRuntimeConfiguration(); |
| 33 | + desktopConnection = TestUtils.setupConnection(DESKTOP_UUID, configuration); |
| 34 | + } |
| 35 | + |
| 36 | + private static RuntimeConfiguration getRuntimeConfiguration() { |
| 37 | + RuntimeConfiguration configuration = new RuntimeConfiguration(); |
| 38 | + configuration.setRuntimeVersion(TestUtils.getRuntimeVersion()); |
| 39 | + configuration.setDevToolsPort(9090); |
| 40 | + configuration.setAdditionalRuntimeArguments("--v=1"); |
| 41 | + |
| 42 | + JSONObject startupApp = new JSONObject(); |
| 43 | + startupApp.put("name", "TVBWebTest"); |
| 44 | + startupApp.put("uuid", appUUID); |
| 45 | + startupApp.put("url", TestUtils.openfin_app_url); |
| 46 | + startupApp.put("applicationIcon", TestUtils.icon_url); |
| 47 | + startupApp.put("autoShow", true); |
| 48 | + startupApp.put("defaultTop", 100); |
| 49 | + startupApp.put("defaultLeft", 100); |
| 50 | + startupApp.put("defaultWidth", 200); |
| 51 | + startupApp.put("defaultHeight", 200); |
| 52 | + startupApp.put("delay_connection", true); |
| 53 | + startupApp.put("frame", true); |
| 54 | + startupApp.put("saveWindowState", false); |
| 55 | + configuration.setStartupApp(startupApp); |
| 56 | + |
| 57 | + return configuration; |
| 58 | + } |
| 59 | + |
| 60 | + @AfterClass |
| 61 | + public static void teardown() throws Exception { |
| 62 | + TestUtils.teardownDesktopConnection(desktopConnection); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void launchFromConfig() throws Exception { |
| 67 | + CountDownLatch latch = new CountDownLatch(1); |
| 68 | + AtomicReference<Boolean> atomicReference = new AtomicReference<>(); |
| 69 | + atomicReference.set(true); |
| 70 | + // check for startup app |
| 71 | + Thread waitThread = new Thread() { |
| 72 | + public void run() { |
| 73 | + while (atomicReference.get()) { |
| 74 | + try { |
| 75 | + if (isWindowCreated(appUUID)) { |
| 76 | + latch.countDown(); |
| 77 | + atomicReference.set(false); |
| 78 | + } else { |
| 79 | + Thread.sleep(1000); |
| 80 | + } |
| 81 | + } catch (Exception ex) { |
| 82 | + logger.error("Error waiting for checking window list", ex); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | + waitThread.start(); |
| 88 | + |
| 89 | + latch.await(10, TimeUnit.SECONDS); |
| 90 | + atomicReference.set(false); |
| 91 | + assertEquals(latch.getCount(), 0); |
| 92 | + } |
| 93 | + |
| 94 | + private boolean isWindowCreated(String uuid) throws Exception { |
| 95 | + CountDownLatch latch = new CountDownLatch(1); |
| 96 | + AtomicReference<Boolean> atomicReference = new AtomicReference<>(); |
| 97 | + atomicReference.set(false); |
| 98 | + OpenFinRuntime runtime = new OpenFinRuntime(desktopConnection); |
| 99 | + runtime.getAllWindows(new AckListener() { |
| 100 | + @Override |
| 101 | + public void onSuccess(Ack ack) { |
| 102 | + if (ack.isSuccessful()) { |
| 103 | + JSONArray data = (JSONArray) ack.getData(); |
| 104 | + logger.debug(String.format("All windows info %s", data.toString())); |
| 105 | + for (int i = 0; i < data.length(); i++) { |
| 106 | + JSONObject window = data.getJSONObject(i); |
| 107 | + if (window.has("uuid") && window.has("mainWindow") && window.getString("uuid").equals(uuid)) { |
| 108 | + atomicReference.set(true); |
| 109 | + } |
| 110 | + } |
| 111 | + latch.countDown(); |
| 112 | + } |
| 113 | + } |
| 114 | + @Override |
| 115 | + public void onError(Ack ack) { |
| 116 | + logger.error("Connection failed: %s", ack.getReason()); |
| 117 | + } |
| 118 | + }); |
| 119 | + latch.await(5, TimeUnit.SECONDS); |
| 120 | + return atomicReference.get(); |
| 121 | + } |
| 122 | +} |
| 123 | + |
0 commit comments