Skip to content

Commit 364b49e

Browse files
committed
Updates 1
1 parent 5e884b9 commit 364b49e

3 files changed

Lines changed: 58 additions & 9 deletions

File tree

tests/src/main/java/io/openliberty/tools/eclipse/test/it/utils/MagicWidgetFinder.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.eclipse.swt.widgets.Composite;
4545
import org.eclipse.swt.widgets.Control;
4646
import org.eclipse.swt.widgets.Display;
47+
import org.eclipse.swt.widgets.Event;
4748
import org.eclipse.swt.widgets.Group;
4849
import org.eclipse.swt.widgets.Item;
4950
import org.eclipse.swt.widgets.Label;
@@ -222,18 +223,32 @@ public static void context(Object widget, Object... args) {
222223

223224
Throwable thrown = null;
224225
SWTBotMenu currMenu = null;
226+
boolean firstAttempt = true;
225227
while (System.nanoTime() < expireTimeInNanos && currMenu == null) {
226228

227229
AbstractSWTBot<?> obj = null;
228230
if (widget instanceof TreeItem) {
229-
230231
obj = new SWTBotTreeItem((TreeItem) widget);
231232
} else if (widget instanceof TableItem) {
232233
obj = new SWTBotTableItem((TableItem) widget);
233234
} else {
234235
throw new RuntimeException("Unable to identify context object type: " + widget.getClass().getName());
235236
}
236237

238+
if (!firstAttempt) {
239+
// On retry: dismiss any stale context menu that may still be open, then
240+
// re-select/focus the widget so Eclipse can repopulate dynamic contributions
241+
// (e.g. "Run As" / "Debug As") before we open the menu again.
242+
dismissOpenContextMenu();
243+
if (widget instanceof TreeItem) {
244+
SWTBotTreeItem ti = (SWTBotTreeItem) obj;
245+
ti.select();
246+
ti.setFocus();
247+
}
248+
pause(2000);
249+
}
250+
firstAttempt = false;
251+
237252
try {
238253
for (int x = 0; x < args.length; x++) {
239254
if (x == 0) {
@@ -252,8 +267,7 @@ public static void context(Object widget, Object... args) {
252267
} catch (Throwable t) {
253268
thrown = t;
254269
currMenu = null;
255-
System.out.println("waiting for context menu.");
256-
pause(2000);
270+
System.out.println("waiting for context menu: " + t.getMessage());
257271
}
258272
} // end-while
259273

@@ -266,6 +280,36 @@ public static void context(Object widget, Object... args) {
266280
}
267281
}
268282

283+
/**
284+
* Dismisses any currently open context menu by sending an Escape key on the
285+
* active display.
286+
*/
287+
private static void dismissOpenContextMenu() {
288+
try {
289+
Display.getDefault().syncExec(new Runnable() {
290+
@Override
291+
public void run() {
292+
// Post an Escape key event to close any open popup/context menu.
293+
Event escDown = new Event();
294+
escDown.type = SWT.KeyDown;
295+
escDown.keyCode = SWT.ESC;
296+
Display.getDefault().post(escDown);
297+
298+
Event escUp = new Event();
299+
escUp.type = SWT.KeyUp;
300+
escUp.keyCode = SWT.ESC;
301+
Display.getDefault().post(escUp);
302+
}
303+
});
304+
// Give the UI thread time to process the Escape and close the menu.
305+
pause(300);
306+
} catch (Throwable t) {
307+
// Non-fatal: if we cannot dismiss the menu, the next contextMenu() call
308+
// will still attempt to open a fresh one.
309+
System.out.println("Could not dismiss open context menu: " + t.getMessage());
310+
}
311+
}
312+
269313
public static Object get(Object w) {
270314
return get(w, Option.getGlobalOptions());
271315

tests/src/main/java/io/openliberty/tools/eclipse/test/it/utils/SWTBotPluginOperations.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,14 @@ public static SWTBotMenu getDebuggerConnectMenuForDebugObject(Object debugObject
139139

140140
SWTBotTreeItem obj = new SWTBotTreeItem((TreeItem) debugObject);
141141

142-
// Ensure the tree item is properly selected and focused before accessing context menu.
143-
// This is critical for headless CI environments where context menus can hang.
142+
// Select and focus the item so the workbench selection service has it as the
143+
// active selection. LibertyDebugReconnectHandler.isEnabled() reads the selection
144+
// service, not the object passed here, so the selection must be current before
145+
// the context menu is opened.
146+
// Note: do NOT wait for isEnabled() here — callers may legitimately be checking
147+
// that the menu is *disabled*, and waiting for enabled would mask that state.
144148
obj.select();
145149
obj.setFocus();
146-
SWTBotTestCondition.waitFor(obj::isEnabled, SWTBotTestCondition.MIN_WAIT_MS);
147150

148151
return obj.contextMenu("Connect Liberty Debugger");
149152
}
@@ -216,8 +219,8 @@ public static void terminateLaunch() {
216219
SWTBotTestCondition.waitFor(() -> !isShellVisible("Terminate and Remove"), SWTBotTestCondition.MIN_WAIT_MS);
217220
}
218221

219-
// Validate that the search object is gone.
220-
boolean stillPresent = isObjectInDebugView("[Liberty]");
222+
boolean stillPresent = !SWTBotTestCondition.waitFor(
223+
() -> !isObjectInDebugView(searchObjectName), SWTBotTestCondition.MIN_WAIT_MS);
221224
org.junit.jupiter.api.Assertions.assertFalse(stillPresent,
222225
"Liberty launch was not removed from the Debug view after termination.");
223226
} else {
@@ -976,6 +979,8 @@ public static Object getAppInPackageExplorerTree(String appName) {
976979
// Wait until the tree item is enabled before returning.
977980
SWTBotTreeItem botItem = new SWTBotTreeItem((TreeItem) project);
978981
SWTBotTestCondition.waitFor(botItem::isEnabled, SWTBotTestCondition.SHORT_WAIT_MS);
982+
botItem.select();
983+
botItem.setFocus();
979984

980985
return project;
981986
}

tests/src/main/java/io/openliberty/tools/eclipse/test/it/utils/SWTBotTestCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class SWTBotTestCondition {
4040
public static final int SHORT_WAIT_MS = 15000;
4141
public static final int MID_WAIT_MS = 30000;
4242
public static final int LARGE_WAIT_MS = 60000;
43-
public static final int SERVER_WAIT_MS = 120000;
43+
public static final int SERVER_WAIT_MS = 180000;
4444

4545
/**
4646
* Polls the input condition every {@value #POLL_INTERVAL_MS} ms until it returns

0 commit comments

Comments
 (0)