Try to kill playback service but do not fail if it does not stop
Android has no reliable way to stop a service instantly. Calling stopSelf marks allows the system to destroy the service but the actual call to onDestroy takes until the next GC of the system, which we can not influence. Try to wait for the service at least a bit.
This commit is contained in:
parent
e2aa83f047
commit
44aa0a3239
|
@ -1,6 +1,7 @@
|
|||
package de.test.antennapod;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import androidx.annotation.IdRes;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.test.InstrumentationRegistry;
|
||||
|
@ -15,11 +16,15 @@ import androidx.test.espresso.util.TreeIterables;
|
|||
import android.view.View;
|
||||
import de.danoeh.antennapod.R;
|
||||
import de.danoeh.antennapod.activity.MainActivity;
|
||||
import de.danoeh.antennapod.core.service.playback.PlaybackService;
|
||||
import de.danoeh.antennapod.core.storage.PodDBAdapter;
|
||||
import de.danoeh.antennapod.dialog.RatingDialog;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.core.ConditionTimeoutException;
|
||||
import org.hamcrest.Matcher;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import static androidx.test.espresso.Espresso.onView;
|
||||
|
@ -156,4 +161,19 @@ public class EspressoTestUtils {
|
|||
public static ViewInteraction onDrawerItem(Matcher<View> viewMatcher) {
|
||||
return onView(allOf(viewMatcher, withId(R.id.txtvTitle)));
|
||||
}
|
||||
|
||||
public static void tryKillPlaybackService() {
|
||||
Context context = InstrumentationRegistry.getTargetContext();
|
||||
context.stopService(new Intent(context, PlaybackService.class));
|
||||
try {
|
||||
// Android has no reliable way to stop a service instantly.
|
||||
// Calling stopSelf marks allows the system to destroy the service but the actual call
|
||||
// to onDestroy takes until the next GC of the system, which we can not influence.
|
||||
// Try to wait for the service at least a bit.
|
||||
Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> !PlaybackService.isRunning);
|
||||
} catch (ConditionTimeoutException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().waitForIdleSync();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.view.KeyEvent;
|
||||
import androidx.test.filters.LargeTest;
|
||||
import androidx.test.platform.app.InstrumentationRegistry;
|
||||
import androidx.test.rule.ActivityTestRule;
|
||||
|
@ -11,6 +12,7 @@ import de.danoeh.antennapod.R;
|
|||
import de.danoeh.antennapod.activity.MainActivity;
|
||||
import de.danoeh.antennapod.core.feed.FeedItem;
|
||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||
import de.danoeh.antennapod.core.receiver.MediaButtonReceiver;
|
||||
import de.danoeh.antennapod.core.service.playback.PlaybackService;
|
||||
import de.danoeh.antennapod.core.service.playback.PlayerStatus;
|
||||
import de.danoeh.antennapod.core.storage.DBReader;
|
||||
|
@ -87,10 +89,7 @@ public class PlaybackTest {
|
|||
@After
|
||||
public void tearDown() throws Exception {
|
||||
activityTestRule.finishActivity();
|
||||
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
|
||||
context.stopService(new Intent(context, PlaybackService.class));
|
||||
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> !PlaybackService.isRunning);
|
||||
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
|
||||
EspressoTestUtils.tryKillPlaybackService();
|
||||
uiTestUtils.tearDown();
|
||||
}
|
||||
|
||||
|
|
|
@ -60,9 +60,7 @@ public class AutoDownloadTest {
|
|||
@After
|
||||
public void tearDown() throws Exception {
|
||||
ClientConfig.dbTasksCallbacks = dbTasksCallbacksOrig;
|
||||
context.stopService(new Intent(context, PlaybackService.class));
|
||||
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> !PlaybackService.isRunning);
|
||||
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
|
||||
EspressoTestUtils.tryKillPlaybackService();
|
||||
stubFeedsServer.tearDown();
|
||||
}
|
||||
|
||||
|
|
|
@ -68,9 +68,7 @@ public class SpeedChangeTest {
|
|||
UserPreferences.setPlaybackSpeedArray(new String[] {"1.00", "2.00", "3.00"});
|
||||
availableSpeeds = UserPreferences.getPlaybackSpeedArray();
|
||||
|
||||
context.sendBroadcast(new Intent(PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
|
||||
Awaitility.await().until(() -> !PlaybackService.isRunning);
|
||||
|
||||
EspressoTestUtils.tryKillPlaybackService();
|
||||
activityRule.launchActivity(new Intent());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue