Skip to content
Closed
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,60 @@
package com.d4rk.androidtutorials.java.utils;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import com.amrdeveloper.codeview.CodeView;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.junit.Test;

/**
* Tests for {@link CodeHighlighter} ensuring themes register syntax patterns and refresh highlighting.
*/
public class CodeHighlighterTest {

@Test
public void applyJavaTheme_addsPatternsAndReHighlights() {
CodeView codeView = mock(CodeView.class);
List<Pattern> patterns = new ArrayList<>();
List<Integer> colors = new ArrayList<>();

doAnswer(invocation -> {
patterns.add(invocation.getArgument(0));
colors.add(invocation.getArgument(1));
return null;
}).when(codeView).addSyntaxPattern(any(Pattern.class), anyInt());

CodeHighlighter.applyJavaTheme(codeView);

Choose a reason for hiding this comment

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

[P1] Guard unit tests against Android Color dependency

Running CodeHighlighter.applyJavaTheme(codeView) in a local unit test invokes android.graphics.Color.parseColor, but the project’s test configuration only includes plain JUnit/Mockito and no Robolectric or Android runtime stubs. When ./gradlew test executes, this call will throw RuntimeException: Method parseColor in android.graphics.Color not mocked before any assertions run (same for applyXmlTheme later in the file), causing the entire test task to fail. Consider moving these tests under androidTest, adding Robolectric, or mocking the color parsing so the tests can execute in a JVM environment.

Useful? React with 👍 / 👎.


assertEquals(5, patterns.size());
assertEquals(5, colors.size());
verify(codeView).reHighlightSyntax();
}

@Test
public void applyXmlTheme_addsPatternsAndReHighlights() {
CodeView codeView = mock(CodeView.class);
List<Pattern> patterns = new ArrayList<>();
List<Integer> colors = new ArrayList<>();

doAnswer(invocation -> {
patterns.add(invocation.getArgument(0));
colors.add(invocation.getArgument(1));
return null;
}).when(codeView).addSyntaxPattern(any(Pattern.class), anyInt());

CodeHighlighter.applyXmlTheme(codeView);

assertEquals(4, patterns.size());
assertEquals(4, colors.size());
verify(codeView).reHighlightSyntax();
}
}
Loading