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