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 ;
34
25
import com .google .firebase .FirebaseOptions ;
35
26
import com .google .firebase .auth .EmailAuthProvider ;
36
27
import com .google .firebase .auth .FacebookAuthProvider ;
28
+ import com .google .firebase .auth .FirebaseAuth ;
37
29
import com .google .firebase .auth .FirebaseUser ;
38
30
import com .google .firebase .auth .GoogleAuthProvider ;
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
36
import java .util .ArrayList ;
48
37
import java .util .Collection ;
49
38
import java .util .List ;
50
39
51
- import static junit . framework . Assert . assertEquals ;
40
+ import static org . mockito . ArgumentMatchers . eq ;
52
41
import static org .mockito .Mockito .mock ;
53
42
import static org .mockito .Mockito .spy ;
54
43
import static org .mockito .Mockito .when ;
55
44
56
- public class TestHelper {
57
- private static final String APPLICATION_ID = "testAppId" ;
58
- private static final String API_KEY = "fakeKey" ;
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
+ }
59
55
60
56
public static void initialize () {
61
57
spyContextAndResources ();
@@ -72,18 +68,13 @@ private static void spyContextAndResources() {
72
68
when (RuntimeEnvironment .application .getResources ()).thenReturn (spiedResources );
73
69
}
74
70
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 );
86
- }
71
+ private static void initializeApp (Context context ) {
72
+ if (!FirebaseApp .getApps (context ).isEmpty ()) return ;
73
+
74
+ FirebaseApp .initializeApp (context , new FirebaseOptions .Builder ()
75
+ .setApiKey ("fake" )
76
+ .setApplicationId ("fake" )
77
+ .build ());
87
78
}
88
79
89
80
private static void initializeProviders () {
@@ -138,36 +129,4 @@ public static FlowParameters getFlowParameters(Collection<String> providerIds) {
138
129
true );
139
130
}
140
131
141
- public static void verifyCredentialSaveStarted (@ NonNull Activity activity ,
142
- @ Nullable String providerId ,
143
- @ Nullable String email ,
144
- @ Nullable String password ,
145
- @ Nullable String phoneNumber ) {
146
-
147
- ShadowActivity shadowActivity = Shadows .shadowOf (activity );
148
- Intent startedIntent = shadowActivity .getNextStartedActivity ();
149
-
150
- // Verify that CredentialSaveActivity is next up
151
- Assert .assertEquals (startedIntent .getComponent ().getClassName (),
152
- CredentialSaveActivity .class .getName ());
153
-
154
- // Check the credential passed
155
- Credential credential = startedIntent .getParcelableExtra (ExtraConstants .CREDENTIAL );
156
-
157
- // Check the password
158
- assertEquals (credential .getPassword (), password );
159
-
160
- // Non-password credentials have a provider ID
161
- if (TextUtils .isEmpty (password )) {
162
- assertEquals (credential .getAccountType (),
163
- ProviderUtils .providerIdToAccountType (providerId ));
164
- }
165
-
166
- // ID can either be email or phone number
167
- if (!TextUtils .isEmpty (phoneNumber )) {
168
- assertEquals (credential .getId (), phoneNumber );
169
- } else {
170
- assertEquals (credential .getId (), email );
171
- }
172
- }
173
132
}
0 commit comments