Path: blob/master/test/jdk/javax/swing/JEditorPane/8195095/ImageViewTest.java
58462 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 8195095 8206238 820863827* @summary Tests if Images are scaled correctly in JEditorPane.28* @run main ImageViewTest29*/30import java.awt.Robot;31import java.awt.Point;32import java.awt.Color;33import java.awt.Insets;3435import javax.swing.JEditorPane;36import javax.swing.SwingUtilities;37import javax.swing.JFrame;38import javax.swing.WindowConstants;3940public class ImageViewTest {4142private static JFrame f;4344private static void test(Robot r, JEditorPane editorPane,45final int WIDTH, final int HEIGHT ) throws Exception {4647SwingUtilities.invokeAndWait(() -> {48f = new JFrame();49editorPane.setEditable(false);50f.add(editorPane);51f.setSize(WIDTH + 20, HEIGHT + 40);52f.setLocationRelativeTo(null);53f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);54//This line will trigger the imageupdate, and consequently, the view55//will be populated with the appropriate color when the pixel color56//is queried by robot.57editorPane.getUI().getPreferredSize(editorPane);58f.setVisible(true);59});6061r.waitForIdle();62r.delay(500);6364SwingUtilities.invokeAndWait(() -> {65Insets insets = editorPane.getInsets();66Point loc = editorPane.getLocationOnScreen();6768final Color blue = Color.BLUE;69final int offset = 10;7071Color center = r.getPixelColor(loc.x + insets.left + WIDTH / 2,72loc.y + insets.top + HEIGHT / 2);73Color left = r.getPixelColor(loc.x + insets.left + offset,74loc.y + insets.top + HEIGHT / 2);75Color right = r.getPixelColor(loc.x + insets.left + WIDTH - offset,76loc.y + insets.top + HEIGHT / 2);77Color bottom = r.getPixelColor(loc.x + insets.left + WIDTH / 2,78loc.y + insets.top + HEIGHT - offset);79Color top = r.getPixelColor(loc.x + insets.left + WIDTH / 2,80loc.y + insets.top + offset);8182f.dispose();8384System.out.println("center color: " + center);85System.out.println("left color: " + left);86System.out.println("right color: " + right);87System.out.println("bottom color: " + bottom);88System.out.println("top color: " + top);89System.out.println();9091if (!(blue.equals(center) && blue.equals(left) && blue.equals(right) &&92blue.equals(top) && blue.equals(bottom))) {93throw new RuntimeException("Test failed: Image not scaled correctly");94}95});9697r.waitForIdle();98}99100public static void main(String[] args) throws Exception {101102final String ABSOLUTE_FILE_PATH = ImageViewTest.class.getResource("circle.png").getPath();103104System.out.println(ABSOLUTE_FILE_PATH);105106Robot r = new Robot();107108final JEditorPane[] editorPanes = new JEditorPane[11];109110SwingUtilities.invokeAndWait(() -> {111editorPanes[0] = new JEditorPane("text/html",112"<img height=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");113114editorPanes[1] = new JEditorPane("text/html",115"<img width=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");116117editorPanes[2] = new JEditorPane("text/html",118"<img width=\"200\" height=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");119120editorPanes[3] = new JEditorPane("text/html",121"<img src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");122123editorPanes[4] = new JEditorPane("text/html",124"<img width=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");125126editorPanes[5] = new JEditorPane("text/html",127"<img height=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");128129editorPanes[6] = new JEditorPane("text/html",130"<img width=\"100\" height=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");131132editorPanes[7] = new JEditorPane("text/html",133"<img width=\"50\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");134135editorPanes[8] = new JEditorPane("text/html",136"<img height=\"50\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");137138editorPanes[9] = new JEditorPane("text/html",139"<img width=\"300\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");140141editorPanes[10] = new JEditorPane("text/html",142"<img height=\"300\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");143144});145146r.waitForIdle();147148System.out.println("Test with only height set to 200");149test(r, editorPanes[0], 200, 200);150151System.out.println("Test with only width set to 200");152test(r, editorPanes[1], 200, 200);153154System.out.println("Test with both of them set");155test(r, editorPanes[2], 200, 200);156157System.out.println("Test with none of them set to 200");158test(r, editorPanes[3], 200, 200);159160System.out.println("Test with only width set to 100");161test(r, editorPanes[4], 100, 100);162163System.out.println("Test with only height set to 100");164test(r, editorPanes[5], 100, 100);165166System.out.println("Test with both width and height set to 100");167test(r, editorPanes[6], 100, 100);168169System.out.println("Test with only width set to 50");170test(r, editorPanes[7], 50, 50);171172System.out.println("Test with only height set to 50");173test(r, editorPanes[8], 50, 50);174175System.out.println("Test with only width set to 300");176test(r, editorPanes[9], 300, 300);177178System.out.println("Test with only height set to 300");179test(r, editorPanes[10], 300, 300);180181System.out.println("Test Passed.");182}183}184185186