Skip to content

Commit

Permalink
test onBackInvokedCallback for different SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
maRci002 committed Jan 23, 2024
1 parent 8127c91 commit acc36fd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
*/
@VisibleForTesting
public void registerOnBackInvokedCallback() {
if (Build.VERSION.SDK_INT >= 33 && onBackInvokedCallback != null) {
if (Build.VERSION.SDK_INT >= 33) {
getOnBackInvokedDispatcher()
.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback);
Expand All @@ -675,18 +675,20 @@ public void registerOnBackInvokedCallback() {
*/
@VisibleForTesting
public void unregisterOnBackInvokedCallback() {
if (Build.VERSION.SDK_INT >= 33 && onBackInvokedCallback != null) {
if (Build.VERSION.SDK_INT >= 33) {
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(onBackInvokedCallback);
hasRegisteredBackCallback = false;
}
}

final OnBackInvokedCallback onBackInvokedCallback = createOnBackInvokedCallback();
final OnBackInvokedCallback onBackInvokedCallback =
Build.VERSION.SDK_INT < 33 ? null : createOnBackInvokedCallback();

@NonNull
@TargetApi(33)
@RequiresApi(33)
private OnBackInvokedCallback createOnBackInvokedCallback() {
if (Build.VERSION.SDK_INT == 33) {
return this::onBackPressed;
} else if (Build.VERSION.SDK_INT >= 34) {
if (Build.VERSION.SDK_INT >= 34) {
return new OnBackAnimationCallback() {
@Override
public void onBackInvoked() {
Expand All @@ -710,7 +712,7 @@ public void onBackStarted(@NonNull BackEvent backEvent) {
};
}

return null;
return this::onBackPressed;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.os.Bundle;
import android.window.BackEvent;
import android.window.OnBackAnimationCallback;
import android.window.OnBackInvokedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
Expand Down Expand Up @@ -125,8 +126,47 @@ public void itUnregistersOnBackInvokedCallbackOnRelease() {
}

@Test
@TargetApi(34)
public void itHandlesOnBackAnimationCallbackAsExpected() {
@Config(maxSdk = Build.VERSION_CODES.S_V2)
public void onBackInvokedCallbackIsNullForSdk32OrLower() {
Intent intent = FlutterActivity.createDefaultIntent(ctx);
ActivityController<FlutterActivity> activityController =
Robolectric.buildActivity(FlutterActivity.class, intent);
FlutterActivity flutterActivity = activityController.get();

flutterActivity.onCreate(null);

assertNull(
"onBackInvokedCallback should be null for SDK 32 or lower",
flutterActivity.onBackInvokedCallback);
}

@Test
@Config(
sdk = Build.VERSION_CODES.TIRAMISU,
minSdk = Build.VERSION_CODES.TIRAMISU,
maxSdk = Build.VERSION_CODES.TIRAMISU)
@TargetApi(Build.VERSION_CODES.TIRAMISU)
public void onBackInvokedCallbackIsOnBackInvokedCallbackForSdk33() {
Intent intent = FlutterActivityWithReportFullyDrawn.createDefaultIntent(ctx);
ActivityController<FlutterActivityWithReportFullyDrawn> activityController =
Robolectric.buildActivity(FlutterActivityWithReportFullyDrawn.class, intent);
FlutterActivityWithReportFullyDrawn activity = spy(activityController.get());

activity.onCreate(null);

assertNotNull(
"onBackInvokedCallback should not be null for SDK 33", activity.onBackInvokedCallback);

OnBackInvokedCallback callback = activity.onBackInvokedCallback;

callback.onBackInvoked();
verify(activity, times(1)).onBackPressed();
}

@Test
@Config(sdk = Build.VERSION_CODES.UPSIDE_DOWN_CAKE, minSdk = Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
@TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void itHandlesOnBackAnimationCallbackAsExpectedForSdk34OrHigher() {
Intent intent = FlutterActivityWithReportFullyDrawn.createDefaultIntent(ctx);
ActivityController<FlutterActivityWithReportFullyDrawn> activityController =
Robolectric.buildActivity(FlutterActivityWithReportFullyDrawn.class, intent);
Expand All @@ -135,7 +175,7 @@ public void itHandlesOnBackAnimationCallbackAsExpected() {
activity.onCreate(null);

assertTrue(
"onBackInvokedCallback should be an instance of OnBackAnimationCallback",
"onBackInvokedCallback should be an instance of OnBackAnimationCallback for SDK 34 or higher",
activity.onBackInvokedCallback instanceof OnBackAnimationCallback);

OnBackAnimationCallback callback = (OnBackAnimationCallback) activity.onBackInvokedCallback;
Expand Down

0 comments on commit acc36fd

Please sign in to comment.