Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
FlutterRestorationPluginTest.mm
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterRestorationPlugin.h"
6
7#import <OCMock/OCMock.h>
8#import <XCTest/XCTest.h>
9
10#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
11#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
12#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
13#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
14
16
17@interface FlutterRestorationPlugin ()
18- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
19@end
20
21@interface FlutterRestorationPluginTest : XCTestCase
22@end
23
24@implementation FlutterRestorationPluginTest {
25 id restorationChannel;
26}
27
28- (void)setUp {
29 [super setUp];
30 restorationChannel = OCMClassMock([FlutterMethodChannel class]);
31}
32
33- (void)tearDown {
34 [restorationChannel stopMocking];
35
36 [super tearDown];
37}
38
39#pragma mark - Tests
40
42 FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test"
43 project:nil
44 allowHeadlessExecution:YES
45 restorationEnabled:YES];
46 [engine run];
47 FlutterViewController* flutterViewController =
48 [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
49 FlutterRestorationPlugin* restorationPlugin = flutterViewController.restorationPlugin;
50
51 NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding];
52 [restorationPlugin setRestorationData:data];
53
54 NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initRequiringSecureCoding:YES];
55 [flutterViewController encodeRestorableStateWithCoder:archiver];
56
57 [restorationPlugin setRestorationData:nil];
58
59 NSKeyedUnarchiver* unarchiver =
60 [[NSKeyedUnarchiver alloc] initForReadingWithData:archiver.encodedData];
61 [flutterViewController decodeRestorableStateWithCoder:unarchiver];
62
63 XCTAssert([[restorationPlugin restorationData] isEqualToData:data],
64 "Restoration state data must be equal");
65}
66
68 FlutterRestorationPlugin* restorationPlugin =
69 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
70
72 __block id capturedResult;
73 [restorationPlugin handleMethodCall:methodCall
74 result:^(id _Nullable result) {
75 capturedResult = result;
76 }];
77 XCTAssertNil(capturedResult);
78
79 NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding];
80 [restorationPlugin setRestorationData:data];
81 XCTAssertEqual([capturedResult count], 2u);
82 XCTAssertEqual([capturedResult objectForKey:@"enabled"], @YES);
83 XCTAssertEqualObjects([[capturedResult objectForKey:@"data"] data], data);
84}
85
87 FlutterRestorationPlugin* restorationPlugin =
88 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:NO];
89
91 __block id capturedResult;
92 [restorationPlugin handleMethodCall:methodCall
93 result:^(id _Nullable result) {
94 capturedResult = result;
95 }];
96 XCTAssertEqual([capturedResult count], 1u);
97 XCTAssertEqual([capturedResult objectForKey:@"enabled"], @NO);
98}
99
101 FlutterRestorationPlugin* restorationPlugin =
102 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
103
104 NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding];
105 [restorationPlugin setRestorationData:data];
106
107 __block id capturedResult;
109 [restorationPlugin handleMethodCall:methodCall
110 result:^(id _Nullable result) {
111 capturedResult = result;
112 }];
113 XCTAssertEqual([capturedResult count], 2u);
114 XCTAssertEqual([capturedResult objectForKey:@"enabled"], @YES);
115 XCTAssertEqualObjects([[capturedResult objectForKey:@"data"] data], data);
116}
117
119 FlutterRestorationPlugin* restorationPlugin =
120 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
121
123 __block id capturedResult;
124 [restorationPlugin handleMethodCall:methodCall
125 result:^(id _Nullable result) {
126 capturedResult = result;
127 }];
128 XCTAssertNil(capturedResult);
129
130 [restorationPlugin markRestorationComplete];
131 XCTAssertEqual([capturedResult count], 1u);
132 XCTAssertEqual([capturedResult objectForKey:@"enabled"], @YES);
133}
134
136 FlutterRestorationPlugin* restorationPlugin =
137 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
138
139 [restorationPlugin markRestorationComplete];
140
141 __block id capturedResult;
143 [restorationPlugin handleMethodCall:methodCall
144 result:^(id _Nullable result) {
145 capturedResult = result;
146 }];
147 XCTAssertEqual([capturedResult count], 1u);
148 XCTAssertEqual([capturedResult objectForKey:@"enabled"], @YES);
149}
150
152 FlutterRestorationPlugin* restorationPlugin =
153 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
154 [restorationPlugin markRestorationComplete];
155
156 NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding];
160 [restorationPlugin handleMethodCall:methodCall
161 result:^(id _Nullable result) {
162 XCTAssertNil(result);
163 }];
164 XCTAssertEqualObjects([restorationPlugin restorationData], data);
165}
166
168 FlutterRestorationPlugin* restorationPlugin =
169 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
170 [restorationPlugin markRestorationComplete];
171
172 NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding];
176 [restorationPlugin handleMethodCall:methodCall
177 result:^(id _Nullable result) {
178 XCTAssertNil(result);
179 }];
180 XCTAssertEqualObjects([restorationPlugin restorationData], data);
181
182 __block id capturedResult;
183 methodCall = [FlutterMethodCall methodCallWithMethodName:@"get" arguments:nil];
184 [restorationPlugin handleMethodCall:methodCall
185 result:^(id _Nullable result) {
186 capturedResult = result;
187 }];
188 XCTAssertEqual([capturedResult count], 2u);
189 XCTAssertEqual([capturedResult objectForKey:@"enabled"], @YES);
190 XCTAssertEqualObjects([[capturedResult objectForKey:@"data"] data], data);
191}
192
193- (void)testResetClearsData {
194 FlutterRestorationPlugin* restorationPlugin =
195 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
196 [restorationPlugin markRestorationComplete];
197
198 NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding];
202 [restorationPlugin handleMethodCall:methodCall
203 result:^(id _Nullable result) {
204 XCTAssertNil(result);
205 }];
206 XCTAssertEqualObjects([restorationPlugin restorationData], data);
207
208 [restorationPlugin reset];
209 XCTAssertNil([restorationPlugin restorationData]);
210
211 __block id capturedResult;
212 methodCall = [FlutterMethodCall methodCallWithMethodName:@"get" arguments:nil];
213 [restorationPlugin handleMethodCall:methodCall
214 result:^(id _Nullable result) {
215 capturedResult = result;
216 }];
217 XCTAssertEqual([capturedResult count], 1u);
218 XCTAssertEqual([capturedResult objectForKey:@"enabled"], @YES);
219}
220
221@end
void(^ FlutterResult)(id _Nullable result)
int count
FlutterEngine engine
Definition main.cc:68
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
void handleMethodCall:result:(FlutterMethodCall *call, [result] FlutterResult result)
instancetype typedDataWithBytes:(NSData *data)
void decodeRestorableStateWithCoder:(NSCoder *coder)
FlutterRestorationPlugin * restorationPlugin()
void encodeRestorableStateWithCoder:(NSCoder *coder)