Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support re-initialize #2323

Merged
merged 5 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add LottieInitializeTest
  • Loading branch information
drewhamilton committed Jun 23, 2023
commit 5c293aa834f567e90092a935d3d282bc56f1fb3a
128 changes: 128 additions & 0 deletions lottie/src/test/java/com/airbnb/lottie/LottieInitializeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.airbnb.lottie;

import static org.junit.Assert.assertNotNull;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.test.platform.app.InstrumentationRegistry;

import com.airbnb.lottie.network.LottieFetchResult;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.InputStream;
import java.util.Objects;

public class LottieInitializeTest extends BaseTest {

@Rule
public final TemporaryFolder temporaryFolder1 = new TemporaryFolder();

@Rule
public final TemporaryFolder temporaryFolder2 = new TemporaryFolder();

private final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

@Before
public void setExecutor() {
LottieTask.EXECUTOR = Runnable::run;
}

@Test
public void fetchAfterSecondInitialize() {
initializeLottie(temporaryFolder1);
// Fetching here causes the resource to be cached in temporaryFolder1:
LottieResult<LottieComposition> result1 = LottieCompositionFactory.fromUrlSync(context, "resources://test1.json");
assertNotNull(result1.getValue());

// Manually delete to simulate the end of a test:
temporaryFolder1.delete();

initializeLottie(temporaryFolder2);
// Fetching here fails if L.setCacheProvider doesn't reset both its internal networkFetcher and its internal networkCache, because
// temporaryFolder1 has been deleted:
LottieResult<LottieComposition> result2 = LottieCompositionFactory.fromUrlSync(context, "resources://test1.json");
assertNotNull(result2.getValue());
}

private void initializeLottie(TemporaryFolder temporaryFolder) {
LottieConfig lottieConfig = new LottieConfig.Builder()
.setNetworkCacheDir(temporaryFolder.getRoot())
.setNetworkFetcher(url -> {
if (url.startsWith("resources://")) {
InputStream stream = Objects.requireNonNull(getClass().getClassLoader())
.getResourceAsStream(url.substring("resources://".length()));
if (stream != null) {
return new LottieFetchSuccess(stream);
}
}

return new LottieFetchFailure("Could not load <$url>");
})
.build();
Lottie.initialize(lottieConfig);
}

private static class LottieFetchSuccess implements LottieFetchResult {

@NonNull private final InputStream jsonStream;

LottieFetchSuccess(@NonNull InputStream jsonStream) {
this.jsonStream = jsonStream;
}

@Override public boolean isSuccessful() {
return true;
}

@Override @NonNull public InputStream bodyByteStream() {
return jsonStream;
}

@Override public String contentType() {
return "application/json";
}

@Override @Nullable public String error() {
return null;
}

@Override public void close() {
// No-op
}
}

private static class LottieFetchFailure implements LottieFetchResult {

@NonNull private final String errorMessage;

LottieFetchFailure(@NonNull String errorMessage) {
this.errorMessage = errorMessage;
}

@Override public boolean isSuccessful() {
return false;
}

@Override @NonNull public InputStream bodyByteStream() {
throw new RuntimeException("LottieFetchFailure has no body");
}

@Override @Nullable public String contentType() {
return null;
}

@Override public String error() {
return errorMessage;
}

@Override public void close() {
// No-op
}
}
}
116 changes: 116 additions & 0 deletions lottie/src/test/resources/test1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"v": "4.11.1",
"fr": 60,
"ip": 0,
"op": 180,
"w": 300,
"h": 300,
"nm": "Comp 1",
"ddd": 0,
"assets": [],
"layers": [
{
"ddd": 0,
"ind": 1,
"ty": 4,
"nm": "Shape Layer 1",
"sr": 1,
"ks": {
"o": {
"a": 0,
"k": 100,
"ix": 11
},
"r": {
"a": 0,
"k": 0,
"ix": 10
},
"p": {
"a": 0,
"k": [
150,
150,
0
],
"ix": 2
},
"a": {
"a": 0,
"k": [
0,
0,
0
],
"ix": 1
},
"s": {
"a": 0,
"k": [
100,
100,
100
],
"ix": 6
}
},
"ao": 0,
"shapes": [
{
"ty": "rc",
"d": 1,
"s": {
"a": 0,
"k": [
100,
100
],
"ix": 2
},
"p": {
"a": 0,
"k": [
0,
0
],
"ix": 3
},
"r": {
"a": 0,
"k": 0,
"ix": 4
},
"nm": "Rectangle Path 1",
"mn": "ADBE Vector Shape - Rect",
"hd": false
},
{
"ty": "fl",
"c": {
"a": 0,
"k": [
0.928262987324,
0,
0,
1
],
"ix": 4
},
"o": {
"a": 0,
"k": 100,
"ix": 5
},
"r": 1,
"nm": "Fill 1",
"mn": "ADBE Vector Graphic - Fill",
"hd": false
}
],
"ip": 0,
"op": 180,
"st": 0,
"bm": 0
}
]
}