Coverage Report - org.eclipse.swtbot.forms.finder.widgets.SWTBotHyperlink
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotHyperlink
21%
4/19
0%
0/2
1.167
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2010 Chris Aniszczyk and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  * 
 8  
  * Contributors:
 9  
  *     Chris Aniszczyk <caniszczyk@gmail.com> - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.forms.finder.widgets;
 12  
 
 13  
 import java.util.regex.Matcher;
 14  
 import java.util.regex.Pattern;
 15  
 
 16  
 import org.eclipse.swt.SWT;
 17  
 import org.eclipse.swt.widgets.Event;
 18  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 19  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 20  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 21  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 22  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 23  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 24  
 import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
 25  
 import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl;
 26  
 import org.eclipse.ui.forms.widgets.Hyperlink;
 27  
 import org.hamcrest.SelfDescribing;
 28  
 
 29  
 /**
 30  
  * This represents a {@link Hyperlink} widget.
 31  
  * 
 32  
  * @author Chris Aniszczyk &lt;caniszczyk [at] gmail [dot] com&gt;
 33  
  * @version $Id$
 34  
  */
 35  
 @SWTBotWidget(clasz = Hyperlink.class, preferredName = "hyperlink", referenceBy = { ReferenceBy.MNEMONIC })
 36  
 public class SWTBotHyperlink extends AbstractSWTBotControl<Hyperlink> {
 37  
 
 38  
         /**
 39  
          * Constructs a new instance with the given widget.
 40  
          * 
 41  
          * @param w the widget.
 42  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 43  
          */
 44  
         public SWTBotHyperlink(Hyperlink w) throws WidgetNotFoundException {
 45  1
                 super(w);
 46  1
         }
 47  
 
 48  
         /**
 49  
          * Constructs a new instance with the given widget.
 50  
          * 
 51  
          * @param w the widget.
 52  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 53  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 54  
          */
 55  
         public SWTBotHyperlink(Hyperlink w, SelfDescribing description) throws WidgetNotFoundException {
 56  1
                 super(w, description);
 57  1
         }
 58  
 
 59  
         public AbstractSWTBot<Hyperlink> click() {
 60  0
                 return click(true);
 61  
         }
 62  
 
 63  
         /**
 64  
          * Clicks on the hyperlink with the specified text.
 65  
          * 
 66  
          * @param hyperlinkText the text of the hyperlink in case there are more than one hyperlinks.
 67  
          * @return itself.
 68  
          */
 69  
         public AbstractSWTBot<Hyperlink> click(String hyperlinkText) {
 70  0
                 log.debug(MessageFormat.format("Clicking on {0}", SWTUtils.getText(widget))); //$NON-NLS-1$
 71  0
                 String text = getText();
 72  0
                 boolean isText = text.contains(">" + hyperlinkText + "<");
 73  0
                 Assert.isLegal(isText, "Link does not contain text (" + hyperlinkText + "). It contains (" + text + ")");
 74  
 
 75  0
                 hyperlinkText = extractHyperlinkTextOrHREF(hyperlinkText, text);
 76  0
                 notify(SWT.Selection, createHyperlinkEvent(hyperlinkText));
 77  
 
 78  0
                 log.debug(MessageFormat.format("Clicked on {0}", SWTUtils.getText(widget))); //$NON-NLS-1$
 79  0
                 return click(true);
 80  
         }
 81  
 
 82  
         private String extractHyperlinkTextOrHREF(String hyperlinkText, String text) {
 83  0
                 Pattern pattern = Pattern.compile(".*<[aA] [hH][rR][eE][fF]\\s*=\\s*['\"](.*)['\"]>" + hyperlinkText + "</[aA]>.*");
 84  0
                 Matcher matcher = pattern.matcher(text);
 85  0
                 return matcher.find() ? matcher.group(1) : hyperlinkText;
 86  
         }
 87  
 
 88  
         private Event createHyperlinkEvent(String hyperlinkText) {
 89  0
                 Event e = createEvent();
 90  0
                 e.text = hyperlinkText;
 91  0
                 return e;
 92  
         }
 93  
 }