Skip to content

Commit

Permalink
Test for a case when FragmentManager is destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvavra committed Mar 2, 2022
1 parent f65ada7 commit 1dfebb0
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions library/src/test/java/com/okta/oidc/OktaResultFragmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@

package com.okta.oidc;

import static android.app.Activity.RESULT_OK;
import static com.okta.oidc.AuthenticationResultHandler.handler;
import static com.okta.oidc.OktaAuthenticationActivity.EXTRA_EXCEPTION;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.test.platform.app.InstrumentationRegistry;

import com.okta.oidc.net.request.ProviderConfiguration;
Expand All @@ -38,11 +48,6 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static android.app.Activity.RESULT_OK;
import static com.okta.oidc.AuthenticationResultHandler.handler;
import static com.okta.oidc.OktaAuthenticationActivity.EXTRA_EXCEPTION;
import static org.mockito.Mockito.verify;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = 27)
public class OktaResultFragmentTest {
Expand Down Expand Up @@ -134,6 +139,29 @@ public void handleAuthorizationResponseLogoutSuccess() throws AuthorizationExcep
assert (resultTypeCapture.getValue() == AuthenticationResultHandler.ResultType.SIGN_OUT);
}

@Test
public void handleAuthorizationResponseLogoutSuccessWhenFragmentManagerIsDestroyed() throws AuthorizationException {
FragmentManager mockFragmentManager = mock(FragmentManager.class);
FragmentActivity spyActivity = spy(mActivity);
when(spyActivity.getSupportFragmentManager()).thenReturn(mockFragmentManager);
when(mockFragmentManager.isDestroyed()).thenReturn(true);

OktaResultFragment.addLogoutFragment(TestValues.getAuthorizeRequest(mConfig, null), mCustomTabOptions, mActivity, new String[]{});
handler().setAuthenticationListener(listener);
Intent intent = new Intent();
intent.setData(Uri.parse("com.okta.test:/logout?state=" + CUSTOM_STATE));

getOktaResultFragment(mActivity).onActivityResult(OktaResultFragment.REQUEST_CODE_SIGN_OUT, RESULT_OK, intent);

ArgumentCaptor<AuthenticationResultHandler.StateResult> resultCapture = ArgumentCaptor.forClass(AuthenticationResultHandler.StateResult.class);
ArgumentCaptor<AuthenticationResultHandler.ResultType> resultTypeCapture = ArgumentCaptor.forClass(AuthenticationResultHandler.ResultType.class);
verify(listener).postResult(resultCapture.capture(), resultTypeCapture.capture());

assert (getOktaResultFragment(mActivity) == null);
assert (resultCapture.getValue().getStatus() == AuthenticationResultHandler.Status.LOGGED_OUT);
assert (resultTypeCapture.getValue() == AuthenticationResultHandler.ResultType.SIGN_OUT);
}

@Test
public void handleAuthorizationResponseLogoutFailed() throws AuthorizationException {
OktaResultFragment.addLogoutFragment(TestValues.getAuthorizeRequest(mConfig, null), mCustomTabOptions, mActivity, new String[]{});
Expand Down

0 comments on commit 1dfebb0

Please sign in to comment.