Add flag to indicate whether the platform supports state restoration (flutter/engine#19717)
This commit is contained in:
committed by
GitHub
parent
c6313931cb
commit
5555b36877
@@ -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<String, Object> packageData(byte[] data) {
|
||||
final Map<String, Object> packaged = new HashMap<String, Object>();
|
||||
packaged.put("enabled", true); // Android supports state restoration.
|
||||
packaged.put("data", data);
|
||||
return packaged;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String, Object> 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<String, Object> expected = new HashMap<>();
|
||||
expected.put("enabled", true);
|
||||
expected.put("data", null);
|
||||
verify(result).success(expected);
|
||||
|
||||
restorationChannel.setRestorationData(data);
|
||||
assertEquals(restorationChannel.getRestorationData(), null);
|
||||
|
||||
ArgumentCaptor<MethodChannel.Result> resultCapture =
|
||||
ArgumentCaptor.forClass(MethodChannel.Result.class);
|
||||
verify(rawChannel).invokeMethod(eq("push"), eq(data), resultCapture.capture());
|
||||
Map<String, Object> 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user