Skip to content

Remove testing hacks #1337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions auth/src/main/java/com/firebase/ui/auth/AuthUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import android.support.annotation.RestrictTo;
import android.support.annotation.StringDef;
import android.support.annotation.StyleRes;
import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
import android.util.Log;

Expand Down Expand Up @@ -92,9 +91,6 @@
*/
public final class AuthUI {

@VisibleForTesting
protected static FirebaseAuth sDefaultAuth;

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public static final String TAG = "AuthUI";

Expand Down Expand Up @@ -147,13 +143,7 @@ public final class AuthUI {

private AuthUI(FirebaseApp app) {
mApp = app;

// TODO: This is a bad testing hack
if (sDefaultAuth != null) {
mAuth = sDefaultAuth;
} else {
mAuth = FirebaseAuth.getInstance(mApp);
}
mAuth = FirebaseAuth.getInstance(mApp);

try {
mAuth.setFirebaseUIVersion(BuildConfig.VERSION_NAME);
Expand Down
20 changes: 9 additions & 11 deletions auth/src/test/java/com/firebase/ui/auth/AuthUITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import com.firebase.ui.auth.testhelpers.TestConstants;
import com.firebase.ui.auth.testhelpers.TestHelper;
import com.firebase.ui.auth.util.ExtraConstants;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FirebaseAuth;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -32,20 +30,20 @@
import java.util.Arrays;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.mock;

@RunWith(RobolectricTestRunner.class)
public class AuthUITest {
private AuthUI mAuthUi;

@Before
public void setUp() {
TestHelper.initialize();
AuthUI.sDefaultAuth = mock(FirebaseAuth.class);
mAuthUi = AuthUI.getInstance(TestHelper.MOCK_APP);
}

@Test
public void testCreateStartIntent_shouldHaveEmailAsDefaultProvider() {
FlowParameters flowParameters = AuthUI
.getInstance()
FlowParameters flowParameters = mAuthUi
.createSignInIntentBuilder()
.build()
.getParcelableExtra(ExtraConstants.FLOW_PARAMS);
Expand All @@ -56,15 +54,15 @@ public void testCreateStartIntent_shouldHaveEmailAsDefaultProvider() {

@Test(expected = IllegalArgumentException.class)
public void testCreateStartIntent_shouldOnlyAllowOneInstanceOfAnIdp() {
SignInIntentBuilder startIntent = AuthUI.getInstance().createSignInIntentBuilder();
SignInIntentBuilder startIntent = mAuthUi.createSignInIntentBuilder();
startIntent.setAvailableProviders(Arrays.asList(
new IdpConfig.EmailBuilder().build(),
new IdpConfig.EmailBuilder().build()));
}

@Test
public void testCreatingStartIntent() {
FlowParameters flowParameters = AuthUI.getInstance()
FlowParameters flowParameters = mAuthUi
.createSignInIntentBuilder()
.setAvailableProviders(Arrays.asList(
new IdpConfig.EmailBuilder().build(),
Expand All @@ -75,21 +73,21 @@ public void testCreatingStartIntent() {
.getParcelableExtra(ExtraConstants.FLOW_PARAMS);

assertEquals(3, flowParameters.providerInfo.size());
assertEquals(FirebaseApp.getInstance().getName(), flowParameters.appName);
assertEquals(TestHelper.MOCK_APP.getName(), flowParameters.appName);
assertEquals(TestConstants.TOS_URL, flowParameters.termsOfServiceUrl);
assertEquals(TestConstants.PRIVACY_URL, flowParameters.privacyPolicyUrl);
assertEquals(AuthUI.getDefaultTheme(), flowParameters.themeId);
}

@Test(expected = NullPointerException.class)
public void testCreatingStartIntent_withNullTos_expectEnforcesNonNullTosUrl() {
SignInIntentBuilder startIntent = AuthUI.getInstance().createSignInIntentBuilder();
SignInIntentBuilder startIntent = mAuthUi.createSignInIntentBuilder();
startIntent.setTosAndPrivacyPolicyUrls(null, TestConstants.PRIVACY_URL);
}

@Test(expected = NullPointerException.class)
public void testCreatingStartIntent_withNullPp_expectEnforcesNonNullPpUrl() {
SignInIntentBuilder startIntent = AuthUI.getInstance().createSignInIntentBuilder();
SignInIntentBuilder startIntent = mAuthUi.createSignInIntentBuilder();
startIntent.setTosAndPrivacyPolicyUrls(TestConstants.TOS_URL, null);
}
}
79 changes: 19 additions & 60 deletions auth/src/test/java/com/firebase/ui/auth/testhelpers/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,44 @@

package com.firebase.ui.auth.testhelpers;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.AuthUI.IdpConfig;
import com.firebase.ui.auth.R;
import com.firebase.ui.auth.data.model.FlowParameters;
import com.firebase.ui.auth.ui.credentials.CredentialSaveActivity;
import com.firebase.ui.auth.util.ExtraConstants;
import com.firebase.ui.auth.util.data.ProviderUtils;
import com.google.android.gms.auth.api.credentials.Credential;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FacebookAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.firebase.auth.PhoneAuthProvider;
import com.google.firebase.auth.TwitterAuthProvider;

import org.junit.Assert;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowActivity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static junit.framework.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

public class TestHelper {
private static final String APPLICATION_ID = "testAppId";
private static final String API_KEY = "fakeKey";
public final class TestHelper {
public static final FirebaseApp MOCK_APP;

static {
FirebaseApp app = mock(FirebaseApp.class);
when(app.get(eq(FirebaseAuth.class))).thenReturn(mock(FirebaseAuth.class));
when(app.getApplicationContext()).thenReturn(RuntimeEnvironment.application);
when(app.getName()).thenReturn(FirebaseApp.DEFAULT_APP_NAME);
MOCK_APP = app;
}

public static void initialize() {
spyContextAndResources();
Expand All @@ -72,18 +68,13 @@ private static void spyContextAndResources() {
when(RuntimeEnvironment.application.getResources()).thenReturn(spiedResources);
}

private static FirebaseApp initializeApp(Context context) {
try {
return FirebaseApp.initializeApp(
context,
new FirebaseOptions.Builder()
.setApiKey(API_KEY)
.setApplicationId(APPLICATION_ID)
.build(),
FirebaseApp.DEFAULT_APP_NAME);
} catch (IllegalStateException e) {
return FirebaseApp.getInstance(FirebaseApp.DEFAULT_APP_NAME);
}
private static void initializeApp(Context context) {
if (!FirebaseApp.getApps(context).isEmpty()) return;

FirebaseApp.initializeApp(context, new FirebaseOptions.Builder()
.setApiKey("fake")
.setApplicationId("fake")
.build());
}

private static void initializeProviders() {
Expand Down Expand Up @@ -138,36 +129,4 @@ public static FlowParameters getFlowParameters(Collection<String> providerIds) {
true);
}

public static void verifyCredentialSaveStarted(@NonNull Activity activity,
@Nullable String providerId,
@Nullable String email,
@Nullable String password,
@Nullable String phoneNumber) {

ShadowActivity shadowActivity = Shadows.shadowOf(activity);
Intent startedIntent = shadowActivity.getNextStartedActivity();

// Verify that CredentialSaveActivity is next up
Assert.assertEquals(startedIntent.getComponent().getClassName(),
CredentialSaveActivity.class.getName());

// Check the credential passed
Credential credential = startedIntent.getParcelableExtra(ExtraConstants.CREDENTIAL);

// Check the password
assertEquals(credential.getPassword(), password);

// Non-password credentials have a provider ID
if (TextUtils.isEmpty(password)) {
assertEquals(credential.getAccountType(),
ProviderUtils.providerIdToAccountType(providerId));
}

// ID can either be email or phone number
if (!TextUtils.isEmpty(phoneNumber)) {
assertEquals(credential.getId(), phoneNumber);
} else {
assertEquals(credential.getId(), email);
}
}
}