Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotLink
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotLink
89%
17/19
100%
2/2
1.167
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2009 Ketan Padegaonkar 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  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.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.swt.widgets.Link;
 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.utils.internal.Assert;
 23  
 import org.hamcrest.SelfDescribing;
 24  
 
 25  
 /**
 26  
  * This represents a {@link Link} widget.
 27  
  * 
 28  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 29  
  * @version $Id$
 30  
  */
 31  
 @SWTBotWidget(clasz = Link.class, preferredName = "link", referenceBy = { ReferenceBy.MNEMONIC })
 32  
 public class SWTBotLink extends AbstractSWTBotControl<Link> {
 33  
 
 34  
         /**
 35  
          * Constructs a new instance with the given widget.
 36  
          * 
 37  
          * @param w the widget.
 38  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 39  
          */
 40  
         public SWTBotLink(Link w) throws WidgetNotFoundException {
 41  0
                 super(w);
 42  0
         }
 43  
 
 44  
         /**
 45  
          * Constructs a new instance with the given widget.
 46  
          * 
 47  
          * @param w the widget.
 48  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 49  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 50  
          */
 51  
         public SWTBotLink(Link w, SelfDescribing description) throws WidgetNotFoundException {
 52  3
                 super(w, description);
 53  3
         }
 54  
 
 55  
         public AbstractSWTBot<Link> click() {
 56  1
                 return click(true);
 57  
         }
 58  
 
 59  
         /**
 60  
          * Clicks on the hyperlink with the specified text.
 61  
          * 
 62  
          * @param hyperlinkText the text of the hyperlink in case there are more than one hyperlinks.
 63  
          * @return itself.
 64  
          */
 65  
         public AbstractSWTBot<Link> click(String hyperlinkText) {
 66  2
                 String text = getText();
 67  2
                 boolean isText = text.contains(">" + hyperlinkText + "<");
 68  2
                 Assert.isLegal(isText, "Link does not contain text (" + hyperlinkText + "). It contains (" + text + ")");
 69  
 
 70  2
                 hyperlinkText = extractHyperlinkTextOrHREF(hyperlinkText, text);
 71  2
                 notify(SWT.MouseDown, createMouseEvent(0, 0, 1, SWT.NONE, 1));
 72  2
                 notify(SWT.Selection, createHyperlinkEvent(hyperlinkText));
 73  2
                 notify(SWT.MouseUp, createMouseEvent(0, 0, 1, SWT.BUTTON1, 1));
 74  2
                 return this;
 75  
         }
 76  
 
 77  
         private String extractHyperlinkTextOrHREF(String hyperlinkText, String text) {
 78  2
                 Pattern pattern = Pattern.compile(".*<[aA] [hH][rR][eE][fF]\\s*=\\s*['\"](.*)['\"]>" + hyperlinkText + "</[aA]>.*");
 79  2
                 Matcher matcher = pattern.matcher(text);
 80  2
                 return matcher.find() ? matcher.group(1) : hyperlinkText;
 81  
         }
 82  
 
 83  
         private Event createHyperlinkEvent(String hyperlinkText) {
 84  2
                 Event e = createEvent();
 85  2
                 e.text = hyperlinkText;
 86  2
                 return e;
 87  
         }
 88  
 }