Skip to content

Commit

Permalink
actually get the notification permission before tests start
Browse files Browse the repository at this point in the history
  • Loading branch information
CampelloManuel committed Jan 16, 2024
1 parent 74203f1 commit 9a431d2
Showing 1 changed file with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.preference.PreferenceManager;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.espresso.intent.rule.IntentsTestRule;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.GrantPermissionRule;

import com.nononsenseapps.helpers.NnnLogger;
Expand All @@ -27,22 +29,19 @@ public class BaseTestClass {

/**
* A JUnit {@link Rule @Rule} to launch your activity under test. This replaces
* for ActivityInstrumentationTestCase2.
* Rules are executed for each test method and will run before
* any of your setup code in the @Before method.
* This will create and launch of the activity for you and also expose
* the activity under test. To get a reference to the activity you can use:
* {@link IntentsTestRule#getActivity()}
* ActivityInstrumentationTestCase2. Rules are executed for each test method and will run
* before any of your setup code in the @Before method. To get a reference to the activity
* you can use: {@link IntentsTestRule#getActivity()}
* <br/>
* NOTE: the alternative, {@link ActivityScenarioRule}, <b>DOES NOT WORK</b>
* NOTE: the newer alternative, {@link ActivityScenarioRule}, <b>DOES NOT WORK</b>
*/
@SuppressWarnings("deprecation")
@Rule
public final IntentsTestRule<ActivityMain_> mActRule =
new IntentsTestRule<>(ActivityMain_.class);
public IntentsTestRule<ActivityMain_> mActRule;


/**
* API 33 requires permission for notifications, older APIs crash if you run this
* Since API 33 we need permission for notifications
*/
@Rule
public GrantPermissionRule mNotifRule;
Expand All @@ -63,6 +62,28 @@ public void clearAppData() {
DatabaseHandler.resetDatabase(context);
}

/**
* Tries in many ways to give notification permission for OS versions that need it
*/
private void giveNotifyPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// this permission works only on API >= 33, it crashes on older versions!
mNotifRule = GrantPermissionRule.grant(Manifest.permission.POST_NOTIFICATIONS);

String command = "pm grant " +
ApplicationProvider.getApplicationContext().getPackageName() + " " +
Manifest.permission.POST_NOTIFICATIONS;

// this one is more likely to work
InstrumentationRegistry
.getInstrumentation()
.getUiAutomation()
.executeShellCommand(command);
} else {
mNotifRule = null;
}
}

/**
* Many times, on the github VM, the tests fail with RootViewWithoutFocusException,
* I think it's due to the emulator being slow. Let's launch the activity and wait
Expand All @@ -73,11 +94,13 @@ public void launchAndWait() {
// ensure that this is called BEFORE trying to start the activity
clearAppData();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// this permission works only on API >= 33, it crashes on older versions!
mNotifRule = GrantPermissionRule.grant(Manifest.permission.POST_NOTIFICATIONS);
} else
mNotifRule = null;
// first, acquire all the required permissions ...
giveNotifyPermission();

// ... then, create and run the entry point to the app
mActRule = new IntentsTestRule<>(ActivityMain_.class);
Intent launchApp = new Intent(ApplicationProvider.getApplicationContext(), ActivityMain_.class);
mActRule.launchActivity(launchApp);

try {
// it responds => we can return now
Expand Down

0 comments on commit 9a431d2

Please sign in to comment.