From ffb5848c3c95cb45dbffc2446b425902c213628f Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Sat, 22 Jun 2019 15:20:46 +0300 Subject: [PATCH] Moved LinkResolver to independent entity --- _CHANGES.md | 5 ++- .../java/io/noties/markwon/LinkResolver.java | 14 ++++++ .../io/noties/markwon/LinkResolverDef.java | 6 +-- .../noties/markwon/MarkwonConfiguration.java | 9 ++-- .../noties/markwon/core/spans/LinkSpan.java | 12 ++--- .../markwon/image/ImageSizeResolver.java | 3 +- .../image/ImageSizeResolverDefTest.java | 44 ++++++++++++++++++- markwon-linkify/README.md | 2 +- 8 files changed, 75 insertions(+), 20 deletions(-) create mode 100644 markwon-core/src/main/java/io/noties/markwon/LinkResolver.java diff --git a/_CHANGES.md b/_CHANGES.md index 5d98cd37..3cd3b21a 100644 --- a/_CHANGES.md +++ b/_CHANGES.md @@ -10,6 +10,7 @@ * removed MarkwonPlugin#configureHtmlRenderer -> now part of HtmlPlugin * TagHandler now has `supportedTags()` method * html is moved completely to html-plugin -* OnTextAddedListener +* OnTextAddedListener (CorePlugin) * ImageSizeResolver signature change (accept AsyncDrawable) -* JLatexMathPlugin builder has vertical & horizontal padding \ No newline at end of file +* JLatexMathPlugin builder has vertical & horizontal padding +* LinkResolver is now an independent entity (previously part of LinkSpan) \ No newline at end of file diff --git a/markwon-core/src/main/java/io/noties/markwon/LinkResolver.java b/markwon-core/src/main/java/io/noties/markwon/LinkResolver.java new file mode 100644 index 00000000..1a6c9010 --- /dev/null +++ b/markwon-core/src/main/java/io/noties/markwon/LinkResolver.java @@ -0,0 +1,14 @@ +package io.noties.markwon; + +import android.view.View; + +import androidx.annotation.NonNull; + +/** + * @see LinkResolverDef + * @see MarkwonConfiguration.Builder#linkResolver(LinkResolver) + * @since 4.0.0-SNAPSHOT + */ +public interface LinkResolver { + void resolve(@NonNull View view, @NonNull String link); +} diff --git a/markwon-core/src/main/java/io/noties/markwon/LinkResolverDef.java b/markwon-core/src/main/java/io/noties/markwon/LinkResolverDef.java index 0b9e30fb..999dcee5 100644 --- a/markwon-core/src/main/java/io/noties/markwon/LinkResolverDef.java +++ b/markwon-core/src/main/java/io/noties/markwon/LinkResolverDef.java @@ -10,11 +10,9 @@ import android.view.View; import androidx.annotation.NonNull; -import io.noties.markwon.core.spans.LinkSpan; - -public class LinkResolverDef implements LinkSpan.Resolver { +public class LinkResolverDef implements LinkResolver { @Override - public void resolve(View view, @NonNull String link) { + public void resolve(@NonNull View view, @NonNull String link) { final Uri uri = Uri.parse(link); final Context context = view.getContext(); final Intent intent = new Intent(Intent.ACTION_VIEW, uri); diff --git a/markwon-core/src/main/java/io/noties/markwon/MarkwonConfiguration.java b/markwon-core/src/main/java/io/noties/markwon/MarkwonConfiguration.java index c7f0f04d..b911e945 100644 --- a/markwon-core/src/main/java/io/noties/markwon/MarkwonConfiguration.java +++ b/markwon-core/src/main/java/io/noties/markwon/MarkwonConfiguration.java @@ -3,7 +3,6 @@ package io.noties.markwon; import androidx.annotation.NonNull; import io.noties.markwon.core.MarkwonTheme; -import io.noties.markwon.core.spans.LinkSpan; import io.noties.markwon.image.AsyncDrawableLoader; import io.noties.markwon.image.ImageSizeResolver; import io.noties.markwon.image.ImageSizeResolverDef; @@ -26,7 +25,7 @@ public class MarkwonConfiguration { private final MarkwonTheme theme; private final AsyncDrawableLoader asyncDrawableLoader; private final SyntaxHighlight syntaxHighlight; - private final LinkSpan.Resolver linkResolver; + private final LinkResolver linkResolver; private final UrlProcessor urlProcessor; private final ImageSizeResolver imageSizeResolver; @@ -59,7 +58,7 @@ public class MarkwonConfiguration { } @NonNull - public LinkSpan.Resolver linkResolver() { + public LinkResolver linkResolver() { return linkResolver; } @@ -87,7 +86,7 @@ public class MarkwonConfiguration { private MarkwonTheme theme; private AsyncDrawableLoader asyncDrawableLoader; private SyntaxHighlight syntaxHighlight; - private LinkSpan.Resolver linkResolver; + private LinkResolver linkResolver; private UrlProcessor urlProcessor; private ImageSizeResolver imageSizeResolver; private MarkwonSpansFactory spansFactory; @@ -111,7 +110,7 @@ public class MarkwonConfiguration { } @NonNull - public Builder linkResolver(@NonNull LinkSpan.Resolver linkResolver) { + public Builder linkResolver(@NonNull LinkResolver linkResolver) { this.linkResolver = linkResolver; return this; } diff --git a/markwon-core/src/main/java/io/noties/markwon/core/spans/LinkSpan.java b/markwon-core/src/main/java/io/noties/markwon/core/spans/LinkSpan.java index 4bcbfd11..f8483423 100644 --- a/markwon-core/src/main/java/io/noties/markwon/core/spans/LinkSpan.java +++ b/markwon-core/src/main/java/io/noties/markwon/core/spans/LinkSpan.java @@ -6,19 +6,19 @@ import android.view.View; import androidx.annotation.NonNull; +import io.noties.markwon.LinkResolver; import io.noties.markwon.core.MarkwonTheme; public class LinkSpan extends URLSpan { - public interface Resolver { - void resolve(View view, @NonNull String link); - } - private final MarkwonTheme theme; private final String link; - private final Resolver resolver; + private final LinkResolver resolver; - public LinkSpan(@NonNull MarkwonTheme theme, @NonNull String link, @NonNull Resolver resolver) { + public LinkSpan( + @NonNull MarkwonTheme theme, + @NonNull String link, + @NonNull LinkResolver resolver) { super(link); this.theme = theme; this.link = link; diff --git a/markwon-core/src/main/java/io/noties/markwon/image/ImageSizeResolver.java b/markwon-core/src/main/java/io/noties/markwon/image/ImageSizeResolver.java index 456afae0..ecd7bbcb 100644 --- a/markwon-core/src/main/java/io/noties/markwon/image/ImageSizeResolver.java +++ b/markwon-core/src/main/java/io/noties/markwon/image/ImageSizeResolver.java @@ -5,9 +5,10 @@ import android.graphics.Rect; import androidx.annotation.NonNull; /** + * @see ImageSizeResolverDef + * @see io.noties.markwon.MarkwonConfiguration.Builder#imageSizeResolver(ImageSizeResolver) * @since 1.0.1 */ -@SuppressWarnings({"WeakerAccess", "unused"}) public abstract class ImageSizeResolver { /** diff --git a/markwon-core/src/test/java/io/noties/markwon/image/ImageSizeResolverDefTest.java b/markwon-core/src/test/java/io/noties/markwon/image/ImageSizeResolverDefTest.java index 33280b03..5030986c 100644 --- a/markwon-core/src/test/java/io/noties/markwon/image/ImageSizeResolverDefTest.java +++ b/markwon-core/src/test/java/io/noties/markwon/image/ImageSizeResolverDefTest.java @@ -1,16 +1,22 @@ package io.noties.markwon.image; import android.graphics.Rect; +import android.graphics.drawable.Drawable; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -import static org.junit.Assert.assertEquals; import static io.noties.markwon.image.ImageSizeResolverDef.UNIT_EM; import static io.noties.markwon.image.ImageSizeResolverDef.UNIT_PERCENT; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) @Config(manifest = Config.NONE) @@ -23,6 +29,42 @@ public class ImageSizeResolverDefTest { def = new ImageSizeResolverDef(); } + @Test + public void correct_redirect() { + // @since 4.0.0-SNAPSHOT the main method is changed to accept AsyncDrawable + + final ImageSizeResolverDef def = mock(ImageSizeResolverDef.class, Mockito.CALLS_REAL_METHODS); + final AsyncDrawable drawable = mock(AsyncDrawable.class); + + final ImageSize imageSize = mock(ImageSize.class); + final Drawable result = mock(Drawable.class); + final Rect rect = mock(Rect.class); + when(result.getBounds()).thenReturn(rect); + + when(drawable.getImageSize()).thenReturn(imageSize); + when(drawable.getResult()).thenReturn(result); + when(drawable.getLastKnownCanvasWidth()).thenReturn(111); + when(drawable.getLastKnowTextSize()).thenReturn(24.0F); + + def.resolveImageSize(drawable); + + final ArgumentCaptor imageSizeArgumentCaptor = ArgumentCaptor.forClass(ImageSize.class); + final ArgumentCaptor rectArgumentCaptor = ArgumentCaptor.forClass(Rect.class); + final ArgumentCaptor integerArgumentCaptor = ArgumentCaptor.forClass(Integer.class); + final ArgumentCaptor floatArgumentCaptor = ArgumentCaptor.forClass(Float.class); + + verify(def).resolveImageSize( + imageSizeArgumentCaptor.capture(), + rectArgumentCaptor.capture(), + integerArgumentCaptor.capture(), + floatArgumentCaptor.capture()); + + assertEquals(imageSize, imageSizeArgumentCaptor.getValue()); + assertEquals(rect, rectArgumentCaptor.getValue()); + assertEquals((Integer) 111, integerArgumentCaptor.getValue()); + assertEquals((Float) 24.0F, floatArgumentCaptor.getValue()); + } + @Test public void no_image_size() { // no image size returns image original bounds diff --git a/markwon-linkify/README.md b/markwon-linkify/README.md index ccc0c08e..e57f87ca 100644 --- a/markwon-linkify/README.md +++ b/markwon-linkify/README.md @@ -6,4 +6,4 @@ existing links and keep only the ones it creates. Please note that usage of this plugin introduces significant performance drop due to not optimal implementation of underlying `android.text.util.Linkify`. If you have any ideas of how -to improve this - PR are welcome! \ No newline at end of file +to improve this - PRs are welcome! \ No newline at end of file