1 /*
   2  * Copyright (c) 2016, 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 8142406
  27  * @author a.stepanov
  28  * @summary [HiDPI] [macosx] check that for a pair of images
  29  *          (image.ext, image@2x.ext) the 1st one is loaded
  30  *          in case if the 2nd is corrupted
  31  *
  32  * @requires (os.family == "mac")
  33  *
  34  * @library ../../../../lib/testlibrary/
  35  * @build ExtendedRobot
  36  * @run main Corrupted2XImageTest
  37  */
  38 
  39 import java.awt.*;
  40 import java.awt.geom.AffineTransform;
  41 import java.awt.image.*;
  42 import java.io.*;
  43 
  44 import javax.imageio.ImageIO;
  45 
  46 public class Corrupted2XImageTest extends Frame {
  47 
  48     private static final int SZ = 200;
  49     private static final Color C = Color.BLUE;
  50 
  51     private final String format, name1x, name2x;
  52 
  53     public Corrupted2XImageTest(String format) throws IOException {
  54 
  55         this.format = format;
  56         name1x = "test." + format;
  57         name2x = "test@2x." + format;
  58         createFiles();
  59     }
  60 
  61     private void UI() {
  62 
  63         setTitle(format);
  64         setSize(SZ, SZ);
  65         setResizable(false);
  66         setLocation(50, 50);
  67         setVisible(true);
  68     }
  69 
  70     @Override
  71     public void paint(Graphics g) {
  72 
  73         Image img = Toolkit.getDefaultToolkit().getImage(
  74             new File(name1x).getAbsolutePath());
  75         g.drawImage(img, 0, 0, this);
  76     }
  77 
  78     private void createFiles() throws IOException {
  79 
  80         BufferedImage img =
  81             new BufferedImage(SZ, SZ, BufferedImage.TYPE_INT_RGB);
  82         Graphics g = img.getGraphics();
  83         g.setColor(C);
  84         g.fillRect(0, 0, SZ, SZ);
  85         ImageIO.write(img, format, new File(name1x));
  86 
  87         // corrupted @2x "image" - just a text file
  88         Writer writer = new BufferedWriter(new OutputStreamWriter(
  89             new FileOutputStream(new File(name2x)), "utf-8"));
  90         writer.write("corrupted \"image\"");
  91         writer.close();
  92     }
  93 
  94     // need this for jpg
  95     private static boolean cmpColors(Color c1, Color c2) {
  96 
  97         int tol = 10;
  98         return (
  99             Math.abs(c2.getRed()   - c1.getRed()  ) < tol &&
 100             Math.abs(c2.getGreen() - c1.getGreen()) < tol &&
 101             Math.abs(c2.getBlue()  - c1.getBlue() ) < tol);
 102     }
 103 
 104     private void doTest() throws Exception {
 105 
 106         ExtendedRobot r = new ExtendedRobot();
 107         System.out.println("format: " + format);
 108         r.waitForIdle(1000);
 109         EventQueue.invokeAndWait(this::UI);
 110         r.waitForIdle(1000);
 111         Point loc = getLocationOnScreen();
 112         Color c = r.getPixelColor(loc.x + SZ / 2, loc.y + SZ / 2);
 113         if (!cmpColors(c, C)) {
 114             throw new RuntimeException("test failed, color = " + c); }
 115         System.out.println("ok");
 116         dispose();
 117     }
 118 
 119     private static boolean is2x() {
 120 
 121         AffineTransform tr = GraphicsEnvironment.getLocalGraphicsEnvironment().
 122             getDefaultScreenDevice().getDefaultConfiguration().
 123             getDefaultTransform();
 124         return Math.min(tr.getScaleX(), tr.getScaleY()) > 1.001;
 125     }
 126 
 127     public static void main(String[] args) throws Exception {
 128 
 129         // TODO: update the test with sun.java2d.uiScale option (remove is2x())
 130         // after fix of JDK-8150844 enh. to run it on non-HiDPI machines as well
 131         if (is2x()) {
 132             // formats supported by Toolkit.getImage()
 133             for (String format: new String[]{"gif", "jpg", "png"}) {
 134                 (new Corrupted2XImageTest(format)).doTest();
 135             }
 136         } else {
 137             System.out.println("this test is for HiDPI only");
 138         }
 139     }
 140 }