Skip to content

Commit 7eb90f3

Browse files
Add tests for code view utilities and highlighter
1 parent 31e7acb commit 7eb90f3

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.d4rk.androidtutorials.java.utils;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.anyInt;
5+
import static org.mockito.Mockito.inOrder;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.times;
8+
9+
import com.amrdeveloper.codeview.CodeView;
10+
11+
import org.junit.Test;
12+
import org.mockito.InOrder;
13+
14+
import java.util.regex.Pattern;
15+
16+
/**
17+
* Tests for {@link CodeHighlighter}.
18+
*/
19+
public class CodeHighlighterTest {
20+
21+
@Test
22+
public void applyJavaTheme_resetsPatternsAndReHighlights() {
23+
CodeView codeView = mock(CodeView.class);
24+
25+
CodeHighlighter.applyJavaTheme(codeView);
26+
27+
InOrder order = inOrder(codeView);
28+
order.verify(codeView).resetSyntaxPatternList();
29+
order.verify(codeView).resetHighlighter();
30+
order.verify(codeView, times(5)).addSyntaxPattern(any(Pattern.class), anyInt());
31+
order.verify(codeView).reHighlightSyntax();
32+
}
33+
34+
@Test
35+
public void applyXmlTheme_resetsPatternsAndReHighlights() {
36+
CodeView codeView = mock(CodeView.class);
37+
38+
CodeHighlighter.applyXmlTheme(codeView);
39+
40+
InOrder order = inOrder(codeView);
41+
order.verify(codeView).resetSyntaxPatternList();
42+
order.verify(codeView).resetHighlighter();
43+
order.verify(codeView, times(4)).addSyntaxPattern(any(Pattern.class), anyInt());
44+
order.verify(codeView).reHighlightSyntax();
45+
}
46+
}

app/src/test/java/com/d4rk/androidtutorials/java/utils/CodeViewUtilsTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,24 @@ private void verifyDefaults(CodeView view, Typeface typeface) {
2626
}
2727

2828
@Test
29-
public void applyDefaults_configuresAllViews() {
29+
public void applyDefaults_configuresNonNullViews() {
3030
Typeface typeface = mock(Typeface.class);
3131
CodeView first = mock(CodeView.class);
3232
CodeView second = mock(CodeView.class);
3333

34-
CodeViewUtils.applyDefaults(typeface, first, second, null);
34+
CodeViewUtils.applyDefaults(typeface, first, second);
35+
36+
verifyDefaults(first, typeface);
37+
verifyDefaults(second, typeface);
38+
}
39+
40+
@Test
41+
public void applyDefaults_ignoresNullViews() {
42+
Typeface typeface = mock(Typeface.class);
43+
CodeView first = mock(CodeView.class);
44+
CodeView second = mock(CodeView.class);
45+
46+
CodeViewUtils.applyDefaults(typeface, null, first, null, second, null);
3547

3648
verifyDefaults(first, typeface);
3749
verifyDefaults(second, typeface);

0 commit comments

Comments
 (0)