1 /*
   2  * Copyright (c) 2010, 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 6636983
  27  * @summary test that composed text at the line starts is handled correctly
  28  * @author Sergey Groznyh
  29  * @run main bug6636983
  30  */
  31 
  32 import sun.swing.SwingUtilities2;
  33 
  34 import javax.swing.*;
  35 import javax.swing.text.*;
  36 import javax.swing.text.html.HTMLDocument;
  37 import java.awt.*;
  38 import java.awt.event.InputMethodEvent;
  39 import java.awt.event.KeyEvent;
  40 import java.text.AttributedString;
  41 
  42 public class bug6636983 {
  43     private Robot robot;
  44 
  45     private final AttributedString Hiragana_A = new AttributedString("\u3042");
  46 
  47     void sendInputMethodEvent() {
  48         InputMethodEvent ime = new InputMethodEvent(
  49                 ep,
  50                 InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
  51                 Hiragana_A.getIterator(),
  52                 0,
  53                 null,
  54                 null);
  55         ep.dispatchEvent(ime);
  56     }
  57 
  58     void checkComposedTextRun() {
  59         HTMLDocument d = (HTMLDocument) ep.getDocument();
  60         ElementIterator it = new ElementIterator(d.getDefaultRootElement());
  61 
  62         while (true) {
  63             Element e = it.next();
  64             if (e == null) {
  65                 throw new RuntimeException("no composed text found");
  66             }
  67             AttributeSet a = e.getAttributes();
  68             if (a.isDefined(StyleConstants.ComposedTextAttribute)) {
  69                 if (!AbstractDocument.ContentElementName.equals(a.getAttribute(StyleConstants.NameAttribute))) {
  70                     throw new RuntimeException("AbstractDocument.ContentElementName.equals(a.getAttribute(StyleConstants.NameAttribute)) is false");
  71                 }
  72 
  73                 if (a.isDefined(SwingUtilities2.IMPLIED_CR)) {
  74                     throw new RuntimeException("a.isDefined(SwingUtilities2.IMPLIED_CR) is true");
  75                 }
  76 
  77                 return;
  78             }
  79         }
  80 
  81     }
  82 
  83     JEditorPane ep;
  84 
  85     void initAtParagraphStart() {
  86         ep.setText("A<p>B");
  87         hitKey(KeyEvent.VK_LEFT);
  88     }
  89 
  90     void sendAtParagraphStart() {
  91         sendInputMethodEvent();
  92     }
  93 
  94     void checkAtParagraphStart() {
  95         checkComposedTextRun();
  96     }
  97 
  98     void initAfterBRElement() {
  99         ep.setText("A<br>B");
 100         hitKey(KeyEvent.VK_LEFT);
 101     }
 102 
 103     void sendAtBRElement() {
 104         sendInputMethodEvent();
 105     }
 106 
 107     void checkAtBrElement() {
 108         checkComposedTextRun();
 109     }
 110 
 111     private void hitKey(int keycode) {
 112         robot.keyPress(keycode);
 113         robot.keyRelease(keycode);
 114         robot.delay(550); // The magic number equals JRobot.DEFAULT_DELAY
 115     }
 116 
 117     private void run() throws Exception {
 118         robot = new Robot();
 119 
 120         ep = new JEditorPane();
 121         ep.setContentType("text/html");
 122         ep.setPreferredSize(new Dimension(100, 100));
 123 
 124         JFrame frame = new JFrame("Test: " + getClass().getName());
 125 
 126         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 127         frame.add(ep);
 128         frame.setVisible(true);
 129     }
 130 
 131     public static void main(String[] args) throws Throwable {
 132         SwingUtilities.invokeAndWait(new Runnable() {
 133             public void run() {
 134                 try {
 135                     bug6636983 bug6636983 = new bug6636983();
 136 
 137                     bug6636983.run();
 138                     bug6636983.initAtParagraphStart();
 139                     bug6636983.sendAtParagraphStart();
 140                     bug6636983.checkAtParagraphStart();
 141                     bug6636983.initAfterBRElement();
 142                     bug6636983.sendAtBRElement();
 143                     bug6636983.checkAtBrElement();
 144 
 145                     System.out.println("OK");
 146                 } catch (Exception e) {
 147                     throw new RuntimeException("The test failed", e);
 148                 }
 149             }
 150         });
 151     }
 152 }