Markwon.hasPlugin method
This commit is contained in:
parent
7a598829a9
commit
705dec0571
@ -88,6 +88,19 @@ public abstract class Markwon {
|
||||
|
||||
public abstract void setParsedMarkdown(@NonNull TextView textView, @NonNull Spanned markdown);
|
||||
|
||||
/**
|
||||
* Requests information if certain plugin has been registered. Please note that this
|
||||
* method will check for super classes also, so if supplied with {@code markwon.hasPlugin(MarkwonPlugin.class)}
|
||||
* this method (if has at least one plugin) will return true. If for example a custom
|
||||
* (subclassed) version of a {@link CorePlugin} has been registered and given name
|
||||
* {@code CorePlugin2}, then both {@code markwon.hasPlugin(CorePlugin2.class)} and
|
||||
* {@code markwon.hasPlugin(CorePlugin.class)} will return true.
|
||||
*
|
||||
* @param plugin type to query
|
||||
* @return true if a plugin is used when configuring this {@link Markwon} instance
|
||||
*/
|
||||
public abstract boolean hasPlugin(@NonNull Class<? extends MarkwonPlugin> plugin);
|
||||
|
||||
/**
|
||||
* Builder for {@link Markwon}.
|
||||
* <p>
|
||||
|
@ -95,4 +95,16 @@ class MarkwonImpl extends Markwon {
|
||||
plugin.afterSetText(textView);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPlugin(@NonNull Class<? extends MarkwonPlugin> type) {
|
||||
boolean result = false;
|
||||
for (MarkwonPlugin plugin : plugins) {
|
||||
if (type.isAssignableFrom(plugin.getClass())) {
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -16,9 +16,11 @@ import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
@ -229,4 +231,30 @@ public class MarkwonImplTest {
|
||||
|
||||
verify(plugin, times(1)).afterSetText(eq(textView));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void has_plugin() {
|
||||
|
||||
final class First extends AbstractMarkwonPlugin {
|
||||
}
|
||||
|
||||
final class Second extends AbstractMarkwonPlugin {
|
||||
}
|
||||
|
||||
final List<MarkwonPlugin> plugins = Collections.singletonList((MarkwonPlugin) new First());
|
||||
|
||||
final MarkwonImpl impl = new MarkwonImpl(
|
||||
TextView.BufferType.SPANNABLE,
|
||||
mock(Parser.class),
|
||||
mock(MarkwonVisitor.class),
|
||||
plugins);
|
||||
|
||||
assertTrue("First", impl.hasPlugin(First.class));
|
||||
assertFalse("Second", impl.hasPlugin(Second.class));
|
||||
|
||||
// can use super types. So if we ask if CorePlugin is registered,
|
||||
// but it was subclassed, we would still have true returned from this method
|
||||
assertTrue("AbstractMarkwonPlugin", impl.hasPlugin(AbstractMarkwonPlugin.class));
|
||||
assertTrue("MarkwonPlugin", impl.hasPlugin(MarkwonPlugin.class));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user