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