Skip to content

Commit

Permalink
test: use new Robolectric Looper
Browse files Browse the repository at this point in the history
  • Loading branch information
thatfiredev committed Sep 2, 2024
1 parent 0f161d6 commit 8f2e69d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.firebase.ui.auth.ui.email;

import android.content.Intent;
import android.os.Looper;
import android.widget.Button;

import com.firebase.ui.auth.AuthUI;
Expand All @@ -36,6 +37,7 @@
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.LooperMode;
import org.robolectric.shadows.ShadowActivity;

import java.util.Collections;
Expand All @@ -44,8 +46,10 @@

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.robolectric.Shadows.shadowOf;

@RunWith(RobolectricTestRunner.class)
@LooperMode(LooperMode.Mode.PAUSED)
public class EmailActivityTest {

private static final String EMAIL = "email";
Expand Down Expand Up @@ -109,15 +113,18 @@ public void testOnClickResendEmail_expectSendEmailLinkFlowStarted() {

emailActivity.onClickResendEmail(EMAIL);

shadowOf(Looper.getMainLooper()).idle();

EmailLinkFragment fragment = (EmailLinkFragment) emailActivity
.getSupportFragmentManager().findFragmentByTag(EmailLinkFragment.TAG);

assertThat(fragment).isNotNull();
}


@Test
public void testSignUpButton_validatesFields() {

shadowOf(Looper.getMainLooper()).idle();
EmailActivity emailActivity = createActivity(EmailAuthProvider.PROVIDER_ID);

// Trigger RegisterEmailFragment (bypass check email)
Expand Down Expand Up @@ -164,7 +171,7 @@ public void testSetDefaultEmail_expectWelcomeBackPasswordPrompt() {
emailActivity.onExistingEmailUser(new User.Builder(EmailAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build());

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

Expand All @@ -176,8 +183,11 @@ public void testSetDefaultEmail_expectRegisterEmailFragment() {

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

shadowOf(Looper.getMainLooper()).idle();

RegisterEmailFragment registerEmailFragment = (RegisterEmailFragment) emailActivity
.getSupportFragmentManager().findFragmentByTag(RegisterEmailFragment.TAG);

assertThat(registerEmailFragment).isNotNull();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.firebase.ui.auth.viewmodel;

import android.app.Application;
import android.os.Looper;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.ErrorCodes;
Expand Down Expand Up @@ -41,6 +42,7 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.LooperMode;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -55,11 +57,13 @@
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

/**
* Unit tests for {@link EmailLinkSignInHandler}.
*/
@RunWith(RobolectricTestRunner.class)
@LooperMode(LooperMode.Mode.PAUSED)
public class EmailLinkSignInHandlerTest {

private static final String EMAIL_LINK =
Expand Down Expand Up @@ -402,6 +406,7 @@ public void testStartSignIn_linkingFlow_expectSuccessfulLink() {

mHandler.startSignIn();


// Validate regular sign in
ArgumentCaptor<EmailAuthCredential> credentialCaptor
= ArgumentCaptor.forClass(EmailAuthCredential.class);
Expand All @@ -421,6 +426,8 @@ public void testStartSignIn_linkingFlow_expectSuccessfulLink() {
assertThat(mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext()))
.isNull();

shadowOf(Looper.getMainLooper()).idle();

// Validate IdpResponse
ArgumentCaptor<Resource<IdpResponse>> captor =
ArgumentCaptor.forClass(Resource.class);
Expand Down Expand Up @@ -467,6 +474,8 @@ public void testStartSignIn_linkingFlow_expectUserCollisionException() {

mHandler.startSignIn();

shadowOf(Looper.getMainLooper()).idle();

ArgumentCaptor<Resource<IdpResponse>> captor =
ArgumentCaptor.forClass(Resource.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.firebase.ui.auth.viewmodel;

import android.app.Application;
import android.os.Looper;

import com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException;
import com.firebase.ui.auth.IdpResponse;
Expand Down Expand Up @@ -32,6 +33,7 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.LooperMode;

import java.util.Collections;

Expand All @@ -44,6 +46,7 @@
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

/**
* Unit tests for {@link LinkingSocialProviderResponseHandler}.
Expand All @@ -55,6 +58,7 @@
* is anonymous, a triple linking case occurs.
*/
@RunWith(RobolectricTestRunner.class)
@LooperMode(LooperMode.Mode.PAUSED)
public class LinkingSocialProviderResponseHandlerTest {

private static final String MICROSOFT_PROVIDER = "microsoft.com";
Expand Down Expand Up @@ -94,6 +98,7 @@ public void testSignIn_withSameIdp_expectSuccess() {
.thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));

mHandler.startSignIn(response);
shadowOf(Looper.getMainLooper()).idle();

verify(mMockAuth).signInWithCredential(any(GoogleAuthCredential.class));

Expand Down Expand Up @@ -173,6 +178,7 @@ public void testSignIn_withDifferentIdp_expectSuccess() {
null));

mHandler.startSignIn(response);
shadowOf(Looper.getMainLooper()).idle();

verify(mMockAuth).signInWithCredential(any(GoogleAuthCredential.class));
verify(FakeAuthResult.INSTANCE.getUser()).linkWithCredential(facebookAuthCredential);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.app.Application;
import android.os.Looper;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.ErrorCodes;
Expand Down Expand Up @@ -40,6 +41,7 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.LooperMode;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -53,11 +55,13 @@
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

/**
* Unit tests for {@link SmartLockHandler}.
*/
@RunWith(RobolectricTestRunner.class)
@LooperMode(LooperMode.Mode.PAUSED)
public class SocialProviderResponseHandlerTest {
@Mock FirebaseAuth mMockAuth;
@Mock FirebaseUser mUser;
Expand Down Expand Up @@ -89,6 +93,7 @@ public void testSignInIdp_success() {
.build();

mHandler.startSignIn(response);
shadowOf(Looper.getMainLooper()).idle();

verify(mMockAuth).signInWithCredential(any(AuthCredential.class));

Expand Down Expand Up @@ -134,6 +139,7 @@ public void testSignInIdp_disabled() {
.setToken(TestConstants.TOKEN)
.build();
mHandler.startSignIn(response);
shadowOf(Looper.getMainLooper()).idle();

verify(mResultObserver).onChanged(
argThat(ResourceMatchers.isFailureWithCode(ErrorCodes.ERROR_USER_DISABLED)));
Expand All @@ -158,6 +164,8 @@ public void testSignInIdp_resolution() {

mHandler.startSignIn(response);

shadowOf(Looper.getMainLooper()).idle();

verify(mMockAuth).signInWithCredential(any(AuthCredential.class));
verify(mMockAuth).fetchSignInMethodsForEmail(any(String.class));

Expand Down Expand Up @@ -194,6 +202,7 @@ public void testSignInIdp_anonymousUserUpgradeEnabledAndNewUser_expectSuccess()
.build();

mHandler.startSignIn(response);
shadowOf(Looper.getMainLooper()).idle();

verify(mMockAuth.getCurrentUser()).linkWithCredential(any(AuthCredential.class));

Expand Down Expand Up @@ -227,6 +236,7 @@ public void testSignInIdp_anonymousUserUpgradeEnabledAndExistingUserWithSameIdp_
.build();

mHandler.startSignIn(response);
shadowOf(Looper.getMainLooper()).idle();

verify(mMockAuth.getCurrentUser()).linkWithCredential(any(AuthCredential.class));

Expand Down Expand Up @@ -266,6 +276,7 @@ public void testSignInIdp_anonymousUserUpgradeEnabledAndExistingIdpUserWithDiffe
.build();

mHandler.startSignIn(response);
shadowOf(Looper.getMainLooper()).idle();

verify(mMockAuth.getCurrentUser()).linkWithCredential(any(AuthCredential.class));

Expand Down Expand Up @@ -311,6 +322,8 @@ public void testSignInIdp_anonymousUserUpgradeEnabledAndExistingPasswordUserWith

mHandler.startSignIn(response);

shadowOf(Looper.getMainLooper()).idle();

verify(mMockAuth.getCurrentUser()).linkWithCredential(any(AuthCredential.class));

InOrder inOrder = inOrder(mResultObserver);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.firebase.ui.auth.viewmodel;

import android.app.Application;
import android.os.Looper;

import com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException;
import com.firebase.ui.auth.IdpResponse;
Expand Down Expand Up @@ -33,6 +34,7 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.LooperMode;

import java.util.Collections;

Expand All @@ -44,11 +46,13 @@
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

/**
* Unit tests for {@link WelcomeBackPasswordHandler}.
*/
@RunWith(RobolectricTestRunner.class)
@LooperMode(LooperMode.Mode.PAUSED)
public class WelcomeBackPasswordHandlerTest {

@Mock FirebaseAuth mMockAuth;
Expand Down Expand Up @@ -103,6 +107,7 @@ public void testSignIn_linksIdpCredential() {

// Kick off the sign in flow
mHandler.startSignIn(TestConstants.EMAIL, TestConstants.PASSWORD, response, credential);
shadowOf(Looper.getMainLooper()).idle();

// Verify that we get a loading event
verify(mResponseObserver).onChanged(argThat(ResourceMatchers.isLoading()));
Expand All @@ -128,6 +133,7 @@ public void testSignIn_propagatesFailure() {

// Kick off the sign in flow
mHandler.startSignIn(TestConstants.EMAIL, TestConstants.PASSWORD, null, null);
shadowOf(Looper.getMainLooper()).idle();

// Verify that we get a loading event
verify(mResponseObserver).onChanged(argThat(ResourceMatchers.isLoading()));
Expand Down

0 comments on commit 8f2e69d

Please sign in to comment.