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