From 6e98f402bd3763bc5df6a30ea87d53cceb30154f Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Sun, 17 Apr 2016 22:44:42 -0400 Subject: [PATCH] Make stand-alone debugger SWTBot test more robust - Wait until binary file exists after building instead of sleep - Replace waiting loop with waitUntil methods with bigger timeouts - Replace menu detection with faster and more reliable method: - Find menu items in specific shell - Use waitUntil with small timeout to detect absent menu items Change-Id: I5239fa5dab9e091936addf6ceb9ef05095d23bd3 Signed-off-by: Marc-Andre Laperle --- .../application/tests/StandaloneTest.java | 64 +++++++-------- .../application/tests/StandaloneTest1.java | 82 ++++++++----------- 2 files changed, 63 insertions(+), 83 deletions(-) diff --git a/debug/org.eclipse.cdt.debug.application.tests/src/org/eclipse/cdt/debug/application/tests/StandaloneTest.java b/debug/org.eclipse.cdt.debug.application.tests/src/org/eclipse/cdt/debug/application/tests/StandaloneTest.java index 23544ac16b8..5947b4466a6 100644 --- a/debug/org.eclipse.cdt.debug.application.tests/src/org/eclipse/cdt/debug/application/tests/StandaloneTest.java +++ b/debug/org.eclipse.cdt.debug.application.tests/src/org/eclipse/cdt/debug/application/tests/StandaloneTest.java @@ -10,17 +10,19 @@ *******************************************************************************/ package org.eclipse.cdt.debug.application.tests; -import static org.junit.Assert.assertNotNull; - import org.eclipse.core.runtime.IPath; import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; import org.eclipse.swtbot.swt.finder.SWTBot; import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; +import org.eclipse.swtbot.swt.finder.waits.Conditions; +import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; import org.junit.After; public abstract class StandaloneTest { + private static final String C_C_STAND_ALONE_DEBUGGER_TITLE = "Eclipse C/C++ Stand-alone Debugger"; + private static final String DEBUG_NEW_EXECUTABLE_TITLE = "Debug New Executable"; protected static SWTBot bot; protected static String projectName; protected static SWTBotShell mainShell; @@ -28,45 +30,22 @@ public abstract class StandaloneTest { public static void init(String projectName) throws Exception { SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US"; + SWTBotPreferences.TIMEOUT = 20000; - Utilities.getDefault().buildProject(projectName); bot = new SWTBot(); + Utilities.getDefault().buildProject(projectName); + final IPath executablePath = Utilities.getDefault().getProjectPath(projectName).append("a.out"); // $NON-NLS-1$ + bot.waitUntil(new WaitForFileCondition(executablePath)); - SWTBotShell executableShell = null; - for (int i = 0, attempts = 100; i < attempts; i++) { - for (SWTBotShell shell : bot.shells()) { - if (shell.getText().contains("Debug New Executable")) { - executableShell = shell; - shell.setFocus(); - break; - } - } - if (executableShell != null) - break; - bot.sleep(10); - } - - IPath executablePath = Utilities.getDefault().getProjectPath(projectName).append("a.out"); // $NON-NLS-1$ + bot.waitUntil(Conditions.shellIsActive(DEBUG_NEW_EXECUTABLE_TITLE)); + SWTBotShell executableShell = bot.shell(DEBUG_NEW_EXECUTABLE_TITLE); + executableShell.setFocus(); executableShell.bot().textWithLabel("Binary: ").typeText(executablePath.toOSString()); - bot.sleep(2000); - executableShell.bot().button("OK").click(); - mainShell = null; - for (int i = 0, attempts = 100; i < attempts; i++) { - for (SWTBotShell shell : bot.shells()) { - if (shell.getText().contains("C/C++ Stand-alone Debugger")) { - mainShell = shell; - shell.setFocus(); - break; - } - } - if (mainShell != null) - break; - bot.sleep(10); - } - assertNotNull(mainShell); + bot.waitUntil(Conditions.shellIsActive(C_C_STAND_ALONE_DEBUGGER_TITLE)); + mainShell = bot.shell(C_C_STAND_ALONE_DEBUGGER_TITLE); } @After @@ -92,4 +71,21 @@ public abstract class StandaloneTest { // mainShell.activate(); } + private static final class WaitForFileCondition extends DefaultCondition { + private final IPath executablePath; + + private WaitForFileCondition(IPath executablePath) { + this.executablePath = executablePath; + } + + @Override + public boolean test() throws Exception { + return executablePath.toFile().exists(); + } + + @Override + public String getFailureMessage() { + return "Could not find executable after build."; + } + } } diff --git a/debug/org.eclipse.cdt.debug.application.tests/src/org/eclipse/cdt/debug/application/tests/StandaloneTest1.java b/debug/org.eclipse.cdt.debug.application.tests/src/org/eclipse/cdt/debug/application/tests/StandaloneTest1.java index 7ee5899ad5a..2fe1ecd2dfb 100644 --- a/debug/org.eclipse.cdt.debug.application.tests/src/org/eclipse/cdt/debug/application/tests/StandaloneTest1.java +++ b/debug/org.eclipse.cdt.debug.application.tests/src/org/eclipse/cdt/debug/application/tests/StandaloneTest1.java @@ -10,15 +10,20 @@ *******************************************************************************/ package org.eclipse.cdt.debug.application.tests; +import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import org.eclipse.core.runtime.IPath; -import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; +import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; +import org.eclipse.swtbot.swt.finder.waits.Conditions; +import org.eclipse.swtbot.swt.finder.waits.WaitForObjectCondition; import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu; import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; import org.eclipse.swtbot.swt.finder.widgets.SWTBotText; +import org.eclipse.swtbot.swt.finder.widgets.TimeoutException; +import org.hamcrest.Matcher; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -37,57 +42,25 @@ public class StandaloneTest1 extends StandaloneTest { @Test public void Test1() throws Exception { // Verify the top-level menus are there - SWTBotMenu fileMenu = bot.menu("File"); + SWTBotMenu fileMenu = mainShell.menu().menu("File"); assertNotNull(fileMenu); - SWTBotMenu editMenu = bot.menu("Edit"); + SWTBotMenu editMenu = mainShell.menu().menu("Edit"); assertNotNull(editMenu); - SWTBotMenu searchMenu = bot.menu("Search"); + SWTBotMenu searchMenu = mainShell.menu().menu("Search"); assertNotNull(searchMenu); - SWTBotMenu runMenu = bot.menu("Run"); + SWTBotMenu runMenu = mainShell.menu().menu("Run"); assertNotNull(runMenu); - SWTBotMenu windowMenu = bot.menu("Window"); + SWTBotMenu windowMenu = mainShell.menu().menu("Window"); assertNotNull(windowMenu); - SWTBotMenu helpMenu = bot.menu("Help"); + SWTBotMenu helpMenu = mainShell.menu().menu("Help"); assertNotNull(helpMenu); // Verify other common top-level menus are not there - SWTBotMenu notThere = null; - try { - notThere = bot.menu("Navigate"); - } catch (WidgetNotFoundException e) { - // correct - } - assertNull(notThere); - try { - notThere = bot.menu("Refactor"); - } catch (WidgetNotFoundException e) { - // correct - } - assertNull(notThere); - try { - notThere = bot.menu("Source"); - } catch (WidgetNotFoundException e) { - // correct - } - assertNull(notThere); - try { - notThere = bot.menu("Target"); - } catch (WidgetNotFoundException e) { - // correct - } - assertNull(notThere); - try { - // We want to prove there isn't a top-level Project menu - // There happens to be a lower-level Project menu from the Text menu - // Verify we find it, but no other menus named Project - SWTBotMenu textMenu = bot.menu("Text"); - @SuppressWarnings("unused") - SWTBotMenu projectMenu = textMenu.menu("Project"); - notThere = bot.menu("Project", 1); - } catch (WidgetNotFoundException e) { - // correct - } - assertNull(notThere); + assertMenuAbsent(mainShell, "Navigate"); + assertMenuAbsent(mainShell, "Refactor"); + assertMenuAbsent(mainShell, "Source"); + assertMenuAbsent(mainShell, "Target"); + assertMenuAbsent(mainShell, "Project"); SWTBotMenu attachExecutableDialog = fileMenu.menu("Debug Attached Executable..."); assertNotNull(attachExecutableDialog); @@ -104,7 +77,7 @@ public class StandaloneTest1 extends StandaloneTest { shell.bot().textWithLabel("Arguments: ").setText("1 2 3"); bot.sleep(2000); - bot.button("OK").click(); + shell.bot().button("OK").click(); bot.sleep(1000); @@ -121,10 +94,10 @@ public class StandaloneTest1 extends StandaloneTest { bot.sleep(2000); - bot.button("Cancel").click(); + shell.bot().button("Cancel").click(); bot.sleep(2000); - + SWTBotMenu exitMenu = fileMenu.menu("Exit"); assertNotNull(exitMenu); exitMenu.click(); @@ -135,5 +108,16 @@ public class StandaloneTest1 extends StandaloneTest { bot.sleep(1000); } - + private void assertMenuAbsent(SWTBotShell shell, String menuText) { + boolean found = false; + try { + final Matcher matcher = withMnemonic(menuText); + WaitForObjectCondition waitForMenuItem = Conditions.waitForMenuItem(shell.menu(), matcher, false, 0); + bot.waitUntil(waitForMenuItem, 50); + found = true; + } catch (TimeoutException e) { + // correct + } + assertFalse(found); + } }