1 /*
   2  * Copyright (c) 2012, 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 7158712
  26    @summary Synth Property "ComboBox.popupInsets" is ignored
  27    @library ../../../regtesthelpers
  28    @author Pavel Porvatov
  29 */
  30 
  31 import javax.swing.*;
  32 import javax.swing.plaf.basic.BasicComboPopup;
  33 import javax.swing.plaf.synth.SynthLookAndFeel;
  34 import java.awt.*;
  35 import java.awt.event.InputEvent;
  36 import java.io.ByteArrayInputStream;
  37 import java.util.concurrent.Callable;
  38 
  39 public class bug7158712 {
  40     private static final String SYNTH_XML = "<synth>" +
  41             "    <style id=\"all\">" +
  42             "      <font name=\"Dialog\" size=\"12\"/>" +
  43             "    </style>" +
  44             "    <bind style=\"all\" type=\"REGION\" key=\".*\"/>" +
  45             "    <style id=\"arrowButton\">" +
  46             "      <property key=\"ArrowButton.size\" type=\"integer\" value=\"18\"/>" +
  47             "    </style>" +
  48             "    <bind style=\"arrowButton\" type=\"region\" key=\"ArrowButton\"/>" +
  49             "    <style id=\"comboBox\">" +
  50             "      <property key=\"ComboBox.popupInsets\" type=\"insets\" value=\"-5 -5 5 -5\"/>" +
  51             "    </style>" +
  52             "    <bind style=\"comboBox\" type=\"region\" key=\"ComboBox\"/>" +
  53             "</synth>";
  54 
  55     private static JComboBox<String> comboBox;
  56 
  57     public static void main(String[] args) throws Exception {
  58         Robot robot = new Robot();
  59 
  60         robot.setAutoDelay(500);
  61 
  62         SynthLookAndFeel laf = new SynthLookAndFeel();
  63 
  64         laf.load(new ByteArrayInputStream(SYNTH_XML.getBytes("UTF8")), bug7158712.class);
  65 
  66         UIManager.setLookAndFeel(laf);
  67 
  68         EventQueue.invokeAndWait(new Runnable() {
  69             public void run() {
  70                 comboBox = new JComboBox<>(
  71                         new String[]{"Very Looooooooooooooooooooong Text Item 1", "Item 2"});
  72 
  73                 JFrame frame = new JFrame();
  74 
  75                 frame.add(comboBox, BorderLayout.NORTH);
  76                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  77                 frame.setSize(new Dimension(400, 300));
  78                 frame.setLocationRelativeTo(null);
  79                 frame.setVisible(true);
  80             }
  81         });
  82 
  83         robot.waitForIdle();
  84 
  85         Point comboBoxLocation = Util.invokeOnEDT(new Callable<Point>() {
  86             @Override
  87             public Point call() throws Exception {
  88                 return comboBox.getLocationOnScreen();
  89             }
  90         });
  91 
  92         robot.mouseMove(comboBoxLocation.x, comboBoxLocation.y);
  93         robot.mousePress(InputEvent.BUTTON1_MASK);
  94         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  95 
  96         SwingUtilities.invokeAndWait(new Runnable() {
  97             @Override
  98             public void run() {
  99                 BasicComboPopup popup = (BasicComboPopup) comboBox.getAccessibleContext().getAccessibleChild(0);
 100 
 101                 Point popupPoint = popup.getLocationOnScreen();
 102                 Point comboBoxPoint = comboBox.getLocationOnScreen();
 103 
 104                 if (comboBoxPoint.x - 5 != popupPoint.x ||
 105                         comboBoxPoint.y + comboBox.getHeight() - 5 != popupPoint.y) {
 106                     throw new RuntimeException("Invalid popup coordinates. Popup location: " + popupPoint +
 107                             ", comboBox location: " + comboBoxPoint);
 108                 }
 109 
 110                 System.out.println("Test bug7158712 passed");
 111             }
 112         });
 113     }
 114 }