Skip to content

Commit

Permalink
Bugfix: PublicAPI stop should not start TrackRecordingService in fore…
Browse files Browse the repository at this point in the history
…ground.

Actually, TrackRecordingService will start in foreground if needed.

Fixes #1850.
  • Loading branch information
dennisguse committed Feb 10, 2024
1 parent f7991a6 commit ee55be5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package de.dennisguse.opentracks.publicapi;

import android.content.Context;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.rule.GrantPermissionRule;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.TestUtil;
import de.dennisguse.opentracks.settings.PreferencesUtils;
import de.dennisguse.opentracks.util.IntentUtils;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class PublicApiTest {

@Rule
public GrantPermissionRule mGrantPermissionRule = TestUtil.createGrantPermissionRule();

private final Context context = ApplicationProvider.getApplicationContext();

@Test
public void StartTest() {
PreferencesUtils.setBoolean(R.string.publicapi_enabled_key, true);

context.startActivity(IntentUtils.newIntent(context, StartRecording.class));
}

@Test
public void StartStopTest() throws InterruptedException {
PreferencesUtils.setBoolean(R.string.publicapi_enabled_key, true);

context.startActivity(IntentUtils.newIntent(context, StartRecording.class));

Thread.sleep(5000);

context.startActivity(IntentUtils.newIntent(context, StopRecording.class));
}

@Test
public void StopAndWait() throws InterruptedException {
PreferencesUtils.setBoolean(R.string.publicapi_enabled_key, true);

context.startActivity(IntentUtils.newIntent(context, StopRecording.class));

Thread.sleep(10000);

//No ForegroundServiceDidNotStartInTimeException should be happening.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import de.dennisguse.opentracks.BuildConfig;

Expand Down Expand Up @@ -81,7 +80,8 @@ public void bind(@NonNull Context context) {
}

Log.i(TAG, "Binding the service.");
int flags = BuildConfig.DEBUG ? Context.BIND_DEBUG_UNBIND : 0;

int flags = Context.BIND_AUTO_CREATE + (BuildConfig.DEBUG ? Context.BIND_DEBUG_UNBIND : 0);
context.bindService(new Intent(context, TrackRecordingService.class), serviceConnection, flags);
}

Expand Down Expand Up @@ -142,6 +142,6 @@ public static void execute(Context context, Callback callback) {
new TrackRecordingServiceConnection(withUnbind)
.bind(context);

ContextCompat.startForegroundService(context, new Intent(context, TrackRecordingService.class));
// ContextCompat.startForegroundService(context, new Intent(context, TrackRecordingService.class));
}
}

0 comments on commit ee55be5

Please sign in to comment.