Skip to content

Commit

Permalink
Fixed expected intents in tests when permission is not granted
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed May 9, 2019
1 parent 3eddbd8 commit 64dc634
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.io.File;

import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -65,6 +64,6 @@ public void buttonsShouldLaunchCorrectIntents() {
public void buttonsShouldNotLaunchIntentsWhenPermissionsDenied() {
stubAllRuntimePermissionsGranted(false);

assertNull(getIntentLaunchedByClick(R.id.capture_image));
assertIntentNotStarted(activity, getIntentLaunchedByClick(R.id.capture_image));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.io.File;

import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -101,6 +100,6 @@ public void buttonsShouldLaunchCorrectIntents() {
public void buttonsShouldNotLaunchIntentsWhenPermissionsDenied() {
stubAllRuntimePermissionsGranted(false);

assertNull(getIntentLaunchedByClick(R.id.capture_audio));
assertIntentNotStarted(activity, getIntentLaunchedByClick(R.id.capture_audio));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import org.odk.collect.android.activities.ScannerWithFlashlightActivity;
import org.odk.collect.android.widgets.base.BinaryWidgetTest;

import static org.junit.Assert.assertNull;

/**
* @author James Knight
*/
Expand Down Expand Up @@ -62,6 +60,6 @@ public void buttonsShouldLaunchCorrectIntents() {
public void buttonsShouldNotLaunchIntentsWhenPermissionsDenied() {
stubAllRuntimePermissionsGranted(false);

assertNull(getIntentLaunchedByClick(R.id.simple_button));
assertIntentNotStarted(activity, getIntentLaunchedByClick(R.id.simple_button));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.odk.collect.android.widgets.base.BinaryWidgetTest;
import org.robolectric.annotation.Config;

import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -106,7 +105,7 @@ public void buttonsShouldLaunchCorrectIntents() {
public void buttonsShouldNotLaunchIntentsWhenPermissionsDenied() {
stubAllRuntimePermissionsGranted(false);

assertNull(getIntentLaunchedByClick(R.id.get_point));
assertNull(getIntentLaunchedByClick(R.id.get_location));
assertIntentNotStarted(activity, getIntentLaunchedByClick(R.id.get_point));
assertIntentNotStarted(activity, getIntentLaunchedByClick(R.id.get_location));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -125,6 +124,6 @@ public void buttonsShouldLaunchCorrectIntents() {
public void buttonsShouldNotLaunchIntentsWhenPermissionsDenied() {
stubAllRuntimePermissionsGranted(false);

assertNull(getIntentLaunchedByClick(R.id.simple_button));
assertIntentNotStarted(activity, getIntentLaunchedByClick(R.id.simple_button));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -124,6 +123,6 @@ public void buttonsShouldLaunchCorrectIntents() {
public void buttonsShouldNotLaunchIntentsWhenPermissionsDenied() {
stubAllRuntimePermissionsGranted(false);

assertNull(getIntentLaunchedByClick(R.id.simple_button));
assertIntentNotStarted(activity, getIntentLaunchedByClick(R.id.simple_button));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.io.File;

import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -75,6 +74,6 @@ public void buttonsShouldLaunchCorrectIntents() {
public void buttonsShouldNotLaunchIntentsWhenPermissionsDenied() {
stubAllRuntimePermissionsGranted(false);

assertNull(getIntentLaunchedByClick(R.id.capture_image));
assertIntentNotStarted(activity, getIntentLaunchedByClick(R.id.capture_image));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.File;

import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -118,6 +117,6 @@ public void buttonsShouldLaunchCorrectIntents() {
public void buttonsShouldNotLaunchIntentsWhenPermissionsDenied() {
stubAllRuntimePermissionsGranted(false);

assertNull(getIntentLaunchedByClick(R.id.capture_video));
assertIntentNotStarted(activity, getIntentLaunchedByClick(R.id.capture_video));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import android.content.Context;
import android.content.Intent;

import com.karumi.dexter.DexterActivity;

import org.javarosa.core.model.data.IAnswerData;
import org.odk.collect.android.fakes.FakePermissionUtils;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.interfaces.ButtonWidget;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.robolectric.Shadows.shadowOf;


Expand Down Expand Up @@ -54,4 +57,11 @@ protected void assertTypeEquals(String type, Intent intent) {
protected void assertExtraEquals(String key, Object value, Intent intent) {
assertEquals(intent.getExtras().get(key), value);
}

// After upgrading gradle and some dependencies if an intent can't be started because of not granted
// permissions the null value or DexterActivity is returned. It works randomly and depends on the
// order of tests but both results are ok.
protected void assertIntentNotStarted(Context context, Intent intent) {
assertTrue(intent == null || new ComponentName(context, DexterActivity.class).equals(intent.getComponent()));
}
}

0 comments on commit 64dc634

Please sign in to comment.