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 7159566
  27   @summary The choice positioned in the top of applet when clicking the choice.
  28   @author Petr Pchelko
  29   @library ../../regtesthelpers
  30   @build Util
  31   @run main ChoiceLocationTest
  32  */
  33 
  34 import java.awt.*;
  35 import javax.swing.*;
  36 import java.awt.event.InputEvent;
  37 import java.util.stream.Stream;
  38 
  39 import test.java.awt.regtesthelpers.Util;
  40 
  41 public class ChoiceLocationTest {
  42 
  43     private static final int FRAME_LOCATION = 100;
  44     private static final int FRAME_SIZE = 400;
  45     private static final int CLICK_STEP = 5;
  46 
  47     private static String[] choiceItems = new String[] {
  48             "test item 1",
  49             "test item 2",
  50             "test item 3"
  51     };
  52 
  53     private static volatile Frame frame;
  54     private static volatile Choice choice;
  55 
  56     public static void main(String[] args) throws Exception {
  57         try {
  58             SwingUtilities.invokeAndWait(ChoiceLocationTest::initAndShowUI);
  59             Robot r = new Robot();
  60             r.waitForIdle();
  61             r.delay(100);
  62 
  63             Util.clickOnComp(choice, r);
  64 
  65             choice.addItemListener(event -> {throw new RuntimeException("Failed: the choice popup is in the wrong place");});
  66 
  67             // Test: click in several places on the top of the frame an ensure there' no choice there
  68             Point locOnScreen = frame.getLocationOnScreen();
  69             int x = locOnScreen.x + FRAME_SIZE / 2;
  70             for (int y = locOnScreen.y + frame.getInsets().top + 10; y < locOnScreen.y + FRAME_SIZE / 3 ; y += CLICK_STEP) {
  71                 r.mouseMove(x, y);
  72                 r.waitForIdle();
  73                 r.mousePress(InputEvent.BUTTON1_MASK);
  74                 r.mouseRelease(InputEvent.BUTTON1_MASK);
  75                 r.waitForIdle();
  76                 r.delay(100);
  77             }
  78         } finally {
  79             if (frame != null) {
  80                 frame.dispose();
  81             }
  82         }
  83 
  84     }
  85 
  86     private static void initAndShowUI() {
  87         frame = new Frame("Test frame");
  88         choice = new Choice();
  89         Stream.of(choiceItems).forEach(choice::add);
  90         frame.add(choice);
  91         frame.setBounds(FRAME_LOCATION, FRAME_LOCATION, FRAME_SIZE, FRAME_SIZE);
  92         frame.setVisible(true);
  93     }
  94 
  95 
  96 }