1
1
package com .bumptech .glide .module ;
2
2
3
3
import static com .google .common .truth .Truth .assertThat ;
4
+ import static org .junit .Assert .assertThrows ;
5
+ import static org .mockito .ArgumentMatchers .anyInt ;
6
+ import static org .mockito .ArgumentMatchers .anyString ;
4
7
import static org .mockito .ArgumentMatchers .eq ;
8
+ import static org .mockito .Mockito .doThrow ;
5
9
import static org .mockito .Mockito .mock ;
6
10
import static org .mockito .Mockito .when ;
7
11
8
12
import android .content .Context ;
9
13
import android .content .pm .ApplicationInfo ;
10
14
import android .content .pm .PackageManager ;
15
+ import android .content .pm .PackageManager .NameNotFoundException ;
11
16
import android .os .Bundle ;
12
17
import androidx .annotation .NonNull ;
13
18
import com .bumptech .glide .Glide ;
16
21
import java .util .List ;
17
22
import org .junit .Before ;
18
23
import org .junit .Test ;
24
+ import org .junit .function .ThrowingRunnable ;
19
25
import org .junit .runner .RunWith ;
20
26
import org .mockito .Mock ;
21
27
import org .mockito .MockitoAnnotations ;
27
33
@ SuppressWarnings ("deprecation" )
28
34
public class ManifestParserTest {
29
35
private static final String MODULE_VALUE = "GlideModule" ;
36
+ private static final String PACKAGE_NAME = "com.bumptech.test" ;
30
37
31
38
@ Mock private Context context ;
32
39
private ManifestParser parser ;
@@ -38,17 +45,27 @@ public void setUp() throws PackageManager.NameNotFoundException {
38
45
applicationInfo = new ApplicationInfo ();
39
46
applicationInfo .metaData = new Bundle ();
40
47
41
- String packageName = "com.bumptech.test" ;
42
- when (context .getPackageName ()).thenReturn (packageName );
48
+ when (context .getPackageName ()).thenReturn (PACKAGE_NAME );
43
49
44
50
PackageManager pm = mock (PackageManager .class );
45
- when (pm .getApplicationInfo (eq (packageName ), eq (PackageManager .GET_META_DATA )))
51
+ when (pm .getApplicationInfo (eq (PACKAGE_NAME ), eq (PackageManager .GET_META_DATA )))
46
52
.thenReturn (applicationInfo );
47
53
when (context .getPackageManager ()).thenReturn (pm );
48
54
49
55
parser = new ManifestParser (context );
50
56
}
51
57
58
+ // TODO(#4977): Remove this after the bug in Compose's previews is fixed.
59
+ @ Test
60
+ public void parse_withNullApplicationInfo_doesNotThrow () throws NameNotFoundException {
61
+ PackageManager pm = mock (PackageManager .class );
62
+ when (pm .getApplicationInfo (anyString (), anyInt ())).thenReturn (null );
63
+ when (context .getPackageManager ()).thenReturn (pm );
64
+
65
+ parser = new ManifestParser (context );
66
+ parser .parse ();
67
+ }
68
+
52
69
@ Test
53
70
public void testParse_returnsEmptyListIfNoModulesListed () {
54
71
assertThat (parser .parse ()).isEmpty ();
@@ -78,7 +95,6 @@ public void testParse_withMultipleValidModuleNames_returnsListContainingModules(
78
95
@ Test
79
96
public void testParse_withValidModuleName_ignoresMetadataWithoutGlideModuleValue () {
80
97
applicationInfo .metaData .putString (TestModule1 .class .getName (), MODULE_VALUE + "test" );
81
-
82
98
assertThat (parser .parse ()).isEmpty ();
83
99
}
84
100
@@ -96,13 +112,35 @@ public void testThrows_whenClassInManifestIsNotAModule() {
96
112
parser .parse ();
97
113
}
98
114
99
- @ Test (expected = RuntimeException .class )
100
- public void testThrows_whenPackageNameNotFound () {
101
- when (context .getPackageName ()).thenReturn ("fakePackageName" );
115
+ @ Test
116
+ public void parse_withNullMetadata_doesNotThrow () throws NameNotFoundException {
117
+ PackageManager pm = mock (PackageManager .class );
118
+ ApplicationInfo applicationInfo = new ApplicationInfo ();
119
+ applicationInfo .metaData = null ;
120
+ when (pm .getApplicationInfo (eq (PACKAGE_NAME ), eq (PackageManager .GET_META_DATA )))
121
+ .thenReturn (applicationInfo );
122
+ when (context .getPackageManager ()).thenReturn (pm );
102
123
103
124
parser .parse ();
104
125
}
105
126
127
+ @ Test
128
+ public void parse_withMissingName_throwsRuntimeException () throws NameNotFoundException {
129
+ PackageManager pm = mock (PackageManager .class );
130
+ doThrow (new NameNotFoundException ("name" )).when (pm ).getApplicationInfo (anyString (), anyInt ());
131
+ when (context .getPackageManager ()).thenReturn (pm );
132
+
133
+ assertThrows (
134
+ "Unable to find metadata to parse GlideModules" ,
135
+ RuntimeException .class ,
136
+ new ThrowingRunnable () {
137
+ @ Override
138
+ public void run () {
139
+ parser .parse ();
140
+ }
141
+ });
142
+ }
143
+
106
144
private void addModuleToManifest (Class <?> moduleClass ) {
107
145
addToManifest (moduleClass .getName ());
108
146
}
0 commit comments