< prev index next >

test/java/awt/image/multiresolution/MultiresolutionIconTest.java

Print this page




  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 8150724 8151303
  27  * @author a.stepanov
  28  * @summary Check that correct resolution variants are chosen for icons
  29  *          when multiresolution image is used for their construction.
  30  *
  31  * @requires (os.family != "mac")
  32  *
  33  * @library ../../../../lib/testlibrary/
  34  * @build ExtendedRobot
  35  * @run main/othervm/timeout=240 -Dsun.java2d.uiScale=1 MultiresolutionIconTest
  36  * @run main/othervm/timeout=240 -Dsun.java2d.uiScale=2 MultiresolutionIconTest
  37  */
  38 
  39 
  40 // TODO: please remove the "@requires" tag after 8151303 fix
  41 
  42 
  43 import java.awt.*;
  44 import java.awt.event.InputEvent;
  45 import java.awt.geom.AffineTransform;
  46 import java.awt.image.BaseMultiResolutionImage;
  47 import java.awt.image.BufferedImage;
  48 import javax.swing.*;
  49 
  50 public class MultiresolutionIconTest extends JFrame {
  51 
  52     private final static int SZ = 100;
  53     private final static int N = 5; // number of components
  54 
  55     private final static String SCALE = "sun.java2d.uiScale";
  56     private final static Color C1X = Color.RED;
  57     private final static Color C2X = Color.BLUE;
  58 
  59     private JLabel lbl;
  60     private JTabbedPane tabbedPane;
  61 
  62     private final ExtendedRobot r;
  63 
  64     private static BufferedImage generateImage(int sz, Color c) {
  65 


 111 
 112         JRadioButton rbn = new JRadioButton(icon);
 113         rbn.setHorizontalAlignment(SwingConstants.CENTER);
 114         p.add(rbn);
 115 
 116         JCheckBox cbx = new JCheckBox(icon);
 117         cbx.setHorizontalAlignment(SwingConstants.CENTER);
 118         p.add(cbx);
 119 
 120         lbl = new JLabel(icon);
 121         p.add(lbl);
 122 
 123         tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
 124         tabbedPane.addTab("", tabIcon, p);
 125         getContentPane().add(tabbedPane);
 126 
 127         setResizable(false);
 128         setVisible(true);
 129     }
 130 
 131     private static boolean is2x() {
 132 
 133         AffineTransform tr = GraphicsEnvironment.getLocalGraphicsEnvironment().
 134             getDefaultScreenDevice().getDefaultConfiguration().
 135             getDefaultTransform();
 136         return (Math.min(tr.getScaleX(), tr.getScaleY()) > 1.001);
 137     }
 138 
 139     private boolean checkPressedColor(int x, int y, Color ok) {
 140 
 141         r.mouseMove(x, y);
 142         r.waitForIdle();
 143         r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 144         r.waitForIdle(100);
 145         Color c = r.getPixelColor(x, y);
 146         r.waitForIdle(100);
 147         r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 148         r.waitForIdle(100);
 149         if (!c.equals(ok)) { return false; }
 150         // check the icon's color hasn't changed
 151         // after the mouse was released
 152         c = r.getPixelColor(x, y);
 153         return c.equals(ok);
 154     }
 155 
 156     private boolean checkTabIcon(
 157         int xStart, int xEnd, int yStart, int yEnd, Color ok, Color nok) {
 158 
 159         for (int y = yStart; y < yEnd; y += 2) {
 160             for (int x = xStart; x < xEnd; x += 2) {
 161                 Color c = r.getPixelColor(x, y);
 162                 if (c.equals(nok)) { return false; }
 163                 else if (c.equals(ok)) {
 164                     // shift a bit to avoid the selection effects
 165                     return checkPressedColor(x + 5, y + 5, ok);
 166                 }
 167             }
 168         }
 169 
 170         return false; // didn't find the icon
 171     }
 172 
 173 
 174     private void doTest() {
 175 
 176         r.waitForIdle(2000);
 177         String scale = System.getProperty(SCALE);
 178         System.out.println("scale = " + scale);
 179         Color
 180             expected   = is2x() ? C2X : C1X,
 181             unexpected = is2x() ? C1X : C2X;
 182 
 183         Point p = lbl.getLocationOnScreen();
 184         int x = p.x + lbl.getWidth() / 2;
 185         int y = p.y + lbl.getHeight() / 2;
 186         int w = lbl.getWidth();
 187 
 188         boolean ok = true, curr;
 189         Color c;
 190         String components[] = new String[]{
 191             "JLabel", "JCheckBox", "JRadioButton", "JToggleButton", "JButton"};
 192         for (int i = 0; i < N; i++) {
 193 
 194             curr = true;
 195             int t = x - i * w;
 196 
 197             // check icon color
 198             c = r.getPixelColor(t, y);
 199             System.out.print(components[i] + " icon: ");
 200             if (!c.equals(expected)) {
 201                 curr = false;


 210             r.waitForIdle();
 211         }
 212 
 213         int x0 = tabbedPane.getLocationOnScreen().x;
 214         int x1 = x - ((N - 1) * w + w / 2);
 215         int y0 = getLocationOnScreen().y;
 216         int y1 = y0 + getHeight();
 217         curr = checkTabIcon(x0, x1, y0, y1, expected, unexpected);
 218 
 219         System.out.println("JTabbedPane icon: " + (curr ? "ok" : "nok"));
 220         ok = ok && curr;
 221 
 222         if (!ok) { throw new RuntimeException("test failed"); }
 223 
 224         r.waitForIdle();
 225         dispose();
 226     }
 227 
 228     public static void main(String[] args) throws Exception {
 229 
 230         // TODO: remove is2x() check after JDK-8150844 fix
 231         if (is2x() != "2".equals(System.getProperty(SCALE))) { return; }
 232 
 233         for (UIManager.LookAndFeelInfo LF: UIManager.getInstalledLookAndFeels()) {
 234             System.out.println("\nL&F: " + LF.getName());
 235             (new MultiresolutionIconTest(LF)).doTest();
 236         }
 237     }
 238 }


  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 8150724 8151303
  27  * @author a.stepanov
  28  * @summary Check that correct resolution variants are chosen for icons
  29  *          when multiresolution image is used for their construction.
  30  *


  31  * @library ../../../../lib/testlibrary/
  32  * @build ExtendedRobot
  33  * @run main/othervm/timeout=240 -Dsun.java2d.uiScale=1 MultiresolutionIconTest
  34  * @run main/othervm/timeout=240 -Dsun.java2d.uiScale=2 MultiresolutionIconTest
  35  */
  36 
  37 
  38 // TODO: please remove the "@requires" tag after 8151303 fix
  39 
  40 
  41 import java.awt.*;
  42 import java.awt.event.InputEvent;

  43 import java.awt.image.BaseMultiResolutionImage;
  44 import java.awt.image.BufferedImage;
  45 import javax.swing.*;
  46 
  47 public class MultiresolutionIconTest extends JFrame {
  48 
  49     private final static int SZ = 100;
  50     private final static int N = 5; // number of components
  51 
  52     private final static String SCALE = "sun.java2d.uiScale";
  53     private final static Color C1X = Color.RED;
  54     private final static Color C2X = Color.BLUE;
  55 
  56     private JLabel lbl;
  57     private JTabbedPane tabbedPane;
  58 
  59     private final ExtendedRobot r;
  60 
  61     private static BufferedImage generateImage(int sz, Color c) {
  62 


 108 
 109         JRadioButton rbn = new JRadioButton(icon);
 110         rbn.setHorizontalAlignment(SwingConstants.CENTER);
 111         p.add(rbn);
 112 
 113         JCheckBox cbx = new JCheckBox(icon);
 114         cbx.setHorizontalAlignment(SwingConstants.CENTER);
 115         p.add(cbx);
 116 
 117         lbl = new JLabel(icon);
 118         p.add(lbl);
 119 
 120         tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
 121         tabbedPane.addTab("", tabIcon, p);
 122         getContentPane().add(tabbedPane);
 123 
 124         setResizable(false);
 125         setVisible(true);
 126     }
 127 








 128     private boolean checkPressedColor(int x, int y, Color ok) {
 129 
 130         r.mouseMove(x, y);
 131         r.waitForIdle();
 132         r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 133         r.waitForIdle(100);
 134         Color c = r.getPixelColor(x, y);
 135         r.waitForIdle(100);
 136         r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 137         r.waitForIdle(100);
 138         if (!c.equals(ok)) { return false; }
 139         // check the icon's color hasn't changed
 140         // after the mouse was released
 141         c = r.getPixelColor(x, y);
 142         return c.equals(ok);
 143     }
 144 
 145     private boolean checkTabIcon(
 146         int xStart, int xEnd, int yStart, int yEnd, Color ok, Color nok) {
 147 
 148         for (int y = yStart; y < yEnd; y += 2) {
 149             for (int x = xStart; x < xEnd; x += 2) {
 150                 Color c = r.getPixelColor(x, y);
 151                 if (c.equals(nok)) { return false; }
 152                 else if (c.equals(ok)) {
 153                     // shift a bit to avoid the selection effects
 154                     return checkPressedColor(x + 5, y + 5, ok);
 155                 }
 156             }
 157         }
 158 
 159         return false; // didn't find the icon
 160     }
 161 
 162 
 163     private void doTest() {
 164 
 165         r.waitForIdle(2000);
 166         String scale = System.getProperty(SCALE);
 167         boolean is2x = "2".equals(scale);
 168         Color expected = is2x ? C2X : C1X;
 169         Color unexpected = is2x ? C1X : C2X;

 170 
 171         Point p = lbl.getLocationOnScreen();
 172         int x = p.x + lbl.getWidth() / 2;
 173         int y = p.y + lbl.getHeight() / 2;
 174         int w = lbl.getWidth();
 175 
 176         boolean ok = true, curr;
 177         Color c;
 178         String components[] = new String[]{
 179             "JLabel", "JCheckBox", "JRadioButton", "JToggleButton", "JButton"};
 180         for (int i = 0; i < N; i++) {
 181 
 182             curr = true;
 183             int t = x - i * w;
 184 
 185             // check icon color
 186             c = r.getPixelColor(t, y);
 187             System.out.print(components[i] + " icon: ");
 188             if (!c.equals(expected)) {
 189                 curr = false;


 198             r.waitForIdle();
 199         }
 200 
 201         int x0 = tabbedPane.getLocationOnScreen().x;
 202         int x1 = x - ((N - 1) * w + w / 2);
 203         int y0 = getLocationOnScreen().y;
 204         int y1 = y0 + getHeight();
 205         curr = checkTabIcon(x0, x1, y0, y1, expected, unexpected);
 206 
 207         System.out.println("JTabbedPane icon: " + (curr ? "ok" : "nok"));
 208         ok = ok && curr;
 209 
 210         if (!ok) { throw new RuntimeException("test failed"); }
 211 
 212         r.waitForIdle();
 213         dispose();
 214     }
 215 
 216     public static void main(String[] args) throws Exception {
 217 



 218         for (UIManager.LookAndFeelInfo LF: UIManager.getInstalledLookAndFeels()) {
 219             System.out.println("\nL&F: " + LF.getName());
 220             (new MultiresolutionIconTest(LF)).doTest();
 221         }
 222     }
 223 }
< prev index next >