1
+ // Copyright 2021 Google LLC
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ package com .google .firebase .crashlytics .internal .common ;
16
+
17
+ import static androidx .test .core .app .ApplicationProvider .getApplicationContext ;
18
+ import static org .mockito .Mockito .mock ;
19
+ import static org .mockito .Mockito .when ;
20
+ import static com .google .common .truth .Truth .assertThat ;
21
+ import static org .robolectric .Shadows .shadowOf ;
22
+
23
+ import android .content .Context ;
24
+ import android .os .Bundle ;
25
+
26
+ import com .google .firebase .FirebaseApp ;
27
+
28
+ import org .junit .Before ;
29
+ import org .junit .Test ;
30
+ import org .junit .runner .RunWith ;
31
+ import org .robolectric .RobolectricTestRunner ;
32
+
33
+ @ RunWith (RobolectricTestRunner .class )
34
+ public class DataCollectionArbiterRobolectricTest {
35
+
36
+ private Context testContext ;
37
+ private FirebaseApp firebaseApp ;
38
+
39
+ private static final String FIREBASE_CRASHLYTICS_COLLECTION_ENABLED =
40
+ "firebase_crashlytics_collection_enabled" ;
41
+
42
+ @ Before
43
+ public void setUp () {
44
+ testContext = getApplicationContext ();
45
+ firebaseApp = mock (FirebaseApp .class );
46
+ when (firebaseApp .getApplicationContext ()).thenReturn (testContext );
47
+ }
48
+
49
+ private DataCollectionArbiter getDataCollectionArbiter (FirebaseApp app ) {
50
+ return new DataCollectionArbiter (app );
51
+ }
52
+
53
+ @ Test
54
+ public void testSetCrashlyticsDataCollectionEnabled_overridesOtherSettings () {
55
+ // Ensure that Manifest metadata is set to false.
56
+ editManifestApplicationMetadata (testContext )
57
+ .putBoolean (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED , false );
58
+
59
+ // Mock FirebaseApp to return default data collection as false.
60
+ when (firebaseApp .isDataCollectionDefaultEnabled ()).thenReturn (false );
61
+
62
+ DataCollectionArbiter arbiter = getDataCollectionArbiter (firebaseApp );
63
+
64
+ // Setting explicitly to true should override both manifest and default settings.
65
+ arbiter .setCrashlyticsDataCollectionEnabled (true );
66
+ assertThat (arbiter .isAutomaticDataCollectionEnabled ()).isTrue ();
67
+
68
+ // Setting explicitly to false should also override the previous value
69
+ arbiter .setCrashlyticsDataCollectionEnabled (false );
70
+ assertThat (arbiter .isAutomaticDataCollectionEnabled ()).isFalse ();
71
+
72
+ arbiter .setCrashlyticsDataCollectionEnabled (null );
73
+ //Expecting `false` result since manifest metadata value is `false`
74
+ assertThat (arbiter .isAutomaticDataCollectionEnabled ()).isFalse ();
75
+ }
76
+
77
+ @ Test
78
+ public void testManifestMetadata_respectedWhenNoOverride () {
79
+ editManifestApplicationMetadata (testContext )
80
+ .putBoolean (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED , true );
81
+
82
+ DataCollectionArbiter arbiter = getDataCollectionArbiter (firebaseApp );
83
+
84
+ assertThat (arbiter .isAutomaticDataCollectionEnabled ()).isTrue ();
85
+
86
+ editManifestApplicationMetadata (testContext )
87
+ .putBoolean (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED , false );
88
+
89
+ arbiter = getDataCollectionArbiter (firebaseApp );
90
+
91
+ assertThat (arbiter .isAutomaticDataCollectionEnabled ()).isFalse ();
92
+ }
93
+
94
+ @ Test
95
+ public void testDefaultDataCollection_usedWhenNoOverrideOrManifestSetting () {
96
+ editManifestApplicationMetadata (testContext )
97
+ .remove (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED );
98
+
99
+ DataCollectionArbiter arbiter = getDataCollectionArbiter (firebaseApp );
100
+
101
+ when (firebaseApp .isDataCollectionDefaultEnabled ()).thenReturn (true );
102
+ assertThat (arbiter .isAutomaticDataCollectionEnabled ()).isTrue ();
103
+
104
+ when (firebaseApp .isDataCollectionDefaultEnabled ()).thenReturn (false );
105
+ assertThat (arbiter .isAutomaticDataCollectionEnabled ()).isFalse ();
106
+
107
+ //No Test of `null` return for firebaseApp.isDataCollectionDefaultEnabled(), since it will never return `null` value
108
+ }
109
+
110
+ private Bundle editManifestApplicationMetadata (Context context ) {
111
+ return shadowOf (context .getPackageManager ())
112
+ .getInternalMutablePackageInfo (context .getPackageName ())
113
+ .applicationInfo
114
+ .metaData ;
115
+ }
116
+ }
0 commit comments