From 5555b36877c7b3fb890e1f59a3cd9a4379f39bb7 Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Tue, 14 Jul 2020 11:01:25 -0700 Subject: [PATCH] Add flag to indicate whether the platform supports state restoration (flutter/engine#19717) --- .../systemchannels/RestorationChannel.java | 15 ++++++++++++--- .../RestorationChannelTest.java | 19 +++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/RestorationChannel.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/RestorationChannel.java index 5e4a57ef41..3b05cca9e8 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/RestorationChannel.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/RestorationChannel.java @@ -10,6 +10,8 @@ import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.StandardMethodCodec; +import java.util.HashMap; +import java.util.Map; /** * System channel to exchange restoration data between framework and engine. @@ -79,7 +81,7 @@ public class RestorationChannel { engineHasProvidedData = true; if (pendingFrameworkRestorationChannelRequest != null) { // If their is a pending request from the framework, answer it. - pendingFrameworkRestorationChannelRequest.success(data); + pendingFrameworkRestorationChannelRequest.success(packageData(data)); pendingFrameworkRestorationChannelRequest = null; restorationData = data; } else if (frameworkHasRequestedData) { @@ -90,7 +92,7 @@ public class RestorationChannel { // e.g. when the engine is attached to a new activity. channel.invokeMethod( "push", - data, + packageData(data), new MethodChannel.Result() { @Override public void success(Object result) { @@ -142,7 +144,7 @@ public class RestorationChannel { case "get": frameworkHasRequestedData = true; if (engineHasProvidedData || !waitForRestorationData) { - result.success(restorationData); + result.success(packageData(restorationData)); // Do not delete the restoration data on the engine side after sending it to the // framework. We may need to hand this data back to the operating system if the // framework never modifies the data (and thus doesn't send us any @@ -157,4 +159,11 @@ public class RestorationChannel { } } }; + + private Map packageData(byte[] data) { + final Map packaged = new HashMap(); + packaged.put("enabled", true); // Android supports state restoration. + packaged.put("data", data); + return packaged; + } } diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/RestorationChannelTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/RestorationChannelTest.java index c4cffca601..5068b10a33 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/RestorationChannelTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/RestorationChannelTest.java @@ -11,6 +11,8 @@ import static org.mockito.Mockito.verifyZeroInteractions; import android.annotation.TargetApi; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; +import java.util.HashMap; +import java.util.Map; import org.json.JSONException; import org.junit.Test; import org.junit.runner.RunWith; @@ -51,12 +53,15 @@ public class RestorationChannelTest { restorationChannel.setRestorationData(data); verify(rawChannel, times(0)).invokeMethod(any(), any()); - verify(result).success(data); + Map expected = new HashMap<>(); + expected.put("enabled", true); + expected.put("data", data); + verify(result).success(expected); // Next get request is answered right away. MethodChannel.Result result2 = mock(MethodChannel.Result.class); argumentCaptor.getValue().onMethodCall(new MethodCall("get", null), result2); - verify(result2).success(data); + verify(result2).success(expected); } @Test @@ -72,14 +77,20 @@ public class RestorationChannelTest { MethodChannel.Result result = mock(MethodChannel.Result.class); argumentCaptor.getValue().onMethodCall(new MethodCall("get", null), result); - verify(result).success(null); + Map expected = new HashMap<>(); + expected.put("enabled", true); + expected.put("data", null); + verify(result).success(expected); restorationChannel.setRestorationData(data); assertEquals(restorationChannel.getRestorationData(), null); ArgumentCaptor resultCapture = ArgumentCaptor.forClass(MethodChannel.Result.class); - verify(rawChannel).invokeMethod(eq("push"), eq(data), resultCapture.capture()); + Map expected2 = new HashMap<>(); + expected2.put("enabled", true); + expected2.put("data", data); + verify(rawChannel).invokeMethod(eq("push"), eq(expected2), resultCapture.capture()); resultCapture.getValue().success(null); assertEquals(restorationChannel.getRestorationData(), data); }