31
31
import com .firebase .ui .auth .util .data .ProviderUtils ;
32
32
import com .google .android .gms .auth .api .credentials .Credential ;
33
33
import com .google .firebase .FirebaseApp ;
34
- import com .google .firebase .FirebaseOptions ;
35
34
import com .google .firebase .auth .EmailAuthProvider ;
36
35
import com .google .firebase .auth .FacebookAuthProvider ;
36
+ import com .google .firebase .auth .FirebaseAuth ;
37
37
import com .google .firebase .auth .FirebaseUser ;
38
38
import com .google .firebase .auth .GoogleAuthProvider ;
39
39
import com .google .firebase .auth .PhoneAuthProvider ;
44
44
import org .robolectric .Shadows ;
45
45
import org .robolectric .shadows .ShadowActivity ;
46
46
47
+ import java .lang .reflect .Field ;
48
+ import java .lang .reflect .ParameterizedType ;
49
+ import java .lang .reflect .Type ;
47
50
import java .util .ArrayList ;
48
51
import java .util .Collection ;
49
52
import java .util .List ;
53
+ import java .util .Map ;
50
54
51
55
import static junit .framework .Assert .assertEquals ;
56
+ import static org .mockito .ArgumentMatchers .eq ;
52
57
import static org .mockito .Mockito .mock ;
53
58
import static org .mockito .Mockito .spy ;
54
59
import static org .mockito .Mockito .when ;
55
60
56
61
public class TestHelper {
57
- private static final String APPLICATION_ID = "testAppId" ;
58
- private static final String API_KEY = "fakeKey" ;
59
-
60
62
public static void initialize () {
61
63
spyContextAndResources ();
62
64
AuthUI .setApplicationContext (RuntimeEnvironment .application );
@@ -72,18 +74,42 @@ private static void spyContextAndResources() {
72
74
when (RuntimeEnvironment .application .getResources ()).thenReturn (spiedResources );
73
75
}
74
76
75
- private static FirebaseApp initializeApp (Context context ) {
76
- try {
77
- return FirebaseApp .initializeApp (
78
- context ,
79
- new FirebaseOptions .Builder ()
80
- .setApiKey (API_KEY )
81
- .setApplicationId (APPLICATION_ID )
82
- .build (),
83
- FirebaseApp .DEFAULT_APP_NAME );
84
- } catch (IllegalStateException e ) {
85
- return FirebaseApp .getInstance (FirebaseApp .DEFAULT_APP_NAME );
77
+ private static void initializeApp (Context context ) {
78
+ if (!FirebaseApp .getApps (context ).isEmpty ()) { return ; }
79
+
80
+ for (Field field : FirebaseApp .class .getDeclaredFields ()) {
81
+ field .setAccessible (true );
82
+
83
+ Object o ;
84
+ try {
85
+ o = field .get (null );
86
+ } catch (IllegalAccessException e ) {
87
+ throw new IllegalStateException (e );
88
+ } catch (NullPointerException e ) {
89
+ continue ; // Instance field, move on
90
+ }
91
+
92
+ Type genericType = field .getGenericType ();
93
+ if (o instanceof Map && genericType instanceof ParameterizedType ) {
94
+ Type [] parameterTypes = ((ParameterizedType ) genericType ).getActualTypeArguments ();
95
+ if (parameterTypes .length != 2 || parameterTypes [0 ] != String .class
96
+ || parameterTypes [1 ] != FirebaseApp .class ) {
97
+ continue ;
98
+ }
99
+
100
+ //noinspection unchecked
101
+ Map <String , FirebaseApp > instances = (Map <String , FirebaseApp >) o ;
102
+
103
+ instances .put (FirebaseApp .DEFAULT_APP_NAME , mock (FirebaseApp .class ));
104
+
105
+ break ;
106
+ }
86
107
}
108
+
109
+ FirebaseApp app = FirebaseApp .getInstance ();
110
+ when (app .get (eq (FirebaseAuth .class ))).thenReturn (mock (FirebaseAuth .class ));
111
+ when (app .getApplicationContext ()).thenReturn (context );
112
+ when (app .getName ()).thenReturn (FirebaseApp .DEFAULT_APP_NAME );
87
113
}
88
114
89
115
private static void initializeProviders () {
@@ -138,7 +164,7 @@ public static FlowParameters getFlowParameters(Collection<String> providerIds) {
138
164
true );
139
165
}
140
166
141
- public static void verifyCredentialSaveStarted (@ NonNull Activity activity ,
167
+ public static void verifyCredentialSaveStarted (@ NonNull Activity activity ,
142
168
@ Nullable String providerId ,
143
169
@ Nullable String email ,
144
170
@ Nullable String password ,
0 commit comments