| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2014 École Polytechnique de Montréal |
| 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 | * Contributors: |
| 10 | * Geneviève Bastien - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.ui.tests.project.model; |
| 14 | |
| 15 | import static org.junit.Assert.assertEquals; |
| 16 | import static org.junit.Assert.assertFalse; |
| 17 | import static org.junit.Assert.assertNotNull; |
| 18 | import static org.junit.Assert.fail; |
| 19 | |
| 20 | import java.util.List; |
| 21 | import java.util.concurrent.TimeoutException; |
| 22 | |
| 23 | import org.eclipse.core.runtime.CoreException; |
| 24 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 25 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; |
| 26 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfAnalysisElement; |
| 27 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; |
| 28 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
| 29 | import org.eclipse.linuxtools.tmf.ui.tests.shared.ProjectModelTestData; |
| 30 | import org.eclipse.linuxtools.tmf.ui.tests.stubs.analysis.TestAnalysisUi; |
| 31 | import org.junit.After; |
| 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| 34 | |
| 35 | /** |
| 36 | * Test suite for the {@link TmfAnalysisElement} class. |
| 37 | * |
| 38 | * @author Geneviève Bastien |
| 39 | */ |
| 40 | public class ProjectModelAnalysisTest { |
| 41 | |
| 42 | /** ID of analysis module in UI */ |
| 43 | public static final String MODULE_UI = "org.eclipse.linuxtools.tmf.ui.tests.test"; |
| 44 | private TmfProjectElement fixture; |
| 45 | |
| 46 | /** |
| 47 | * Perform pre-test initialization. |
| 48 | */ |
| 49 | @Before |
| 50 | public void setUp() { |
| 51 | try { |
| 52 | fixture = ProjectModelTestData.getFilledProject(); |
| 53 | } catch (CoreException e) { |
| 54 | fail(e.getMessage()); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Cleans up the project after tests have been executed |
| 60 | */ |
| 61 | @After |
| 62 | public void cleanUp() { |
| 63 | ProjectModelTestData.deleteProject(fixture); |
| 64 | } |
| 65 | |
| 66 | private TmfTraceElement getTraceElement() { |
| 67 | TmfTraceElement trace = null; |
| 68 | for (ITmfProjectModelElement element : fixture.getTracesFolder().getChildren()) { |
| 69 | if (element instanceof TmfTraceElement) { |
| 70 | TmfTraceElement traceElement = (TmfTraceElement) element; |
| 71 | if (traceElement.getName().equals(ProjectModelTestData.getTraceName())) { |
| 72 | trace = traceElement; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | assertNotNull(trace); |
| 77 | return trace; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Test the getAvailableAnalysis() method |
| 82 | */ |
| 83 | @Test |
| 84 | public void testListAnalysis() { |
| 85 | TmfTraceElement trace = getTraceElement(); |
| 86 | |
| 87 | /* Make sure the analysis list is not empty */ |
| 88 | List<TmfAnalysisElement> analysisList = trace.getAvailableAnalysis(); |
| 89 | assertFalse(analysisList.isEmpty()); |
| 90 | |
| 91 | /* Make sure TestAnalysisUi is there */ |
| 92 | TmfAnalysisElement analysis = null; |
| 93 | for (TmfAnalysisElement analysisElement : analysisList) { |
| 94 | if (analysisElement.getAnalysisId().equals(MODULE_UI)) { |
| 95 | analysis = analysisElement; |
| 96 | } |
| 97 | } |
| 98 | assertNotNull(analysis); |
| 99 | |
| 100 | assertEquals("Test analysis in UI", analysis.getName()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Test if the list of available analysis is correctly populated by the |
| 105 | * content provider |
| 106 | */ |
| 107 | @Test |
| 108 | public void testPopulate() { |
| 109 | TmfTraceElement trace = getTraceElement(); |
| 110 | |
| 111 | /* Make sure the analysis list is not empty */ |
| 112 | List<ITmfProjectModelElement> analysisList = trace.getChildren(); |
| 113 | assertFalse(analysisList.isEmpty()); |
| 114 | |
| 115 | /* Make sure TestAnalysisUi is there */ |
| 116 | TmfAnalysisElement analysis = null; |
| 117 | for (ITmfProjectModelElement element : analysisList) { |
| 118 | if (element instanceof TmfAnalysisElement) { |
| 119 | TmfAnalysisElement analysisElement = (TmfAnalysisElement) element; |
| 120 | if (analysisElement.getAnalysisId().equals(MODULE_UI)) { |
| 121 | analysis = analysisElement; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | assertNotNull(analysis); |
| 126 | |
| 127 | assertEquals("Test analysis in UI", analysis.getName()); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Test the instantiateAnalysis method |
| 132 | */ |
| 133 | @Test |
| 134 | public void testInstantiate() { |
| 135 | TmfTraceElement traceElement = getTraceElement(); |
| 136 | |
| 137 | TmfAnalysisElement analysis = null; |
| 138 | for (TmfAnalysisElement analysisElement : traceElement.getAvailableAnalysis()) { |
| 139 | if (analysisElement.getAnalysisId().equals(MODULE_UI)) { |
| 140 | analysis = analysisElement; |
| 141 | } |
| 142 | } |
| 143 | assertNotNull(analysis); |
| 144 | |
| 145 | /* Instantiate an analysis on a trace that is closed */ |
| 146 | traceElement.closeEditors(); |
| 147 | analysis.activateParent(); |
| 148 | |
| 149 | try { |
| 150 | ProjectModelTestData.delayUntilTraceOpened(traceElement); |
| 151 | } catch (TimeoutException e) { |
| 152 | fail("The analysis parent did not open in a reasonable time"); |
| 153 | } |
| 154 | ITmfTrace trace = traceElement.getTrace(); |
| 155 | |
| 156 | assertNotNull(trace); |
| 157 | TestAnalysisUi module = (TestAnalysisUi) trace.getAnalysisModule(analysis.getAnalysisId()); |
| 158 | assertNotNull(module); |
| 159 | |
| 160 | } |
| 161 | } |