66import java .util .UUID ;
77import java .util .concurrent .CountDownLatch ;
88import java .util .concurrent .TimeUnit ;
9+ import java .util .concurrent .atomic .AtomicReference ;
910
1011import static junit .framework .Assert .assertEquals ;
1112import static junit .framework .Assert .assertFalse ;
@@ -130,21 +131,35 @@ public void onError(Ack ack) {
130131
131132 public static Application runApplication (ApplicationOptions options , DesktopConnection desktopConnection ) throws Exception {
132133 Application application = createApplication (options , desktopConnection );
133- runApplication (application );
134+ runApplication (application , true );
134135 return application ;
135136 }
136137
137- public static void runApplication (Application application ) throws Exception {
138- CountDownLatch startedLatch = new CountDownLatch (1 );
138+ /**
139+ * Run an application.
140+ *
141+ * @param application
142+ * @param checkAppConnected true if it needs to wait for app-connected event
143+ * @throws Exception
144+ */
145+ public static void runApplication (Application application , boolean checkAppConnected ) throws Exception {
146+ CountDownLatch startedLatch = new CountDownLatch (1 );
147+ CountDownLatch connectedLatch = new CountDownLatch (1 );
139148 EventListener listener = new EventListener () {
140149 @ Override
141150 public void eventReceived (ActionEvent actionEvent ) {
142151 if (actionEvent .getType ().equals ("started" )) {
143152 startedLatch .countDown ();
144153 }
154+ else if (actionEvent .getType ().equals ("app-connected" )) {
155+ connectedLatch .countDown ();
156+ }
145157 }
146158 };
147159 addEventListener (application , "started" , listener );
160+ if (checkAppConnected ) {
161+ addEventListener (application .getWindow (), "app-connected" , listener );
162+ }
148163
149164 CountDownLatch runLatch = new CountDownLatch (1 );
150165 application .run (new AckListener () {
@@ -161,6 +176,10 @@ public void onError(Ack ack) {
161176 runLatch .await (5 , TimeUnit .SECONDS );
162177 assertEquals ("Run application timeout " + application .getUuid (), runLatch .getCount (), 0 );
163178 assertEquals ("Start application timeout " + application .getUuid (), startedLatch .getCount (), 0 );
179+ if (checkAppConnected ) {
180+ connectedLatch .await (5 , TimeUnit .SECONDS );
181+ assertEquals (connectedLatch .getCount (), 0 );
182+ }
164183 }
165184
166185 public static void addEventListener (Application application , String evenType , EventListener eventListener ) throws Exception {
@@ -217,16 +236,48 @@ public static Window createChildWindow(Application application, WindowOptions ch
217236
218237 final CountDownLatch windowCreatedLatch = new CountDownLatch (1 );
219238 // use window-end-load event to wait for the window to finish loading
220- application .addEventListener ("window-end-load" , actionEvent -> {
221- if (actionEvent .getEventObject ().has ("name" )) {
222- if (childOptions .getName ().equals (actionEvent .getEventObject ().getString ("name" ))) {
223- windowCreatedLatch .countDown ();
224- }
239+ addEventListener (application , "window-end-load" , actionEvent -> {
240+ logger .info (actionEvent .getEventObject ().toString ());
241+ if (actionEvent .getEventObject ().has ("name" )) {
242+ if (childOptions .getName ().equals (actionEvent .getEventObject ().getString ("name" ))) {
243+ windowCreatedLatch .countDown ();
244+ }
225245 }
226- }, null );
246+ });
227247 application .createChildWindow (childOptions , null );
228248 windowCreatedLatch .await (10 , TimeUnit .SECONDS );
229249 assertEquals ("createChildWindow timeout" , windowCreatedLatch .getCount (), 0 );
230250 return Window .wrap (application .getOptions ().getUUID (), childOptions .getName (), desktopConnection );
231251 }
252+
253+ public static void addEventListener (Window window , String evenType , EventListener eventListener ) throws Exception {
254+ logger .debug ("addEventListener " + evenType );
255+ CountDownLatch latch = new CountDownLatch (1 );
256+ window .addEventListener (evenType , eventListener , new AckListener () {
257+ @ Override
258+ public void onSuccess (Ack ack ) {
259+ latch .countDown ();
260+ logger .debug ("addEventListener ack " + ack .isSuccessful ());
261+ }
262+ @ Override
263+ public void onError (Ack ack ) {
264+ logger .error (String .format ("Error adding event listener %s %s" , evenType , ack .getReason ()));
265+ }
266+ });
267+ latch .await (5 , TimeUnit .SECONDS );
268+ assertEquals ("addEventListener timeout " + evenType , latch .getCount (), 0 );
269+ }
270+
271+ public static WindowBounds getBounds (Window window ) throws Exception {
272+ CountDownLatch latch = new CountDownLatch (1 );
273+ AtomicReference <WindowBounds > windowBoundsAtomicReference = new AtomicReference <>();
274+ window .getBounds (result -> {
275+ windowBoundsAtomicReference .set (result );
276+ latch .countDown ();
277+ }, null );
278+ latch .await (5 , TimeUnit .SECONDS );
279+ assertEquals ("getBounds timeout" , latch .getCount (), 0 );
280+ return windowBoundsAtomicReference .get ();
281+ }
282+
232283}
0 commit comments