Coverage Report - org.eclipse.swtbot.forms.finder.widgets.SWTBotImageHyperlink
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotImageHyperlink
18%
4/22
0%
0/2
1.083
SWTBotImageHyperlink$1
0%
0/3
N/A
1.083
SWTBotImageHyperlink$2
0%
0/3
N/A
1.083
SWTBotImageHyperlink$3
0%
0/3
N/A
1.083
 
 1  0
 /*******************************************************************************
 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.graphics.Image;
 18  
 import org.eclipse.swt.widgets.Event;
 19  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 20  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 21  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 22  
 import org.eclipse.swtbot.swt.finder.results.Result;
 23  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 24  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 25  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 26  
 import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
 27  
 import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl;
 28  
 import org.eclipse.ui.forms.widgets.ImageHyperlink;
 29  
 import org.hamcrest.SelfDescribing;
 30  
 
 31  
 /**
 32  
  * This represents a {@link ImageHyperlink} widget.
 33  
  * 
 34  
  * @author Chris Aniszczyk &lt;caniszczyk [at] gmail [dot] com&gt;
 35  
  * @version $Id$
 36  
  */
 37  
 @SWTBotWidget(clasz = ImageHyperlink.class, preferredName = "imageHyperlink", referenceBy = { ReferenceBy.MNEMONIC })
 38  
 public class SWTBotImageHyperlink extends AbstractSWTBotControl<ImageHyperlink> {
 39  
 
 40  
         /**
 41  
          * Constructs a new instance with the given widget.
 42  
          * 
 43  
          * @param w the widget.
 44  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 45  
          */
 46  
         public SWTBotImageHyperlink(ImageHyperlink w) throws WidgetNotFoundException {
 47  2
                 super(w);
 48  2
         }
 49  
 
 50  
         /**
 51  
          * Constructs a new instance with the given widget.
 52  
          * 
 53  
          * @param w the widget.
 54  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 55  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 56  
          */
 57  
         public SWTBotImageHyperlink(ImageHyperlink w, SelfDescribing description) throws WidgetNotFoundException {
 58  2
                 super(w, description);
 59  2
         }
 60  
         
 61  
         public AbstractSWTBot<ImageHyperlink> click() {
 62  0
                 return click(true);
 63  
         }
 64  
 
 65  
         /**
 66  
          * Return the ImageHyperlink's image or <code>null</code>.
 67  
          * 
 68  
          * @return the image of the image hyperlink or <code>null</code>.
 69  
          */
 70  
         public Image image() {
 71  0
                 return syncExec(new Result<Image>() {
 72  
                         public Image run() {
 73  0
                                 return widget.getImage();
 74  
                         }
 75  
                 });
 76  
         }
 77  
         
 78  
         /**
 79  
          * Return the ImageHyperlink's hover image or <code>null</code>.
 80  
          * 
 81  
          * @return the hover image of the image hyperlink or <code>null</code>.
 82  
          */
 83  
         public Image hoverImage() {
 84  0
                 return syncExec(new Result<Image>() {
 85  
                         public Image run() {
 86  0
                                 return widget.getHoverImage();
 87  
                         }
 88  
                 });
 89  
         }
 90  
         
 91  
         /**
 92  
          * Return the ImageHyperlink's active image or <code>null</code>.
 93  
          * 
 94  
          * @return the active image of the image hyperlink or <code>null</code>.
 95  
          */
 96  
         public Image activeImage() {
 97  0
                 return syncExec(new Result<Image>() {
 98  
                         public Image run() {
 99  0
                                 return widget.getActiveImage();
 100  
                         }
 101  
                 });
 102  
         }
 103  
 
 104  
         /**
 105  
          * Clicks on the image hyperlink with the specified text.
 106  
          * 
 107  
          * @param hyperlinkText the text of the image hyperlink in case there are more than one hyperlinks.
 108  
          * @return itself.
 109  
          */
 110  
         public AbstractSWTBot<ImageHyperlink> click(String hyperlinkText) {
 111  0
                 log.debug(MessageFormat.format("Clicked on {0}", SWTUtils.getText(widget))); //$NON-NLS-1$
 112  0
                 String text = getText();
 113  0
                 boolean isText = text.contains(">" + hyperlinkText + "<");
 114  0
                 Assert.isLegal(isText, "Link does not contain text (" + hyperlinkText + "). It contains (" + text + ")");
 115  
 
 116  0
                 hyperlinkText = extractHyperlinkTextOrHREF(hyperlinkText, text);
 117  0
                 notify(SWT.Selection, createHyperlinkEvent(hyperlinkText));
 118  
 
 119  0
                 log.debug(MessageFormat.format("Clicked on {0}", SWTUtils.getText(widget))); //$NON-NLS-1$
 120  0
                 return click(true);
 121  
         }
 122  
 
 123  
         private String extractHyperlinkTextOrHREF(String hyperlinkText, String text) {
 124  0
                 Pattern pattern = Pattern.compile(".*<[aA] [hH][rR][eE][fF]\\s*=\\s*['\"](.*)['\"]>" + hyperlinkText + "</[aA]>.*");
 125  0
                 Matcher matcher = pattern.matcher(text);
 126  0
                 return matcher.find() ? matcher.group(1) : hyperlinkText;
 127  
         }
 128  
 
 129  
         private Event createHyperlinkEvent(String hyperlinkText) {
 130  0
                 Event e = createEvent();
 131  0
                 e.text = hyperlinkText;
 132  0
                 return e;
 133  
         }
 134  
 }