2450N/A/*
2450N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2450N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2450N/A *
2450N/A * This code is free software; you can redistribute it and/or modify it
2450N/A * under the terms of the GNU General Public License version 2 only, as
2450N/A * published by the Free Software Foundation.
2450N/A *
2450N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2450N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2450N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2450N/A * version 2 for more details (a copy is included in the LICENSE file that
2450N/A * accompanied this code).
2450N/A *
2450N/A * You should have received a copy of the GNU General Public License version
2450N/A * 2 along with this work; if not, write to the Free Software Foundation,
2450N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2450N/A *
2450N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2450N/A * or visit www.oracle.com if you need additional information or have any
2450N/A * questions.
2450N/A */
2450N/A
2450N/A/*
2450N/A * @test
2450N/A * @bug 6636983
2450N/A * @summary test that composed text at the line starts is handled correctly
2450N/A * @author Sergey Groznyh
2450N/A * @run main bug6636983
2450N/A */
2450N/A
2450N/Aimport sun.swing.SwingUtilities2;
2450N/A
2450N/Aimport javax.swing.*;
2450N/Aimport javax.swing.text.*;
2450N/Aimport javax.swing.text.html.HTMLDocument;
2450N/Aimport java.awt.*;
2450N/Aimport java.awt.event.InputMethodEvent;
2450N/Aimport java.awt.event.KeyEvent;
2450N/Aimport java.text.AttributedString;
2450N/A
2450N/Apublic class bug6636983 {
2450N/A private Robot robot;
2450N/A
2450N/A private final AttributedString Hiragana_A = new AttributedString("\u3042");
2450N/A
2450N/A void sendInputMethodEvent() {
2450N/A InputMethodEvent ime = new InputMethodEvent(
2450N/A ep,
2450N/A InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
2450N/A Hiragana_A.getIterator(),
2450N/A 0,
2450N/A null,
2450N/A null);
2450N/A ep.dispatchEvent(ime);
2450N/A }
2450N/A
2450N/A void checkComposedTextRun() {
2450N/A HTMLDocument d = (HTMLDocument) ep.getDocument();
2450N/A ElementIterator it = new ElementIterator(d.getDefaultRootElement());
2450N/A
2450N/A while (true) {
2450N/A Element e = it.next();
2450N/A if (e == null) {
2450N/A throw new RuntimeException("no composed text found");
2450N/A }
2450N/A AttributeSet a = e.getAttributes();
2450N/A if (a.isDefined(StyleConstants.ComposedTextAttribute)) {
2450N/A if (!AbstractDocument.ContentElementName.equals(a.getAttribute(StyleConstants.NameAttribute))) {
2450N/A throw new RuntimeException("AbstractDocument.ContentElementName.equals(a.getAttribute(StyleConstants.NameAttribute)) is false");
2450N/A }
2450N/A
2450N/A if (a.isDefined(SwingUtilities2.IMPLIED_CR)) {
2450N/A throw new RuntimeException("a.isDefined(SwingUtilities2.IMPLIED_CR) is true");
2450N/A }
2450N/A
2450N/A return;
2450N/A }
2450N/A }
2450N/A
2450N/A }
2450N/A
2450N/A JEditorPane ep;
2450N/A
2450N/A void initAtParagraphStart() {
2450N/A ep.setText("A<p>B");
2450N/A hitKey(KeyEvent.VK_LEFT);
2450N/A }
2450N/A
2450N/A void sendAtParagraphStart() {
2450N/A sendInputMethodEvent();
2450N/A }
2450N/A
2450N/A void checkAtParagraphStart() {
2450N/A checkComposedTextRun();
2450N/A }
2450N/A
2450N/A void initAfterBRElement() {
2450N/A ep.setText("A<br>B");
2450N/A hitKey(KeyEvent.VK_LEFT);
2450N/A }
2450N/A
2450N/A void sendAtBRElement() {
2450N/A sendInputMethodEvent();
2450N/A }
2450N/A
2450N/A void checkAtBrElement() {
2450N/A checkComposedTextRun();
2450N/A }
2450N/A
2450N/A private void hitKey(int keycode) {
2450N/A robot.keyPress(keycode);
2450N/A robot.keyRelease(keycode);
2450N/A robot.delay(550); // The magic number equals JRobot.DEFAULT_DELAY
2450N/A }
2450N/A
2450N/A private void run() throws Exception {
2450N/A robot = new Robot();
2450N/A
2450N/A ep = new JEditorPane();
2450N/A ep.setContentType("text/html");
2450N/A ep.setPreferredSize(new Dimension(100, 100));
2450N/A
2450N/A JFrame frame = new JFrame("Test: " + getClass().getName());
2450N/A
2450N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2450N/A frame.add(ep);
2450N/A frame.setVisible(true);
2450N/A }
2450N/A
2450N/A public static void main(String[] args) throws Throwable {
2450N/A SwingUtilities.invokeAndWait(new Runnable() {
2450N/A public void run() {
2450N/A try {
2450N/A bug6636983 bug6636983 = new bug6636983();
2450N/A
2450N/A bug6636983.run();
2450N/A bug6636983.initAtParagraphStart();
2450N/A bug6636983.sendAtParagraphStart();
2450N/A bug6636983.checkAtParagraphStart();
2450N/A bug6636983.initAfterBRElement();
2450N/A bug6636983.sendAtBRElement();
2450N/A bug6636983.checkAtBrElement();
2450N/A
2450N/A System.out.println("OK");
2450N/A } catch (Exception e) {
2450N/A throw new RuntimeException("The test failed", e);
2450N/A }
2450N/A }
2450N/A });
2450N/A }
2450N/A}