1 /*
   2  * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   test
  26   @bug 6387275
  27   @summary List: the focus is at the top of the first item, XAWT
  28   @author Dmitry.Cherepanov@SUN.COM area=awt.list
  29   @run applet FocusEmptyListTest.html
  30 */
  31 
  32 import java.applet.Applet;
  33 import java.awt.*;
  34 import java.lang.reflect.*;
  35 import java.awt.peer.ListPeer;
  36 
  37 import sun.awt.AWTAccessor;
  38 
  39 public class FocusEmptyListTest extends Applet {
  40 
  41     public void init() {
  42         setLayout(new BorderLayout());
  43     }//End  init()
  44 
  45     public void start() {
  46         boolean isXToolkit = Toolkit.getDefaultToolkit()
  47             .getClass().getName().equals("sun.awt.X11.XToolkit");
  48         if (!isXToolkit) {
  49             System.out.println("The test is XAWT-only.");
  50             return;
  51         }
  52 
  53         List list = new List();
  54         Object isIndexDisplayed = null;
  55         setLayout(new FlowLayout());
  56 
  57         getToolkit().addAWTEventListener(System.out::println,
  58             AWTEvent.FOCUS_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK);
  59 
  60         add(list);
  61         list.add("item1");
  62 
  63         setSize(200, 200);
  64         setVisible(true);
  65         validate();
  66 
  67         list.removeAll();
  68 
  69         try {
  70 
  71             // peer = List.getPeer()
  72             ListPeer peer = AWTAccessor.getComponentAccessor().getPeer(list);
  73             System.out.println("peer = " + peer);
  74             Class peerClass = peer.getClass();
  75             System.out.println("peer's class = " + peerClass);
  76 
  77             // isIndexDisplayed = peer.isIndexDisplayed(-1)
  78             Method isIndexDisplayedM
  79                 = peerClass.getDeclaredMethod("isIndexDisplayed", Integer.TYPE);
  80             System.out.println("method = " + isIndexDisplayedM);
  81             isIndexDisplayedM.setAccessible(true);
  82             isIndexDisplayed = isIndexDisplayedM.invoke(peer, -1);
  83             System.out.println("isIndexDisplayed=" + isIndexDisplayed);
  84 
  85         } catch (Throwable thr) {
  86             throw new RuntimeException("TEST FAILED: " + thr);
  87         }
  88 
  89         if ((Boolean) isIndexDisplayed) {
  90             throw new RuntimeException("TEST FAILED: -1 should be"
  91                 + " invisible index");
  92         }
  93 
  94     }// start()
  95 
  96 }// class AutomaticAppletTest