1 /*
   2  * Copyright (c) 2006, 2014, 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   @test
  25   @key headful
  26   @bug 6399679
  27   @summary Choice is not invalidated when the frame gets resized programmatically when the drop-down is visible
  28   @author andrei.dmitriev area=awt.choice
  29   @library ../../../../lib/testlibrary
  30   @build jdk.testlibrary.OSInfo
  31   @run main ResizeAutoClosesChoice
  32 */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 
  37 import jdk.testlibrary.OSInfo;
  38 
  39 public class ResizeAutoClosesChoice
  40 {
  41     static Frame frame = new Frame("Test Frame");
  42     static Choice choice1 = new Choice();
  43     static Robot robot;
  44     static Point pt;
  45     static String passed = null;
  46     static Button button = new Button("This button causes Frame to be resized on pack()");
  47     public static void main(String args[]) throws Exception
  48     {
  49         if(OSInfo.getOSType().equals(OSInfo.OSType.MACOSX)) {
  50             System.out.println("Not for OS OX");
  51             return;
  52         }
  53 
  54         choice1.setForeground(Color.red);
  55         choice1.setBackground(Color.red);
  56 
  57         frame.setLayout (new BorderLayout ());
  58         for (int i = 1; i<10;i++){
  59             choice1.add("item "+i);
  60         }
  61         frame.setSize(300, 300);
  62         choice1.setLocation(50, 50);
  63         choice1.setSize(70, 20);
  64 
  65         button.setLocation(150, 100);
  66         button.setSize(150, 20);
  67         frame.add(choice1, BorderLayout.SOUTH);
  68         frame.pack();
  69 
  70         frame.validate();
  71         frame.setVisible(true);
  72 
  73         robot = new Robot();
  74         robot.waitForIdle();
  75         pt = choice1.getLocationOnScreen();
  76         robot.mouseMove(pt.x + choice1.getWidth()/10*9, pt.y + choice1.getHeight()/2);
  77         robot.waitForIdle();
  78         robot.mousePress(InputEvent.BUTTON1_MASK);
  79         robot.delay(1000);
  80         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  81 
  82         Color color = robot.getPixelColor(pt.x + choice1.getWidth()/2,
  83                                           pt.y + 3 * choice1.getHeight());
  84         //should take a color on the point on the choice's menu
  85         System.out.println("Choice opened. Color got : "+color);
  86         if ( !color.equals(Color.red) ){
  87             passed = "Choice wasn't opened with the mouse";
  88         }
  89 
  90         Rectangle oldBounds = choice1.getBounds();
  91         System.out.println("Choice's old bounds : "+oldBounds);
  92 
  93         frame.add(button, BorderLayout.NORTH);
  94         //            frame.setSize(500, 500);
  95         frame.pack();
  96         robot.waitForIdle();
  97         System.out.println("Choice's new bounds : "+choice1.getBounds());
  98 
  99         if (!choice1.getBounds().equals(oldBounds)){
 100             pt = choice1.getLocationOnScreen();
 101             color = robot.getPixelColor(pt.x + choice1.getWidth()/2,
 102                                         pt.y + 3 * choice1.getHeight());
 103             System.out.println("Choice opened. Color got : "+color);
 104             if (color.equals(Color.red) ){
 105                 passed = "Choice wasn't closed when toplevel repacked.";
 106             }
 107         } else {
 108             System.out.println("frame.pack didn't changed Choice's size - dropdown menu should remain the same. Test passed.");
 109         }
 110         if (passed != null){
 111             throw new RuntimeException(passed);
 112         }
 113     }
 114 }