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