swtbot: Help stabilize SWTChart tests by waiting for number of series
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / ConditionHelpers.java
index 00a67656442ac297723be4186e7b2d85ab777fa7..07f4aa0a68734cf92a0666f7ad852be6f078970e 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2013, 2015 Ericsson
+ * Copyright (c) 2013, 2016 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -17,6 +17,7 @@ package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared;
 import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName;
 
 import java.util.Arrays;
+import java.util.List;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jface.viewers.StructuredSelection;
@@ -25,6 +26,7 @@ import org.eclipse.jface.wizard.IWizardPage;
 import org.eclipse.jface.wizard.Wizard;
 import org.eclipse.osgi.util.NLS;
 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
+import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory;
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
 import org.eclipse.swtbot.swt.finder.SWTBot;
@@ -39,11 +41,13 @@ import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
 import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractTimeGraphView;
 import org.eclipse.ui.IEditorReference;
 import org.hamcrest.Matcher;
+import org.swtchart.Chart;
 
 /**
  * Is a tree node available
@@ -100,7 +104,7 @@ public final class ConditionHelpers {
 
             @Override
             public String getFailureMessage() {
-                return NLS.bind("No child of tree {0} found with text '{1}'. Child items: {2}",
+                return NLS.bind("No child of tree {0} found with text {1}. Child items: {2}",
                         new String[] { tree.toString(), name, Arrays.toString(tree.getAllItems()) });
             }
         };
@@ -189,6 +193,31 @@ public final class ConditionHelpers {
         };
     }
 
+    /**
+     * Condition to check if the number of direct children of the
+     * provided tree item equals the specified count.
+     *
+     * @param treeItem
+     *            the SWTBot tree item
+     * @param count
+     *            the expected count
+     * @return ICondition for verification
+     */
+    public static ICondition treeItemCount(final SWTBotTreeItem treeItem, int count) {
+        return new SWTBotTestCondition() {
+            @Override
+            public boolean test() throws Exception {
+                return treeItem.rowCount() == count;
+            }
+
+            @Override
+            public String getFailureMessage() {
+                return NLS.bind("Tree item count: {0} expected: {1}",
+                        treeItem.rowCount(), count);
+            }
+        };
+    }
+
     /**
      * Checks if the wizard's shell is null
      *
@@ -273,7 +302,7 @@ public final class ConditionHelpers {
                     if( cell == null ) {
                         return false;
                     }
-                    return cell.endsWith(content);
+                    return cell.contains(content);
                 } catch (Exception e) {
                 }
                 return false;
@@ -489,6 +518,7 @@ public final class ConditionHelpers {
         private @NonNull TmfTimeRange fSelectionRange;
         private @NonNull ITmfTimestamp fVisibleTime;
         private AbstractTimeGraphView fView;
+        private String fFailureMessage;
 
         private TimeGraphIsReadyCondition(AbstractTimeGraphView view, @NonNull TmfTimeRange selectionRange, @NonNull ITmfTimestamp visibleTime) {
             fView = view;
@@ -498,18 +528,28 @@ public final class ConditionHelpers {
 
         @Override
         public boolean test() throws Exception {
-            if (!ConditionHelpers.selectionRange(fSelectionRange).test()) {
+            ICondition selectionRangeCondition = ConditionHelpers.selectionRange(fSelectionRange);
+            if (!selectionRangeCondition.test()) {
+                fFailureMessage = selectionRangeCondition.getFailureMessage();
                 return false;
             }
-            if (!TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange().contains(fVisibleTime)) {
+            @NonNull TmfTimeRange curWindowRange = TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange();
+            if (!curWindowRange.contains(fVisibleTime)) {
+                fFailureMessage = "Current window range " + curWindowRange + " does not contain " + fVisibleTime;
                 return false;
             }
-            return !fView.isDirty();
+
+            if (fView.isDirty()) {
+                fFailureMessage = "Time graph is dirty";
+                return false;
+
+            }
+            return true;
         }
 
         @Override
         public String getFailureMessage() {
-            return "Time graph is not ready";
+            return fFailureMessage;
         }
     }
 
@@ -530,4 +570,133 @@ public final class ConditionHelpers {
     public static ICondition timeGraphIsReadyCondition(AbstractTimeGraphView view, @NonNull TmfTimeRange selectionRange, @NonNull ITmfTimestamp visibleTime) {
         return new TimeGraphIsReadyCondition(view, selectionRange, visibleTime);
     }
+
+    private static class NumberOfEventsCondition extends DefaultCondition {
+
+        private ITmfTrace fTrace;
+        private long fNbEvents;
+
+        private NumberOfEventsCondition(ITmfTrace trace, long nbEvents) {
+            fTrace = trace;
+            fNbEvents = nbEvents;
+        }
+
+        @Override
+        public boolean test() throws Exception {
+            return fTrace.getNbEvents() == fNbEvents;
+        }
+
+        @Override
+        public String getFailureMessage() {
+            return fTrace.getName() + " did not contain the expected number of " + fNbEvents + " events. Current: " + fTrace.getNbEvents();
+        }
+    }
+
+    /**
+     * Wait until the trace contains the specified number of events.
+     *
+     * @param trace
+     *            the trace
+     * @param nbEvents
+     *            the number of events to wait for
+     * @return ICondition for verification
+     */
+    public static ICondition numberOfEventsInTrace(ITmfTrace trace, long nbEvents) {
+        return new NumberOfEventsCondition(trace, nbEvents);
+    }
+
+    /**
+     * Wait until there is an active events editor. A title can also be
+     * specified to wait until a more specific editor.
+     */
+    public static final class ActiveEventsEditor extends DefaultCondition {
+        private final SWTWorkbenchBot fWorkbenchBot;
+        private SWTBotEditor fEditor;
+        private String fEditorTitle;
+
+        /**
+         * Wait until there is an active events editor.
+         *
+         * @param workbenchBot
+         *            a workbench bot
+         * @param editorTitle
+         *            If specified, wait until an active events editor with this
+         *            title. Can be set to null.
+         */
+        public ActiveEventsEditor(SWTWorkbenchBot workbenchBot, String editorTitle) {
+            fWorkbenchBot = workbenchBot;
+            fEditorTitle = editorTitle;
+        }
+
+        @Override
+        public boolean test() throws Exception {
+            List<SWTBotEditor> editors = fWorkbenchBot.editors(WidgetMatcherFactory.withPartId(TmfEventsEditor.ID));
+            for (SWTBotEditor e : editors) {
+                // We are careful not to call e.getWidget() here because it actually forces the editor to show.
+                // This is especially a problem for cases where we wait until there is no active editor.
+                if (e.isActive()) {
+                    if (fEditorTitle != null && !fEditorTitle.equals(e.getTitle())) {
+                        return false;
+                    }
+                    fEditor = e;
+                    return true;
+                }
+            }
+            return false;
+        }
+
+        @Override
+        public String getFailureMessage() {
+            String editorMessage = fEditorTitle != null ? " " + fEditorTitle : "";
+            return "Active events editor" + editorMessage + " not found";
+        }
+
+        /**
+         * @return The active editor found
+         */
+        public SWTBotEditor getActiveEditor() {
+            return fEditor;
+        }
+    }
+
+    private static class NumberOfSeries extends DefaultCondition {
+        private String fFailureMessage;
+        private Chart fChart;
+        private final int fNumberOfSeries;
+
+        public NumberOfSeries(Chart chart, int numberOfSeries) {
+            fChart = chart;
+            fNumberOfSeries = numberOfSeries;
+        }
+
+        @Override
+        public boolean test() throws Exception {
+            int length = fChart.getSeriesSet().getSeries().length;
+            if (length != fNumberOfSeries){
+                fFailureMessage = "Chart did not contain the expected number series. Actual " + length + ", expected " + fNumberOfSeries;
+                return false;
+            }
+
+            return true;
+        }
+
+        @Override
+        public String getFailureMessage() {
+            return fFailureMessage;
+        }
+    }
+
+    /**
+     * Wait until the chart has the specified number of series.
+     *
+     * @param chart
+     *            the chart
+     * @param numberOfSeries
+     *            the number of expected series
+     *
+     * @return ICondition for verification
+     */
+    public static ICondition numberOfSeries(Chart chart, int numberOfSeries) {
+        return new NumberOfSeries(chart, numberOfSeries);
+    }
 }
This page took 0.029329 seconds and 5 git commands to generate.