Fix core tests

This commit is contained in:
Dimitry Ivanov 2019-11-07 17:36:49 +03:00
parent 2488c1047b
commit 0fabf7daff
2 changed files with 49 additions and 58 deletions

View File

@ -49,6 +49,7 @@ public class MarkwonImplTest {
null, null,
mock(Parser.class), mock(Parser.class),
mock(MarkwonVisitorFactory.class), mock(MarkwonVisitorFactory.class),
mock(MarkwonConfiguration.class),
Collections.singletonList(plugin)); Collections.singletonList(plugin));
impl.parse("whatever"); impl.parse("whatever");
@ -72,6 +73,7 @@ public class MarkwonImplTest {
null, null,
parser, parser,
mock(MarkwonVisitorFactory.class), mock(MarkwonVisitorFactory.class),
mock(MarkwonConfiguration.class),
Arrays.asList(first, second)); Arrays.asList(first, second));
impl.parse("zero"); impl.parse("zero");
@ -99,6 +101,7 @@ public class MarkwonImplTest {
null, null,
mock(Parser.class), mock(Parser.class),
visitorFactory, visitorFactory,
mock(MarkwonConfiguration.class),
Collections.singletonList(plugin)); Collections.singletonList(plugin));
when(visitorFactory.create()).thenReturn(visitor); when(visitorFactory.create()).thenReturn(visitor);
@ -145,6 +148,7 @@ public class MarkwonImplTest {
null, null,
mock(Parser.class), mock(Parser.class),
visitorFactory, visitorFactory,
mock(MarkwonConfiguration.class),
Collections.<MarkwonPlugin>emptyList()); Collections.<MarkwonPlugin>emptyList());
impl.render(mock(Node.class)); impl.render(mock(Node.class));
@ -180,6 +184,7 @@ public class MarkwonImplTest {
null, null,
mock(Parser.class), mock(Parser.class),
visitorFactory, visitorFactory,
mock(MarkwonConfiguration.class),
Collections.singletonList(plugin)); Collections.singletonList(plugin));
final AtomicBoolean flag = new AtomicBoolean(false); final AtomicBoolean flag = new AtomicBoolean(false);
@ -218,6 +223,7 @@ public class MarkwonImplTest {
null, null,
mock(Parser.class), mock(Parser.class),
mock(MarkwonVisitorFactory.class, RETURNS_MOCKS), mock(MarkwonVisitorFactory.class, RETURNS_MOCKS),
mock(MarkwonConfiguration.class),
Collections.singletonList(plugin)); Collections.singletonList(plugin));
final TextView textView = mock(TextView.class); final TextView textView = mock(TextView.class);
@ -265,6 +271,7 @@ public class MarkwonImplTest {
null, null,
mock(Parser.class), mock(Parser.class),
mock(MarkwonVisitorFactory.class), mock(MarkwonVisitorFactory.class),
mock(MarkwonConfiguration.class),
plugins); plugins);
assertTrue("First", impl.hasPlugin(First.class)); assertTrue("First", impl.hasPlugin(First.class));
@ -287,6 +294,7 @@ public class MarkwonImplTest {
textSetter, textSetter,
mock(Parser.class), mock(Parser.class),
mock(MarkwonVisitorFactory.class), mock(MarkwonVisitorFactory.class),
mock(MarkwonConfiguration.class),
Collections.singletonList(plugin)); Collections.singletonList(plugin));
final TextView textView = mock(TextView.class); final TextView textView = mock(TextView.class);
@ -330,6 +338,7 @@ public class MarkwonImplTest {
null, null,
mock(Parser.class), mock(Parser.class),
mock(MarkwonVisitorFactory.class), mock(MarkwonVisitorFactory.class),
mock(MarkwonConfiguration.class),
plugins); plugins);
// should be returned // should be returned
@ -360,6 +369,7 @@ public class MarkwonImplTest {
null, null,
mock(Parser.class), mock(Parser.class),
mock(MarkwonVisitorFactory.class), mock(MarkwonVisitorFactory.class),
mock(MarkwonConfiguration.class),
plugins); plugins);
final List<? extends MarkwonPlugin> list = impl.getPlugins(); final List<? extends MarkwonPlugin> list = impl.getPlugins();

View File

