Skip to content
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

Added setDefaultEmail for EmailBuilder #1866

Merged
merged 6 commits into from
Nov 18, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Allow most auth flows on devices without Google Play services (#1858)
- Add ability to setDefaultEmail to the EmailBuilder (#1670) (contributed by @laurentiu-git)
11 changes: 11 additions & 0 deletions auth/src/main/java/com/firebase/ui/auth/AuthUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,17 @@ public EmailBuilder setForceSameDevice() {
return this;
}

/**
* Sets a default sign in email, if the given email has been registered before, then
* it will ask the user for password, if the given email it's not registered, then
* it starts signing up the default email.
*/
@NonNull
public EmailBuilder setDefaultEmail(String email) {
getParams().putString(ExtraConstants.DEFAULT_EMAIL, email);
return this;
}

@Override
public IdpConfig build() {
if (super.mProviderId.equals(EMAIL_LINK_PROVIDER)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
responseForLinking, forceSameDevice);
switchFragment(fragment, R.id.fragment_register_email, EmailLinkFragment.TAG);
} else {
AuthUI.IdpConfig emailConfig = ProviderUtils.getConfigFromIdps(
getFlowParams().providers, EmailAuthProvider.PROVIDER_ID);

if (emailConfig != null) {
email = emailConfig.getParams().getString(ExtraConstants.DEFAULT_EMAIL);;
}
// Start with check email
CheckEmailFragment fragment = CheckEmailFragment.newInstance(email);
switchFragment(fragment, R.id.fragment_register_email, CheckEmailFragment.TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class ExtraConstants {
public static final String CREDENTIAL = "extra_credential";

public static final String EMAIL = "extra_email";
public static final String DEFAULT_EMAIL = "extra_default_email";
public static final String ALLOW_NEW_EMAILS = "extra_allow_new_emails";
public static final String REQUIRE_NAME = "extra_require_name";
public static final String GOOGLE_SIGN_IN_OPTIONS = "extra_google_sign_in_options";
Expand Down
15 changes: 14 additions & 1 deletion auth/src/test/java/com/firebase/ui/auth/AuthUITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void testPhoneBuilder_withValidDefaultNumberCode_expectSuccess() {
}

@Test
public void testPhoneBuilder_withBlacklistedCountryWithSameCountryCode_expectSucess() {
public void testPhoneBuilder_withBlacklistedCountryWithSameCountryCode_expectSuccess() {
new IdpConfig.PhoneBuilder()
.setDefaultNumber("+1123456789")
.setBlacklistedCountries(Arrays.asList("ca"))
Expand Down Expand Up @@ -333,6 +333,19 @@ public void testEmailBuilder_withAnonymousUpgradeAndNotForcingSameDevice_expectT
(actionCodeSettings).build();
}

@Test
public void testEmailBuilder_withSetDefaultEmail_expectSuccess() {
ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder().setUrl(URL).build();

IdpConfig config = new IdpConfig.EmailBuilder()
.setDefaultEmail(TestConstants.EMAIL)
.setActionCodeSettings(actionCodeSettings)
.build();

assertThat(config.getParams().getString(ExtraConstants.DEFAULT_EMAIL))
.isEqualTo(TestConstants.EMAIL);
}

@Test(expected = IllegalStateException.class)
public void testSignInIntentBuilder_anonymousUpgradeWithEmailLinkCrossDevice_expectThrows() {
ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder().setUrl(URL).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ public static FlowParameters getFlowParameters(Collection<String> providerIds) {

public static FlowParameters getFlowParameters(Collection<String> providerIds,
boolean enableAnonymousUpgrade) {
return getFlowParameters(providerIds, enableAnonymousUpgrade, null);
return getFlowParameters(providerIds, enableAnonymousUpgrade, null, false);
}

public static FlowParameters getFlowParameters(Collection<String> providerIds,
boolean enableAnonymousUpgrade,
AuthMethodPickerLayout customLayout) {
AuthMethodPickerLayout customLayout,
boolean hasDefaultEmail) {
List<IdpConfig> idpConfigs = new ArrayList<>();
for (String providerId : providerIds) {
switch (providerId) {
Expand All @@ -153,7 +154,13 @@ public static FlowParameters getFlowParameters(Collection<String> providerIds,
.setHandleCodeInApp(true).build()).build());
break;
case EmailAuthProvider.PROVIDER_ID:
idpConfigs.add(new IdpConfig.EmailBuilder().build());
if (hasDefaultEmail) { idpConfigs.add(new IdpConfig.EmailBuilder()
.setDefaultEmail(TestConstants.EMAIL)
.build());
} else
{
idpConfigs.add(new IdpConfig.EmailBuilder().build());
}
break;
case PhoneAuthProvider.PROVIDER_ID:
idpConfigs.add(new IdpConfig.PhoneBuilder().build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.firebase.ui.auth.testhelpers.TestHelper;
import com.firebase.ui.auth.util.ExtraConstants;
import com.firebase.ui.auth.util.data.EmailLinkPersistenceManager;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.GoogleAuthProvider;
Expand All @@ -34,6 +35,8 @@
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowActivity;

import java.util.Collections;

Expand Down Expand Up @@ -73,7 +76,7 @@ public void testOnCreate_emailLinkLinkingFlow_expectSendEmailLinkFlowStarted() {
EmailLinkPersistenceManager.getInstance().saveEmail(ApplicationProvider.getApplicationContext(),
EMAIL, TestConstants.SESSION_ID, TestConstants.UID);

EmailActivity emailActivity = createActivity(AuthUI.EMAIL_LINK_PROVIDER, true);
EmailActivity emailActivity = createActivity(AuthUI.EMAIL_LINK_PROVIDER, true, false);

EmailLinkFragment fragment = (EmailLinkFragment) emailActivity
.getSupportFragmentManager().findFragmentByTag(EmailLinkFragment.TAG);
Expand Down Expand Up @@ -141,16 +144,59 @@ public void testSignUpButton_validatesFields() {
passwordLayout.getError().toString());
}

@Test
public void testSetDefaultEmail_validField() {
EmailActivity emailActivity = createActivity(EmailAuthProvider.PROVIDER_ID, false, true);

CheckEmailFragment fragment = (CheckEmailFragment) emailActivity
.getSupportFragmentManager().findFragmentByTag(CheckEmailFragment.TAG);
assertThat(fragment).isNotNull();

TextInputEditText email = emailActivity.findViewById(R.id.email);
assertEquals(TestConstants.EMAIL, email.getText().toString());

}

@Test
public void testSetDefaultEmail_expectWelcomeBackPasswordPrompt() {
EmailActivity emailActivity = createActivity(EmailAuthProvider.PROVIDER_ID, false, true);

emailActivity.onExistingEmailUser(new User.Builder(EmailAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build());

ShadowActivity.IntentForResult nextIntent =
Shadows.shadowOf(emailActivity).getNextStartedActivityForResult();
assertEquals(WelcomeBackPasswordPrompt.class.getName(),
nextIntent.intent.getComponent().getClassName());

}

@Test
public void testSetDefaultEmail_expectRegisterEmailFragment() {
EmailActivity emailActivity = createActivity(EmailAuthProvider.PROVIDER_ID, false, true);

emailActivity.onNewUser(new User.Builder(EmailAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build());

RegisterEmailFragment registerEmailFragment = (RegisterEmailFragment) emailActivity
.getSupportFragmentManager().findFragmentByTag(RegisterEmailFragment.TAG);
assertThat(registerEmailFragment).isNotNull();
}


private EmailActivity createActivity(String providerId) {
return createActivity(providerId, false);
return createActivity(providerId, false, false);
}

private EmailActivity createActivity(String providerId, boolean emailLinkLinkingFlow) {
private EmailActivity createActivity(String providerId, boolean emailLinkLinkingFlow, boolean hasDefaultEmail) {
Intent startIntent = EmailActivity.createIntent(
ApplicationProvider.getApplicationContext(),
TestHelper.getFlowParameters(Collections.singletonList(providerId)));

if (hasDefaultEmail) {
startIntent = EmailActivity.createIntent(
ApplicationProvider.getApplicationContext(),
TestHelper.getFlowParameters(Collections.singletonList(providerId), false, null, true));
}

if (emailLinkLinkingFlow) {
startIntent.putExtra(ExtraConstants.EMAIL, EMAIL);
startIntent.putExtra(ExtraConstants.IDP_RESPONSE, buildGoogleIdpResponse());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testCustomAuthMethodPickerLayout() {
.setEmailButtonId(R.id.email_button)
.build();

AuthMethodPickerActivity authMethodPickerActivity = createActivityWithCustomLayout(providers, customLayout);
AuthMethodPickerActivity authMethodPickerActivity = createActivityWithCustomLayout(providers, customLayout, false);

Button emailButton = authMethodPickerActivity.findViewById(R.id.email_button);
emailButton.performClick();
Expand All @@ -133,7 +133,7 @@ public void testCustomAuthMethodPickerLayoutWithEmailLink() {
.setEmailButtonId(R.id.email_button)
.build();

AuthMethodPickerActivity authMethodPickerActivity = createActivityWithCustomLayout(providers, customLayout);
AuthMethodPickerActivity authMethodPickerActivity = createActivityWithCustomLayout(providers, customLayout, false);

Button emailButton = authMethodPickerActivity.findViewById(R.id.email_button);
emailButton.performClick();
Expand All @@ -146,11 +146,33 @@ public void testCustomAuthMethodPickerLayoutWithEmailLink() {
nextIntent.intent.getComponent().getClassName());
}

@Test
public void testCustomAuthMethodPickerLayoutWithDefaultEmail() {
List<String> providers = Arrays.asList(EmailAuthProvider.PROVIDER_ID);

AuthMethodPickerLayout customLayout = new AuthMethodPickerLayout
.Builder(R.layout.fui_provider_button_email)
.setEmailButtonId(R.id.email_button)
.build();

AuthMethodPickerActivity authMethodPickerActivity = createActivityWithCustomLayout(providers, customLayout, true);
Button emailButton = authMethodPickerActivity.findViewById(R.id.email_button);
emailButton.performClick();

//Expected result -> Directing users to EmailActivity
ShadowActivity.IntentForResult nextIntent =
Shadows.shadowOf(authMethodPickerActivity).getNextStartedActivityForResult();
assertEquals(
EmailActivity.class.getName(),
nextIntent.intent.getComponent().getClassName());
}

private AuthMethodPickerActivity createActivityWithCustomLayout(List<String> providers,
AuthMethodPickerLayout layout) {
AuthMethodPickerLayout layout,
boolean hasDefaultEmail) {
Intent startIntent = AuthMethodPickerActivity.createIntent(
ApplicationProvider.getApplicationContext(),
TestHelper.getFlowParameters(providers, false, layout));
TestHelper.getFlowParameters(providers, false, layout, hasDefaultEmail));

return Robolectric
.buildActivity(AuthMethodPickerActivity.class, startIntent)
Expand Down