1 /*
   2  * Copyright (c) 2013, 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 6299858 7124338
  27   @summary PIT. Focused border not shown on List if selected item is removed, XToolkit
  28   @author Dmitry.Cherepanov@SUN.COM area=awt.list
  29   @run applet FirstItemRemoveTest.html
  30 */
  31 
  32 import java.applet.Applet;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 
  36 public class FirstItemRemoveTest extends Applet
  37 {
  38     List list = new List(4, false);
  39     Panel panel = new Panel();
  40 
  41     public void init()
  42     {
  43         list.add("000");
  44         list.add("111");
  45         list.add("222");
  46         list.add("333");
  47         list.add("444");
  48         list.add("555");
  49 
  50         panel.setLayout(new FlowLayout ());
  51         panel.add(list);
  52 
  53         this.add(panel);
  54         this.setLayout (new FlowLayout ());
  55     }//End  init()
  56 
  57     public void start ()
  58     {
  59         setSize (200,200);
  60         setVisible(true);
  61         validate();
  62 
  63         test();
  64     }// start()
  65 
  66     private void test(){
  67 
  68         if (jdk.testlibrary.OSInfo.getOSType() == jdk.testlibrary.OSInfo.OSType.MACOSX) {
  69             System.err.println("Skipped. This test is not for OS X.");
  70             return;
  71         }
  72 
  73         Robot r;
  74         try {
  75             r = new Robot();
  76         } catch(AWTException e) {
  77             throw new RuntimeException(e.getMessage());
  78         }
  79 
  80         // Removing first item in order to reproduce incorrect behaviour
  81         r.delay(1000);
  82         list.remove(0);
  83         r.delay(1000);
  84 
  85         // Request focus to list
  86         Point loc = this.getLocationOnScreen();
  87         r.delay(1000);
  88 
  89         r.mouseMove(loc.x+10, loc.y+10);
  90         r.delay(10);
  91         r.mousePress(InputEvent.BUTTON1_MASK);
  92         r.delay(10);
  93         r.mouseRelease(InputEvent.BUTTON1_MASK);
  94         r.delay(1000);
  95 
  96         list.requestFocusInWindow();
  97         r.delay(1000);
  98         r.waitForIdle();
  99         if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list){
 100             throw new RuntimeException("Test failed - list isn't focus owner.");
 101         }
 102 
 103         // The focus index should be set to first item after removing
 104         // So if we press VK_SPACE then the selected item will be equals 0.
 105         r.delay(100);
 106         r.keyPress(KeyEvent.VK_SPACE);
 107         r.delay(10);
 108         r.keyRelease(KeyEvent.VK_SPACE);
 109         r.delay(1000);
 110         r.waitForIdle();
 111 
 112         int selectedIndex = list.getSelectedIndex();
 113         if (selectedIndex != 0){
 114             throw new RuntimeException("Test failed. list.getSelectedIndex() = "+selectedIndex);
 115         }
 116 
 117     }
 118 
 119 }// class AutomaticAppletTest