Path: blob/master/test/jdk/javax/swing/JTextPane/JTextPaneDocumentAlignment.java
41149 views
/*1* Copyright (c) 2015, 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 813213627* @summary [PIT] RTL orientation in JEditorPane is broken28* @author Semyon Sadetsky29*/3031import javax.swing.*;32import javax.swing.text.BadLocationException;33import javax.swing.text.SimpleAttributeSet;34import javax.swing.text.StyleConstants;35import java.awt.*;3637public class JTextPaneDocumentAlignment {3839private static JFrame frame;40private static JTextPane jTextPane;41private static int position;4243public static void main(String[] args) throws Exception{44SwingUtilities.invokeAndWait(new Runnable() {45@Override46public void run() {47frame = new JFrame();48frame.setUndecorated(true);49frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);50frame.setSize(200, 200);51jTextPane = new JTextPane();52jTextPane.setContentType("text/html");53jTextPane.setText(54"<html><body><b id='test'>Test</b></body></html>");55SimpleAttributeSet right = new SimpleAttributeSet();56StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);57jTextPane.getStyledDocument()58.setParagraphAttributes(0, 10, right, true);59frame.getContentPane().add(jTextPane);60frame.setVisible(true);61}62});63Robot robot = new Robot();64robot.waitForIdle();65robot.delay(200);66SwingUtilities.invokeAndWait(new Runnable() {67@Override68public void run() {69try {70position = jTextPane.modelToView(1).x;71SimpleAttributeSet center = new SimpleAttributeSet();72StyleConstants.setAlignment(center,73StyleConstants.ALIGN_CENTER);74jTextPane.getStyledDocument()75.setParagraphAttributes(0, 10, center, true);76} catch (BadLocationException e) {77e.printStackTrace();78}79}80});81if(position < 100) {82throw new RuntimeException("Text is not right aligned " + position);83}84SwingUtilities.invokeAndWait(new Runnable() {85@Override86public void run() {87try {88position = jTextPane.modelToView(1).x;89} catch (BadLocationException e) {90e.printStackTrace();91}92frame.dispose();93}94});95if(position < 20) {96throw new RuntimeException("Text is not center aligned " + position);97}98System.out.println("ok");99}100}101102103