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