Corrupted2XImageTest.java revision 17565:b762aafa34e3
1272343Sngie/* 2272343Sngie * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. 3272343Sngie * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4272343Sngie * 5272343Sngie * This code is free software; you can redistribute it and/or modify it 6272343Sngie * under the terms of the GNU General Public License version 2 only, as 7272343Sngie * published by the Free Software Foundation. 8272343Sngie * 9272343Sngie * This code is distributed in the hope that it will be useful, but WITHOUT 10272343Sngie * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11272343Sngie * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12272343Sngie * version 2 for more details (a copy is included in the LICENSE file that 13272343Sngie * accompanied this code). 14272343Sngie * 15272343Sngie * You should have received a copy of the GNU General Public License version 16272343Sngie * 2 along with this work; if not, write to the Free Software Foundation, 17272343Sngie * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18272343Sngie * 19272343Sngie * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20272343Sngie * or visit www.oracle.com if you need additional information or have any 21272343Sngie * questions. 22272343Sngie */ 23272343Sngie 24272343Sngie/** 25272343Sngie * @test 26272343Sngie * @key headful 27272343Sngie * @bug 8142406 28272343Sngie * @author a.stepanov 29272343Sngie * @summary [HiDPI] [macosx] check that for a pair of images 30272343Sngie * (image.ext, image@2x.ext) the 1st one is loaded 31272343Sngie * in case if the 2nd is corrupted 32272343Sngie * 33272343Sngie * @requires (os.family == "mac") 34272343Sngie * 35272343Sngie * @library ../../../../lib/testlibrary/ 36272343Sngie * @build ExtendedRobot 37272343Sngie * @run main Corrupted2XImageTest 38272343Sngie */ 39272343Sngie 40272343Sngieimport java.awt.*; 41272343Sngieimport java.awt.image.*; 42272343Sngieimport java.io.*; 43272343Sngie 44272343Sngieimport javax.imageio.ImageIO; 45272343Sngie 46272343Sngiepublic class Corrupted2XImageTest extends Frame { 47272343Sngie 48272343Sngie private static final int SZ = 200; 49272343Sngie private static final Color C = Color.BLUE; 50272343Sngie 51272343Sngie private final String format, name1x, name2x; 52272343Sngie 53272343Sngie public Corrupted2XImageTest(String format) throws IOException { 54272343Sngie 55272343Sngie this.format = format; 56272343Sngie name1x = "test." + format; 57272343Sngie name2x = "test@2x." + format; 58272343Sngie createFiles(); 59272343Sngie } 60272343Sngie 61272343Sngie private void UI() { 62272343Sngie 63272343Sngie setTitle(format); 64272343Sngie setSize(SZ, SZ); 65272343Sngie setResizable(false); 66272343Sngie setLocation(50, 50); 67272343Sngie setVisible(true); 68272343Sngie } 69272343Sngie 70272343Sngie @Override 71272343Sngie public void paint(Graphics g) { 72272343Sngie 73272343Sngie Image img = Toolkit.getDefaultToolkit().getImage( 74272343Sngie new File(name1x).getAbsolutePath()); 75272343Sngie g.drawImage(img, 0, 0, this); 76272343Sngie } 77272343Sngie 78272343Sngie private void createFiles() throws IOException { 79272343Sngie 80272343Sngie BufferedImage img = 81272343Sngie new BufferedImage(SZ, SZ, BufferedImage.TYPE_INT_RGB); 82272343Sngie Graphics g = img.getGraphics(); 83272343Sngie g.setColor(C); 84272343Sngie g.fillRect(0, 0, SZ, SZ); 85272343Sngie ImageIO.write(img, format, new File(name1x)); 86272343Sngie 87272343Sngie // corrupted @2x "image" - just a text file 88272343Sngie Writer writer = new BufferedWriter(new OutputStreamWriter( 89272343Sngie new FileOutputStream(new File(name2x)), "utf-8")); 90272343Sngie writer.write("corrupted \"image\""); 91272343Sngie writer.close(); 92272343Sngie } 93272343Sngie 94272343Sngie // need this for jpg 95272343Sngie private static boolean cmpColors(Color c1, Color c2) { 96272343Sngie 97272343Sngie int tol = 10; 98272343Sngie return ( 99272343Sngie Math.abs(c2.getRed() - c1.getRed() ) < tol && 100272343Sngie Math.abs(c2.getGreen() - c1.getGreen()) < tol && 101272343Sngie Math.abs(c2.getBlue() - c1.getBlue() ) < tol); 102272343Sngie } 103272343Sngie 104272343Sngie private void doTest() throws Exception { 105272343Sngie 106272343Sngie ExtendedRobot r = new ExtendedRobot(); 107272343Sngie System.out.println("format: " + format); 108272343Sngie r.waitForIdle(1000); 109272343Sngie EventQueue.invokeAndWait(this::UI); 110272343Sngie r.waitForIdle(1000); 111272343Sngie Point loc = getLocationOnScreen(); 112272343Sngie Color c = r.getPixelColor(loc.x + SZ / 2, loc.y + SZ / 2); 113272343Sngie if (!cmpColors(c, C)) { 114272343Sngie throw new RuntimeException("test failed, color = " + c); } 115272343Sngie System.out.println("ok"); 116272343Sngie dispose(); 117272343Sngie } 118272343Sngie 119272343Sngie public static void main(String[] args) throws Exception { 120272343Sngie 121272343Sngie // formats supported by Toolkit.getImage() 122272343Sngie for (String format : new String[]{"gif", "jpg", "png"}) { 123272343Sngie (new Corrupted2XImageTest(format)).doTest(); 124272343Sngie } 125272343Sngie } 126272343Sngie} 127272343Sngie