1 /* 2 * Copyright (c) 2018, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package org.netbeans.jemmy.operators; 27 28 import static org.testng.Assert.assertTrue; 29 import static org.testng.Assert.fail; 30 31 import java.awt.BorderLayout; 32 import java.awt.Dimension; 33 import java.awt.IllegalComponentStateException; 34 import java.io.IOException; 35 import java.net.URL; 36 37 import javax.swing.JEditorPane; 38 import javax.swing.JFrame; 39 import javax.swing.JPanel; 40 import javax.swing.JScrollPane; 41 import javax.swing.JViewport; 42 import javax.swing.event.HyperlinkEvent; 43 import javax.swing.event.HyperlinkListener; 44 45 import org.testng.annotations.AfterClass; 46 import org.testng.annotations.BeforeClass; 47 import org.testng.annotations.Test; 48 49 public class JEditorPaneOperatorTest { 50 51 private final static String PAGE1 = "page1"; 52 private final static String PAGE2 = "page2"; 53 private final static String PAGE1_TEXT = "hi"; 54 private final static String PAGE2_TEXT = "hello"; 55 private final URL page1URL = getClass().getResource("resources/page1.html"); 56 private JFrame frame; 57 private HyperlinkListener listener = null; 58 59 @BeforeClass 60 protected void setUp() throws Exception { 61 frame = new JFrame(); 62 frame.setPreferredSize(new Dimension(800, 600)); 63 frame.pack(); 64 frame.setLocationRelativeTo(null); 65 66 listener = event -> { 67 if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 68 try { 69 ((JEditorPane)event.getSource()).setPage(event.getURL()); 70 } catch (IOException e) { 71 e.printStackTrace(); 72 } 73 } 74 }; 75 } 76 77 @AfterClass 78 protected void tearDown() throws Exception { 79 frame.setVisible(false); 80 frame.dispose(); 81 } 82 83 @Test 84 public void testClickOnReferenceWithScrollPane() throws IOException { 85 JEditorPane editorPane = new JEditorPane(page1URL); 86 editorPane.setEditable(false); 87 88 JScrollPane scroller = new JScrollPane(); 89 JViewport vp = scroller.getViewport(); 90 vp.add(editorPane); 91 92 JPanel panel = new JPanel(); 93 panel.setLayout(new BorderLayout()); 94 panel.add(scroller, BorderLayout.CENTER); 95 frame.getContentPane().add(panel); 96 frame.setVisible(true); 97 JEditorPaneOperator editorPaneOperator = 98 new JEditorPaneOperator(new FrameOperator(frame)); 99 checkPageLoaded(editorPaneOperator, PAGE1, PAGE1_TEXT); 100 101 // Testing reference which doesn't exist 102 String excepMessage = ""; 103 try { 104 editorPaneOperator.clickOnReference(PAGE1); 105 fail(); 106 } catch (IllegalArgumentException e) { 107 excepMessage = e.getMessage(); 108 } 109 assertTrue(excepMessage.contains( 110 "Reference page1 doesn't exist in the document")); 111 try { 112 editorPaneOperator.addHyperlinkListener(listener); 113 // Testing on a short text page 114 editorPaneOperator.clickOnReference(PAGE2); 115 checkPageLoaded(editorPaneOperator, PAGE2, PAGE2_TEXT); 116 117 // Testing on a long text page 118 editorPaneOperator.clickOnReference(PAGE1); 119 editorPaneOperator.waitStateOnQueue(comp 120 -> ((JEditorPane)comp).getPage().toString().contains(PAGE1)); 121 } finally { 122 editorPane.removeHyperlinkListener(listener); 123 frame.getContentPane().remove(panel); 124 } 125 } 126 127 @Test 128 public void testClickOnReferenceWithoutScrollPane() throws IOException { 129 JEditorPane editorPane = new JEditorPane(page1URL); 130 editorPane.setEditable(false); 131 frame.getContentPane().add(editorPane); 132 frame.setVisible(true); 133 JEditorPaneOperator editorPaneOperator = 134 new JEditorPaneOperator(new JFrameOperator(frame)); 135 checkPageLoaded(editorPaneOperator, PAGE1, PAGE1_TEXT); 136 137 try { 138 editorPaneOperator.addHyperlinkListener(listener); 139 // Testing reference on the visible area 140 editorPaneOperator.clickOnReference(PAGE2); 141 checkPageLoaded(editorPaneOperator, PAGE2, PAGE2_TEXT); 142 143 // Testing reference on the non-visible area 144 String excepMessage = ""; 145 try { 146 editorPaneOperator.clickOnReference(PAGE1); 147 fail(); 148 } catch (IllegalComponentStateException e) { 149 excepMessage = e.getMessage(); 150 } 151 assertTrue(excepMessage.equals("Component doesn't contain" 152 + " JScrollPane and Reference is out of visible area")); 153 } finally { 154 editorPaneOperator.removeHyperlinkListener(listener); 155 frame.getContentPane().remove(editorPane); 156 } 157 } 158 159 private void checkPageLoaded(JEditorPaneOperator editorPaneOperator, 160 String page, String text) { 161 editorPaneOperator.waitStateOnQueue(comp 162 -> ((JEditorPane)comp).getPage().toString().contains(page)); 163 editorPaneOperator.selectText(text); 164 editorPaneOperator.waitStateOnQueue(comp 165 -> text.equals(editorPaneOperator.getSelectedText())); 166 } 167 }