1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
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 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
@SWTBotWidget(clasz = Link.class, preferredName = "link", referenceBy = { ReferenceBy.MNEMONIC }) |
32 | |
public class SWTBotLink extends AbstractSWTBotControl<Link> { |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public SWTBotLink(Link w) throws WidgetNotFoundException { |
41 | 0 | super(w); |
42 | 0 | } |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
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 | |
|
61 | |
|
62 | |
|
63 | |
|
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 | |
} |