-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start animatable resources after setting the resource in ImageViewTarget
After 90b3b9f we have a case where a GifDrawable may load a frame synchronously when it’s started. If the GifDrawable was started before it was attached to a View (and gets a non-null Callback) and the GifDrawable had a frame pending, then the GifDrawable would stop itself in the start() call, causing the drawable not to animate. We can avoid this scenario by changing the order in which we start animated drawables and set them on views so that we only start the drawable after its set on the View. Fixes #2541
- Loading branch information
Showing
8 changed files
with
208 additions
and
7 deletions.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
...umentation/src/androidTest/java/com/bumptech/glide/load/resource/gif/GifDrawableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package com.bumptech.glide.load.resource.gif; | ||
|
||
import static android.support.test.InstrumentationRegistry.getTargetContext; | ||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.fail; | ||
import static org.junit.Assume.assumeTrue; | ||
|
||
import android.content.Context; | ||
import android.graphics.drawable.Drawable; | ||
import android.os.Build; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.support.annotation.Nullable; | ||
import android.support.test.runner.AndroidJUnit4; | ||
import android.view.View; | ||
import android.view.WindowManager; | ||
import android.view.WindowManager.LayoutParams; | ||
import android.widget.ImageView; | ||
import com.bumptech.glide.load.DataSource; | ||
import com.bumptech.glide.load.engine.GlideException; | ||
import com.bumptech.glide.load.resource.gif.GifDrawable.GifState; | ||
import com.bumptech.glide.load.resource.gif.GifFrameLoader.OnEveryFrameListener; | ||
import com.bumptech.glide.request.RequestListener; | ||
import com.bumptech.glide.request.target.Target; | ||
import com.bumptech.glide.test.GlideApp; | ||
import com.bumptech.glide.test.ResourceIds; | ||
import com.bumptech.glide.test.TearDownGlide; | ||
import com.bumptech.glide.util.Preconditions; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TestName; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class GifDrawableTest { | ||
@Rule public TestName testName = new TestName(); | ||
@Rule public TearDownGlide tearDownGlide = new TearDownGlide(); | ||
private Context context; | ||
private Handler mainHandler; | ||
|
||
@Before | ||
public void setUp() { | ||
// Required for us to add a View to a Window. | ||
assumeTrue(Build.VERSION.SDK_INT < Build.VERSION_CODES.M); | ||
|
||
context = getTargetContext(); | ||
mainHandler = new Handler(Looper.getMainLooper()); | ||
} | ||
|
||
@Test | ||
public void loadGif_intoImageView_afterStop_restartsGif() | ||
throws ExecutionException, InterruptedException { | ||
// Mimic the state the Drawable can get into if it was loaded into a View previously and stopped | ||
// so that it ended up with a pending frame that finished after the stop call. | ||
final GifDrawable gifDrawable = GlideApp.with(context) | ||
.asGif() | ||
.load(ResourceIds.raw.dl_world_anim) | ||
.submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) | ||
.get(); | ||
final CountDownLatch waitForGifFrame = new CountDownLatch(1); | ||
// Starting/Stopping loads in GIFs must happen on the main thread. | ||
mainHandler.post( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
// Make sure a frame is loaded while the drawable is stopped. | ||
GifState gifState = | ||
(GifState) Preconditions.checkNotNull(gifDrawable.getConstantState()); | ||
gifState.frameLoader.setOnEveryFrameReadyListener(new OnEveryFrameListener() { | ||
@Override | ||
public void onFrameReady() { | ||
waitForGifFrame.countDown(); | ||
} | ||
}); | ||
gifDrawable.start(); | ||
gifDrawable.stop(); | ||
} | ||
}); | ||
waitOrThrow(waitForGifFrame); | ||
|
||
// Load the Drawable with the pending frame into a new View and make sure it ends up in the | ||
// running state. | ||
final ImageView imageView = new ImageView(context); | ||
final WaitForLoad<Drawable> waitForLoad = new WaitForLoad<>(); | ||
// Starting loads into Views must happen on the main thread. | ||
mainHandler | ||
.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
addViewToWindow(imageView); | ||
GlideApp.with(context) | ||
.load(gifDrawable) | ||
.listener(waitForLoad) | ||
.override(Target.SIZE_ORIGINAL) | ||
.into(imageView); | ||
} | ||
}); | ||
waitForLoad.await(); | ||
|
||
GifDrawable drawableFromView = (GifDrawable) imageView.getDrawable(); | ||
assertThat(drawableFromView.isRunning()).isTrue(); | ||
} | ||
|
||
// LayoutParams.TYPE_SYSTEM_ALERT. | ||
@SuppressWarnings("deprecation") | ||
private void addViewToWindow(View view) { | ||
final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); | ||
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; | ||
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; | ||
layoutParams.type = LayoutParams.TYPE_SYSTEM_ALERT; | ||
final WindowManager windowManager = | ||
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | ||
windowManager.addView(view, layoutParams); | ||
} | ||
|
||
private static void waitOrThrow(CountDownLatch latch) { | ||
try { | ||
if (!latch.await(5, TimeUnit.SECONDS)) { | ||
fail("Failed to reach expected condition in the alloted time."); | ||
} | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private final class WaitForLoad<T> implements RequestListener<T> { | ||
private final CountDownLatch countDownLatch = new CountDownLatch(1); | ||
|
||
void await() { | ||
waitOrThrow(countDownLatch); | ||
} | ||
|
||
@Override | ||
public boolean onLoadFailed(@Nullable GlideException e, Object model, | ||
Target<T> target, | ||
boolean isFirstResource) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
@Override | ||
public boolean onResourceReady(T resource, Object model, Target<T> target, | ||
DataSource dataSource, | ||
boolean isFirstResource) { | ||
mainHandler.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.bumptech.glide.instrumentation"> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> | ||
<application /> | ||
</manifest> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters