|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| HeavyWeightT.java | 50% | 95.8% | 91.7% | 93.5% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit.eventdata;
|
|
| 2 |
|
|
| 3 |
import java.util.Vector;
|
|
| 4 |
|
|
| 5 |
import java.awt.event.ActionEvent;
|
|
| 6 |
import java.awt.event.ActionListener;
|
|
| 7 |
import java.awt.event.MouseAdapter;
|
|
| 8 |
import java.awt.event.MouseEvent;
|
|
| 9 |
import javax.swing.JButton;
|
|
| 10 |
import javax.swing.JFrame;
|
|
| 11 |
import javax.swing.JMenuItem;
|
|
| 12 |
import javax.swing.JPopupMenu;
|
|
| 13 |
|
|
| 14 |
import junit.extensions.jfcunit.JFCTestCase;
|
|
| 15 |
import junit.extensions.jfcunit.JFCTestHelper;
|
|
| 16 |
import junit.extensions.jfcunit.finder.JMenuItemFinder;
|
|
| 17 |
import junit.textui.TestRunner;
|
|
| 18 |
import junit.extensions.jfcunit.finder.Finder;
|
|
| 19 |
import junit.extensions.jfcunit.finder.ComponentFinder;
|
|
| 20 |
import junit.extensions.jfcunit.finder.FrameFinder;
|
|
| 21 |
|
|
| 22 |
/**
|
|
| 23 |
* Illustrates the difficulties in clicking a JMenuItem in a
|
|
| 24 |
* heavyweight popup.
|
|
| 25 |
* The WindowMonitor is started in the constructor of this test case.
|
|
| 26 |
* If member variable lightWeightPopupEnabled is set to true
|
|
| 27 |
* the test case will output text "Item selected" to System.out.
|
|
| 28 |
* If lightWeightPopupEnabled is false the popup is heavyweight and
|
|
| 29 |
* the test case fails to select the menu item correctly.
|
|
| 30 |
* In this case the text "Item selected" is not displayed.
|
|
| 31 |
*/
|
|
| 32 |
public class HeavyWeightT |
|
| 33 |
extends JFCTestCase {
|
|
| 34 |
|
|
| 35 |
/**
|
|
| 36 |
* The popup will be lightweight if the value is set to true.
|
|
| 37 |
*/
|
|
| 38 |
private boolean m_lightWeightEnabled = true; |
|
| 39 |
/**
|
|
| 40 |
* Test helper to be used.
|
|
| 41 |
*/
|
|
| 42 |
private static JFCTestHelper s_helper; |
|
| 43 |
/**
|
|
| 44 |
* Events which are collected.
|
|
| 45 |
*/
|
|
| 46 |
private final Vector m_events = new Vector(); |
|
| 47 |
|
|
| 48 |
/**
|
|
| 49 |
* Constructor.
|
|
| 50 |
* @param name Name of the test case.
|
|
| 51 |
*/
|
|
| 52 | 2 |
public HeavyWeightT(final String name) {
|
| 53 | 2 |
super(name);
|
| 54 | 2 |
junit.extensions.jfcunit.WindowMonitor.start(); |
| 55 |
} |
|
| 56 |
|
|
| 57 |
/**
|
|
| 58 |
* Shows a window. A popup is displayed by right-clicking a component.
|
|
| 59 |
* @param light true for light weight component.
|
|
| 60 |
*/
|
|
| 61 | 2 |
void showFrame(final boolean light) { |
| 62 | 2 |
JFrame f = new JFrame("Popup Test"); |
| 63 |
|
|
| 64 | 2 |
MouseAdapter mouseListener = new MouseAdapter() {
|
| 65 | 2 |
public void mouseReleased(final MouseEvent evt) { |
| 66 | 2 |
if (evt.isPopupTrigger()) {
|
| 67 | 2 |
TestPopup popup = new TestPopup(light);
|
| 68 | 2 |
popup.show(evt.getComponent(), evt.getX(), evt.getY()); |
| 69 |
} |
|
| 70 |
} |
|
| 71 |
}; |
|
| 72 | 2 |
JButton button = new JButton();
|
| 73 | 2 |
button.addMouseListener(mouseListener); |
| 74 | 2 |
button.setPreferredSize(new java.awt.Dimension(400, 400));
|
| 75 | 2 |
f.getContentPane().add(button); |
| 76 | 2 |
f.pack(); |
| 77 | 2 |
f.show(); |
| 78 |
} |
|
| 79 |
|
|
| 80 |
/**
|
|
| 81 |
* Setup the test case.
|
|
| 82 |
*/
|
|
| 83 | 2 |
public void setUp() { |
| 84 | 2 |
s_helper = new JFCTestHelper();
|
| 85 |
} |
|
| 86 |
|
|
| 87 |
/**
|
|
| 88 |
* Tear down the test case.
|
|
| 89 |
* @throws Exception may be thrown.
|
|
| 90 |
*/
|
|
| 91 | 2 |
public void tearDown() throws Exception { |
| 92 | 2 |
Finder f = new FrameFinder("Popup Test"); |
| 93 | 2 |
JFrame frame = (JFrame) f.find(); |
| 94 |
// JFrame frame = (JFrame) TestHelper.getWindow("Popup Test");
|
|
| 95 | 2 |
frame.dispose(); |
| 96 |
} |
|
| 97 |
|
|
| 98 |
/**
|
|
| 99 |
* Enter the click and wait for the events.
|
|
| 100 |
* @param evtData AbstractMouseEventData Event to be processed.
|
|
| 101 |
*/
|
|
| 102 | 2 |
private void clickAndWait(final AbstractMouseEventData evtData) { |
| 103 | 2 |
s_helper.enterClickAndLeave(evtData); |
| 104 | 2 |
awtSleep(); |
| 105 |
|
|
| 106 | 2 |
try {
|
| 107 | 2 |
Thread.sleep(1000); |
| 108 |
} catch (InterruptedException ie) {
|
|
| 109 |
// don't do anything, but still catch it
|
|
| 110 |
} |
|
| 111 |
} |
|
| 112 |
|
|
| 113 |
/**
|
|
| 114 |
* Test the heavy weight popup.
|
|
| 115 |
*/
|
|
| 116 | 1 |
public void testPopupHeavy() { |
| 117 | 1 |
popupTest(false);
|
| 118 |
} |
|
| 119 |
|
|
| 120 |
/**
|
|
| 121 |
* Test the light weight component.
|
|
| 122 |
*/
|
|
| 123 | 1 |
public void testPopupLight() { |
| 124 | 1 |
popupTest(true);
|
| 125 |
} |
|
| 126 |
|
|
| 127 |
/**
|
|
| 128 |
* Execute the popup test with the appropriate
|
|
| 129 |
* type of popup menu.
|
|
| 130 |
* @param light true for light weight menus.
|
|
| 131 |
* false for heavy weight menus.
|
|
| 132 |
*/
|
|
| 133 | 2 |
private void popupTest(final boolean light) { |
| 134 |
|
|
| 135 | 2 |
showFrame(light); |
| 136 | 2 |
Finder f = new FrameFinder("Popup Test"); |
| 137 | 2 |
JFrame frame = (JFrame) f.find(); |
| 138 | 2 |
f = new ComponentFinder(JButton.class); |
| 139 | 2 |
JButton button = (JButton) f.find(frame, 0); |
| 140 |
|
|
| 141 |
// Open the popup menu
|
|
| 142 | 2 |
s_helper.enterClickAndLeave(new MouseEventData(this, button, 1, true)); |
| 143 |
|
|
| 144 |
// Find a menu item
|
|
| 145 | 2 |
f = new JMenuItemFinder("Test 1"); |
| 146 | 2 |
JMenuItem menuItem = (JMenuItem) f.find(); |
| 147 |
|
|
| 148 |
// Check that the menu item was found
|
|
| 149 | 2 |
assertNotNull("Menu item not found", menuItem);
|
| 150 |
|
|
| 151 | 2 |
m_events.clear(); |
| 152 |
// Click the menu item
|
|
| 153 | 2 |
clickAndWait(new MouseEventData(this, menuItem)); |
| 154 | 2 |
assertEquals("Click on Menu", 1, m_events.size());
|
| 155 |
|
|
| 156 |
} |
|
| 157 |
|
|
| 158 |
/**
|
|
| 159 |
* Main method.
|
|
| 160 |
* @param args No arguments are required or processed.
|
|
| 161 |
*/
|
|
| 162 | 0 |
public static void main(final String[] args) { |
| 163 |
// Run the test
|
|
| 164 | 0 |
TestRunner.run(HeavyWeightT.class);
|
| 165 | 0 |
System.exit(0); |
| 166 |
} |
|
| 167 |
|
|
| 168 |
/**
|
|
| 169 |
* This is the test popup class.
|
|
| 170 |
*/
|
|
| 171 |
class TestPopup
|
|
| 172 |
extends JPopupMenu {
|
|
| 173 |
|
|
| 174 |
/**
|
|
| 175 |
* Constructor.
|
|
| 176 |
* @param lightWeightPopupEnabled true for light weight components.
|
|
| 177 |
*/
|
|
| 178 | 2 |
public TestPopup(final boolean lightWeightPopupEnabled) { |
| 179 | 2 |
setLightWeightPopupEnabled(lightWeightPopupEnabled); |
| 180 |
|
|
| 181 | 2 |
JMenuItem it1 = new JMenuItem("Test 1"); |
| 182 | 2 |
JMenuItem it2 = new JMenuItem("Test 2"); |
| 183 | 2 |
JMenuItem it3 = new JMenuItem("Test 3"); |
| 184 |
|
|
| 185 | 2 |
this.add(it1);
|
| 186 | 2 |
this.add(it2);
|
| 187 | 2 |
this.add(it3);
|
| 188 | 2 |
this.pack();
|
| 189 |
|
|
| 190 | 2 |
ActionListener listener = new ActionListener() {
|
| 191 | 2 |
public void actionPerformed(final ActionEvent arg0) { |
| 192 | 2 |
m_events.add(arg0); |
| 193 |
} |
|
| 194 |
}; |
|
| 195 |
|
|
| 196 | 2 |
it1.addActionListener(listener); |
| 197 |
} |
|
| 198 |
} |
|
| 199 |
} |
|
| 200 |
|
|
||||||||||