Skip to main content
summaryrefslogtreecommitdiffstats
blob: 74423c8a7631c36b5f288f4d164d576211fb8f8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*******************************************************************************
 * Copyright (c) 2004, 2008 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.tasks.tests.ui;

import junit.framework.TestCase;

import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.mylyn.internal.tasks.ui.editors.TaskHyperlinkDetector;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.tests.TaskTestUtil;
import org.eclipse.mylyn.tasks.tests.connector.MockRepositoryConnectorUi;
import org.eclipse.mylyn.tasks.ui.AbstractRepositoryConnectorUi;
import org.eclipse.mylyn.tasks.ui.AbstractTaskHyperlinkDetector;

/**
 * @author Steffen Pingel
 */
public class TaskHyperlinkDetectorTest extends TestCase {

	protected MockRepositoryConnectorUi connectorUi;

	protected TaskRepository repository;

	@Override
	protected void setUp() throws Exception {
		repository = TaskTestUtil.createMockRepository();
		connectorUi = new MockRepositoryConnectorUi();
	}

	protected IHyperlink[] detect(final String text, int start, int length) {
		AbstractTaskHyperlinkDetector detector = createHyperlinkDetector();
		return detector.detectHyperlinks(new MockTextViewer(text), new Region(start, length), true);
	}

	protected AbstractTaskHyperlinkDetector createHyperlinkDetector() {
		TaskHyperlinkDetector detector = new TaskHyperlinkDetector() {
			@Override
			protected TaskRepository getTaskRepository(ITextViewer textViewer) {
				return repository;
			}

			@Override
			protected AbstractRepositoryConnectorUi getConnectorUi(TaskRepository repository) {
				return connectorUi;
			}
		};
		return detector;
	}

	public void testMultiple() {
		IHyperlink[] links = detect("123 456 789", 4, 5);
		assertNotNull(links);
		assertEquals(2, links.length);
		assertEquals(new Region(4, 3), links[0].getHyperlinkRegion());
		assertEquals(new Region(8, 1), links[1].getHyperlinkRegion());
	}

	public void testMultipleFullRegion() {
		IHyperlink[] links = detect("123 456 789", 0, 11);
		assertEquals(3, links.length);
		assertEquals(new Region(0, 3), links[0].getHyperlinkRegion());
		assertEquals(new Region(4, 3), links[1].getHyperlinkRegion());
		assertEquals(new Region(8, 3), links[2].getHyperlinkRegion());
	}

	public void testSingleZeroLenghtRegion() {
		IHyperlink[] links = detect("123 456 789", 5, 0);
		assertEquals(1, links.length);
	}

	public void testSpaceZeroLengthRegion() {
		IHyperlink[] links = detect("1234  789", 5, 0);
		assertNull(links);
	}

}

Back to the top