14
14
15
15
package com .firebase .ui .auth .testhelpers ;
16
16
17
- import android .app .Activity ;
18
17
import android .content .Context ;
19
- import android .content .Intent ;
20
18
import android .content .res .Resources ;
21
- import android .support .annotation .NonNull ;
22
- import android .support .annotation .Nullable ;
23
- import android .text .TextUtils ;
24
19
25
20
import com .firebase .ui .auth .AuthUI ;
26
21
import com .firebase .ui .auth .AuthUI .IdpConfig ;
27
22
import com .firebase .ui .auth .R ;
28
23
import com .firebase .ui .auth .data .model .FlowParameters ;
29
- import com .firebase .ui .auth .ui .credentials .CredentialSaveActivity ;
30
- import com .firebase .ui .auth .util .ExtraConstants ;
31
- import com .firebase .ui .auth .util .data .ProviderUtils ;
32
- import com .google .android .gms .auth .api .credentials .Credential ;
33
24
import com .google .firebase .FirebaseApp ;
25
+ import com .google .firebase .FirebaseOptions ;
34
26
import com .google .firebase .auth .EmailAuthProvider ;
35
27
import com .google .firebase .auth .FacebookAuthProvider ;
36
28
import com .google .firebase .auth .FirebaseAuth ;
39
31
import com .google .firebase .auth .PhoneAuthProvider ;
40
32
import com .google .firebase .auth .TwitterAuthProvider ;
41
33
42
- import org .junit .Assert ;
43
34
import org .robolectric .RuntimeEnvironment ;
44
- import org .robolectric .Shadows ;
45
- import org .robolectric .shadows .ShadowActivity ;
46
35
47
- import java .lang .reflect .Field ;
48
- import java .lang .reflect .ParameterizedType ;
49
- import java .lang .reflect .Type ;
50
36
import java .util .ArrayList ;
51
37
import java .util .Collection ;
52
38
import java .util .List ;
53
- import java .util .Map ;
54
39
55
- import static junit .framework .Assert .assertEquals ;
56
40
import static org .mockito .ArgumentMatchers .eq ;
57
41
import static org .mockito .Mockito .mock ;
58
42
import static org .mockito .Mockito .spy ;
59
43
import static org .mockito .Mockito .when ;
60
44
61
- public class TestHelper {
45
+ public final class TestHelper {
46
+ public static final FirebaseApp MOCK_APP ;
47
+
48
+ static {
49
+ FirebaseApp app = mock (FirebaseApp .class );
50
+ when (app .get (eq (FirebaseAuth .class ))).thenReturn (mock (FirebaseAuth .class ));
51
+ when (app .getApplicationContext ()).thenReturn (RuntimeEnvironment .application );
52
+ when (app .getName ()).thenReturn (FirebaseApp .DEFAULT_APP_NAME );
53
+ MOCK_APP = app ;
54
+ }
55
+
62
56
public static void initialize () {
63
57
spyContextAndResources ();
64
58
AuthUI .setApplicationContext (RuntimeEnvironment .application );
@@ -75,41 +69,14 @@ private static void spyContextAndResources() {
75
69
}
76
70
77
71
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
- }
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 );
72
+ if (!FirebaseApp .getApps (context ).isEmpty ()) return ;
73
+
74
+ FirebaseApp .initializeApp (
75
+ context ,
76
+ new FirebaseOptions .Builder ()
77
+ .setApiKey ("fake" )
78
+ .setApplicationId ("fake" )
79
+ .build ());
113
80
}
114
81
115
82
private static void initializeProviders () {
@@ -164,36 +131,4 @@ public static FlowParameters getFlowParameters(Collection<String> providerIds) {
164
131
true );
165
132
}
166
133
167
- public static void verifyCredentialSaveStarted (@ NonNull Activity activity ,
168
- @ Nullable String providerId ,
169
- @ Nullable String email ,
170
- @ Nullable String password ,
171
- @ Nullable String phoneNumber ) {
172
-
173
- ShadowActivity shadowActivity = Shadows .shadowOf (activity );
174
- Intent startedIntent = shadowActivity .getNextStartedActivity ();
175
-
176
- // Verify that CredentialSaveActivity is next up
177
- Assert .assertEquals (startedIntent .getComponent ().getClassName (),
178
- CredentialSaveActivity .class .getName ());
179
-
180
- // Check the credential passed
181
- Credential credential = startedIntent .getParcelableExtra (ExtraConstants .CREDENTIAL );
182
-
183
- // Check the password
184
- assertEquals (credential .getPassword (), password );
185
-
186
- // Non-password credentials have a provider ID
187
- if (TextUtils .isEmpty (password )) {
188
- assertEquals (credential .getAccountType (),
189
- ProviderUtils .providerIdToAccountType (providerId ));
190
- }
191
-
192
- // ID can either be email or phone number
193
- if (!TextUtils .isEmpty (phoneNumber )) {
194
- assertEquals (credential .getId (), phoneNumber );
195
- } else {
196
- assertEquals (credential .getId (), email );
197
- }
198
- }
199
134
}
0 commit comments