summaryrefslogtreecommitdiff
path: root/plugins/testng/src/com/theoryinpractice/testng/ui/ResultTreeRenderer.java
blob: ba30f43b66f01706612c46692d668099dee9966a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.theoryinpractice.testng.ui;

import com.intellij.execution.testframework.PoolOfTestIcons;
import com.intellij.icons.AllIcons;
import com.intellij.ui.ColoredTreeCellRenderer;
import com.intellij.ui.SimpleTextAttributes;
import com.theoryinpractice.testng.model.TestNGConsoleProperties;
import com.theoryinpractice.testng.model.TestNodeDescriptor;
import com.theoryinpractice.testng.model.TestProxy;
import com.theoryinpractice.testng.model.TreeRootNode;
import org.testng.remote.strprotocol.MessageHelper;
import org.testng.remote.strprotocol.TestResultMessage;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * @author Hani Suleiman Date: Jul 21, 2005 Time: 11:32:36 PM
 */
public class ResultTreeRenderer extends ColoredTreeCellRenderer
{
    private final TestNGConsoleProperties consoleProperties;

    public ResultTreeRenderer(TestNGConsoleProperties consoleProperties) {
        this.consoleProperties = consoleProperties;
    }

    @Override
    public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        if (node.getUserObject() instanceof TestNodeDescriptor) {
            TestProxy proxy = ((TestNodeDescriptor) node.getUserObject()).getElement();
            if (node == tree.getModel().getRoot()) {
                TreeRootNode root = (TreeRootNode) proxy;
                if (node.getChildCount() == 0) {
                    if ((root.isStarted() && root.isInProgress()) || (root.isInProgress() && !root.isStarted())) {
                        setIcon(PoolOfTestIcons.NOT_RAN);
                        append("Instantiating tests... ", SimpleTextAttributes.REGULAR_ATTRIBUTES);
                    } else if (root.isStarted() && !root.isInProgress()) {
                        setIcon(PoolOfTestIcons.PASSED_ICON);
                        append("All Tests Passed.", SimpleTextAttributes.REGULAR_ATTRIBUTES);
                    } else {
                        setIcon(PoolOfTestIcons.NOT_RAN);
                        append("No Test Results.", SimpleTextAttributes.REGULAR_ATTRIBUTES);
                    }

                } else {
                    setIcon(root.isInProgress() ? Animator.getCurrentFrame() : getIcon(proxy));
                    append(root.isInProgress() ? "Running tests..." : "Test Results", SimpleTextAttributes.REGULAR_ATTRIBUTES);
                }

                if (consoleProperties.isPaused()) {
                    setIcon(AllIcons.RunConfigurations.TestPaused);
                }
            } else {
                if (proxy.getResultMessage() != null) {
                  final TestResultMessage result = proxy.getResultMessage();
                  final String name = TestProxy.toDisplayText(result, consoleProperties.getProject());
                  append(name, SimpleTextAttributes.REGULAR_ATTRIBUTES);
                } else {
                    append(proxy.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
                }
                setIcon(proxy.isInProgress() ? Animator.getCurrentFrame() : proxy.isInterrupted() ? PoolOfTestIcons.NOT_RAN : getIcon(proxy));
            }
        } else {
            setIcon(Animator.getCurrentFrame());
            append(node.getUserObject() != null ? node.getUserObject().toString() : "null", SimpleTextAttributes.REGULAR_ATTRIBUTES);
        }
    }

    private Icon getIcon(TestProxy node) {
        if (node.isResult()) {
            TestResultMessage result = node.getResultMessage();
            switch (result.getResult()) {
                case MessageHelper.PASSED_TEST:
                    if (node.isTearDownFailure()) {
                        return PoolOfTestIcons.TEAR_DOWN_FAILURE;
                    }
                    return PoolOfTestIcons.PASSED_ICON;
                case MessageHelper.SKIPPED_TEST:
                    return PoolOfTestIcons.IGNORED_ICON;
                case MessageHelper.FAILED_TEST:
                    return PoolOfTestIcons.FAILED_ICON;
                case MessageHelper.TEST_STARTED:
                    return PoolOfTestIcons.TERMINATED_ICON;
            }
        } else {
            if (node.getChildCount() == 0) {
              final TestProxy nodeParent = node.getParent();
              if (nodeParent != null && nodeParent.isTearDownFailure()) {
                final TestResultMessage resultMessage = nodeParent.getResultMessage();
                if (resultMessage != null && resultMessage.getResult() == MessageHelper.PASSED_TEST) {
                  return PoolOfTestIcons.FAILED_ICON;
                }
                return PoolOfTestIcons.IGNORED_ICON;
              }
              return PoolOfTestIcons.NOT_RAN;
            }
            boolean hasFail = false;
            boolean hasSkipped = false;
            boolean hasTerminated = false;
            boolean hasTearDownFailure = false;
            for (TestProxy result : node.getChildren()) {
                Icon icon = getIcon(result);
                if (icon == PoolOfTestIcons.FAILED_ICON) {
                    hasFail = true;
                } else if (icon == PoolOfTestIcons.IGNORED_ICON) {
                    hasSkipped = true;
                } else if (icon == PoolOfTestIcons.TERMINATED_ICON) {
                    hasTerminated = true;
                } else if (result.isTearDownFailure()) {
                    hasTearDownFailure = true;
                }
            }
            if (hasTerminated) return PoolOfTestIcons.TERMINATED_ICON;
            if (hasFail) return PoolOfTestIcons.FAILED_ICON;
            if (hasSkipped) return PoolOfTestIcons.IGNORED_ICON;
            if (hasTearDownFailure) return PoolOfTestIcons.TEAR_DOWN_FAILURE;
        }
        return PoolOfTestIcons.PASSED_ICON;
    }
}