tmf.swtbot: Add a failure message to 'isTableCellFilled'
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / ConditionHelpers.java
index 29459f2648eb6393a78768e1f8fd830d8be7b3b2..aa58f69fbfea8e74ffaabb75239412808083ac3c 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,8 @@ 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 java.util.regex.Pattern;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jface.viewers.StructuredSelection;
@@ -25,6 +27,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 +42,14 @@ 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.viewers.xycharts.TmfXYChartViewer;
 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 +106,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()) });
             }
         };
@@ -128,7 +134,7 @@ public final class ConditionHelpers {
 
             @Override
             public String getFailureMessage() {
-                return NLS.bind("No child of table {0} found with text '{1}'.", table, name);
+                return NLS.bind("No child of table {0} found with text ''{1}''.", table, name);
             }
         };
     }
@@ -155,7 +161,7 @@ public final class ConditionHelpers {
 
             @Override
             public String getFailureMessage() {
-                return NLS.bind("No child of tree item {0} found with text '{1}'. Child items: {2}",
+                return NLS.bind("No child of tree item {0} found with text ''{1}''. Child items: {2}",
                         new String[] { treeItem.toString(), name, Arrays.toString(treeItem.getItems()) });
             }
         };
@@ -183,12 +189,37 @@ public final class ConditionHelpers {
 
             @Override
             public String getFailureMessage() {
-                return NLS.bind("Child of tree item {0} found with text '{1}' not removed. Child items: {2}",
+                return NLS.bind("Child of tree item {0} found with text ''{1}'' not removed. Child items: {2}",
                         new String[] { treeItem.toString(), String.valueOf(length), Arrays.toString(treeItem.getItems()) });
             }
         };
     }
 
+    /**
+     * 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,11 +304,20 @@ public final class ConditionHelpers {
                     if( cell == null ) {
                         return false;
                     }
-                    return cell.endsWith(content);
+                    return cell.contains(content);
                 } catch (Exception e) {
                 }
                 return false;
             }
+
+            @Override
+            public String getFailureMessage() {
+                String cell = table.cell(row, column);
+                if (cell == null) {
+                    return NLS.bind("Cell absent, expected: {0}", content);
+                }
+                return NLS.bind("Cell content: {0} expected: {1}", cell, content);
+            }
         };
     }
 
@@ -305,7 +345,7 @@ public final class ConditionHelpers {
             fParentItem = parentItem;
             fName = name;
             /* Project element labels may have count suffix */
-            fRegex = name + "(\\s\\[(\\d)+\\])?";
+            fRegex = Pattern.quote(name) + "(\\s\\[(\\d)+\\])?";
         }
 
         @Override
@@ -541,4 +581,200 @@ 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 XYViewerIsReadyCondition extends DefaultCondition  {
+
+        private TmfXYChartViewer fViewer;
+        private String fFailureMessage;
+
+        private XYViewerIsReadyCondition(TmfXYChartViewer view) {
+            fViewer = view;
+        }
+
+        @Override
+        public boolean test() throws Exception {
+
+            if (fViewer.isDirty()) {
+                fFailureMessage = "Time graph is dirty";
+                return false;
+            }
+            return true;
+        }
+
+        @Override
+        public String getFailureMessage() {
+            return fFailureMessage;
+        }
+    }
+
+    /**
+     *
+     * Wait until the XY chart viewer is ready. The XY chart viewer is
+     * considered ready when it is not updating.
+     *
+     * @param viewer
+     *            the XY chart viewer
+     * @return ICondition for verification
+     */
+    public static ICondition xyViewerIsReadyCondition(TmfXYChartViewer viewer) {
+        return new XYViewerIsReadyCondition(viewer);
+    }
+
+    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);
+    }
+
+    /**
+     * Condition to check if the tree item has children
+     *
+     * @param treeItem
+     *            the tree item that should have children
+     * @return ICondition for verification
+     */
+    public static ICondition treeItemHasChildren(SWTBotTreeItem treeItem) {
+        return new TreeItemHasChildren(treeItem);
+    }
+
+    private static final class TreeItemHasChildren extends DefaultCondition {
+        private SWTBotTreeItem fTreeItem;
+
+        public TreeItemHasChildren(SWTBotTreeItem treeItem) {
+            fTreeItem = treeItem;
+        }
+
+        @Override
+        public boolean test() throws Exception {
+            return fTreeItem.getItems().length > 0;
+        }
+
+        @Override
+        public String getFailureMessage() {
+            return NLS.bind("No child of tree item {0} found.", new String[] { fTreeItem.toString() });
+        }
+    }
 }
This page took 0.027738 seconds and 5 git commands to generate.