lttng: Fix intermittent failure in MemoryUsageViewTest
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.ust.ui.swtbot.tests / src / org / eclipse / tracecompass / lttng2 / ust / ui / swtbot / tests / MemoryUsageViewTest.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10 package org.eclipse.tracecompass.lttng2.ust.ui.swtbot.tests;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.File;
17
18 import org.apache.log4j.ConsoleAppender;
19 import org.apache.log4j.Logger;
20 import org.apache.log4j.SimpleLayout;
21 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
22 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
23 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
24 import org.eclipse.swtbot.swt.finder.matchers.WidgetOfType;
25 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
26 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
27 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
28 import org.eclipse.tracecompass.internal.lttng2.ust.ui.views.memusage.MemoryUsageView;
29 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
30 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
31 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
32 import org.hamcrest.Matcher;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.swtchart.Chart;
39 import org.swtchart.ILineSeries;
40 import org.swtchart.ISeries;
41 import org.swtchart.ISeriesSet;
42
43 /**
44 * Test for the Memory Usage view in trace compass
45 */
46 @RunWith(SWTBotJunit4ClassRunner.class)
47 public class MemoryUsageViewTest {
48
49 private static final int EXPECTED_NUM_SERIES = 4;
50
51 private static final String UST_ID = "org.eclipse.linuxtools.lttng2.ust.tracetype";
52
53 private static final String PROJECT_NAME = "TestForMemory";
54
55 /** The Log4j logger instance. */
56 private static final Logger fLogger = Logger.getRootLogger();
57 private static SWTWorkbenchBot fBot;
58
59 /**
60 * Initialization
61 */
62 @BeforeClass
63 public static void init() {
64 SWTBotUtils.initialize();
65
66 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
67 /* set up for swtbot */
68 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
69 fLogger.addAppender(new ConsoleAppender(new SimpleLayout()));
70 fBot = new SWTWorkbenchBot();
71
72 SWTBotUtils.closeView("welcome", fBot);
73
74 SWTBotUtils.switchToTracingPerspective();
75
76 /* finish waiting for eclipse to load */
77 SWTBotUtils.waitForJobs();
78 }
79
80 /**
81 * Open a trace in an editor
82 */
83 @Before
84 public void beforeTest() {
85 SWTBotUtils.createProject(PROJECT_NAME);
86 SWTBotTreeItem treeItem = SWTBotUtils.selectTracesFolder(fBot, PROJECT_NAME);
87 assertNotNull(treeItem);
88 final CtfTestTrace cygProfile = CtfTestTrace.MEMORY_ANALYSIS;
89 final File file = new File(CtfTmfTestTraceUtils.getTrace(cygProfile).getPath());
90 SWTBotUtils.openTrace(PROJECT_NAME, file.getAbsolutePath(), UST_ID);
91 SWTBotUtils.openView(MemoryUsageView.ID);
92 SWTBotUtils.waitForJobs();
93 }
94
95 /**
96 * Close the editor
97 */
98 @After
99 public void tearDown() {
100 fBot.closeAllEditors();
101 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
102 }
103
104 /**
105 * Test if Memory Usage is populated
106 */
107 @Test
108 public void testOpenMemoryUsage() {
109 SWTBotView viewBot = fBot.viewById(MemoryUsageView.ID);
110 viewBot.setFocus();
111
112 // Do some basic validation
113 Matcher<Chart> matcher = WidgetOfType.widgetOfType(Chart.class);
114 Chart chart = viewBot.bot().widget(matcher);
115
116 // Verify that the chart has 4 series
117 fBot.waitUntil(new DefaultCondition() {
118 private String fFailureMessage;
119
120 @Override
121 public boolean test() throws Exception {
122 int length = chart.getSeriesSet().getSeries().length;
123 if (length != EXPECTED_NUM_SERIES){
124 fFailureMessage = "Chart did not contain the expected number series. Actual " + length + ", expected " + EXPECTED_NUM_SERIES;
125 return false;
126 }
127
128 return true;
129 }
130
131 @Override
132 public String getFailureMessage() {
133 return fFailureMessage;
134 }
135 });
136
137 ISeriesSet seriesSet = chart.getSeriesSet();
138 ISeries[] series = seriesSet.getSeries();
139 assertEquals(EXPECTED_NUM_SERIES, series.length);
140 // Verify that each series is a ILineSeries
141 for (int i = 0; i < series.length; i++) {
142 assertTrue(series[i] instanceof ILineSeries);
143 }
144 }
145
146 }
This page took 0.034433 seconds and 5 git commands to generate.