|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MenuNavigationTestCase.java | 100% | 96.2% | 81.8% | 93.9% |
|
1 |
package menunav;
|
|
2 |
|
|
3 |
import java.awt.event.ActionEvent;
|
|
4 |
import javax.swing.AbstractAction;
|
|
5 |
import javax.swing.JFrame;
|
|
6 |
import javax.swing.JMenu;
|
|
7 |
import javax.swing.JMenuBar;
|
|
8 |
import javax.swing.JMenuItem;
|
|
9 |
|
|
10 |
import junit.extensions.jfcunit.JFCTestCase;
|
|
11 |
import junit.extensions.jfcunit.RobotTestHelper;
|
|
12 |
import junit.extensions.jfcunit.TestHelper;
|
|
13 |
import junit.extensions.jfcunit.eventdata.JMenuMouseEventData;
|
|
14 |
import junit.extensions.jfcunit.eventdata.PathData;
|
|
15 |
import junit.extensions.jfcunit.finder.FrameFinder;
|
|
16 |
import junit.extensions.jfcunit.finder.JMenuItemFinder;
|
|
17 |
import javax.swing.JSeparator;
|
|
18 |
|
|
19 |
/**
|
|
20 |
* <p>Title: Menu Navigation Sample.</p>
|
|
21 |
* <p>Description: Shows how menu navigation may be performed.</p>
|
|
22 |
* <p>Copyright: Copyright (c) 2002</p>
|
|
23 |
* <p>Company: JFCUnit OpenSource Project</p>
|
|
24 |
* @author Kevin Wilson
|
|
25 |
* @version 1.0
|
|
26 |
*/
|
|
27 |
public class MenuNavigationTestCase |
|
28 |
extends JFCTestCase {
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Frame used to host test cases.
|
|
32 |
*/
|
|
33 |
private JFrame m_frame = null; |
|
34 |
|
|
35 |
/**
|
|
36 |
* Constructor.
|
|
37 |
* @param name Name of the test case.
|
|
38 |
*/
|
|
39 | 2 |
public MenuNavigationTestCase(final String name) {
|
40 | 2 |
super(name);
|
41 |
} |
|
42 |
|
|
43 |
/**
|
|
44 |
* Set up the parameters for the test case.
|
|
45 |
* @throws Exception may be thrown.
|
|
46 |
*/
|
|
47 | 2 |
public void setUp() throws Exception { |
48 | 2 |
super.setUp();
|
49 | 2 |
setHelper(new RobotTestHelper());
|
50 |
} |
|
51 |
|
|
52 |
/**
|
|
53 |
* Cleanup any of the windows.
|
|
54 |
* @throws Exception may be thrown.
|
|
55 |
*/
|
|
56 | 2 |
public void tearDown() throws Exception { |
57 | 2 |
closeFrame(); |
58 | 2 |
TestHelper.cleanUp(this);
|
59 | 2 |
super.tearDown();
|
60 |
} |
|
61 |
|
|
62 |
/**
|
|
63 |
* Close the frame.
|
|
64 |
*/
|
|
65 | 4 |
public void closeFrame() { |
66 | 4 |
if (m_frame != null) { |
67 | 2 |
m_frame.setVisible(false);
|
68 | 2 |
m_frame.dispose(); |
69 | 2 |
m_frame = null;
|
70 |
} |
|
71 |
} |
|
72 |
|
|
73 |
/**
|
|
74 |
* Create and show the menu for the test case.
|
|
75 |
*/
|
|
76 | 2 |
public void makeAndShow() { |
77 | 2 |
closeFrame(); |
78 | 2 |
m_frame = new JFrame(getName());
|
79 | 2 |
JMenuBar mb = new JMenuBar();
|
80 | 2 |
JMenu file = new JMenu("File"); |
81 | 2 |
JMenu measure = new JMenu("Measure"); |
82 | 2 |
JMenuItem exit = new JMenuItem(new AbstractAction("Exit") { |
83 | 0 |
public void actionPerformed(final ActionEvent ae) { |
84 | 0 |
System.exit(0); |
85 |
} |
|
86 |
}); |
|
87 |
|
|
88 | 2 |
measure.add(new JMenuItem(new MenuAction("A"))); |
89 | 2 |
measure.add(new JMenuItem(new MenuAction("B"))); |
90 | 2 |
measure.add(new JMenuItem(new MenuAction("C"))); |
91 | 2 |
measure.add(new JMenuItem(new MenuAction("D"))); |
92 | 2 |
measure.add(new JSeparator());
|
93 | 2 |
measure.add(new JSeparator());
|
94 | 2 |
measure.add(new JMenuItem(new MenuAction("E"))); |
95 | 2 |
measure.add(new JMenuItem(new MenuAction("F"))); |
96 | 2 |
measure.add(new JMenuItem(new MenuAction("G"))); |
97 | 2 |
measure.add(new JMenuItem(new MenuAction("H"))); |
98 |
|
|
99 | 2 |
file.add(measure); |
100 | 2 |
file.add(new JSeparator());
|
101 | 2 |
file.add(exit); |
102 | 2 |
mb.add(file); |
103 |
|
|
104 | 2 |
m_frame.getContentPane().add(mb); |
105 | 2 |
m_frame.pack(); |
106 | 2 |
m_frame.show(); |
107 |
|
|
108 |
} |
|
109 |
|
|
110 |
/**
|
|
111 |
* Test the menu bar.
|
|
112 |
*/
|
|
113 | 1 |
public void testMenuBar() { |
114 | 1 |
makeAndShow(); |
115 | 1 |
FrameFinder f = new FrameFinder(getName());
|
116 | 1 |
JFrame frame = (JFrame) f.find(); |
117 |
|
|
118 | 1 |
JMenu file = (JMenu) new JMenuItemFinder("File").find(frame, 0); |
119 | 1 |
PathData path = new PathData(new String[] {"File", "Measure", "H"}); |
120 | 1 |
JMenuBar bar = (JMenuBar) path.getRoot(file); |
121 | 1 |
getHelper().enterClickAndLeave(new JMenuMouseEventData(this, bar, |
122 |
path.getIndexes(bar), 1, 0, false, 0));
|
|
123 | 1 |
flushAWT(); |
124 |
} |
|
125 |
|
|
126 |
/**
|
|
127 |
* Test the menu bar with different parameters.
|
|
128 |
*/
|
|
129 | 1 |
public void testMenuBar2() { |
130 | 1 |
makeAndShow(); |
131 | 1 |
FrameFinder f = new FrameFinder(getName());
|
132 | 1 |
JFrame frame = (JFrame) f.find(); |
133 | 1 |
JMenu file = (JMenu) new JMenuItemFinder("File").find(frame, 0); |
134 | 1 |
PathData path = new PathData(new String[] {"File", "Measure", "B"}); |
135 | 1 |
JMenuBar bar = (JMenuBar) path.getRoot(file); |
136 | 1 |
getHelper().enterClickAndLeave(new JMenuMouseEventData(this, bar, |
137 |
path.getIndexes(bar), 1, 0, false, 0));
|
|
138 | 1 |
flushAWT(); |
139 |
} |
|
140 |
|
|
141 |
/**
|
|
142 |
* Main method to run the test cases in this class.
|
|
143 |
* @param args Arguments to the main method. No arguments
|
|
144 |
* are required.
|
|
145 |
*/
|
|
146 | 0 |
public static void main(final String[] args) { |
147 | 0 |
junit.textui.TestRunner.run(MenuNavigationTestCase.class);
|
148 |
} |
|
149 |
|
|
150 |
/**
|
|
151 |
* Class MenuAction.
|
|
152 |
* This class implements a simple action listener to
|
|
153 |
* print the menu item title.
|
|
154 |
*/
|
|
155 |
private class MenuAction |
|
156 |
extends AbstractAction {
|
|
157 |
/**
|
|
158 |
* Constructor.
|
|
159 |
* @param a Title for the action.
|
|
160 |
*/
|
|
161 | 16 |
public MenuAction(final String a) {
|
162 | 16 |
super(a);
|
163 |
} |
|
164 |
|
|
165 |
/**
|
|
166 |
* Action performed. This method is called when the
|
|
167 |
* Action is performed by the menu item. The Action
|
|
168 |
* name is printed to stderr.
|
|
169 |
* @param ae ActionEvent
|
|
170 |
*/
|
|
171 | 2 |
public void actionPerformed(final ActionEvent ae) { |
172 | 2 |
System.err.println(getValue(AbstractAction.NAME)); |
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
|
|