Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6264384

OppositeComponent test failed

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Cannot Reproduce
    • Affects Version/s: 1.4.2
    • Fix Version/s: None
    • Component/s: client-libs
    • Labels:
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      The following code try to exercise the following assertion:
      " If this focus change occurs with no other Component, then null is returned."

      But following code failed on J2SE 1.4/1.5: the test expects the
      fl1.getOppositeComponent() is null, but Frame is returned instead:

      ========================================================================
      import java.awt.*;
      import java.awt.event.*;

      public class OppositeComponent {
          public void checkCase1() {
              MyFocusListener fl = new MyFocusListener();
              MyFocusListener fl1 = new MyFocusListener();
              KeyboardFocusManager current = KeyboardFocusManager.
                      getCurrentKeyboardFocusManager();
              Frame frame = new Frame();
              frame.setVisible(true);
              createFocusedWindow(frame);
              LWComponent comp = new LWComponent();
              comp.addFocusListener(fl);
              frame.add(comp);
              if (!comp.isFocusOwner()) {
                  comp.requestFocusInWindow();
                  if (!isFocusGained(comp)) {
                      System.out.println("Failed: expected comp "+
                             "to gain the focus");
                      return;
                  }
              }
              System.out.println("comp.isFocusOwner() should return true: "+comp.isFocusOwner());
              current.clearGlobalFocusOwner();
              System.out.println("after clearGlobalFocusOwner() called");
              if (!fl.lostCalled) {
                  System.out.println("Failed, expected focusLost is called");
                  return;
              }
              System.out.println("current FocusOwner should be null: "+current.getFocusOwner());
              System.out.println("frame.isFocused() should return true: "+frame.isFocused());
              
              Component comp1 = new Button();
              comp1.addFocusListener(fl1);
              frame.add(comp1);
              comp1.requestFocusInWindow();
              if (!isFocusGained(comp1)) {
                  System.out.println("Failed: expected comp1 "+
                             "to gain the focus");
                  return;
              }
              if (!comp1.isFocusOwner()) {
                  System.out.println("Expected comp1 is the focusOwner, but got: "+current.getFocusOwner());
                  return;
              }
              if (fl1.getFocusEvent().getOppositeComponent() !=null) {
                  System.out.println("Failed: expected oppositeComponent is null, got: "+
                                     fl1.getFocusEvent().getOppositeComponent());
                  return;
              }
              System.out.println("OKAY");
              frame.dispose();
          }

              
        public static void main(String[] args) {
            OppositeComponent t = new OppositeComponent();
            t.checkCase1();
        }

          private void createFocusedWindow(Window window) {
              window.toFront();
              if (!window.isFocused()) {
                  Rectangle b = window.getBounds();
                  window.setSize(300, 50);
                  if (window instanceof Frame) {
                      ((Frame)window).setTitle("PLEASE FOCUS THIS FRAME");
                  }
                  try {
                      while (!window.isFocused()) {
                          Thread.sleep(500);
                      }
                  } catch (InterruptedException e) {
                      System.out.println("got interrupted Exception");
                  }
                  window.setBounds(b);
              }
          }
          
           private boolean isFocusGained(Component comp) {
              long endTime = System.currentTimeMillis() + 3000;
              try {
                  while (System.currentTimeMillis() < endTime &&
                         !comp.isFocusOwner()) {
                      Thread.sleep(1000);
                  }
              } catch (InterruptedException e) {
                  System.out.println("got interrupted Exception");
              }
              return comp.isFocusOwner();
          }

          class MyFocusListener implements FocusListener {
          
              FocusEvent evt = null;
              boolean gainCalled = false;
              public synchronized void focusGained( FocusEvent fe ) {
                  evt = fe;
                  gainCalled = true;
                  System.out.println("focusGained on: "+fe.getComponent());
                  System.out.println("and its oppositeComp which lost focus: "+fe.getOppositeComponent());
              }

              boolean lostCalled = false;
              public synchronized void focusLost( FocusEvent fe ) {
                  evt = fe;
                  lostCalled = true;
                  System.out.println("focusLost on : "+fe.getComponent());
                  System.out.println("and its oppositeComp which gained focus: "+fe.getOppositeComponent());
                   
              }
              
              public FocusEvent getFocusEvent() {
                  return evt;
              }
                    
          }
          class LWComponent extends Component {
              public LWComponent() {
                  super();
              }
          }
      }
      ============================================================================
          

      ###@###.### 2005-05-02 21:25:56 GMT

        Attachments

          Activity

            People

            • Assignee:
              ant Anton Tarasov
              Reporter:
              hji Huafang Ji (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: