Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 77ab055ae24685e36a0a464c811c3cce971b44cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*******************************************************************************
 * Copyright (c) 2017, 2019 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
 *******************************************************************************/

package org.eclipse.tracecompass.tmf.ui.swtbot.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.matchers.WidgetOfType;
import org.eclipse.swtbot.swt.finder.utils.FileUtils;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.tracecompass.tmf.core.model.TmfCommonXAxisModel;
import org.eclipse.tracecompass.tmf.core.model.YModel;
import org.eclipse.tracecompass.tmf.core.model.xy.IYModel;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfXYChartViewer;
import org.eclipse.tracecompass.tmf.ui.views.TmfChartView;
import org.eclipse.ui.IViewPart;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.swtchart.Chart;
import org.swtchart.IBarSeries;
import org.swtchart.ILineSeries;
import org.swtchart.ISeries;
import org.swtchart.LineStyle;

import com.google.common.primitives.Doubles;
import com.google.common.primitives.Longs;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * SWTBot tests for viewers using XY data provider
 *
 * @author Yonni Chen
 */
@RunWith(SWTBotJunit4ClassRunner.class)
public abstract class XYDataProviderBaseTest {

    /** The workbench bot */
    protected static SWTWorkbenchBot fBot;

    /** The Log4j logger instance. */
    private static final Logger fLogger = Logger.getRootLogger();

    /** Default project name */
    protected static final String TRACE_PROJECT_NAME = "test";

    private static Gson fGson = new GsonBuilder().setPrettyPrinting().create();

    private SWTBotView fViewBot;
    private Chart fChart;

    /**
     * Before Class
     */
    @BeforeClass
    public static void beforeClass() {
        SWTBotUtils.initialize();

        /* set up for swtbot */
        SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
        SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
        fLogger.removeAllAppenders();
        fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
        fBot = new SWTWorkbenchBot();
        SWTBotUtils.closeView("welcome", fBot);
        /* Create the trace project */
        SWTBotUtils.createProject(TRACE_PROJECT_NAME);
        /* Finish waiting for eclipse to load */
        WaitUtils.waitForJobs();
    }

    /**
     * Close the editor
     */
    @AfterClass
    public static void tearDown() {
        SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
        fLogger.removeAllAppenders();
    }

    /**
     * Set up
     */
    @Before
    public void setup() {
        SWTBotUtils.openView(getViewID());
        fViewBot = fBot.viewById(getViewID());
        fViewBot.show();

        Matcher<Chart> widgetOfType = WidgetOfType.widgetOfType(Chart.class);
        fChart = fViewBot.bot().widget(widgetOfType);
        ITmfTrace trace = getTestTrace();

        File file = new File(trace.getPath());
        SWTBotUtils.openTrace(TRACE_PROJECT_NAME, file.getAbsolutePath(), trace.getTraceTypeId());
        SWTBotUtils.activateEditor(fBot, trace.getName());
    }

    /**
     * After Test
     */
    @After
    public void after() {
        fBot.closeAllEditors();
        SWTBotUtils.closeSecondaryShells(fBot);
        disposeTestTrace();
    }

    /**
     * Get the full path to a test file from this class's bundle.
     *
     * @param bundlePath
     *            path from the bundle
     * @return the absolute path
     */
    private String getFullPath(String bundlePath) {
        try {
            Bundle bundle = FrameworkUtil.getBundle(this.getClass());
            URL location = FileLocator.find(bundle, new Path(bundlePath), null);
            URI uri = FileLocator.toFileURL(location).toURI();
            return new File(uri).getAbsolutePath();
        } catch (Exception e) {
            fail(e.toString());
            return null;
        }
    }

    /**
     * Based on a SWT Chart, we check if data shown is valid with a JSON file.
     * Comparison with the main series and other series if exists
     *
     * @param chart
     *            A SWT Chart
     * @param otherSeries
     *            An array of other series name to check other than the main series
     * @param expectedJson
     *            The path of the JSON file relative to this class's bundle
     * @return True if the serialized chart data matches the JSON file content
     */
    protected boolean isChartDataValid(final Chart chart, String expectedJson, String... otherSeries) {
        /**
         * FIXME : Once CQ for Jackson is approved, use deserialization instead of
         * comparing strings
         */
        String expected = FileUtils.read(getFullPath(expectedJson));
        TmfCommonXAxisModel model = extractModelFromChart(chart, otherSeries);
        String current = fGson.toJson(model);
        return expected.equals(current);
    }

    /**
     * From a SWT Chart, this method extract a {@link TmfCommonXAxisModel} that
     * represents the chart. Since, we unfortunately have no mecanism to deserialize
     * with GSON, we have to compare strings. So, once the model is extract from the
     * Chart, we serialize it and compare with a string
     *
     * @param chart
     *            A SWT Chart
     * @param otherSeries
     *            Name of other series to extract from Chart
     * @return A {@link TmfCommonXAxisModel}
     */
    protected TmfCommonXAxisModel extractModelFromChart(final Chart chart, String... otherSeries) {
        String mainSeriesName = getMainSeriesName();
        ISeries mainSeries = chart.getSeriesSet().getSeries(mainSeriesName);
        if (mainSeries == null) {
            System.out.println("Main Series " + mainSeriesName + " not found in chart");
            return null;
        }

        /* X and Y Values shown in chart */
        double[] xMain = mainSeries.getXSeries();
        double[] yMain = mainSeries.getYSeries();

        Map<@NonNull String, @NonNull IYModel> yModels = new LinkedHashMap<>();
        yModels.put(mainSeriesName, new YModel(-1, mainSeriesName, Objects.requireNonNull(yMain)));

        for (String other : otherSeries) {
            if (other != null) {
                ISeries series = chart.getSeriesSet().getSeries(other);
                if (series == null) {
                    System.out.println("Series " + other + " not found in chart");
                    return null;
                }

                /* X and Y Values shown in chart */
                double[] xSeries = series.getXSeries();
                double[] ySeries = series.getYSeries();

                /* Series should have the same x axis values, not finished updating all series*/
                if (!Arrays.equals(xMain, xSeries)) {
                    System.out.println("Series don't currently have the same x axis values");
                    return null;
                }
                yModels.put(other, new YModel(-1, other, Objects.requireNonNull(ySeries)));
            }
        }

        long[] x = Longs.toArray(Doubles.asList(xMain));
        assertNotNull(x);
        return new TmfCommonXAxisModel(getTitle(), x, yModels);
    }

    /**
     * Gets the ChartViewer from a TmfChartView
     *
     * @param viewPart
     *            The IViewPart
     * @return The ChartViewer from the IViewPart
     */
    protected static TmfXYChartViewer getChartViewer(IViewPart viewPart) {
        if (viewPart instanceof TmfChartView) {
            return ((TmfChartView) viewPart).getChartViewer();
        }
        return null;
    }

    /**
     * Verify the style of a series in the XY chart
     *
     * @param seriesName
     *            The name of the series
     * @param expectedType
     *            Expected type of the series
     * @param expectedColor
     *            Expected color of the series. If the color is arbitrary, a value
     *            of <code>null</code> will skip color check
     * @param expectedLineStyle
     *            Expected line style of the series
     * @param isArea
     *            Parameter should be true if expected series show area, false
     *            either
     */
    protected void verifySeriesStyle(String seriesName, ISeries.SeriesType expectedType, @Nullable RGB expectedColor, LineStyle expectedLineStyle, boolean isArea) {
        /* Make sure the UI update is complete */
        UIThreadRunnable.syncExec(() -> {});

        ISeries series = fChart.getSeriesSet().getSeries(seriesName);
        assertNotNull(series);
        assertTrue(series.isVisible());

        /* Color, type and style */
        assertEquals(expectedType, series.getType());

        if (expectedType == ISeries.SeriesType.LINE) {
            ILineSeries line = (ILineSeries) series;
            if (expectedColor != null) {
                assertEquals(expectedColor, line.getLineColor().getRGB());
            }
            assertEquals(expectedLineStyle, line.getLineStyle());
            assertEquals(isArea, line.isAreaEnabled());
        } else if (expectedType == ISeries.SeriesType.BAR) {
            IBarSeries bar = (IBarSeries) series;
            if (expectedColor != null) {
                assertEquals(expectedColor, bar.getBarColor().getRGB());
            }
            assertTrue(bar.isStackEnabled());
        }
    }

    /**
     * Gets the SWT Chart
     *
     * @return The chart
     *
     */
    protected Chart getChart() {
        return fChart;
    }

    /**
     * Gets the SWT Bot View
     *
     * @return The SWT Bot View
     *
     */
    protected SWTBotView getSWTBotView() {
        return fViewBot;
    }

    /**
     * Gets the main series name of the XY
     *
     * @return The main series name
     */
    protected abstract @NonNull String getMainSeriesName();

    /**
     * Gets the title of the XY
     *
     * @return The title
     */
    protected abstract @NonNull String getTitle();

    /**
     * Gets the view ID
     *
     * @return The view ID
     */
    protected abstract String getViewID();

    /**
     * Gets the trace on which the test will be run
     *
     * @return The trace
     */
    protected abstract ITmfTrace getTestTrace();

    /**
     * Disposes the trace on which the test was run
     */
    protected abstract void disposeTestTrace();
}

Back to the top