Skip to content

Commit

Permalink
Add tests for AppSettingsDialogTest listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrique Faria committed Jul 27, 2018
1 parent 29143fe commit ffa9b74
Showing 1 changed file with 121 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package pub.devrel.easypermissions;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.widget.Button;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
Expand All @@ -27,6 +31,8 @@
import pub.devrel.easypermissions.testhelper.TestSupportFragment;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.robolectric.Shadows.shadowOf;
Expand All @@ -47,6 +53,10 @@ public class AppSettingsDialogTest {
private ActivityController<TestActivity> activityController;
private FragmentController<TestFragment> fragmentController;
private SupportFragmentController<TestSupportFragment> supportFragmentController;
@Mock
private DialogInterface.OnClickListener positiveListener;
@Mock
private DialogInterface.OnClickListener negativeListener;
@Captor
private ArgumentCaptor<Integer> integerCaptor;
@Captor
Expand All @@ -67,12 +77,12 @@ public void tearDown() {
// ------ From Activity ------

@Test
public void shouldStartExpectedSettingsDialog_whenBuildingFromActivity() {
public void shouldShowExpectedSettingsDialog_whenBuildingFromActivity() {
new AppSettingsDialog.Builder(spyActivity)
.setTitle(TITLE)
.setRationale(RATIONALE)
.setPositiveButton(POSITIVE)
.setNegativeButton(NEGATIVE)
.setTitle(android.R.string.dialog_alert_title)
.setRationale(android.R.string.unknownName)
.setPositiveButton(android.R.string.ok)
.setNegativeButton(android.R.string.cancel)
.setThemeResId(R.style.Theme_AppCompat)
.build()
.show();
Expand All @@ -88,17 +98,51 @@ public void shouldStartExpectedSettingsDialog_whenBuildingFromActivity() {
assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class);
}

// ------ From Fragment ------
@Test
public void shouldPositiveListener_whenClickingPositiveButtonFromActivity() {
AlertDialog alertDialog = new AppSettingsDialog.Builder(spyActivity)
.setTitle(TITLE)
.setRationale(RATIONALE)
.setPositiveButton(POSITIVE)
.setNegativeButton(NEGATIVE)
.setThemeResId(R.style.Theme_AppCompat)
.build()
.showDialog(positiveListener, negativeListener);
Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
positive.performClick();

verify(positiveListener, times(1))
.onClick(any(DialogInterface.class), anyInt());
}

@Test
public void shouldStartExpectedSettingsDialog_whenBuildingFromFragment() {
new AppSettingsDialog.Builder(spyFragment)
public void shouldNegativeListener_whenClickingPositiveButtonFromActivity() {
AlertDialog alertDialog = new AppSettingsDialog.Builder(spyActivity)
.setTitle(TITLE)
.setRationale(RATIONALE)
.setPositiveButton(POSITIVE)
.setNegativeButton(NEGATIVE)
.setThemeResId(R.style.Theme_AppCompat)
.build()
.showDialog(positiveListener, negativeListener);
Button positive = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
positive.performClick();

verify(negativeListener, times(1))
.onClick(any(DialogInterface.class), anyInt());
}

// ------ From Fragment ------

@Test
public void shouldShowExpectedSettingsDialog_whenBuildingFromFragment() {
new AppSettingsDialog.Builder(spyFragment)
.setTitle(android.R.string.dialog_alert_title)
.setRationale(android.R.string.unknownName)
.setPositiveButton(android.R.string.ok)
.setNegativeButton(android.R.string.cancel)
.setThemeResId(R.style.Theme_AppCompat)
.build()
.show();

verify(spyFragment, times(1))
Expand All @@ -112,10 +156,44 @@ public void shouldStartExpectedSettingsDialog_whenBuildingFromFragment() {
assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class);
}

@Test
public void shouldPositiveListener_whenClickingPositiveButtonFromFragment() {
AlertDialog alertDialog = new AppSettingsDialog.Builder(spyFragment)
.setTitle(TITLE)
.setRationale(RATIONALE)
.setPositiveButton(POSITIVE)
.setNegativeButton(NEGATIVE)
.setThemeResId(R.style.Theme_AppCompat)
.build()
.showDialog(positiveListener, negativeListener);
Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
positive.performClick();

verify(positiveListener, times(1))
.onClick(any(DialogInterface.class), anyInt());
}

@Test
public void shouldNegativeListener_whenClickingPositiveButtonFromFragment() {
AlertDialog alertDialog = new AppSettingsDialog.Builder(spyFragment)
.setTitle(TITLE)
.setRationale(RATIONALE)
.setPositiveButton(POSITIVE)
.setNegativeButton(NEGATIVE)
.setThemeResId(R.style.Theme_AppCompat)
.build()
.showDialog(positiveListener, negativeListener);
Button positive = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
positive.performClick();

verify(negativeListener, times(1))
.onClick(any(DialogInterface.class), anyInt());
}

// ------ From Support Fragment ------

@Test
public void shouldStartExpectedSettingsDialog_whenBuildingFromSupportFragment() {
public void shouldShowExpectedSettingsDialog_whenBuildingFromSupportFragment() {
new AppSettingsDialog.Builder(spySupportFragment)
.setTitle(android.R.string.dialog_alert_title)
.setRationale(android.R.string.unknownName)
Expand All @@ -136,6 +214,40 @@ public void shouldStartExpectedSettingsDialog_whenBuildingFromSupportFragment()
assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class);
}

@Test
public void shouldPositiveListener_whenClickingPositiveButtonFromSupportFragment() {
AlertDialog alertDialog = new AppSettingsDialog.Builder(spySupportFragment)
.setTitle(TITLE)
.setRationale(RATIONALE)
.setPositiveButton(POSITIVE)
.setNegativeButton(NEGATIVE)
.setThemeResId(R.style.Theme_AppCompat)
.build()
.showDialog(positiveListener, negativeListener);
Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
positive.performClick();

verify(positiveListener, times(1))
.onClick(any(DialogInterface.class), anyInt());
}

@Test
public void shouldNegativeListener_whenClickingPositiveButtonFromSupportFragment() {
AlertDialog alertDialog = new AppSettingsDialog.Builder(spySupportFragment)
.setTitle(TITLE)
.setRationale(RATIONALE)
.setPositiveButton(POSITIVE)
.setNegativeButton(NEGATIVE)
.setThemeResId(R.style.Theme_AppCompat)
.build()
.showDialog(positiveListener, negativeListener);
Button positive = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
positive.performClick();

verify(negativeListener, times(1))
.onClick(any(DialogInterface.class), anyInt());
}

private void setUpActivityAndFragment() {
activityController = Robolectric.buildActivity(TestActivity.class)
.create().start().resume();
Expand Down

0 comments on commit ffa9b74

Please sign in to comment.