Fix typo in AsyncDrawable waitingForDimensions

This commit is contained in:
Dimitry Ivanov 2018-12-03 17:19:31 +03:00
parent 577c3fc782
commit 582b9209d9
2 changed files with 111 additions and 4 deletions

View File

@ -24,7 +24,7 @@ public class AsyncDrawable extends Drawable {
private float textSize; private float textSize;
// @since 2.0.1 for use-cases when image is loaded faster than span is drawn and knows canvas width // @since 2.0.1 for use-cases when image is loaded faster than span is drawn and knows canvas width
private boolean waitingForDemensions; private boolean waitingForDimensions;
/** /**
* @since 1.0.1 * @since 1.0.1
@ -98,11 +98,11 @@ public class AsyncDrawable extends Drawable {
if (canvasWidth == 0) { if (canvasWidth == 0) {
// we still have no bounds - wait for them // we still have no bounds - wait for them
waitingForDemensions = true; waitingForDimensions = true;
return; return;
} }
waitingForDemensions = false; waitingForDimensions = false;
final Rect bounds = resolveBounds(); final Rect bounds = resolveBounds();
result.setBounds(bounds); result.setBounds(bounds);
@ -119,7 +119,7 @@ public class AsyncDrawable extends Drawable {
this.canvasWidth = width; this.canvasWidth = width;
this.textSize = textSize; this.textSize = textSize;
if (waitingForDemensions) { if (waitingForDimensions) {
initBounds(); initBounds();
} }
} }

View File

@ -0,0 +1,107 @@
package ru.noties.markwon.spans;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import ru.noties.markwon.renderer.ImageSize;
import ru.noties.markwon.renderer.ImageSizeResolver;
import ru.noties.markwon.renderer.ImageSizeResolverDef;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class AsyncDrawableTest {
private ImageSizeResolver imageSizeResolver;
@Before
public void before() {
imageSizeResolver = new ImageSizeResolverDef();
}
@Test
public void no_dimensions_await() {
// when drawable have no known dimensions yet, it will await for them
final AsyncDrawable drawable = new AsyncDrawable("",
mock(AsyncDrawable.Loader.class),
imageSizeResolver,
new ImageSize(new ImageSize.Dimension(100.F, "%"), null));
final Drawable result = new AbstractDrawable();
result.setBounds(0, 0, 0, 0);
assertFalse(drawable.hasResult());
drawable.setResult(result);
assertTrue(drawable.hasResult());
assertTrue(result.getBounds().isEmpty());
drawable.initWithKnownDimensions(100, 1);
assertEquals(
new Rect(0, 0, 100, 0),
result.getBounds()
);
}
@Test
public void previous_result_detached() {
// when result is present it will be detached (setCallback(null))
final AsyncDrawable drawable = new AsyncDrawable("",
mock(AsyncDrawable.Loader.class),
imageSizeResolver,
null);
drawable.setCallback2(mock(Drawable.Callback.class));
drawable.initWithKnownDimensions(100, 1);
final Drawable result1 = new AbstractDrawable();
final Drawable result2 = new AbstractDrawable();
drawable.setResult(result1);
assertNotNull(result1.getCallback());
drawable.setResult(result2);
assertNull(result1.getCallback());
assertNotNull(result2.getCallback());
}
private static class AbstractDrawable extends Drawable {
@Override
public void draw(@NonNull Canvas canvas) {
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return 0;
}
}
}