forked from firka/flutter
[iOS] Fixes state restoration not work (flutter/engine#31511)
This commit is contained in:
@@ -9,6 +9,8 @@
|
||||
|
||||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
|
||||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
|
||||
|
||||
FLUTTER_ASSERT_ARC
|
||||
|
||||
@@ -36,6 +38,32 @@ FLUTTER_ASSERT_ARC
|
||||
|
||||
#pragma mark - Tests
|
||||
|
||||
- (void)testRestoratonViewControllerEncodeAndDecode {
|
||||
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test"
|
||||
project:nil
|
||||
allowHeadlessExecution:YES
|
||||
restorationEnabled:YES];
|
||||
[engine run];
|
||||
FlutterViewController* flutterViewController =
|
||||
[[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
|
||||
FlutterRestorationPlugin* restorationPlugin = flutterViewController.restorationPlugin;
|
||||
|
||||
NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding];
|
||||
[restorationPlugin setRestorationData:data];
|
||||
|
||||
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initRequiringSecureCoding:YES];
|
||||
[flutterViewController encodeRestorableStateWithCoder:archiver];
|
||||
|
||||
[restorationPlugin setRestorationData:nil];
|
||||
|
||||
NSKeyedUnarchiver* unarchiver =
|
||||
[[NSKeyedUnarchiver alloc] initForReadingWithData:archiver.encodedData];
|
||||
[flutterViewController decodeRestorableStateWithCoder:unarchiver];
|
||||
|
||||
XCTAssert([[restorationPlugin restorationData] isEqualToData:data],
|
||||
"Restoration state data must be equal");
|
||||
}
|
||||
|
||||
- (void)testRestorationEnabledWaitsForData {
|
||||
FlutterRestorationPlugin* restorationPlugin =
|
||||
[[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
|
||||
|
||||
@@ -32,6 +32,8 @@
|
||||
static constexpr int kMicrosecondsPerSecond = 1000 * 1000;
|
||||
static constexpr CGFloat kScrollViewContentSize = 2.0;
|
||||
|
||||
static NSString* const kFlutterRestorationStateAppData = @"FlutterRestorationStateAppData";
|
||||
|
||||
NSNotificationName const FlutterSemanticsUpdateNotification = @"FlutterSemanticsUpdate";
|
||||
NSNotificationName const FlutterViewControllerWillDealloc = @"FlutterViewControllerWillDealloc";
|
||||
NSNotificationName const FlutterViewControllerHideHomeIndicator =
|
||||
@@ -1811,12 +1813,17 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
|
||||
|
||||
- (void)encodeRestorableStateWithCoder:(NSCoder*)coder {
|
||||
NSData* restorationData = [[_engine.get() restorationPlugin] restorationData];
|
||||
[coder encodeDataObject:restorationData];
|
||||
[coder encodeBytes:(const unsigned char*)restorationData.bytes
|
||||
length:restorationData.length
|
||||
forKey:kFlutterRestorationStateAppData];
|
||||
[super encodeRestorableStateWithCoder:coder];
|
||||
}
|
||||
|
||||
- (void)decodeRestorableStateWithCoder:(NSCoder*)coder {
|
||||
NSData* restorationData = [coder decodeDataObject];
|
||||
NSUInteger restorationDataLength;
|
||||
const unsigned char* restorationBytes = [coder decodeBytesForKey:kFlutterRestorationStateAppData
|
||||
returnedLength:&restorationDataLength];
|
||||
NSData* restorationData = [NSData dataWithBytes:restorationBytes length:restorationDataLength];
|
||||
[[_engine.get() restorationPlugin] setRestorationData:restorationData];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user