| 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 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.ui.tests.statistics; |
| 14 | |
| 15 | import junit.framework.TestCase; |
| 16 | |
| 17 | import org.eclipse.jface.viewers.ColumnLabelProvider; |
| 18 | import org.eclipse.jface.viewers.Viewer; |
| 19 | import org.eclipse.jface.viewers.ViewerComparator; |
| 20 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfBaseColumnData; |
| 21 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfBaseColumnData.ITmfColumnPercentageProvider; |
| 22 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTree; |
| 23 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode; |
| 24 | import org.eclipse.swt.SWT; |
| 25 | import org.eclipse.swt.graphics.Image; |
| 26 | import org.eclipse.ui.ISharedImages; |
| 27 | import org.eclipse.ui.PlatformUI; |
| 28 | |
| 29 | /** |
| 30 | * TmfBaseColumnData Test Case. |
| 31 | */ |
| 32 | @SuppressWarnings("nls") |
| 33 | public class TmfBaseColumnDataTest extends TestCase { |
| 34 | |
| 35 | // ------------------------------------------------------------------------ |
| 36 | // Fields |
| 37 | // ------------------------------------------------------------------------ |
| 38 | |
| 39 | private int fAlignment; |
| 40 | private int fWidth; |
| 41 | private String fHeader; |
| 42 | private String fToolTip; |
| 43 | private ColumnLabelProvider fLabelProvider; |
| 44 | private ViewerComparator fComparator; |
| 45 | private ITmfColumnPercentageProvider fPercentageProvider; |
| 46 | private TmfStatisticsTreeNode fTreeNode; |
| 47 | private String fTraceName; |
| 48 | private TmfBaseColumnData fBaseColumnData; |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | // Housekeeping |
| 52 | // ------------------------------------------------------------------------ |
| 53 | |
| 54 | private void init() { |
| 55 | fHeader = "test Column1"; |
| 56 | fWidth = 300; |
| 57 | fAlignment = SWT.LEFT; |
| 58 | fToolTip = "Tooltip " + fHeader; |
| 59 | fLabelProvider = new ColumnLabelProvider() { |
| 60 | @Override |
| 61 | public String getText(Object element) { |
| 62 | return ((TmfStatisticsTreeNode) element).getKey(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public Image getImage(Object element) { |
| 67 | return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); |
| 68 | } |
| 69 | }; |
| 70 | fComparator = new ViewerComparator() { |
| 71 | @Override |
| 72 | public int compare(Viewer viewer, Object e1, Object e2) { |
| 73 | TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1; |
| 74 | TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2; |
| 75 | |
| 76 | return n1.getKey().compareTo(n2.getKey()); |
| 77 | } |
| 78 | }; |
| 79 | fPercentageProvider = new ITmfColumnPercentageProvider() { |
| 80 | @Override |
| 81 | public double getPercentage(TmfStatisticsTreeNode node) { |
| 82 | TmfStatisticsTreeNode parent = node; |
| 83 | do { |
| 84 | parent = parent.getParent(); |
| 85 | } while (parent != null && parent.getValues().getTotal() == 0); |
| 86 | |
| 87 | if (parent == null) { |
| 88 | return 0; |
| 89 | } |
| 90 | return (double) node.getValues().getTotal() / parent.getValues().getTotal(); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | TmfStatisticsTree baseData = new TmfStatisticsTree(); |
| 95 | fTraceName = "trace1"; |
| 96 | fTreeNode = new TmfStatisticsTreeNode(baseData, fTraceName); |
| 97 | |
| 98 | fBaseColumnData = new TmfBaseColumnData(fHeader, fWidth, fAlignment, fToolTip, fLabelProvider, fComparator, fPercentageProvider); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void setUp() throws Exception { |
| 103 | super.setUp(); |
| 104 | init(); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void tearDown() throws Exception { |
| 109 | super.tearDown(); |
| 110 | } |
| 111 | |
| 112 | // ------------------------------------------------------------------------ |
| 113 | // getHeader |
| 114 | // ------------------------------------------------------------------------ |
| 115 | |
| 116 | /** |
| 117 | * Test get header |
| 118 | */ |
| 119 | public void testGetHeader() { |
| 120 | assertEquals("getHeader", 0, fBaseColumnData.getHeader().compareTo(fHeader)); |
| 121 | } |
| 122 | |
| 123 | // ------------------------------------------------------------------------ |
| 124 | // getWidth |
| 125 | // ------------------------------------------------------------------------ |
| 126 | |
| 127 | /** |
| 128 | * Test getting of column width. |
| 129 | */ |
| 130 | public void testGetWidth() { |
| 131 | assertEquals("getWidth", fWidth, fBaseColumnData.getWidth()); |
| 132 | } |
| 133 | |
| 134 | // ------------------------------------------------------------------------ |
| 135 | // getAlignment |
| 136 | // ------------------------------------------------------------------------ |
| 137 | |
| 138 | /** |
| 139 | * Test getting of alignment value |
| 140 | */ |
| 141 | public void testGetAlignment() { |
| 142 | assertEquals("getAlignment", fAlignment, fBaseColumnData.getAlignment()); |
| 143 | } |
| 144 | |
| 145 | // ------------------------------------------------------------------------ |
| 146 | // getToolTip |
| 147 | // ------------------------------------------------------------------------ |
| 148 | |
| 149 | /** |
| 150 | * Test getting of tooltip. |
| 151 | */ |
| 152 | public void testGetTooltip() { |
| 153 | assertEquals("getTooltip", fToolTip, fBaseColumnData.getTooltip()); |
| 154 | } |
| 155 | |
| 156 | // ------------------------------------------------------------------------ |
| 157 | // getLabelProvider |
| 158 | // ------------------------------------------------------------------------ |
| 159 | |
| 160 | /** |
| 161 | * Test getting of label provider |
| 162 | */ |
| 163 | public void testGetLabelProvider() { |
| 164 | assertEquals("getLabelProvider", 0, fBaseColumnData.getLabelProvider().getText(fTreeNode).compareTo(fLabelProvider.getText(fTreeNode))); |
| 165 | assertTrue("getLabelProvider", fBaseColumnData.getLabelProvider().getImage(fTreeNode).equals(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT))); |
| 166 | assertTrue("getLabelProvider", fBaseColumnData.getLabelProvider().equals(fLabelProvider)); |
| 167 | } |
| 168 | |
| 169 | // ------------------------------------------------------------------------ |
| 170 | // getComparator |
| 171 | // ------------------------------------------------------------------------ |
| 172 | |
| 173 | /** |
| 174 | * Test getting of comparator. |
| 175 | */ |
| 176 | public void testGetComparator() { |
| 177 | assertTrue("getComparator", fBaseColumnData.getComparator().equals(fComparator)); |
| 178 | } |
| 179 | |
| 180 | // ------------------------------------------------------------------------ |
| 181 | // getPercentageProvider |
| 182 | // ------------------------------------------------------------------------ |
| 183 | |
| 184 | /** |
| 185 | * Test getting of percentage provider. |
| 186 | */ |
| 187 | public void testGetPercentageProvider() { |
| 188 | assertTrue("getPercentageProvider", fBaseColumnData.getPercentageProvider().equals(fPercentageProvider)); |
| 189 | } |
| 190 | } |