From 5664514d004573b98c448f3f06aac6cbe6247e46 Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Mon, 24 Nov 2014 11:30:04 -0500 Subject: [PATCH] swtbot: Wait for the presence of a text pattern in the console view Instead of relying only on a sleep, wait until the specified console view has the specified text pattern. Change-Id: I196ac1f1c04212c536b9bd5917156f24158df469 Signed-off-by: Marc-Andre Laperle --- .../cdt/autotools/ui/tests/AbstractTest.java | 37 ++++++++++ .../ui/tests/TestEnvironmentVars.java | 11 +-- .../autotools/ui/tests/TestMakeTargets.java | 11 +-- .../autotools/ui/tests/TestToolActions.java | 70 ++++--------------- 4 files changed, 55 insertions(+), 74 deletions(-) diff --git a/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/AbstractTest.java b/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/AbstractTest.java index d5d1e866e38..6c75d099f04 100644 --- a/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/AbstractTest.java +++ b/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/AbstractTest.java @@ -20,6 +20,8 @@ import static org.eclipse.swtbot.swt.finder.waits.Conditions.widgetIsEnabled; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.regex.Pattern; + import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectNature; import org.eclipse.core.resources.IWorkspace; @@ -38,6 +40,7 @@ import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.Widget; import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; +import org.eclipse.swtbot.swt.finder.SWTBot; import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; import org.eclipse.swtbot.swt.finder.finders.ContextMenuHelper; import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; @@ -46,6 +49,7 @@ import org.eclipse.swtbot.swt.finder.results.VoidResult; 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.waits.ICondition; import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot; import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox; import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu; @@ -397,4 +401,37 @@ public abstract class AbstractTest { bot.closeAllEditors(); mainShell.activate(); } + + protected ICondition consoleTextMatches(SWTBotView consoleView, Pattern pattern) { + return new ConsoleTextMatches(consoleView, pattern); + } + + protected class ConsoleTextMatches implements ICondition { + private final SWTBotView view; + private Pattern pattern; + + public ConsoleTextMatches(SWTBotView view, Pattern pattern) { + this.view = view; + this.pattern = pattern; + } + + @Override + public boolean test() throws Exception { + if (view.isActive()) { + String output = view.bot().styledText().getText(); + java.util.regex.Matcher m = pattern.matcher(output); + return m.matches(); + } + return false; + } + + @Override + public void init(SWTBot bot) { + } + + @Override + public String getFailureMessage() { + return null; + } + } } diff --git a/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestEnvironmentVars.java b/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestEnvironmentVars.java index 1e9ca4d01d6..34e2209820a 100644 --- a/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestEnvironmentVars.java +++ b/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestEnvironmentVars.java @@ -107,14 +107,10 @@ public class TestEnvironmentVars extends AbstractTest { } } assertTrue(f.exists()); - bot.sleep(1000); SWTBotView consoleView = viewConsole("Configure"); - String output = consoleView.bot().styledText().getText(); Pattern p = Pattern.compile(".*--enable-somevar.*", Pattern.DOTALL); - Matcher m = p.matcher(output); // We should see the expanded some_var variable in the console - assertTrue(m.matches()); - + bot.waitUntil(consoleTextMatches(consoleView, p)); setEnvVarOnCommandLine(); } @@ -157,15 +153,12 @@ public class TestEnvironmentVars extends AbstractTest { clickContextMenu(projectExplorer.bot().tree().select(projectName), "Reconfigure Project"); focusMainShell(); - bot.sleep(3000); SWTBotView consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - String output = consoleView.bot().styledText().getText(); Pattern p = Pattern.compile( ".*a boat.*a train.*car.*a wagon.*a plane.*skates.*", Pattern.DOTALL); - Matcher m = p.matcher(output); - assertTrue(m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); } } \ No newline at end of file diff --git a/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestMakeTargets.java b/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestMakeTargets.java index 5ec9b9c1413..8a9d2baf057 100644 --- a/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestMakeTargets.java +++ b/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestMakeTargets.java @@ -13,7 +13,6 @@ package org.eclipse.cdt.autotools.ui.tests; import static org.junit.Assert.assertTrue; import java.io.File; -import java.util.regex.Matcher; import java.util.regex.Pattern; import org.eclipse.core.resources.IProject; @@ -63,13 +62,10 @@ public class TestMakeTargets extends AbstractTest { shell.activate(); bot.table().getTableItem("info").select(); bot.button("Build").click(); - bot.sleep(3000); SWTBotView consoleView = viewConsole("CDT Build Console"); - String output = consoleView.bot().styledText().getText(); Pattern p = Pattern.compile(".*make info.*", Pattern.DOTALL); - Matcher m = p.matcher(output); - assertTrue(m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); // Make Targets using right-click on project. clickProjectContextMenu("Make Targets", "Build..."); @@ -77,14 +73,11 @@ public class TestMakeTargets extends AbstractTest { shell.activate(); bot.table().getTableItem("check").select(); bot.button("Build").click(); - bot.sleep(3000); consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - output = consoleView.bot().styledText().getText(); p = Pattern.compile(".*make check.*Making check in src.*", Pattern.DOTALL); - m = p.matcher(output); - assertTrue(m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); } } diff --git a/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestToolActions.java b/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestToolActions.java index f0ddf311daf..01a68d63bdf 100644 --- a/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestToolActions.java +++ b/build/org.eclipse.cdt.autotools.ui.tests/src/org/eclipse/cdt/autotools/ui/tests/TestToolActions.java @@ -13,7 +13,6 @@ package org.eclipse.cdt.autotools.ui.tests; import static org.junit.Assert.assertTrue; import java.io.File; -import java.util.regex.Matcher; import java.util.regex.Pattern; import org.eclipse.core.runtime.IPath; @@ -67,12 +66,10 @@ public class TestToolActions extends AbstractTest { bot.sleep(1000); SWTBotView consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - String output = consoleView.bot().styledText().getText(); // Verify we got some help output to the console Pattern p = Pattern.compile(".*Invoking aclocal in.*" + projectName + ".*aclocal --help.*Usage: aclocal.*", Pattern.DOTALL); - Matcher m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); // Verify we still don't have an aclocal.m4 file yet f = new File(path.toOSString()); assertTrue(!f.exists()); @@ -82,14 +79,11 @@ public class TestToolActions extends AbstractTest { shell = bot.shell("Aclocal Options"); shell.activate(); bot.button("OK").click(); - bot.sleep(1000); consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - output = consoleView.bot().styledText().getText(); p = Pattern.compile(".*Invoking aclocal in.*" + projectName + ".*aclocal.*", Pattern.DOTALL); - m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); // We need to wait until the aclocal.m4 file is created so // sleep a bit and look for it...give up after 20 seconds for (int i = 0; i < 40; ++i) { @@ -114,15 +108,12 @@ public class TestToolActions extends AbstractTest { f.delete(); } clickProjectContextMenu("Invoke Autotools", "Invoke Autoconf"); - bot.sleep(1000); SWTBotView consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - String output = consoleView.bot().styledText().getText(); // Verify we got some help output to the console Pattern p = Pattern.compile(".*Invoking autoconf in.*" + projectName + ".*autoconf.*", Pattern.DOTALL); - Matcher m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); // We need to wait until the configure file is created so // sleep a bit and look for it...give up after 20 seconds for (int i = 0; i < 40; ++i) { @@ -142,14 +133,11 @@ public class TestToolActions extends AbstractTest { clickVolatileContextMenu( projectExplorer.bot().tree().select("configure.ac"), "Invoke Autotools", "Invoke Autoconf"); - bot.sleep(1000); consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - output = consoleView.bot().styledText().getText(); p = Pattern.compile(".*Invoking autoconf in.*" + projectName + ".*autoconf.*", Pattern.DOTALL); - m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); // We need to wait until the configure file is created so // sleep a bit and look for it...give up after 20 seconds for (int i = 0; i < 40; ++i) { @@ -186,15 +174,12 @@ public class TestToolActions extends AbstractTest { shell.activate(); bot.text(0).typeText("--help"); bot.button("OK").click(); - bot.sleep(1000); SWTBotView consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - String output = consoleView.bot().styledText().getText(); // Verify we got some help output to the console Pattern p = Pattern.compile(".*Invoking automake in.*" + projectName + ".*automake --help.*Usage:.*", Pattern.DOTALL); - Matcher m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); // Verify we still don't have Makefile.in files yet f = new File(path.toOSString()); assertTrue(!f.exists()); @@ -210,15 +195,12 @@ public class TestToolActions extends AbstractTest { // here bot.text(1).typeText("Makefile src/Makefile"); bot.button("OK").click(); - bot.sleep(2000); consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - output = consoleView.bot().styledText().getText(); p = Pattern.compile(".*Invoking automake in.*" + projectName + ".*automake --add-missing Makefile src/Makefile.*", Pattern.DOTALL); - m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); // We need to wait until the Makefile.in files are created so // sleep a bit and look for it...give up after 20 seconds for (int i = 0; i < 40; ++i) { @@ -245,15 +227,12 @@ public class TestToolActions extends AbstractTest { shell.activate(); bot.text(0).typeText("--help"); bot.button("OK").click(); - bot.sleep(1000); SWTBotView consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - String output = consoleView.bot().styledText().getText(); // Verify we got some help output to the console Pattern p = Pattern.compile(".*Invoking libtoolize in.*" + projectName + ".*libtoolize --help.*Usage: libtoolize.*", Pattern.DOTALL); - Matcher m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); } // Verify we can access the libtoolize tool @@ -264,15 +243,12 @@ public class TestToolActions extends AbstractTest { shell.activate(); bot.text(0).typeText("--help"); bot.button("OK").click(); - bot.sleep(2000); SWTBotView consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - String output = consoleView.bot().styledText().getText(); // Verify we got some help output to the console Pattern p = Pattern.compile(".*Invoking autoheader in.*" + projectName + ".*autoheader --help.*Usage:.*autoheader.*", Pattern.DOTALL); - Matcher m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); } // Verify we can access the autoreconf tool @@ -305,15 +281,12 @@ public class TestToolActions extends AbstractTest { shell.activate(); bot.text(0).typeText("--help"); bot.button("OK").click(); - bot.sleep(1000); SWTBotView consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - String output = consoleView.bot().styledText().getText(); // Verify we got some help output to the console Pattern p = Pattern.compile(".*Invoking autoreconf in.*" + projectName + ".*autoreconf --help.*Usage: .*autoreconf.*", Pattern.DOTALL); - Matcher m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); clickProjectContextMenu("Invoke Autotools", "Invoke Autoreconf"); shell = bot.shell("Autoreconf Options"); shell.activate(); @@ -454,75 +427,60 @@ public class TestToolActions extends AbstractTest { shell.activate(); bot.text(0).typeText("--help"); bot.button("OK").click(); - bot.sleep(1000); SWTBotView consoleView = viewConsole("Autotools"); consoleView.setFocus(); - String output = consoleView.bot().styledText().getText(); // Verify we got some help output to the console Pattern p = Pattern.compile(".*Invoking aclocal in.*" + projectName + ".*automake --help.*Usage:.*automake.*", Pattern.DOTALL); - Matcher m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); clickProjectContextMenu("Invoke Autotools", "Invoke Automake"); shell = bot.shell("Automake Options"); shell.activate(); bot.text(0).typeText("--help"); bot.button("OK").click(); - bot.sleep(1000); consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - output = consoleView.bot().styledText().getText(); // Verify we got some help output to the console p = Pattern.compile(".*Invoking automake in.*" + projectName + ".*autoconf --help.*Usage:.*autoconf.*", Pattern.DOTALL); - m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); clickProjectContextMenu("Invoke Autotools", "Invoke Autoheader"); shell = bot.shell("Autoheader Options"); shell.activate(); bot.text(0).typeText("--help"); bot.button("OK").click(); - bot.sleep(1000); consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - output = consoleView.bot().styledText().getText(); // Verify we got some help output to the console p = Pattern.compile(".*Invoking autoheader in.*" + projectName + ".*autoreconf --help.*Usage:.*autoreconf.*", Pattern.DOTALL); - m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); clickProjectContextMenu("Invoke Autotools", "Invoke Autoreconf"); shell = bot.shell("Autoreconf Options"); shell.activate(); bot.text(0).typeText("--help"); bot.button("OK").click(); - bot.sleep(1000); consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - output = consoleView.bot().styledText().getText(); // Verify we got some help output to the console p = Pattern.compile(".*Invoking autoreconf in.*" + projectName + ".*libtoolize --help.*Usage:.*libtoolize.*", Pattern.DOTALL); - m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); clickProjectContextMenu("Invoke Autotools", "Invoke Libtoolize"); shell = bot.shell("Libtoolize Options"); shell.activate(); bot.text(0).typeText("--help"); bot.button("OK").click(); - bot.sleep(1000); consoleView = bot.viewByPartName("Console"); consoleView.setFocus(); - output = consoleView.bot().styledText().getText(); // Verify we got some help output to the console p = Pattern.compile(".*Invoking libtoolize in.*" + projectName + ".*aclocal --help.*Usage:.*aclocal.*", Pattern.DOTALL); - m = p.matcher(output); - assertTrue("Got: " + output, m.matches()); + bot.waitUntil(consoleTextMatches(consoleView, p)); } }