|
| 1 | +package com.bumptech.glide.signature; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertNotNull; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.when; |
| 6 | + |
| 7 | +import android.content.Context; |
| 8 | +import android.content.pm.PackageManager.NameNotFoundException; |
| 9 | +import androidx.test.core.app.ApplicationProvider; |
| 10 | +import com.bumptech.glide.load.Key; |
| 11 | +import com.bumptech.glide.tests.KeyTester; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Rule; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.mockito.Answers; |
| 17 | +import org.robolectric.RobolectricTestRunner; |
| 18 | +import org.robolectric.RuntimeEnvironment; |
| 19 | +import org.robolectric.annotation.Config; |
| 20 | + |
| 21 | +@RunWith(RobolectricTestRunner.class) |
| 22 | +@Config(sdk = 28) |
| 23 | +public class AndroidResourceSignatureTest { |
| 24 | + @Rule public final KeyTester keyTester = new KeyTester(); |
| 25 | + private Context context; |
| 26 | + |
| 27 | + @Before |
| 28 | + public void setUp() { |
| 29 | + context = ApplicationProvider.getApplicationContext(); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testCanGetKeyForSignature() { |
| 34 | + Key key = AndroidResourceSignature.obtain(context); |
| 35 | + assertNotNull(key); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testKeyForSignatureIsTheSameAcrossCallsInTheSamePackage() { |
| 40 | + keyTester |
| 41 | + .addEquivalenceGroup( |
| 42 | + AndroidResourceSignature.obtain(context), AndroidResourceSignature.obtain(context)) |
| 43 | + .addEquivalenceGroup(new ObjectKey("test")) |
| 44 | + .addRegressionTest( |
| 45 | + ApplicationVersionSignature.obtain(context), |
| 46 | + "5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9") |
| 47 | + .test(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testKeyForSignatureDiffersByNightMode() { |
| 52 | + RuntimeEnvironment.setQualifiers("notnight"); |
| 53 | + keyTester |
| 54 | + .addEquivalenceGroup( |
| 55 | + AndroidResourceSignature.obtain(context), AndroidResourceSignature.obtain(context)) |
| 56 | + .addRegressionTest( |
| 57 | + AndroidResourceSignature.obtain(context), |
| 58 | + "265d958bdae1bea56e45cc31f4db672c22893b66fef85617bbc78742bd912207"); |
| 59 | + RuntimeEnvironment.setQualifiers("night"); |
| 60 | + keyTester |
| 61 | + .addEquivalenceGroup( |
| 62 | + AndroidResourceSignature.obtain(context), AndroidResourceSignature.obtain(context)) |
| 63 | + .addRegressionTest( |
| 64 | + AndroidResourceSignature.obtain(context), |
| 65 | + "96c9b8d5bb071ccd67df50cd9a0059640ebd02db78d08f07611ec145ce44a638"); |
| 66 | + |
| 67 | + keyTester.test(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testUnresolvablePackageInfo() throws NameNotFoundException { |
| 72 | + Context context = mock(Context.class, Answers.RETURNS_DEEP_STUBS); |
| 73 | + String packageName = "my.package"; |
| 74 | + when(context.getPackageName()).thenReturn(packageName); |
| 75 | + when(context.getPackageManager().getPackageInfo(packageName, 0)) |
| 76 | + .thenThrow(new NameNotFoundException("test")); |
| 77 | + |
| 78 | + Key key = AndroidResourceSignature.obtain(context); |
| 79 | + |
| 80 | + assertNotNull(key); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testMissingPackageInfo() throws NameNotFoundException { |
| 85 | + Context context = mock(Context.class, Answers.RETURNS_DEEP_STUBS); |
| 86 | + String packageName = "my.package"; |
| 87 | + when(context.getPackageName()).thenReturn(packageName); |
| 88 | + when(context.getPackageManager().getPackageInfo(packageName, 0)).thenReturn(null); |
| 89 | + |
| 90 | + Key key = AndroidResourceSignature.obtain(context); |
| 91 | + |
| 92 | + assertNotNull(key); |
| 93 | + } |
| 94 | +} |
0 commit comments