@ -64,13 +64,13 @@ public class EditorActivity extends Activity {
private void simple_process() { private void simple_process() {
// Process highlight in-place (right after text has changed) // Process highlight in-place (right after text has changed)
// obtain Markwon instance // obtain Markwon instance
final Markwon markwon = Markwon.create(this); final Markwon markwon = Markwon.create(this);
// create editor // create editor
final MarkwonEditor editor = MarkwonEditor.create(markwon); final MarkwonEditor editor = MarkwonEditor.create(markwon);
// set edit listener // set edit listener
editText.addTextChangedListener(MarkwonEditorTextWatcher.withProcess(editor)); editText.addTextChangedListener(MarkwonEditorTextWatcher.withProcess(editor));
} }
@ -80,25 +80,6 @@ public class EditorActivity extends Activity {
final Markwon markwon = Markwon.create(this); final Markwon markwon = Markwon.create(this);
final MarkwonEditor editor = MarkwonEditor.create(markwon); final MarkwonEditor editor = MarkwonEditor.create(markwon);
editor.process(editText.getText());
// please note that MarkwonEditor operates on caller thread,
// fi you wish to execute this operation in background - this method
// must be called from background thread
editor.preRender(editText.getText(), new MarkwonEditor.PreRenderResultListener() {
@Override
public void onPreRenderResult(@NonNull MarkwonEditor.PreRenderResult result) {
// it's wise to check if rendered result is for the same input,
// for example by matching raw input
if (editText.getText().toString().equals(result.resultEditable().toString())) {
// if you are in background thread do not forget
// to execute dispatch in main thread
result.dispatchTo(editText.getText());
}
}
});
editText.addTextChangedListener(MarkwonEditorTextWatcher.withPreRender( editText.addTextChangedListener(MarkwonEditorTextWatcher.withPreRender(
editor, editor,
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
@ -108,9 +89,9 @@ editor.preRender(editText.getText(), new MarkwonEditor.PreRenderResultListener()
private void custom_punctuation_span() { private void custom_punctuation_span() {
// Use own punctuation span // Use own punctuation span
final MarkwonEditor editor = MarkwonEditor.builder(Markwon.create(this)) final MarkwonEditor editor = MarkwonEditor.builder(Markwon.create(this))
.withPunctuationSpan(CustomPunctuationSpan.class, CustomPunctuationSpan::new) .withPunctuationSpan(CustomPunctuationSpan.class, CustomPunctuationSpan::new)
.build(); .build();
editText.addTextChangedListener(MarkwonEditorTextWatcher.withProcess(editor)); editText.addTextChangedListener(MarkwonEditorTextWatcher.withProcess(editor));
} }
@ -118,35 +99,35 @@ final MarkwonEditor editor = MarkwonEditor.builder(Markwon.create(this))
private void additional_edit_span() { private void additional_edit_span() {
// An additional span is used to highlight strong-emphasis // An additional span is used to highlight strong-emphasis
final MarkwonEditor editor = MarkwonEditor.builder(Markwon.create(this)) final MarkwonEditor editor = MarkwonEditor.builder(Markwon.create(this))
// This is required for edit-span cache // This is required for edit-span cache
// We could use Markwon `StrongEmphasisSpan` here, but I use a different // We could use Markwon `StrongEmphasisSpan` here, but I use a different
// one to indicate that those are completely unrelated spans and must be // one to indicate that those are completely unrelated spans and must be
// treated differently. // treated differently.
.includeEditSpan(Bold.class, Bold::new) .includeEditSpan(Bold.class, Bold::new)
.withEditSpanHandler(new MarkwonEditor.EditSpanHandler() { .withEditSpanHandler(new MarkwonEditor.EditSpanHandler() {
@Override @Override
public void handle( public void handle(
@NonNull MarkwonEditor.EditSpanStore store, @NonNull MarkwonEditor.EditSpanStore store,
@NonNull Editable editable, @NonNull Editable editable,
@NonNull String input, @NonNull String input,
@NonNull Object span, @NonNull Object span,
int spanStart, int spanStart,
int spanTextLength) { int spanTextLength) {
if (span instanceof StrongEmphasisSpan) { if (span instanceof StrongEmphasisSpan) {
editable.setSpan( editable.setSpan(
// `includeEditSpan(Bold.class, Bold::new)` ensured that we have // `includeEditSpan(Bold.class, Bold::new)` ensured that we have
// a span here to use (either reuse existing or create a new one) // a span here to use (either reuse existing or create a new one)
store.get(Bold.class), store.get(Bold.class),
spanStart, spanStart,
// we know that strong emphasis is delimited with 2 characters on both sides // we know that strong emphasis is delimited with 2 characters on both sides
spanStart + spanTextLength + 4, spanStart + spanTextLength + 4,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
); );
} }
} }
}) })
.build(); .build();
editText.addTextChangedListener(MarkwonEditorTextWatcher.withProcess(editor)); editText.addTextChangedListener(MarkwonEditorTextWatcher.withProcess(editor));
} }
@ -335,11 +316,11 @@ final MarkwonEditor editor = MarkwonEditor.builder(Markwon.create(this))
} }
} }
private static class CustomPunctuationSpan extends ForegroundColorSpan { private static class CustomPunctuationSpan extends ForegroundColorSpan {
CustomPunctuationSpan() { CustomPunctuationSpan() {
super(0xFFFF0000); // RED super(0xFFFF0000); // RED
}
} }
}
private static class Bold extends MetricAffectingSpan { private static class Bold extends MetricAffectingSpan {
public Bold() { public Bold() {