analysis.ui: add Export to TSV action test
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests / src / org / eclipse / tracecompass / analysis / os / linux / ui / swtbot / tests / latency / SystemCallLatencyTableAnalysisTest.java
index f952178b4d6bbd2c47b22bb9313337a876ee47f1..269834b1d92a9a16497232b758f825ed9c3c655e 100644 (file)
 
 package org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests.latency;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Random;
+import java.util.stream.Collectors;
 
 import org.apache.log4j.ConsoleAppender;
 import org.apache.log4j.Logger;
@@ -33,13 +44,18 @@ import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
 import org.eclipse.swtbot.swt.finder.results.BoolResult;
 import org.eclipse.swtbot.swt.finder.results.Result;
 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
+import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
+import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.table.AbstractSegmentStoreTableView;
 import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.table.AbstractSegmentStoreTableViewer;
 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.latency.SystemCallLatencyView;
 import org.eclipse.tracecompass.segmentstore.core.BasicSegment;
 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 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.tests.shared.WaitUtils;
 import org.eclipse.ui.IViewPart;
 import org.eclipse.ui.IViewReference;
 import org.eclipse.ui.PlatformUI;
@@ -92,7 +108,7 @@ public class SystemCallLatencyTableAnalysisTest {
         /* Switch perspectives */
         switchTracingPerspective();
         /* Finish waiting for eclipse to load */
-        SWTBotUtils.waitForJobs();
+        WaitUtils.waitForJobs();
 
     }
 
@@ -285,6 +301,76 @@ public class SystemCallLatencyTableAnalysisTest {
         bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "998,001", 0, 2));
     }
 
+    /**
+     * Test creating a tsv
+     *
+     * @throws NoSuchMethodException
+     *             Error creating the tsv
+     * @throws IOException
+     *             no such file or the file is locked.
+     */
+    @Test
+    public void testWriteToTsv() throws NoSuchMethodException, IOException {
+
+        List<@NonNull BasicSegment> fixture = new ArrayList<>();
+        for (int i = 1; i <= 20; i++) {
+            int start = i;
+            final int delta = i;
+            int end = start + delta * delta;
+            fixture.add(new BasicSegment(start, end));
+        }
+        assertNotNull(fTable);
+        fTable.updateModel(fixture);
+        SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
+        SWTBot bot = new SWTBot();
+        bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1", 0, 2));
+        SWTWorkbenchBot swtWorkbenchBot = new SWTWorkbenchBot();
+        SWTBotView viewBot = swtWorkbenchBot.viewById(VIEW_ID);
+        List<String> actionResult = Arrays.asList(testToTsv(viewBot));
+        String absolutePath = TmfTraceManager.getTemporaryDirPath() + File.separator + "syscallLatencyTest.testWriteToTsv.tsv";
+        TmfFileDialogFactory.setOverrideFiles(absolutePath);
+        SWTBotMenu menuBot = viewBot.viewMenu().menu("Export to TSV");
+        try {
+            assertTrue(menuBot.isEnabled());
+            assertTrue(menuBot.isVisible());
+            menuBot.click();
+
+            try (BufferedReader br = new BufferedReader(new FileReader(absolutePath))) {
+                List<String> lines = br.lines().collect(Collectors.toList());
+                assertEquals("Both reads", actionResult, lines);
+            }
+        } finally {
+            new File(absolutePath).delete();
+        }
+
+    }
+
+    private String[] testToTsv(SWTBotView view) throws NoSuchMethodException {
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        assertNotNull(os);
+        Class<@NonNull AbstractSegmentStoreTableView> clazz = AbstractSegmentStoreTableView.class;
+        Method method = clazz.getDeclaredMethod("exportToTsv", java.io.OutputStream.class);
+        method.setAccessible(true);
+        final Exception[] except = new Exception[1];
+        UIThreadRunnable.syncExec(() -> {
+            try {
+                method.invoke(fLatencyView, os);
+            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+                except[0] = e;
+            }
+        });
+        assertNull(except[0]);
+        @SuppressWarnings("null")
+        String[] lines = String.valueOf(os).split(System.getProperty("line.separator"));
+        assertNotNull(lines);
+        assertEquals("number of lines", 21, lines.length);
+        assertEquals("header", "Start Time\tEnd Time\tDuration", lines[0]);
+        // not a straight up string compare due to time zones. Kathmandu and
+        // Eucla have 15 minute time zones.
+        assertTrue("line 1", lines[1].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s001\\t\\d\\d:\\d\\d:00.000 000 002\\t1"));
+        return lines;
+    }
+
     /**
      * Test with an actual trace, this is more of an integration test than a
      * unit test. This test is a slow one too. If some analyses are not well
@@ -304,9 +390,9 @@ public class SystemCallLatencyTableAnalysisTest {
         bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
         SWTBotUtils.createProject(PROJECT_NAME);
         SWTBotUtils.openTrace(PROJECT_NAME, tracePath, TRACE_TYPE);
-        SWTBotUtils.waitForJobs();
+        WaitUtils.waitForJobs();
         createTable();
-        SWTBotUtils.waitForJobs();
+        WaitUtils.waitForJobs();
         SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
         bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "24,100", 0, 2));
         tableBot.header("Duration").click();
This page took 0.031118 seconds and 5 git commands to generate.