-
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.
Include day/night mode in resource id cache keys.
- Loading branch information
Showing
3 changed files
with
152 additions
and
9 deletions.
There are no files selected for viewing
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
49 changes: 49 additions & 0 deletions
49
library/src/main/java/com/bumptech/glide/signature/AndroidResourceSignature.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,49 @@ | ||
package com.bumptech.glide.signature; | ||
|
||
import android.content.Context; | ||
import android.content.res.Configuration; | ||
import androidx.annotation.NonNull; | ||
import com.bumptech.glide.load.Key; | ||
import com.bumptech.glide.util.Util; | ||
import java.nio.ByteBuffer; | ||
import java.security.MessageDigest; | ||
|
||
public final class AndroidResourceSignature implements Key { | ||
|
||
private final int nightMode; | ||
private final Key applicationVersion; | ||
|
||
@NonNull | ||
public static Key obtain(@NonNull Context context) { | ||
Key signature = ApplicationVersionSignature.obtain(context); | ||
int nightMode = | ||
context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; | ||
return new AndroidResourceSignature(nightMode, signature); | ||
} | ||
|
||
private AndroidResourceSignature(int nightMode, Key applicationVersion) { | ||
this.nightMode = nightMode; | ||
this.applicationVersion = applicationVersion; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o instanceof AndroidResourceSignature) { | ||
AndroidResourceSignature that = (AndroidResourceSignature) o; | ||
return nightMode == that.nightMode && applicationVersion.equals(that.applicationVersion); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Util.hashCode(applicationVersion, nightMode); | ||
} | ||
|
||
@Override | ||
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { | ||
applicationVersion.updateDiskCacheKey(messageDigest); | ||
byte[] nightModeData = ByteBuffer.allocate(4).putInt(nightMode).array(); | ||
messageDigest.update(nightModeData); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
library/test/src/test/java/com/bumptech/glide/signature/AndroidResourceSignatureTest.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,94 @@ | ||
package com.bumptech.glide.signature; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageManager.NameNotFoundException; | ||
import androidx.test.core.app.ApplicationProvider; | ||
import com.bumptech.glide.load.Key; | ||
import com.bumptech.glide.tests.KeyTester; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Answers; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.annotation.Config; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
@Config(sdk = 28) | ||
public class AndroidResourceSignatureTest { | ||
@Rule public final KeyTester keyTester = new KeyTester(); | ||
private Context context; | ||
|
||
@Before | ||
public void setUp() { | ||
context = ApplicationProvider.getApplicationContext(); | ||
} | ||
|
||
@Test | ||
public void testCanGetKeyForSignature() { | ||
Key key = AndroidResourceSignature.obtain(context); | ||
assertNotNull(key); | ||
} | ||
|
||
@Test | ||
public void testKeyForSignatureIsTheSameAcrossCallsInTheSamePackage() { | ||
keyTester | ||
.addEquivalenceGroup( | ||
AndroidResourceSignature.obtain(context), AndroidResourceSignature.obtain(context)) | ||
.addEquivalenceGroup(new ObjectKey("test")) | ||
.addRegressionTest( | ||
ApplicationVersionSignature.obtain(context), | ||
"5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9") | ||
.test(); | ||
} | ||
|
||
@Test | ||
public void testKeyForSignatureDiffersByNightMode() { | ||
RuntimeEnvironment.setQualifiers("notnight"); | ||
keyTester | ||
.addEquivalenceGroup( | ||
AndroidResourceSignature.obtain(context), AndroidResourceSignature.obtain(context)) | ||
.addRegressionTest( | ||
AndroidResourceSignature.obtain(context), | ||
"265d958bdae1bea56e45cc31f4db672c22893b66fef85617bbc78742bd912207"); | ||
RuntimeEnvironment.setQualifiers("night"); | ||
keyTester | ||
.addEquivalenceGroup( | ||
AndroidResourceSignature.obtain(context), AndroidResourceSignature.obtain(context)) | ||
.addRegressionTest( | ||
AndroidResourceSignature.obtain(context), | ||
"96c9b8d5bb071ccd67df50cd9a0059640ebd02db78d08f07611ec145ce44a638"); | ||
|
||
keyTester.test(); | ||
} | ||
|
||
@Test | ||
public void testUnresolvablePackageInfo() throws NameNotFoundException { | ||
Context context = mock(Context.class, Answers.RETURNS_DEEP_STUBS); | ||
String packageName = "my.package"; | ||
when(context.getPackageName()).thenReturn(packageName); | ||
when(context.getPackageManager().getPackageInfo(packageName, 0)) | ||
.thenThrow(new NameNotFoundException("test")); | ||
|
||
Key key = AndroidResourceSignature.obtain(context); | ||
|
||
assertNotNull(key); | ||
} | ||
|
||
@Test | ||
public void testMissingPackageInfo() throws NameNotFoundException { | ||
Context context = mock(Context.class, Answers.RETURNS_DEEP_STUBS); | ||
String packageName = "my.package"; | ||
when(context.getPackageName()).thenReturn(packageName); | ||
when(context.getPackageManager().getPackageInfo(packageName, 0)).thenReturn(null); | ||
|
||
Key key = AndroidResourceSignature.obtain(context); | ||
|
||
assertNotNull(key); | ||
} | ||
} |