3535import clipboard .ClipboardCommands ;
3636
3737public class RemoteClipboard implements ClipboardCommands {
38+ /**
39+ * Set to true to manually launch the ClipboardTest. This is useful so you can
40+ * debug the Swing side.
41+ */
42+ private static boolean DEBUG_REMOTE = false ;
3843 private ClipboardCommands remote ;
3944 private Process remoteClipboardProcess ;
4045 private Path remoteClipboardTempDir ;
4146
4247 public void start () throws Exception {
4348 assertNull (remote , "Create a new instance to restart" );
49+
50+ int port = DEBUG_REMOTE ? ClipboardCommands .DEFAULT_PORT : launchRemote ();
51+ try {
52+ Registry reg = LocateRegistry .getRegistry ("127.0.0.1" , port );
53+ long stopTime = System .currentTimeMillis () + 10000 ;
54+ do {
55+ try {
56+ remote = (ClipboardCommands ) reg .lookup (ClipboardCommands .ID );
57+ break ;
58+ } catch (NotBoundException e ) {
59+ // try again because the remote app probably hasn't bound yet
60+ }
61+ } while (System .currentTimeMillis () < stopTime );
62+ } catch (RemoteException e ) {
63+
64+ Integer exitValue = null ;
65+ boolean waitFor = false ;
66+ try {
67+ waitFor = remoteClipboardProcess .waitFor (5 , TimeUnit .SECONDS );
68+ if (waitFor ) {
69+ exitValue = remoteClipboardProcess .exitValue ();
70+ }
71+ } catch (InterruptedException e1 ) {
72+ Thread .interrupted ();
73+ }
74+
75+ String message = "Failed to get remote clipboards command, this seems to happen on macOS on I-build tests. Exception: "
76+ + e .toString () + " waitFor: " + waitFor + " exitValue: " + exitValue ;
77+
78+ // Give some diagnostic information to help track down why this fails on build
79+ // machine. We only hard error on Linux, for other platforms we allow test to
80+ // just be skipped until we track down what is causing
81+ // https://github.com/eclipse-platform/eclipse.platform.swt/issues/2553
82+ assumeTrue (SwtTestUtil .isGTK , message );
83+ throw new RuntimeException (message , e );
84+ }
85+ assertNotNull (remote );
86+
87+ // Run a no-op on the Swing event loop so that we know it is idle
88+ // and we can continue startup
89+ remote .waitUntilReady ();
90+ remote .setFocus ();
91+ remote .waitUntilReady ();
92+ }
93+
94+ private int launchRemote () throws IOException {
4495 /*
4596 * The below copy using getPath may be redundant (i.e. it may be possible to run
4697 * the class files from where they currently reside in the bin folder or the
@@ -66,7 +117,7 @@ public void start() throws Exception {
66117 String javaExe = javaHome + "/bin/java" + (SwtTestUtil .isWindowsOS ? ".exe" : "" );
67118 assertTrue (Files .exists (Path .of (javaExe )));
68119
69- ProcessBuilder pb = new ProcessBuilder (javaExe , "clipboard.ClipboardTest" )
120+ ProcessBuilder pb = new ProcessBuilder (javaExe , "clipboard.ClipboardTest" , "-autoport" )
70121 .directory (remoteClipboardTempDir .toFile ());
71122 pb .inheritIO ();
72123 pb .redirectOutput (Redirect .PIPE );
@@ -85,47 +136,7 @@ public void start() throws Exception {
85136 throw new RuntimeException ("Failed to get port" );
86137 });
87138 assertNotEquals (0 , port );
88- try {
89- Registry reg = LocateRegistry .getRegistry ("127.0.0.1" , port );
90- long stopTime = System .currentTimeMillis () + 10000 ;
91- do {
92- try {
93- remote = (ClipboardCommands ) reg .lookup (ClipboardCommands .ID );
94- break ;
95- } catch (NotBoundException e ) {
96- // try again because the remote app probably hasn't bound yet
97- }
98- } while (System .currentTimeMillis () < stopTime );
99- } catch (RemoteException e ) {
100-
101- Integer exitValue = null ;
102- boolean waitFor = false ;
103- try {
104- waitFor = remoteClipboardProcess .waitFor (5 , TimeUnit .SECONDS );
105- if (waitFor ) {
106- exitValue = remoteClipboardProcess .exitValue ();
107- }
108- } catch (InterruptedException e1 ) {
109- Thread .interrupted ();
110- }
111-
112- String message = "Failed to get remote clipboards command, this seems to happen on macOS on I-build tests. Exception: "
113- + e .toString () + " waitFor: " + waitFor + " exitValue: " + exitValue ;
114-
115- // Give some diagnostic information to help track down why this fails on build
116- // machine. We only hard error on Linux, for other platforms we allow test to
117- // just be skipped until we track down what is causing
118- // https://github.com/eclipse-platform/eclipse.platform.swt/issues/2553
119- assumeTrue (SwtTestUtil .isGTK , message );
120- throw new RuntimeException (message , e );
121- }
122- assertNotNull (remote );
123-
124- // Run a no-op on the Swing event loop so that we know it is idle
125- // and we can continue startup
126- remote .waitUntilReady ();
127- remote .setFocus ();
128- remote .waitUntilReady ();
139+ return port ;
129140 }
130141
131142 @ Override
@@ -207,7 +218,7 @@ public void waitUntilReady() throws RemoteException {
207218 }
208219
209220 @ Override
210- public void waitForButtonPress () throws RemoteException {
221+ public void waitForButtonPress () throws RemoteException {
211222 remote .waitForButtonPress ();
212223 }
213224}
0 commit comments