Merge branch 'master' into lttng-kepler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.tests / src / org / eclipse / linuxtools / tmf / ui / tests / statistics / TmfTreeContentProviderTest.java
1 /*******************************************************************************
2 * Copyright (c) 2011 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 * Contributors:
10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial design and implementation
11 * Bernd Hufmann - Fixed warnings
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.tests.statistics;
15
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.Vector;
19
20 import junit.framework.TestCase;
21
22 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
23 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
24 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
25 import org.eclipse.linuxtools.tmf.core.event.TmfEventType;
26 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
27 import org.eclipse.linuxtools.tmf.core.util.TmfFixedArray;
28 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.ITmfExtraEventInfo;
29 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.AbsTmfStatisticsTree;
30 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.Messages;
31 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfBaseStatisticsTree;
32 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode;
33 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfTreeContentProvider;
34
35 /**
36 * TmfTreeContentProvider Test Cases.
37 */
38 @SuppressWarnings("nls")
39 public class TmfTreeContentProviderTest extends TestCase {
40
41 // ------------------------------------------------------------------------
42 // Fields
43 // ------------------------------------------------------------------------
44
45 private String fTestName = null;
46
47 private final String fContext = "UnitTest";
48 private final String fTypeId1 = "Some type1";
49 private final String fTypeId2 = "Some type2";
50
51 private final String fLabel0 = "label1";
52 private final String fLabel1 = "label2";
53 private final String[] fLabels = new String[] { fLabel0, fLabel1 };
54
55 private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5);
56 private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5);
57
58 private final String fSource = "Source";
59
60 private final TmfEventType fType1 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels));
61 private final TmfEventType fType2 = new TmfEventType(fContext, fTypeId2, TmfEventField.makeRoot(fLabels));
62
63 private final String fReference = "Some reference";
64
65 private final TmfEvent fEvent1;
66 private final TmfEvent fEvent2;
67
68 private final TmfEventField fContent1;
69 private final TmfEventField fContent2;
70
71 private final TmfBaseStatisticsTree fStatsData;
72
73 private final ITmfExtraEventInfo fExtraInfo;
74
75 private final TmfTreeContentProvider treeProvider;
76
77 // ------------------------------------------------------------------------
78 // Housekeeping
79 // ------------------------------------------------------------------------
80
81 /**
82 * @param name
83 * of the test
84 */
85 public TmfTreeContentProviderTest(final String name) {
86 super(name);
87
88 fTestName = name;
89
90 fContent1 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some content");
91 fEvent1 = new TmfEvent(null, fTimestamp1, fSource, fType1, fContent1, fReference);
92
93 fContent2 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other content");
94 fEvent2 = new TmfEvent(null, fTimestamp2, fSource, fType2, fContent2, fReference);
95
96 fStatsData = new TmfBaseStatisticsTree();
97 fExtraInfo = new ITmfExtraEventInfo() {
98 @Override
99 public String getTraceName() {
100 return name;
101 }
102 };
103 fStatsData.registerEvent(fEvent1, fExtraInfo);
104 fStatsData.registerEvent(fEvent2, fExtraInfo);
105
106 treeProvider = new TmfTreeContentProvider();
107 }
108
109 // ------------------------------------------------------------------------
110 // GetChildren
111 // ------------------------------------------------------------------------
112
113 /**
114 * Test getting of children.
115 */
116 public void testGetChildren() {
117 Object[] objectArray = treeProvider.getChildren(fStatsData.getOrCreate(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes)));
118 TmfStatisticsTreeNode[] childrenNode = Arrays.asList(objectArray).toArray(new TmfStatisticsTreeNode[0]);
119
120 Collection<TmfFixedArray<String>> childrenExpected = new Vector<TmfFixedArray<String>>();
121 childrenExpected.add(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName()));
122 childrenExpected.add(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent2.getType().getName()));
123
124 assertEquals("getChildren", childrenExpected.size(), childrenNode.length);
125 // assertTrue("getChildren", childrenPath.equals(childrenExpected));
126 for (TmfStatisticsTreeNode childNode : childrenNode) {
127 if (childrenExpected.contains(childNode.getPath())) {
128 childrenExpected.remove(childNode.getPath());
129 } else {
130 fail();
131 }
132 }
133 }
134
135 // ------------------------------------------------------------------------
136 // GetParent
137 // ------------------------------------------------------------------------
138
139 /**
140 * Test getting of parent.
141 */
142 public void testGetParent() {
143 TmfStatisticsTreeNode parent = (TmfStatisticsTreeNode) treeProvider.getParent(fStatsData.get(new TmfFixedArray<String>(fTestName)));
144
145 assertNotNull("getParent", parent);
146 assertTrue("getParent", parent.getPath().equals(AbsTmfStatisticsTree.ROOT));
147 }
148
149 // ------------------------------------------------------------------------
150 // HasChildren
151 // ------------------------------------------------------------------------
152 /**
153 * Test checking for children.
154 */
155 public void testHasChildren() {
156 Boolean hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(AbsTmfStatisticsTree.ROOT));
157 assertTrue("hasChildren", hasChildren);
158
159 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(new TmfFixedArray<String>(fTestName)));
160 assertTrue("hasChildren", hasChildren);
161
162 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes)));
163 assertTrue("hasChildren", hasChildren);
164
165 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName())));
166 assertFalse("hasChildren", hasChildren);
167 }
168
169 // ------------------------------------------------------------------------
170 // GetElements
171 // ------------------------------------------------------------------------
172
173 /**
174 * Test getting of elements.
175 */
176 public void testGetElements() {
177 Object[] objectElements = treeProvider.getElements(fStatsData.get(AbsTmfStatisticsTree.ROOT));
178 TmfStatisticsTreeNode[] nodeElements = Arrays.asList(objectElements).toArray(new TmfStatisticsTreeNode[0]);
179 assertEquals("getElements", 1, nodeElements.length);
180 assertTrue("getElements", nodeElements[0].getPath().equals(new TmfFixedArray<String>(fTestName)));
181 }
182 }
This page took 0.035289 seconds and 6 git commands to generate.