Advertisement
jdalbey

FocusTraversalTest.java

May 2nd, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. package psphistory;
  2.  
  3. import java.awt.Color;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.FocusEvent;
  7. import java.awt.event.KeyEvent;
  8. import javax.swing.JButton;
  9. import javax.swing.JLabel;
  10. import javax.swing.JTextField;
  11. import static junit.framework.Assert.assertEquals;
  12. import junit.framework.TestCase;
  13.  
  14. /**
  15. * Test of MyFocusTraversalPolicy, MyKeyAdapter, & MyFocusListener
  16. *
  17. * @author jdalbey
  18. * @version 5/2/2017
  19. */
  20. public class FocusTraversalTest extends TestCase implements ActionListener
  21. {
  22. private boolean actionFlag; // was actionperformed called?
  23.  
  24. public FocusTraversalTest(String testName)
  25. {
  26. super(testName);
  27. }
  28.  
  29. @Override
  30. public void setUp()
  31. {
  32. actionFlag = false;
  33. }
  34.  
  35. public void testMyFocusTraversalPolicy()
  36. {
  37. HistoryView hv = new HistoryView(null);
  38. HistoryView.MyFocusTraversalPolicy ftp = hv.new MyFocusTraversalPolicy();
  39. JLabel one = new JLabel();
  40. JLabel two = new JLabel();
  41. ftp.addComponent(one);
  42. ftp.addComponent(two);
  43. assertEquals(two, ftp.getComponentAfter(null, one));
  44. assertEquals(one, ftp.getComponentAfter(null, two));
  45. assertEquals(two, ftp.getComponentBefore(null, one));
  46. assertEquals(one, ftp.getComponentBefore(null, two));
  47. assertEquals(one, ftp.getDefaultComponent(null));
  48. assertEquals(one, ftp.getFirstComponent(null));
  49. assertEquals(two, ftp.getLastComponent(null));
  50. }
  51.  
  52. public void testMyKeyAdapter()
  53. {
  54. HistoryView hv = new HistoryView(null);
  55. HistoryView.MyKeyAdapter ka = hv.new MyKeyAdapter();
  56. JTextField tf = new JTextField();
  57. KeyEvent evt = new KeyEvent(tf, 0, 0, 0, 0);
  58. ka.keyPressed(evt);
  59. assertEquals(Color.white, tf.getBackground());
  60. }
  61.  
  62. public void testMyFocusListener()
  63. {
  64. HistoryView hv = new HistoryView(null);
  65. HistoryView.MyFocusListener fl = hv.new MyFocusListener();
  66. JButton btn = new JButton();
  67. JTextField tf = new JTextField();
  68. hv.btnNextPgm.addActionListener(this);
  69. FocusEvent evt = new FocusEvent(tf, FocusEvent.FOCUS_LOST, true, hv.btnNextPgm);
  70. fl.focusGained(null);
  71. fl.focusLost(evt);
  72. assertTrue(actionFlag);
  73. actionFlag = false;
  74. // these tests shouldn't trigger actionPerformed
  75. evt = new FocusEvent(tf, FocusEvent.FOCUS_LOST, true, btn);
  76. fl.focusLost(evt);
  77. evt = new FocusEvent(btn, FocusEvent.FOCUS_LOST, true, hv.btnNextPgm);
  78. fl.focusLost(evt);
  79. assertFalse(actionFlag);
  80. }
  81.  
  82. @Override
  83. public void actionPerformed(ActionEvent ae)
  84. {
  85. actionFlag = true;
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement