Skip to content

Commit 519f887

Browse files
committed
Remove all hacks
Signed-off-by: Alex Saveau <[email protected]>
1 parent 076b865 commit 519f887

File tree

2 files changed

+29
-93
lines changed

2 files changed

+29
-93
lines changed

auth/src/test/java/com/firebase/ui/auth/AuthUITest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.firebase.ui.auth.testhelpers.TestConstants;
2121
import com.firebase.ui.auth.testhelpers.TestHelper;
2222
import com.firebase.ui.auth.util.ExtraConstants;
23-
import com.google.firebase.FirebaseApp;
2423
import com.google.firebase.auth.EmailAuthProvider;
2524

2625
import org.junit.Before;
@@ -34,15 +33,17 @@
3433

3534
@RunWith(RobolectricTestRunner.class)
3635
public class AuthUITest {
36+
private AuthUI mAuthUi;
37+
3738
@Before
3839
public void setUp() {
3940
TestHelper.initialize();
41+
mAuthUi = AuthUI.getInstance(TestHelper.MOCK_APP);
4042
}
4143

4244
@Test
4345
public void testCreateStartIntent_shouldHaveEmailAsDefaultProvider() {
44-
FlowParameters flowParameters = AuthUI
45-
.getInstance()
46+
FlowParameters flowParameters = mAuthUi
4647
.createSignInIntentBuilder()
4748
.build()
4849
.getParcelableExtra(ExtraConstants.FLOW_PARAMS);
@@ -53,15 +54,15 @@ public void testCreateStartIntent_shouldHaveEmailAsDefaultProvider() {
5354

5455
@Test(expected = IllegalArgumentException.class)
5556
public void testCreateStartIntent_shouldOnlyAllowOneInstanceOfAnIdp() {
56-
SignInIntentBuilder startIntent = AuthUI.getInstance().createSignInIntentBuilder();
57+
SignInIntentBuilder startIntent = mAuthUi.createSignInIntentBuilder();
5758
startIntent.setAvailableProviders(Arrays.asList(
5859
new IdpConfig.EmailBuilder().build(),
5960
new IdpConfig.EmailBuilder().build()));
6061
}
6162

6263
@Test
6364
public void testCreatingStartIntent() {
64-
FlowParameters flowParameters = AuthUI.getInstance()
65+
FlowParameters flowParameters = mAuthUi
6566
.createSignInIntentBuilder()
6667
.setAvailableProviders(Arrays.asList(
6768
new IdpConfig.EmailBuilder().build(),
@@ -72,21 +73,21 @@ public void testCreatingStartIntent() {
7273
.getParcelableExtra(ExtraConstants.FLOW_PARAMS);
7374

7475
assertEquals(3, flowParameters.providerInfo.size());
75-
assertEquals(FirebaseApp.getInstance().getName(), flowParameters.appName);
76+
assertEquals(TestHelper.MOCK_APP.getName(), flowParameters.appName);
7677
assertEquals(TestConstants.TOS_URL, flowParameters.termsOfServiceUrl);
7778
assertEquals(TestConstants.PRIVACY_URL, flowParameters.privacyPolicyUrl);
7879
assertEquals(AuthUI.getDefaultTheme(), flowParameters.themeId);
7980
}
8081

8182
@Test(expected = NullPointerException.class)
8283
public void testCreatingStartIntent_withNullTos_expectEnforcesNonNullTosUrl() {
83-
SignInIntentBuilder startIntent = AuthUI.getInstance().createSignInIntentBuilder();
84+
SignInIntentBuilder startIntent = mAuthUi.createSignInIntentBuilder();
8485
startIntent.setTosAndPrivacyPolicyUrls(null, TestConstants.PRIVACY_URL);
8586
}
8687

8788
@Test(expected = NullPointerException.class)
8889
public void testCreatingStartIntent_withNullPp_expectEnforcesNonNullPpUrl() {
89-
SignInIntentBuilder startIntent = AuthUI.getInstance().createSignInIntentBuilder();
90+
SignInIntentBuilder startIntent = mAuthUi.createSignInIntentBuilder();
9091
startIntent.setTosAndPrivacyPolicyUrls(TestConstants.TOS_URL, null);
9192
}
9293
}

auth/src/test/java/com/firebase/ui/auth/testhelpers/TestHelper.java

Lines changed: 20 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,15 @@
1414

1515
package com.firebase.ui.auth.testhelpers;
1616

17-
import android.app.Activity;
1817
import android.content.Context;
19-
import android.content.Intent;
2018
import android.content.res.Resources;
21-
import android.support.annotation.NonNull;
22-
import android.support.annotation.Nullable;
23-
import android.text.TextUtils;
2419

2520
import com.firebase.ui.auth.AuthUI;
2621
import com.firebase.ui.auth.AuthUI.IdpConfig;
2722
import com.firebase.ui.auth.R;
2823
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;
3324
import com.google.firebase.FirebaseApp;
25+
import com.google.firebase.FirebaseOptions;
3426
import com.google.firebase.auth.EmailAuthProvider;
3527
import com.google.firebase.auth.FacebookAuthProvider;
3628
import com.google.firebase.auth.FirebaseAuth;
@@ -39,26 +31,28 @@
3931
import com.google.firebase.auth.PhoneAuthProvider;
4032
import com.google.firebase.auth.TwitterAuthProvider;
4133

42-
import org.junit.Assert;
4334
import org.robolectric.RuntimeEnvironment;
44-
import org.robolectric.Shadows;
45-
import org.robolectric.shadows.ShadowActivity;
4635

47-
import java.lang.reflect.Field;
48-
import java.lang.reflect.ParameterizedType;
49-
import java.lang.reflect.Type;
5036
import java.util.ArrayList;
5137
import java.util.Collection;
5238
import java.util.List;
53-
import java.util.Map;
5439

55-
import static junit.framework.Assert.assertEquals;
5640
import static org.mockito.ArgumentMatchers.eq;
5741
import static org.mockito.Mockito.mock;
5842
import static org.mockito.Mockito.spy;
5943
import static org.mockito.Mockito.when;
6044

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+
6256
public static void initialize() {
6357
spyContextAndResources();
6458
AuthUI.setApplicationContext(RuntimeEnvironment.application);
@@ -75,41 +69,14 @@ private static void spyContextAndResources() {
7569
}
7670

7771
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());
11380
}
11481

11582
private static void initializeProviders() {
@@ -164,36 +131,4 @@ public static FlowParameters getFlowParameters(Collection<String> providerIds) {
164131
true);
165132
}
166133

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-
}
199134
}

0 commit comments

Comments
 (0)