Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package psphistory;
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.FocusEvent;
- import java.awt.event.KeyEvent;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import static junit.framework.Assert.assertEquals;
- import junit.framework.TestCase;
- /**
- * Test of MyFocusTraversalPolicy, MyKeyAdapter, & MyFocusListener
- *
- * @author jdalbey
- * @version 5/2/2017
- */
- public class FocusTraversalTest extends TestCase implements ActionListener
- {
- private boolean actionFlag; // was actionperformed called?
- public FocusTraversalTest(String testName)
- {
- super(testName);
- }
- @Override
- public void setUp()
- {
- actionFlag = false;
- }
- public void testMyFocusTraversalPolicy()
- {
- HistoryView hv = new HistoryView(null);
- HistoryView.MyFocusTraversalPolicy ftp = hv.new MyFocusTraversalPolicy();
- JLabel one = new JLabel();
- JLabel two = new JLabel();
- ftp.addComponent(one);
- ftp.addComponent(two);
- assertEquals(two, ftp.getComponentAfter(null, one));
- assertEquals(one, ftp.getComponentAfter(null, two));
- assertEquals(two, ftp.getComponentBefore(null, one));
- assertEquals(one, ftp.getComponentBefore(null, two));
- assertEquals(one, ftp.getDefaultComponent(null));
- assertEquals(one, ftp.getFirstComponent(null));
- assertEquals(two, ftp.getLastComponent(null));
- }
- public void testMyKeyAdapter()
- {
- HistoryView hv = new HistoryView(null);
- HistoryView.MyKeyAdapter ka = hv.new MyKeyAdapter();
- JTextField tf = new JTextField();
- KeyEvent evt = new KeyEvent(tf, 0, 0, 0, 0);
- ka.keyPressed(evt);
- assertEquals(Color.white, tf.getBackground());
- }
- public void testMyFocusListener()
- {
- HistoryView hv = new HistoryView(null);
- HistoryView.MyFocusListener fl = hv.new MyFocusListener();
- JButton btn = new JButton();
- JTextField tf = new JTextField();
- hv.btnNextPgm.addActionListener(this);
- FocusEvent evt = new FocusEvent(tf, FocusEvent.FOCUS_LOST, true, hv.btnNextPgm);
- fl.focusGained(null);
- fl.focusLost(evt);
- assertTrue(actionFlag);
- actionFlag = false;
- // these tests shouldn't trigger actionPerformed
- evt = new FocusEvent(tf, FocusEvent.FOCUS_LOST, true, btn);
- fl.focusLost(evt);
- evt = new FocusEvent(btn, FocusEvent.FOCUS_LOST, true, hv.btnNextPgm);
- fl.focusLost(evt);
- assertFalse(actionFlag);
- }
- @Override
- public void actionPerformed(ActionEvent ae)
- {
- actionFlag = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement