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.image.*;
  41 import java.io.*;
  42 
  43 import javax.imageio.ImageIO;
  44 
  45 public class Corrupted2XImageTest extends Frame {
  46 
  47     private static final int SZ = 200;
  48     private static final Color C = Color.BLUE;
  49 
  50     private final String format, name1x, name2x;
  51 
  52     public Corrupted2XImageTest(String format) throws IOException {
  53 
  54         this.format = format;
  55         name1x = "test." + format;
  56         name2x = "test@2x." + format;
  57         createFiles();
  58     }
  59 
  60     private void UI() {
  61 
  62         setTitle(format);
  63         setSize(SZ, SZ);
  64         setResizable(false);
  65         setLocation(50, 50);
  66         setVisible(true);
  67     }
  68 
  69     @Override
  70     public void paint(Graphics g) {
  71 
  72         Image img = Toolkit.getDefaultToolkit().getImage(
  73             new File(name1x).getAbsolutePath());
  74         g.drawImage(img, 0, 0, this);
  75     }
  76 
  77     private void createFiles() throws IOException {
  78 
  79         BufferedImage img =
  80             new BufferedImage(SZ, SZ, BufferedImage.TYPE_INT_RGB);
  81         Graphics g = img.getGraphics();
  82         g.setColor(C);
  83         g.fillRect(0, 0, SZ, SZ);
  84         ImageIO.write(img, format, new File(name1x));
  85 
  86         // corrupted @2x "image" - just a text file
  87         Writer writer = new BufferedWriter(new OutputStreamWriter(
  88             new FileOutputStream(new File(name2x)), "utf-8"));
  89         writer.write("corrupted \"image\"");
  90         writer.close();
  91     }
  92 
  93     // need this for jpg
  94     private static boolean cmpColors(Color c1, Color c2) {
  95 
  96         int tol = 10;
  97         return (
  98             Math.abs(c2.getRed()   - c1.getRed()  ) < tol &&
  99             Math.abs(c2.getGreen() - c1.getGreen()) < tol &&
 100             Math.abs(c2.getBlue()  - c1.getBlue() ) < tol);
 101     }
 102 
 103     private void doTest() throws Exception {
 104 
 105         ExtendedRobot r = new ExtendedRobot();
 106         System.out.println("format: " + format);
 107         r.waitForIdle(1000);
 108         EventQueue.invokeAndWait(this::UI);
 109         r.waitForIdle(1000);
 110         Point loc = getLocationOnScreen();
 111         Color c = r.getPixelColor(loc.x + SZ / 2, loc.y + SZ / 2);
 112         if (!cmpColors(c, C)) {
 113             throw new RuntimeException("test failed, color = " + c); }
 114         System.out.println("ok");
 115         dispose();
 116     }
 117 
 118     public static void main(String[] args) throws Exception {
 119 
 120         // formats supported by Toolkit.getImage()
 121         for (String format : new String[]{"gif", "jpg", "png"}) {
 122             (new Corrupted2XImageTest(format)).doTest();
 123         }
 124     }
 125 }