Merge branch 'master' into lttng_2_0_control_dev
[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.views.statistics.ITmfExtraEventInfo;
29 import org.eclipse.linuxtools.tmf.ui.views.statistics.model.AbsTmfStatisticsTree;
30 import org.eclipse.linuxtools.tmf.ui.views.statistics.model.Messages;
31 import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseStatisticsTree;
32 import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeNode;
33 import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfTreeContentProvider;
34
35 @SuppressWarnings("nls")
36 public class TmfTreeContentProviderTest extends TestCase {
37
38 // ------------------------------------------------------------------------
39 // Fields
40 // ------------------------------------------------------------------------
41
42 private String fTestName = null;
43
44 private final String fContext = "UnitTest";
45 private final String fTypeId1 = "Some type1";
46 private final String fTypeId2 = "Some type2";
47
48 private final String fLabel0 = "label1";
49 private final String fLabel1 = "label2";
50 private final String[] fLabels = new String[] { fLabel0, fLabel1 };
51
52 private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5);
53 private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5);
54
55 private final String fSource = "Source";
56
57 private final TmfEventType fType1 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels));
58 private final TmfEventType fType2 = new TmfEventType(fContext, fTypeId2, TmfEventField.makeRoot(fLabels));
59
60 private final String fReference = "Some reference";
61
62 private final TmfEvent fEvent1;
63 private final TmfEvent fEvent2;
64
65 private final TmfEventField fContent1;
66 private final TmfEventField fContent2;
67
68 private final TmfBaseStatisticsTree fStatsData;
69
70 private final ITmfExtraEventInfo fExtraInfo;
71
72 private final TmfTreeContentProvider treeProvider;
73
74 // ------------------------------------------------------------------------
75 // Housekeeping
76 // ------------------------------------------------------------------------
77
78 /**
79 * @param name
80 * of the test
81 */
82 public TmfTreeContentProviderTest(final String name) {
83 super(name);
84
85 fTestName = name;
86
87 fContent1 = new TmfEventField(ITmfEventField.ROOT_ID, "Some content");
88 fEvent1 = new TmfEvent(null, fTimestamp1, fSource, fType1, fContent1, fReference);
89
90 fContent2 = new TmfEventField(ITmfEventField.ROOT_ID, "Some other content");
91 fEvent2 = new TmfEvent(null, fTimestamp2, fSource, fType2, fContent2, fReference);
92
93 fStatsData = new TmfBaseStatisticsTree();
94 fExtraInfo = new ITmfExtraEventInfo() {
95 @Override
96 public String getTraceName() {
97 return name;
98 }
99 };
100 fStatsData.registerEvent(fEvent1, fExtraInfo);
101 fStatsData.registerEvent(fEvent2, fExtraInfo);
102
103 treeProvider = new TmfTreeContentProvider();
104 }
105
106 // ------------------------------------------------------------------------
107 // GetChildren
108 // ------------------------------------------------------------------------
109
110 public void testGetChildren() {
111 Object[] objectArray = treeProvider.getChildren(fStatsData.getOrCreate(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes)));
112 TmfStatisticsTreeNode[] childrenNode = Arrays.asList(objectArray).toArray(new TmfStatisticsTreeNode[0]);
113
114 Collection<TmfFixedArray<String>> childrenExpected = new Vector<TmfFixedArray<String>>();
115 childrenExpected.add(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()));
116 childrenExpected.add(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent2.getType().toString()));
117
118 assertEquals("getChildren", childrenExpected.size(), childrenNode.length);
119 // assertTrue("getChildren", childrenPath.equals(childrenExpected));
120 for (TmfStatisticsTreeNode childNode : childrenNode) {
121 if (childrenExpected.contains(childNode.getPath())) {
122 childrenExpected.remove(childNode.getPath());
123 } else {
124 fail();
125 }
126 }
127 }
128
129 // ------------------------------------------------------------------------
130 // GetParent
131 // ------------------------------------------------------------------------
132
133 public void testGetParent() {
134 TmfStatisticsTreeNode parent = (TmfStatisticsTreeNode) treeProvider.getParent(fStatsData.get(new TmfFixedArray<String>(fTestName)));
135
136 assertNotNull("getParent", parent);
137 assertTrue("getParent", parent.getPath().equals(AbsTmfStatisticsTree.ROOT));
138 }
139
140 // ------------------------------------------------------------------------
141 // HasChildren
142 // ------------------------------------------------------------------------
143
144 public void testHasChildren() {
145 Boolean hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(AbsTmfStatisticsTree.ROOT));
146 assertTrue("hasChildren", hasChildren);
147
148 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(new TmfFixedArray<String>(fTestName)));
149 assertTrue("hasChildren", hasChildren);
150
151 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes)));
152 assertTrue("hasChildren", hasChildren);
153
154 hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(new TmfFixedArray<String>(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())));
155 assertFalse("hasChildren", hasChildren);
156 }
157
158 // ------------------------------------------------------------------------
159 // GetElements
160 // ------------------------------------------------------------------------
161
162 public void testGetElements() {
163 Object[] objectElements = treeProvider.getElements(fStatsData.get(AbsTmfStatisticsTree.ROOT));
164 TmfStatisticsTreeNode[] nodeElements = Arrays.asList(objectElements).toArray(new TmfStatisticsTreeNode[0]);
165 assertEquals("getElements", 1, nodeElements.length);
166 assertTrue("getElements", nodeElements[0].getPath().equals(new TmfFixedArray<String>(fTestName)));
167 }
168 }
This page took 0.036233 seconds and 6 git commands to generate.