swtbot: Make SWTBotTimeGraph/Entry.getEntry() wait for entry
authorPatrick Tasse <patrick.tasse@gmail.com>
Tue, 2 May 2017 17:39:11 +0000 (13:39 -0400)
committerPatrick Tasse <patrick.tasse@gmail.com>
Tue, 2 May 2017 19:25:32 +0000 (15:25 -0400)
These methods currently try only once to find the entry but output
"Timed out waiting for..." when it fails.

To match the failure message, and for increased test robustness, make
the methods actually use a wait condition.

Change-Id: Ic1c797b4fa59e44179f0af03fdbb7ae6a27b23bf
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/96240
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Hudson CI
tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotTimeGraph.java
tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotTimeGraphEntry.java

index ddb2441c6dbf1320ea2ab1b748a07552b46600be..977429e63dcb9d1292e48a89bb1e60b259db7ca9 100644 (file)
@@ -16,6 +16,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
+import java.util.concurrent.atomic.AtomicReference;
 
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.ITableLabelProvider;
@@ -27,10 +28,12 @@ import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 import org.eclipse.swtbot.swt.finder.matchers.WidgetOfType;
 import org.eclipse.swtbot.swt.finder.results.ArrayResult;
 import org.eclipse.swtbot.swt.finder.results.IntResult;
+import org.eclipse.swtbot.swt.finder.results.ListResult;
 import org.eclipse.swtbot.swt.finder.results.Result;
 import org.eclipse.swtbot.swt.finder.utils.TableCollection;
 import org.eclipse.swtbot.swt.finder.utils.TableRow;
 import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl;
+import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphControl;
 
@@ -92,24 +95,34 @@ public class SWTBotTimeGraph extends AbstractSWTBotControl<TimeGraphControl> {
      *             if the entry was not found.
      */
     public SWTBotTimeGraphEntry getEntry(String... names) throws WidgetNotFoundException {
-        List<ITimeGraphEntry> entries = Arrays.asList(widget.getExpandedElements());
-        ITableLabelProvider labelProvider = widget.getLabelProvider();
-        ITimeGraphEntry parent = null;
-        for (String name : names) {
-            boolean found = false;
-            for (ITimeGraphEntry entry : entries) {
-                String label = labelProvider == null ? entry.getName() : labelProvider.getColumnText(entry, 0);
-                if (Objects.equals(entry.getParent(), parent) && name.equals(label)) {
-                    parent = entry;
-                    found = true;
-                    break;
+        AtomicReference<ITimeGraphEntry> parent = new AtomicReference<>();
+        AtomicReference<String> missing = new AtomicReference<>();
+        WaitUtils.waitUntil(timegraph -> {
+            List<ITimeGraphEntry> entries = syncExec(new ListResult<ITimeGraphEntry>() {
+                @Override
+                public List<ITimeGraphEntry> run() {
+                    return Arrays.asList(timegraph.getExpandedElements());
+                }
+            });
+            ITableLabelProvider labelProvider = timegraph.getLabelProvider();
+            for (String name : names) {
+                boolean found = false;
+                for (ITimeGraphEntry entry : entries) {
+                    String label = labelProvider == null ? entry.getName() : labelProvider.getColumnText(entry, 0);
+                    if (Objects.equals(entry.getParent(), parent.get()) && name.equals(label)) {
+                        parent.set(entry);
+                        found = true;
+                        break;
+                    }
+                }
+                if (!found) {
+                    missing.set(name);
+                    return false;
                 }
             }
-            if (!found) {
-                throw new WidgetNotFoundException("Timed out waiting for time graph entry " + name); //$NON-NLS-1$
-            }
-        }
-        return new SWTBotTimeGraphEntry(widget, parent);
+            return true;
+        }, widget, "Timed out waiting for time graph entry " + missing.get());
+        return new SWTBotTimeGraphEntry(widget, parent.get());
     }
 
     /**
index 9df541df4672e156b173327f23a6b2a0f2dd11cf..65b9a748f68dfc5c7b36cd1cf4214cd0dd5558ec 100644 (file)
@@ -13,7 +13,9 @@
 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
 
 import org.eclipse.jface.viewers.ITableLabelProvider;
 import org.eclipse.swt.SWT;
@@ -26,12 +28,14 @@ import org.eclipse.swtbot.swt.finder.SWTBot;
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 import org.eclipse.swtbot.swt.finder.results.ArrayResult;
+import org.eclipse.swtbot.swt.finder.results.ListResult;
 import org.eclipse.swtbot.swt.finder.results.Result;
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 import org.eclipse.swtbot.swt.finder.waits.Conditions;
 import org.eclipse.swtbot.swt.finder.waits.WaitForObjectCondition;
 import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl;
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotRootMenu;
+import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.ITimeDataProvider;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphControl;
@@ -156,21 +160,27 @@ public class SWTBotTimeGraphEntry extends AbstractSWTBotControl<TimeGraphControl
      * @return the child entry
      */
     public SWTBotTimeGraphEntry getEntry(String name) {
-        return syncExec(new Result<SWTBotTimeGraphEntry>() {
-            @Override
-            public SWTBotTimeGraphEntry run() {
-                ITableLabelProvider labelProvider = widget.getLabelProvider();
-                for (ITimeGraphEntry entry : widget.getExpandedElements()) {
-                    if (fEntry.equals(entry.getParent())) {
-                        String label = labelProvider == null ? entry.getName() : labelProvider.getColumnText(entry, 0);
-                        if (name.equals(label)) {
-                            return new SWTBotTimeGraphEntry(widget, entry);
-                        }
+        AtomicReference<ITimeGraphEntry> found = new AtomicReference<>();
+        WaitUtils.waitUntil(timegraph -> {
+            List<ITimeGraphEntry> entries = syncExec(new ListResult<ITimeGraphEntry>() {
+                @Override
+                public List<ITimeGraphEntry> run() {
+                    return Arrays.asList(timegraph.getExpandedElements());
+                }
+            });
+            ITableLabelProvider labelProvider = timegraph.getLabelProvider();
+            for (ITimeGraphEntry entry : entries) {
+                if (fEntry.equals(entry.getParent())) {
+                    String label = labelProvider == null ? entry.getName() : labelProvider.getColumnText(entry, 0);
+                    if (name.equals(label)) {
+                        found.set(entry);
+                        return true;
                     }
                 }
-                throw new WidgetNotFoundException("Timed out waiting for time graph entry " + name); //$NON-NLS-1$
             }
-        });
+            return false;
+        }, widget, "Timed out waiting for time graph entry " + name);
+        return new SWTBotTimeGraphEntry(widget, found.get());
     }
 
     /**
This page took 0.026843 seconds and 5 git commands to generate.