Skip to main content
summaryrefslogtreecommitdiffstats
blob: d00f9c8d83f6a37209b179f9f0a183ce3544efd0 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*******************************************************************************
 * Copyright (c) 2013 Red Hat Inc.
 * 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:
 *     Red Hat Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.perf.tests;

import static org.junit.Assert.assertEquals;
import org.eclipse.linuxtools.internal.perf.ui.SourceDisassemblyView;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Shell;
import org.junit.Test;

/**
 * Tests for perf source disassembly find text dialog.
 */
public class FindActionTest {
	private static final String SAMPLE_STRS[] = new String[] { "sample", "styled", "text", " ", "tyled", "ample" };

	@Test
	public void testFindAction() {
		for (String strFind : SAMPLE_STRS) {
			int[] offsets = new int[] { 0, strFind.length()/2, strFind.length()};
			for (int offset : offsets) {
				testForwardSearch(offset , strFind);
				testBackwardSearch(offset , strFind);
			}
		}
	}

	/**
	 * Test forward search functionality.
	 * @param offset offset at which searching starts
	 * @param findStr the string to find
	 */
	private void testForwardSearch(int offset, String findStr) {
		StubSourceDisassemblyView stubView = new StubSourceDisassemblyView();
		String searchStr = stubView.getContent();
		int[] indices = new int[]{
				/*
				 * SourceDisassemblyView#findAndSelect parameters:
				 * 		- widgetOffset, findstring, searchforward, casesensitive, wholeword
				 */
				stubView.findAndSelect(offset, findStr, true, true, false),
				stubView.findAndSelect(offset, findStr, true, false, false)
		};

		int expected = -1;
		for(int actual : indices){
			expected =  searchStr.indexOf(findStr, offset);
			assertEquals("Failed on the following case:"
					+ " offset= " + offset
					+ " substring= " + findStr,
					expected, actual);
		}
	}

	/**
	 * Test backward search functionality.
	 * @param offset offset at which searching starts
	 * @param findStr the string to find
	 */
	private void testBackwardSearch(int offset, String findStr) {

		StubSourceDisassemblyView stubView = new StubSourceDisassemblyView();
		String searchStr = stubView.getContent().substring(0, offset);
		int[] indices = new int[]{
			/*
			 * SourceDisassemblyView#findAndSelect parameters:
			 * 		- widgetOffset, findString, searchForward, caseSensitive, wholeWord
			 */
			stubView.findAndSelect(offset, findStr, false, true, false),
			stubView.findAndSelect(offset, findStr, false, false, false)
		};

		int expected = -1;
		for(int actual : indices){
			expected =  searchStr.lastIndexOf(findStr, offset - 1);
			assertEquals("Failed on the following case:"
					+ " offset= " + offset
					+ " substring= " + findStr,
					expected, actual);
		}
	}

	/**
	 * Stub source disassemlby view part.
	 */
	private static class StubSourceDisassemblyView extends
			SourceDisassemblyView {
		public StubSourceDisassemblyView() {
			Shell parent = new Shell();
			StyledText txt = new StyledText(parent, SWT.DEFAULT);
			txt.setText("sample styled text");
			txt.setEditable(false);
			setStyledText(txt);
		}
	}
}

Back to the top