Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.d4rk.androidtutorials.java.utils;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;

import com.amrdeveloper.codeview.CodeView;

import org.junit.Test;
import org.mockito.InOrder;

import java.util.regex.Pattern;

/**
* Tests for {@link CodeHighlighter}.
*/
public class CodeHighlighterTest {

@Test
public void applyJavaTheme_resetsPatternsAndReHighlights() {
CodeView codeView = mock(CodeView.class);

CodeHighlighter.applyJavaTheme(codeView);

InOrder order = inOrder(codeView);
order.verify(codeView).resetSyntaxPatternList();
order.verify(codeView).resetHighlighter();
order.verify(codeView, times(5)).addSyntaxPattern(any(Pattern.class), anyInt());
order.verify(codeView).reHighlightSyntax();
}

@Test
public void applyXmlTheme_resetsPatternsAndReHighlights() {
CodeView codeView = mock(CodeView.class);

CodeHighlighter.applyXmlTheme(codeView);

InOrder order = inOrder(codeView);
order.verify(codeView).resetSyntaxPatternList();
order.verify(codeView).resetHighlighter();
order.verify(codeView, times(4)).addSyntaxPattern(any(Pattern.class), anyInt());
order.verify(codeView).reHighlightSyntax();
Comment on lines +21 to +44

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Avoid Android framework calls in host-unit tests

Both new tests execute CodeHighlighter.apply*Theme, which internally calls android.graphics.Color.parseColor. Because these tests live under app/src/test/java they run on the host JVM without an Android runtime or Robolectric, so parseColor will throw RuntimeException("Stub!") and the suite will fail even when the SDK is installed. Consider moving these checks to androidTest or adding Robolectric/mocking the color parsing so the tests can run in the existing unit-test environment.

Useful? React with 👍 / 👎.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,24 @@ private void verifyDefaults(CodeView view, Typeface typeface) {
}

@Test
public void applyDefaults_configuresAllViews() {
public void applyDefaults_configuresNonNullViews() {
Typeface typeface = mock(Typeface.class);
CodeView first = mock(CodeView.class);
CodeView second = mock(CodeView.class);

CodeViewUtils.applyDefaults(typeface, first, second, null);
CodeViewUtils.applyDefaults(typeface, first, second);

verifyDefaults(first, typeface);
verifyDefaults(second, typeface);
}

@Test
public void applyDefaults_ignoresNullViews() {
Typeface typeface = mock(Typeface.class);
CodeView first = mock(CodeView.class);
CodeView second = mock(CodeView.class);

CodeViewUtils.applyDefaults(typeface, null, first, null, second, null);

verifyDefaults(first, typeface);
verifyDefaults(second, typeface);
Expand Down
Loading