testdev/ite/src/com.nokia.testfw.resultview/src/com/nokia/testfw/resultview/view/TestResultTree.java
changeset 1 96906a986c3b
equal deleted inserted replaced
0:f1112f777ce9 1:96906a986c3b
       
     1 /*
       
     2  * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  */
       
    17 package com.nokia.testfw.resultview.view;
       
    18 
       
    19 import java.util.Hashtable;
       
    20 import java.util.Stack;
       
    21 
       
    22 import org.eclipse.swt.SWT;
       
    23 import org.eclipse.swt.graphics.Image;
       
    24 import org.eclipse.swt.layout.GridData;
       
    25 import org.eclipse.swt.widgets.Composite;
       
    26 import org.eclipse.swt.widgets.Tree;
       
    27 import org.eclipse.swt.widgets.TreeItem;
       
    28 
       
    29 import com.nokia.testfw.core.model.result.TestCaseResult;
       
    30 import com.nokia.testfw.core.model.result.TestResult;
       
    31 import com.nokia.testfw.core.model.result.TestRunResult;
       
    32 import com.nokia.testfw.core.model.result.TestSuiteResult;
       
    33 import com.nokia.testfw.resultview.ResultViewPlugin;
       
    34 
       
    35 public class TestResultTree {
       
    36 	private static final Image passedIcon = ResultViewPlugin
       
    37 			.getImage("obj16/testok.gif"); //$NON-NLS-1$
       
    38 	private static final Image failedIcon = ResultViewPlugin
       
    39 			.getImage("obj16/testfail.gif"); //$NON-NLS-1$
       
    40 	private static final Image testIcon = ResultViewPlugin
       
    41 			.getImage("obj16/test.gif"); //$NON-NLS-1$
       
    42 	private static final Image skipIcon = ResultViewPlugin
       
    43 			.getImage("obj16/testskip.gif"); //$NON-NLS-1$
       
    44 	private static final Image runIcon = ResultViewPlugin
       
    45 			.getImage("obj16/testrun.gif"); //$NON-NLS-1$
       
    46 
       
    47 	Tree tree;
       
    48 	Hashtable<String, TreeItem> treeItems;
       
    49 
       
    50 	public TestResultTree(Composite parent) {
       
    51 		treeItems = new Hashtable<String, TreeItem>();
       
    52 		tree = new Tree(parent, SWT.V_SCROLL | SWT.SINGLE);
       
    53 		GridData gridData = new GridData(GridData.FILL_BOTH
       
    54 				| GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
       
    55 
       
    56 		tree.setLayoutData(gridData);
       
    57 	}
       
    58 
       
    59 	public Tree getTree() {
       
    60 		return tree;
       
    61 	}
       
    62 
       
    63 	/**
       
    64 	 * initial the tree with empty test result
       
    65 	 */
       
    66 	public void init(TestRunResult runResult) {
       
    67 		// create a root node
       
    68 		for (TestResult result : runResult.getResults()) {
       
    69 			createTreeItems(null, result);
       
    70 		}
       
    71 	}
       
    72 
       
    73 	/**
       
    74 	 * update one test case result
       
    75 	 * 
       
    76 	 * @param result
       
    77 	 */
       
    78 	public void update(TestCaseResult result) {
       
    79 		// since it is using same result object, just redraw the tree
       
    80 		TreeItem item = treeItems.get(result.getUniqueName());
       
    81 		updateTreeItem(item);
       
    82 		// make sure to show this item in the tree
       
    83 		tree.showItem(item);
       
    84 		// update parent suite is any
       
    85 		TestSuiteResult suite;
       
    86 		TestResult child = result;
       
    87 		while ((suite = child.getParent()) != null) {
       
    88 			updateTreeItem(treeItems.get(suite.getUniqueName()));
       
    89 			child = suite;
       
    90 		}
       
    91 	}
       
    92 
       
    93 	public boolean addTestCase(TestCaseResult result) {
       
    94 		if (treeItems.get(result.getUniqueName()) != null)
       
    95 			return false;
       
    96 
       
    97 		TestSuiteResult parent = result.getParent();
       
    98 		if (parent == null) {
       
    99 			createNewTreeItem(null, result);
       
   100 		} else {
       
   101 			Stack<TestResult> stack = new Stack<TestResult>();
       
   102 			TreeItem parentItem = null;
       
   103 			stack.push(result);
       
   104 			while (parent != null) {
       
   105 				parentItem = treeItems.get(parent.getUniqueName());
       
   106 				if (parentItem == null) {
       
   107 					stack.push(parent);
       
   108 				} else {
       
   109 					break;
       
   110 				}
       
   111 				parent = parent.getParent();
       
   112 			}
       
   113 			while (!stack.isEmpty()) {
       
   114 				TestResult test = stack.pop();
       
   115 				parentItem = createNewTreeItem(parentItem, test);
       
   116 			}
       
   117 		}
       
   118 		return true;
       
   119 	}
       
   120 
       
   121 	/**
       
   122 	 * reset the result tree and clean it is contents
       
   123 	 */
       
   124 	public void reset() {
       
   125 		tree.removeAll();
       
   126 		treeItems.clear();
       
   127 	}
       
   128 
       
   129 	/**
       
   130 	 * create one tree item to show a testcase(suite) result
       
   131 	 * 
       
   132 	 * @param testResult
       
   133 	 */
       
   134 	protected void createTreeItems(TreeItem parent, TestResult testResult) {
       
   135 		TreeItem item = createNewTreeItem(parent, testResult);
       
   136 		// create items for its children is any
       
   137 		if (testResult instanceof TestSuiteResult) {
       
   138 			TestSuiteResult suite = (TestSuiteResult) testResult;
       
   139 			for (TestResult child : suite.getChildren()) {
       
   140 				createTreeItems(item, child);
       
   141 			}
       
   142 		}
       
   143 	}
       
   144 
       
   145 	private TreeItem createNewTreeItem(TreeItem parentItem, TestResult result) {
       
   146 		TreeItem treeItem = null;
       
   147 		if (parentItem == null) {
       
   148 			treeItem = new TreeItem(tree, SWT.NONE);
       
   149 		} else {
       
   150 			treeItem = new TreeItem(parentItem, SWT.NONE);
       
   151 		}
       
   152 		treeItem.setData(result);
       
   153 		treeItem.setExpanded(true);
       
   154 		updateTreeItem(treeItem);
       
   155 		// set to show all items. may change to only show suite later
       
   156 		tree.showItem(treeItem);
       
   157 		treeItems.put(result.getUniqueName(), treeItem);
       
   158 		return treeItem;
       
   159 	}
       
   160 
       
   161 	private void updateTreeItem(TreeItem item) {
       
   162 		TestResult result = (TestResult) item.getData();
       
   163 		item.setText(result.getDesc());
       
   164 
       
   165 		switch (result.getStatus()) {
       
   166 		case SUCCESS:
       
   167 			item.setImage(passedIcon);
       
   168 			break;
       
   169 		case FAILURE:
       
   170 			item.setImage(failedIcon);
       
   171 			break;
       
   172 		case SKIP:
       
   173 			item.setImage(skipIcon);
       
   174 			break;
       
   175 		case STARTED:
       
   176 			item.setImage(runIcon);
       
   177 			break;
       
   178 		default:
       
   179 			item.setImage(testIcon);
       
   180 		}
       
   181 
       
   182 	}
       
   183 
       
   184 	// // handle the double click selection
       
   185 	// private void setupSelection() {
       
   186 	// tree.addListener(SWT.Selection, new Listener() {
       
   187 	//
       
   188 	// public void handleEvent(Event event) {
       
   189 	// TreeItem item = tree.getSelection()[0];
       
   190 	// if (item == null) {
       
   191 	// return;
       
   192 	// }
       
   193 	// TestResult result = (TestResult) item.getData();
       
   194 	// if (result == null) {
       
   195 	// ResultViewPlugin.log(IStatus.WARNING,
       
   196 	// "can't find selected test result");
       
   197 	// }
       
   198 	// // find the file
       
   199 	// if (result.getFile() == null) {
       
   200 	// ResultViewPlugin.log(IStatus.OK,
       
   201 	// "no locaiton information associated with the test");
       
   202 	// return;
       
   203 	// }
       
   204 	// // find file in workspace
       
   205 	// WorkspaceUtils.openFile(result.getFile(), result.getLine(),
       
   206 	// result.getColumn());
       
   207 	// }
       
   208 	//
       
   209 	// });
       
   210 	// }
       
   211 }