From 2470d687aba656d3b98ffeb53ed45c8b874d2ef0 Mon Sep 17 00:00:00 2001 From: Matthew Khouzam Date: Mon, 26 Jan 2015 17:10:08 -0500 Subject: [PATCH] tmf.ui.swtbot: add filterview tests implements: * tracetype test * contains test * filter node test * apply test * filternode test * matches test * equals test * compare test It does not: * import * export these functions require access to native widgets Change-Id: I00684956c065042add2b8ffbbf0ba9df57f01ab2 Signed-off-by: Matthew Khouzam Reviewed-on: https://git.eclipse.org/r/40407 Reviewed-by: Hudson CI Reviewed-by: Marc-Andre Laperle Tested-by: Marc-Andre Laperle --- .../META-INF/MANIFEST.MF | 3 +- .../swtbot/tests/shared/ConditionHelpers.java | 6 +- .../ui/swtbot/tests/shared/SWTBotUtils.java | 25 ++ .../swtbot/tests/viewers/events/AllTests.java | 3 +- .../viewers/events/FilterViewerTest.java | 367 ++++++++++++++++++ 5 files changed, 401 insertions(+), 3 deletions(-) create mode 100644 org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/FilterViewerTest.java diff --git a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/META-INF/MANIFEST.MF b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/META-INF/MANIFEST.MF index 009f92b6d4..10caa068f7 100644 --- a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/META-INF/MANIFEST.MF @@ -16,7 +16,8 @@ Require-Bundle: org.junit;bundle-version="4.0.0", org.eclipse.tracecompass.tmf.ui, org.eclipse.tracecompass.tmf.core, org.eclipse.tracecompass.tmf.core.tests, - org.eclipse.swtbot.junit4_x + org.eclipse.swtbot.junit4_x, + org.eclipse.swtbot.swt.finder Import-Package: org.apache.log4j, org.apache.log4j.varia, org.eclipse.swtbot.eclipse.finder, diff --git a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ConditionHelpers.java b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ConditionHelpers.java index 5c6551291e..fe7b0b1c03 100644 --- a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ConditionHelpers.java +++ b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ConditionHelpers.java @@ -181,7 +181,11 @@ public final class ConditionHelpers { @Override public boolean test() throws Exception { try { - return content.equals(table.cell(row, column)); + String cell = table.cell(row, column); + if( cell == null ) { + return false; + } + return cell.endsWith(content); } catch (Exception e) { } return false; diff --git a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java index 11dddca25f..6b451dbc99 100644 --- a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java +++ b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java @@ -47,6 +47,7 @@ import org.eclipse.tracecompass.tmf.ui.views.TracingPerspectiveFactory; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IEditorReference; import org.eclipse.ui.IPageLayout; +import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.WorkbenchException; import org.hamcrest.Matcher; @@ -390,4 +391,28 @@ public final class SWTBotUtils { assertNotNull(treeNode); return treeNode; } + + /** + * Open a view by id. + * + * @param id + * view id. + */ + public static void openView(final String id) { + final PartInitException res[] = new PartInitException[1]; + UIThreadRunnable.syncExec(new VoidResult() { + @Override + public void run() { + try { + PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id); + } catch (PartInitException e) { + res[0] = e; + } + } + }); + if (res[0] != null) { + fail(res[0].getMessage()); + } + waitForJobs(); + } } diff --git a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/AllTests.java b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/AllTests.java index 65cb967069..67781d971c 100644 --- a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/AllTests.java +++ b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/AllTests.java @@ -21,7 +21,8 @@ import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ CallsiteEventsInTableTest.class, - CollapseEventsInTableTest.class + CollapseEventsInTableTest.class, + FilterViewerTest.class }) public class AllTests { } diff --git a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/FilterViewerTest.java b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/FilterViewerTest.java new file mode 100644 index 0000000000..33277f305c --- /dev/null +++ b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/FilterViewerTest.java @@ -0,0 +1,367 @@ +/******************************************************************************* + * Copyright (c) 2015 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Matthew Khouzam - Initial API and implementation + *******************************************************************************/ +package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.File; +import java.io.IOException; + +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Logger; +import org.apache.log4j.SimpleLayout; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.swt.widgets.MenuItem; +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.finders.ContextMenuFinder; +import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; +import org.eclipse.swtbot.swt.finder.results.VoidResult; +import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotText; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; +import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile; +import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers; +import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; +import org.eclipse.tracecompass.tmf.ui.views.filter.FilterView; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test for filter views in trace compass + */ +public class FilterViewerTest { + + + private static final String COMPARE = "COMPARE"; + private static final String CONTAINS = "CONTAINS"; + private static final String XMLSTUB_ID = "org.eclipse.linuxtools.tmf.core.tests.xmlstub"; + private static final String TRACETYPE = "Test trace : XML Trace Stub"; + private static final String AND = "AND"; + private static final String WITH_TRACETYPE = "WITH TRACETYPE " + TRACETYPE; + private static final String FILTER_TEST = "FILTER "; + + private static final String TRACE_START = ""; + private static final String EVENT_BEGIN = "" + ""; + private static final String TRACE_END = ""; + + + private static final String PROJECT_NAME = "TestForFiltering"; + + /** The Log4j logger instance. */ + private static final Logger fLogger = Logger.getRootLogger(); + private static final String OR = "OR"; + private static SWTWorkbenchBot fBot; + + private static String makeEvent(int ts, int val) { + return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n"; + } + + private static File fFileLocation; + + /** + * Initialization, creates a temp trace + * + * @throws IOException + * should not happen + */ + @BeforeClass + public static void init() throws IOException { + SWTBotUtils.failIfUIThread(); + Thread.currentThread().setName("SWTBot Thread"); // for the debugger + /* set up for swtbot */ + SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */ + fLogger.addAppender(new ConsoleAppender(new SimpleLayout())); + fBot = new SWTWorkbenchBot(); + + SWTBotUtils.closeView("welcome", fBot); + + SWTBotUtils.switchToTracingPerspective(); + /* finish waiting for eclipse to load */ + SWTBotUtils.waitForJobs(); + fFileLocation = File.createTempFile("sample", ".xml"); + try (BufferedRandomAccessFile braf = new BufferedRandomAccessFile(fFileLocation, "rw")) { + braf.writeBytes(TRACE_START); + for (int i = 0; i < 100; i++) { + braf.writeBytes(makeEvent(i * 100, i % 4)); + } + braf.writeBytes(TRACE_END); + } + } + + /** + * Open a trace in an editor + */ + @Before + public void beforeTest() { + SWTWorkbenchBot bot = new SWTWorkbenchBot(); + SWTBotUtils.createProject(PROJECT_NAME); + SWTBotTreeItem treeItem = SWTBotUtils.selectTracesFolder(bot, PROJECT_NAME); + assertNotNull(treeItem); + SWTBotUtils.openTrace(PROJECT_NAME, fFileLocation.getAbsolutePath(), XMLSTUB_ID); + SWTBotUtils.openView(FilterView.ID); + } + + /** + * Delete the file + */ + @AfterClass + public static void cleanUp() { + fFileLocation.delete(); + SWTBotUtils.deleteProject(PROJECT_NAME, fBot); + } + + /** + * Close the editor + */ + @After + public void tearDown() { + fBot.closeAllEditors(); + } + + /** + * Return all timestamps ending with 100... for reasons + */ + @Test + public void testTimestampFilter() { + SWTBotView viewBot = fBot.viewById(FilterView.ID); + viewBot.setFocus(); + SWTBot filterBot = viewBot.bot(); + SWTBotTree treeBot = filterBot.tree(); + + viewBot.toolbarButton("Add new filter").click(); + treeBot.getTreeItem("FILTER ").select(); + SWTBotText textBot = filterBot.text(); + textBot.setFocus(); + String filterName = "timestamp"; + textBot.setText(filterName); + SWTBotTreeItem filterNodeBot = treeBot.getTreeItem(FILTER_TEST + filterName); + filterNodeBot.click(); + filterNodeBot.contextMenu("TRACETYPE").click(); + filterNodeBot.expand(); + SWTBotCombo comboBot = filterBot.comboBox(); + comboBot.setSelection(TRACETYPE); + filterNodeBot.getNode(WITH_TRACETYPE).expand(); + + // -------------------------------------------------------------------- + // add AND + // -------------------------------------------------------------------- + + filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(AND).click(); + + // -------------------------------------------------------------------- + // add CONTAINS "100" + // -------------------------------------------------------------------- + + filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).contextMenu(CONTAINS).click(); + filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).expand(); + comboBot = filterBot.comboBox(); + comboBot.setSelection(comboBot.itemCount() - 3); + textBot = filterBot.text(); + textBot.setFocus(); + textBot.setText("100"); + filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).getNode("Timestamp CONTAINS \"100\"").select(); + filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).select(); + + viewBot.toolbarButton("Save filters").click(); + + String ret = applyFilter(fBot, filterName); + assertEquals("10/100", ret); + } + + /** + * Return all timestamps ending with 100... for reasons + */ + @Test + public void testTimestampEqualsOr() { + SWTBotView viewBot = fBot.viewById(FilterView.ID); + viewBot.setFocus(); + SWTBot filterBot = viewBot.bot(); + SWTBotTree treeBot = filterBot.tree(); + + viewBot.toolbarButton("Add new filter").click(); + treeBot.getTreeItem("FILTER ").select(); + SWTBotText textBot = filterBot.text(); + textBot.setFocus(); + String filterName = "matchAndEquals"; + textBot.setText(filterName); + SWTBotTreeItem filterNodeBot = treeBot.getTreeItem(FILTER_TEST + filterName); + filterNodeBot.click(); + filterNodeBot.contextMenu("TRACETYPE").click(); + filterNodeBot.expand(); + SWTBotCombo comboBot = filterBot.comboBox(); + comboBot.setSelection(TRACETYPE); + filterNodeBot.getNode(WITH_TRACETYPE).expand(); + + // -------------------------------------------------------------------- + // add OR + // -------------------------------------------------------------------- + + filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(OR).click(); + + // -------------------------------------------------------------------- + // add EQUALS "19...300" + // -------------------------------------------------------------------- + + SWTBotTreeItem orNode = filterNodeBot.getNode(WITH_TRACETYPE).getNode(OR); + orNode.contextMenu("EQUALS").click(); + orNode.expand(); + orNode.getNode(0).select(); + comboBot = filterBot.comboBox(); + //timestamp + comboBot.setSelection(comboBot.itemCount() - 3); + textBot = filterBot.text(); + textBot.setFocus(); + textBot.setText("19:00:00.000 000 300"); + + // -------------------------------------------------------------------- + // add MATCHES "1" + // -------------------------------------------------------------------- + orNode.contextMenu("MATCHES").click(); + orNode.expand(); + orNode.getNode(1).select(); + // contents + comboBot = filterBot.comboBox(); + comboBot.setSelection(comboBot.itemCount() - 1); + textBot = filterBot.text(0); + textBot.setFocus(); + textBot.setText("field"); + textBot = filterBot.text(1); + textBot.setFocus(); + textBot.setText("1"); + + viewBot.toolbarButton("Save filters").click(); + + String ret = applyFilter(fBot, filterName); + assertEquals("26/100", ret); + } + + /** + * test compare field >= 2 + */ + @Test + public void testField01() { + SWTBotView viewBot = fBot.viewById(FilterView.ID); + viewBot.setFocus(); + SWTBot filterBot = viewBot.bot(); + SWTBotTree treeBot = filterBot.tree(); + + viewBot.toolbarButton("Add new filter").click(); + treeBot.getTreeItem("FILTER ").select(); + SWTBotText textBot = filterBot.text(); + textBot.setFocus(); + String filterName = "field"; + textBot.setText(filterName); + SWTBotTreeItem filterNodeBot = treeBot.getTreeItem(FILTER_TEST + filterName); + filterNodeBot.click(); + filterNodeBot.contextMenu("TRACETYPE").click(); + filterNodeBot.expand(); + SWTBotCombo comboBot = filterBot.comboBox(); + comboBot.setSelection(TRACETYPE); + filterNodeBot.getNode(WITH_TRACETYPE).expand(); + + // -------------------------------------------------------------------- + // add Compare > 1.5 + // -------------------------------------------------------------------- + + filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(COMPARE).click(); + SWTBotTreeItem contentNode = filterNodeBot.getNode(WITH_TRACETYPE).getNode("