Commit | Line | Data |
---|---|---|
79e08fd0 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson |
64636df8 | 3 | * |
79e08fd0 BH |
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 | |
64636df8 | 8 | * |
79e08fd0 | 9 | * Contributors: |
09667aa4 | 10 | * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial design and implementation |
79e08fd0 | 11 | * Bernd Hufmann - Fixed warnings |
90ed5958 | 12 | * Alexandre Montplaisir - Port to JUnit4 |
79e08fd0 BH |
13 | *******************************************************************************/ |
14 | ||
2bdf0193 | 15 | package org.eclipse.tracecompass.tmf.ui.tests.statistics; |
79e08fd0 | 16 | |
90ed5958 AM |
17 | import static org.junit.Assert.assertEquals; |
18 | import static org.junit.Assert.assertFalse; | |
19 | import static org.junit.Assert.assertNotNull; | |
20 | import static org.junit.Assert.assertTrue; | |
21 | import static org.junit.Assert.fail; | |
79e08fd0 | 22 | |
90ed5958 | 23 | import java.util.Arrays; |
79e08fd0 | 24 | |
2bdf0193 AM |
25 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
26 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; | |
27 | import org.eclipse.tracecompass.tmf.core.event.TmfEvent; | |
28 | import org.eclipse.tracecompass.tmf.core.event.TmfEventField; | |
29 | import org.eclipse.tracecompass.tmf.core.event.TmfEventType; | |
30 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
0c7471fb | 31 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; |
2bdf0193 AM |
32 | import org.eclipse.tracecompass.tmf.ui.viewers.statistics.model.Messages; |
33 | import org.eclipse.tracecompass.tmf.ui.viewers.statistics.model.TmfStatisticsTree; | |
34 | import org.eclipse.tracecompass.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode; | |
35 | import org.eclipse.tracecompass.tmf.ui.viewers.statistics.model.TmfTreeContentProvider; | |
90ed5958 | 36 | import org.junit.Test; |
79e08fd0 | 37 | |
64636df8 BH |
38 | /** |
39 | * TmfTreeContentProvider Test Cases. | |
40 | */ | |
90ed5958 | 41 | public class TmfTreeContentProviderTest { |
79e08fd0 BH |
42 | |
43 | // ------------------------------------------------------------------------ | |
44 | // Fields | |
45 | // ------------------------------------------------------------------------ | |
46 | ||
90ed5958 | 47 | private static final String fTestName = "TreeContentProviderTest"; |
79e08fd0 BH |
48 | |
49 | private final String fTypeId1 = "Some type1"; | |
50 | private final String fTypeId2 = "Some type2"; | |
64636df8 | 51 | |
79e08fd0 BH |
52 | private final String fLabel0 = "label1"; |
53 | private final String fLabel1 = "label2"; | |
54 | private final String[] fLabels = new String[] { fLabel0, fLabel1 }; | |
55 | ||
065cc19b AM |
56 | private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2); |
57 | private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2); | |
79e08fd0 | 58 | |
e600c338 AM |
59 | private final TmfEventType fType1 = new TmfEventType(fTypeId1, TmfEventField.makeRoot(fLabels)); |
60 | private final TmfEventType fType2 = new TmfEventType(fTypeId2, TmfEventField.makeRoot(fLabels)); | |
79e08fd0 | 61 | |
2771b032 AM |
62 | private final ITmfEvent fEvent1; |
63 | private final ITmfEvent fEvent2; | |
79e08fd0 | 64 | |
4c564a2d FC |
65 | private final TmfEventField fContent1; |
66 | private final TmfEventField fContent2; | |
79e08fd0 | 67 | |
36033ff0 | 68 | private final TmfStatisticsTree fStatsData; |
79e08fd0 | 69 | |
79e08fd0 BH |
70 | private final TmfTreeContentProvider treeProvider; |
71 | ||
72 | // ------------------------------------------------------------------------ | |
73 | // Housekeeping | |
74 | // ------------------------------------------------------------------------ | |
75 | ||
76 | /** | |
90ed5958 | 77 | * Constructor |
79e08fd0 | 78 | */ |
90ed5958 | 79 | public TmfTreeContentProviderTest() { |
214cc822 | 80 | fContent1 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some content", null); |
e1de2fd4 | 81 | fEvent1 = new TmfEvent(null, ITmfContext.UNKNOWN_RANK, fTimestamp1, fType1, fContent1); |
79e08fd0 | 82 | |
214cc822 | 83 | fContent2 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other content", null); |
e1de2fd4 | 84 | fEvent2 = new TmfEvent(null, ITmfContext.UNKNOWN_RANK, fTimestamp2, fType2, fContent2); |
79e08fd0 | 85 | |
36033ff0 | 86 | fStatsData = new TmfStatisticsTree(); |
89c06060 AM |
87 | |
88 | fStatsData.setTotal(fTestName, true, 2); | |
578716e6 MK |
89 | fStatsData.setTypeCount(fTestName, fEvent1.getName(), true, 1); |
90 | fStatsData.setTypeCount(fTestName, fEvent2.getName(), true, 1); | |
79e08fd0 BH |
91 | |
92 | treeProvider = new TmfTreeContentProvider(); | |
93 | } | |
94 | ||
95 | // ------------------------------------------------------------------------ | |
90ed5958 | 96 | // Test methods |
79e08fd0 BH |
97 | // ------------------------------------------------------------------------ |
98 | ||
64636df8 BH |
99 | /** |
100 | * Test getting of children. | |
5673a177 AM |
101 | * FIXME this test was quickly adapted when we removed the TmfFixedArray, |
102 | * but it could be rewritten to be much more simple... | |
64636df8 | 103 | */ |
90ed5958 | 104 | @Test |
79e08fd0 | 105 | public void testGetChildren() { |
7588c810 | 106 | Object[] objectArray = treeProvider.getChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes)); |
79e08fd0 BH |
107 | TmfStatisticsTreeNode[] childrenNode = Arrays.asList(objectArray).toArray(new TmfStatisticsTreeNode[0]); |
108 | ||
5673a177 | 109 | String[][] childrenExpected = new String[][] { |
578716e6 MK |
110 | new String[] { fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getName() }, |
111 | new String[] { fTestName, Messages.TmfStatisticsData_EventTypes, fEvent2.getName() } | |
5673a177 | 112 | }; |
79e08fd0 | 113 | |
5673a177 | 114 | assertEquals("getChildren", childrenExpected.length, childrenNode.length); |
79e08fd0 BH |
115 | // assertTrue("getChildren", childrenPath.equals(childrenExpected)); |
116 | for (TmfStatisticsTreeNode childNode : childrenNode) { | |
5673a177 | 117 | if (!arrayOfArraysContains(childrenExpected, childNode.getPath())) { |
79e08fd0 BH |
118 | fail(); |
119 | } | |
120 | } | |
121 | } | |
122 | ||
5673a177 AM |
123 | private static boolean arrayOfArraysContains(String[][] arrayOfArrays, String[] array) { |
124 | for (String[] curArray : arrayOfArrays) { | |
125 | if (arraysEqual(curArray, array)) { | |
126 | return true; | |
127 | } | |
128 | } | |
129 | return false; | |
130 | } | |
131 | ||
132 | private static boolean arraysEqual(String[] array1, String[] array2) { | |
133 | if (array1.length != array2.length) { | |
134 | return false; | |
135 | } | |
136 | for (int i = 0; i < array1.length; i++) { | |
137 | if (!array1[i].equals(array2[i])) { | |
138 | return false; | |
139 | } | |
140 | } | |
141 | return true; | |
142 | } | |
143 | ||
64636df8 BH |
144 | /** |
145 | * Test getting of parent. | |
146 | */ | |
90ed5958 | 147 | @Test |
79e08fd0 | 148 | public void testGetParent() { |
7588c810 | 149 | TmfStatisticsTreeNode parent = (TmfStatisticsTreeNode) treeProvider.getParent(fStatsData.getNode(fTestName)); |
79e08fd0 BH |
150 | |
151 | assertNotNull("getParent", parent); | |
7588c810 | 152 | assertTrue("getParent", parent.getPath().equals(fStatsData.getRootNode().getPath())); |
79e08fd0 BH |
153 | } |
154 | ||
64636df8 BH |
155 | /** |
156 | * Test checking for children. | |
157 | */ | |
90ed5958 | 158 | @Test |
79e08fd0 | 159 | public void testHasChildren() { |
0126a8ca | 160 | boolean hasChildren = treeProvider.hasChildren(fStatsData.getRootNode()); |
79e08fd0 | 161 | assertTrue("hasChildren", hasChildren); |
64636df8 | 162 | |
7588c810 | 163 | hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName)); |
79e08fd0 | 164 | assertTrue("hasChildren", hasChildren); |
64636df8 | 165 | |
7588c810 | 166 | hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes)); |
79e08fd0 BH |
167 | assertTrue("hasChildren", hasChildren); |
168 | ||
578716e6 | 169 | hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getName())); |
79e08fd0 BH |
170 | assertFalse("hasChildren", hasChildren); |
171 | } | |
172 | ||
64636df8 BH |
173 | /** |
174 | * Test getting of elements. | |
175 | */ | |
90ed5958 | 176 | @Test |
79e08fd0 | 177 | public void testGetElements() { |
7588c810 | 178 | Object[] objectElements = treeProvider.getElements(fStatsData.getRootNode()); |
79e08fd0 BH |
179 | TmfStatisticsTreeNode[] nodeElements = Arrays.asList(objectElements).toArray(new TmfStatisticsTreeNode[0]); |
180 | assertEquals("getElements", 1, nodeElements.length); | |
5673a177 | 181 | assertTrue("getElements", nodeElements[0].getPath()[0].equals(fTestName)); |
79e08fd0 BH |
182 | } |
183 | } |