1 /*******************************************************************************
2  * Copyright (c) 2000, 2020 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Björn Michael <b.michael@gmx.de> - [syntax highlighting] Syntax coloring for abstract classes - https://bugs.eclipse.org/331311
14  *     Björn Michael <b.michael@gmx.de> - [syntax highlighting] Add highlight for inherited fields - https://bugs.eclipse.org/348368
15  *******************************************************************************/
16 package org.eclipse.jdt.text.tests;
17 
18 import org.junit.Rule;
19 import org.junit.Test;
20 
21 import org.eclipse.jface.text.Position;
22 
23 import org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlightings;
24 
25 public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
26 	@Rule
27 	public SemanticHighlightingTestSetup shts= new SemanticHighlightingTestSetup( "/SHTest/src/SHTest.java");
28 
29 	@Test
deprecatedMemberHighlighting()30 	public void deprecatedMemberHighlighting() throws Exception {
31 		setUpSemanticHighlighting(SemanticHighlightings.DEPRECATED_MEMBER);
32 		Position[] expected= new Position[] {
33 				createPosition(0, 12, 13),
34 				createPosition(22, 5, 15),
35 				createPosition(24, 1, 13),
36 				createPosition(24, 15, 16),
37 				createPosition(25, 2, 15),
38 				createPosition(26, 2, 16),
39 				createPosition(27, 10, 10),
40 				createPosition(30, 7, 10),
41 				createPosition(30, 26, 13),
42 		};
43 		Position[] actual= getSemanticHighlightingPositions();
44 		assertEqualPositions(expected, actual);
45 	}
46 
47 	@Test
staticFinalFieldHighlighting()48 	public void staticFinalFieldHighlighting() throws Exception {
49 		setUpSemanticHighlighting(SemanticHighlightings.STATIC_FINAL_FIELD);
50 		Position[] expected= new Position[] {
51 				createPosition(6, 18, 16),
52 				createPosition(35, 37, 16),
53 		};
54 		Position[] actual= getSemanticHighlightingPositions();
55 		assertEqualPositions(expected, actual);
56 	}
57 
58 	@Test
staticFieldHighlighting()59 	public void staticFieldHighlighting() throws Exception {
60 		setUpSemanticHighlighting(SemanticHighlightings.STATIC_FIELD);
61 		Position[] expected= new Position[] {
62 				createPosition(4, 12, 11),
63 				createPosition(6, 18, 16),
64 				createPosition(33, 32, 11),
65 				createPosition(35, 37, 16),
66 		};
67 		Position[] actual= getSemanticHighlightingPositions();
68 		assertEqualPositions(expected, actual);
69 	}
70 
71 	@Test
fieldHighlighting()72 	public void fieldHighlighting() throws Exception {
73 		setUpSemanticHighlighting(SemanticHighlightings.FIELD);
74 		Position[] expected= new Position[] {
75 				createPosition( 3,  5,  5),
76 				createPosition( 4, 12, 11),
77 				createPosition( 5, 11, 10),
78 				createPosition( 6, 18, 16),
79 				createPosition(22,  5, 15),
80 				createPosition(25,  2, 15),
81 				createPosition(31,  9,  6),
82 				createPosition(32,  6, 11),
83 				createPosition(32, 31,  5),
84 				createPosition(33,  6, 17),
85 				createPosition(33, 32, 11),
86 				createPosition(34,  6, 16),
87 				createPosition(34, 36, 10),
88 				createPosition(35,  6, 22),
89 				createPosition(35, 37, 16),
90 				createPosition(48,  6, 14),
91 				createPosition(48, 22,  5),
92 		};
93 		Position[] actual= getSemanticHighlightingPositions();
94 		assertEqualPositions(expected, actual);
95 	}
96 
97 	@Test
inheritedFieldHighlighting()98 	public void inheritedFieldHighlighting() throws Exception {
99 		setUpSemanticHighlighting(SemanticHighlightings.INHERITED_FIELD);
100 		Position[] expected= new Position[] {
101 				createPosition(48, 22,  5),
102 		};
103 		Position[] actual= getSemanticHighlightingPositions();
104 		assertEqualPositions(expected, actual);
105 	}
106 
107 	@Test
numberHighlighting()108 	public void numberHighlighting() throws Exception {
109 		setUpSemanticHighlighting(SemanticHighlightings.NUMBER);
110 		Position[] expected= new Position[] {
111 				createPosition(5, 23, 1),
112 				createPosition(6, 36, 1),
113 				createPosition(8, 21, 1),
114 				createPosition(13, 19, 1),
115 				createPosition(13, 31, 2),
116 			};
117 
118 		Position[] actual= getSemanticHighlightingPositions();
119 		assertEqualPositions(expected, actual);
120 	}
121 
122 	@Test
methodDeclarationHighlighting()123 	public void methodDeclarationHighlighting() throws Exception {
124 		setUpSemanticHighlighting(SemanticHighlightings.METHOD_DECLARATION);
125 		Position[] expected= new Position[] {
126 				createPosition(7, 6, 6),
127 				createPosition(19, 13, 12),
128 				createPosition(20, 15, 14),
129 				createPosition(24, 15, 16),
130 				createPosition(40, 4, 6),
131 		};
132 		Position[] actual= getSemanticHighlightingPositions();
133 		assertEqualPositions(expected, actual);
134 	}
135 
136 	@Test
staticMethodInvocationHighlighting()137 	public void staticMethodInvocationHighlighting() throws Exception {
138 		setUpSemanticHighlighting(SemanticHighlightings.STATIC_METHOD_INVOCATION);
139 		Position[] expected= new Position[] {
140 				createPosition(10, 2, 12),
141 		};
142 		Position[] actual= getSemanticHighlightingPositions();
143 		assertEqualPositions(expected, actual);
144 	}
145 
146 	/*
147 	 * [syntax highlighting] 'Abstract Method Invocation' highlighting also matches declaration
148 	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=73353
149 	 */
150 	@Test
abstractMethodInvocationHighlighting()151 	public void abstractMethodInvocationHighlighting() throws Exception {
152 		setUpSemanticHighlighting(SemanticHighlightings.ABSTRACT_METHOD_INVOCATION);
153 		Position[] expected= new Position[] {
154 				createPosition(11, 2, 14),
155 		};
156 		Position[] actual= getSemanticHighlightingPositions();
157 		assertEqualPositions(expected, actual);
158 	}
159 
160 	@Test
inheritedMethodInvocationHighlighting()161 	public void inheritedMethodInvocationHighlighting() throws Exception {
162 		setUpSemanticHighlighting(SemanticHighlightings.INHERITED_METHOD_INVOCATION);
163 		Position[] expected= new Position[] {
164 				createPosition(12, 2, 8),
165 				createPosition(15, 17, 8),
166 		};
167 		Position[] actual= getSemanticHighlightingPositions();
168 		assertEqualPositions(expected, actual);
169 	}
170 
171 	@Test
localVariableDeclarationHighlighting()172 	public void localVariableDeclarationHighlighting() throws Exception {
173 		setUpSemanticHighlighting(SemanticHighlightings.LOCAL_VARIABLE_DECLARATION);
174 		Position[] expected= new Position[] {
175 				createPosition(7, 17, 5),
176 				createPosition(8, 6, 5),
177 				createPosition(13, 11, 6),
178 				createPosition(14, 26, 6),
179 				createPosition(41, 16, 4),
180 				createPosition(42, 20, 13),
181 				createPosition(43, 15, 7),
182 		};
183 		Position[] actual= getSemanticHighlightingPositions();
184 		assertEqualPositions(expected, actual);
185 	}
186 
187 	@Test
localVariableHighlighting()188 	public void localVariableHighlighting() throws Exception {
189 		setUpSemanticHighlighting(SemanticHighlightings.LOCAL_VARIABLE);
190 		Position[] expected= new Position[] {
191 				createPosition(7, 17, 5),
192 				createPosition(8, 6, 5),
193 				createPosition(8, 13, 5),
194 				createPosition(9, 2, 5),
195 				createPosition(13, 11, 6),
196 				createPosition(13, 22, 6),
197 				createPosition(13, 35, 6),
198 				createPosition(14, 26, 6),
199 				createPosition(15, 3, 5),
200 				createPosition(15, 10, 6),
201 				createPosition(16, 3, 6),
202 				createPosition(41, 16, 4),
203 				createPosition(42, 20, 13),
204 				createPosition(43, 15, 7),
205 		};
206 		Position[] actual= getSemanticHighlightingPositions();
207 		assertEqualPositions(expected, actual);
208 	}
209 
210 	@Test
parameterVariableHighlighting()211 	public void parameterVariableHighlighting() throws Exception {
212 		setUpSemanticHighlighting(SemanticHighlightings.PARAMETER_VARIABLE);
213 		Position[] actual= getSemanticHighlightingPositions();
214 		Position[] expected= new Position[] {
215 				createPosition(7, 17, 5),
216 				createPosition(8, 13, 5),
217 		};
218 		assertEqualPositions(expected, actual);
219 	}
220 
221 	@Test
annotationElementHighlighting()222 	public void annotationElementHighlighting() throws Exception {
223 		setUpSemanticHighlighting(SemanticHighlightings.ANNOTATION_ELEMENT_REFERENCE);
224 		Position[] actual= getSemanticHighlightingPositions();
225 		Position[] expected= new Position[] {
226 				createPosition(38, 19, 5),
227 		};
228 		assertEqualPositions(expected, actual);
229 	}
230 
231 	@Test
typeParameterHighlighting()232 	public void typeParameterHighlighting() throws Exception {
233 		setUpSemanticHighlighting(SemanticHighlightings.TYPE_VARIABLE);
234 		Position[] actual= getSemanticHighlightingPositions();
235 		Position[] expected= new Position[] {
236 				createPosition(39, 15, 1),
237 				createPosition(40, 2, 1),
238 		};
239 		assertEqualPositions(expected, actual);
240 	}
241 
242 	@Test
typeArgumentHighlighting()243 	public void typeArgumentHighlighting() throws Exception {
244 		setUpSemanticHighlighting(SemanticHighlightings.TYPE_ARGUMENT);
245 		Position[] actual= getSemanticHighlightingPositions();
246 		Position[] expected= new Position[] {
247 				createPosition(41, 8, 6),
248 		};
249 		assertEqualPositions(expected, actual);
250 	}
251 
252 	@Test
interfaceHighlighting()253 	public void interfaceHighlighting() throws Exception {
254 		setUpSemanticHighlighting(SemanticHighlightings.INTERFACE);
255 		Position[] actual= getSemanticHighlightingPositions();
256 		Position[] expected= new Position[] {
257 				createPosition(41, 3, 4),
258 				createPosition(42, 3, 16), // annotations are also interfaces
259 		};
260 		assertEqualPositions(expected, actual);
261 	}
262 
263 	@Test
enumHighlighting()264 	public void enumHighlighting() throws Exception {
265 		setUpSemanticHighlighting(SemanticHighlightings.ENUM);
266 		Position[] actual= getSemanticHighlightingPositions();
267 		Position[] expected= new Position[] {
268 				createPosition(43, 3, 11),
269 		};
270 		assertEqualPositions(expected, actual);
271 	}
272 
273 	@Test
annotationHighlighting()274 	public void annotationHighlighting() throws Exception {
275 		setUpSemanticHighlighting(SemanticHighlightings.ANNOTATION);
276 		Position[] actual= getSemanticHighlightingPositions();
277 		Position[] expected= new Position[] {
278 				createPosition(38, 2, 16),
279 				createPosition(42, 3, 16),
280 		};
281 		assertEqualPositions(expected, actual);
282 	}
283 
284 	@Test
abstractClassHighlighting()285 	public void abstractClassHighlighting() throws Exception {
286 		setUpSemanticHighlighting(SemanticHighlightings.ABSTRACT_CLASS);
287 		Position[] actual= getSemanticHighlightingPositions();
288 		Position[] expected= new Position[] {
289 				createPosition( 2, 15,  6),
290 				createPosition(31,  2,  6),
291 				createPosition(31, 17,  6),
292 				createPosition(32, 19,  6),
293 				createPosition(33, 25,  6),
294 				createPosition(34, 24,  6),
295 				createPosition(35, 30,  6),
296 				createPosition(39, 25,  6),
297 				createPosition(47, 23, 14),
298 				createPosition(47, 46,  6),
299 		};
300 		assertEqualPositions(expected, actual);
301 	}
302 
303 	@Test
classHighlighting()304 	public void classHighlighting() throws Exception {
305 		setUpSemanticHighlighting(SemanticHighlightings.CLASS);
306 		Position[] actual= getSemanticHighlightingPositions();
307 		Position[] expected= new Position[] {
308 				createPosition( 2, 15,  6),
309 				createPosition(14, 16,  9),
310 				createPosition(24,  1, 13),
311 				createPosition(27, 10, 10),
312 				createPosition(30,  7, 10),
313 				createPosition(30, 26, 13),
314 				createPosition(31,  2,  6),
315 				createPosition(31, 17,  6),
316 				createPosition(32, 19,  6),
317 				createPosition(33, 25,  6),
318 				createPosition(34, 24,  6),
319 				createPosition(35, 30,  6),
320 				createPosition(39,  7,  7),
321 				createPosition(39, 25,  6),
322 				createPosition(41,  8,  6),
323 				createPosition(47, 23, 14),
324 				createPosition(47, 46,  6),
325 		};
326 		assertEqualPositions(expected, actual);
327 	}
328 }
329