| Flutter iOS Embedder |

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
15 FLUTTER_ASSERT_ARC
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
41 - (void)testRestoratonViewControllerEncodeAndDecode {
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
67 - (void)testRestorationEnabledWaitsForData {
68 FlutterRestorationPlugin* restorationPlugin =
69 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
70
71 FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"get" arguments:nil];
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
86 - (void)testRestorationDisabledRespondsRightAway {
87 FlutterRestorationPlugin* restorationPlugin =
88 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:NO];
89
90 FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"get" arguments:nil];
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
100 - (void)testRespondsRightAwayWhenDataIsSet {
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;
108 FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"get" arguments:nil];
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
118 - (void)testRespondsWithNoDataWhenRestorationIsCompletedWithoutData {
119 FlutterRestorationPlugin* restorationPlugin =
120 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
121
122 FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"get" arguments:nil];
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
135 - (void)testRespondsRightAwayWithNoDataWhenRestorationIsCompleted {
136 FlutterRestorationPlugin* restorationPlugin =
137 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
138
139 [restorationPlugin markRestorationComplete];
140
141 __block id capturedResult;
142 FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"get" arguments:nil];
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
151 - (void)testReturnsDataSetByFramework {
152 FlutterRestorationPlugin* restorationPlugin =
153 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
154 [restorationPlugin markRestorationComplete];
155
156 NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding];
157 FlutterMethodCall* methodCall = [FlutterMethodCall
158 methodCallWithMethodName:@"put"
159 arguments:[FlutterStandardTypedData typedDataWithBytes:data]];
160 [restorationPlugin handleMethodCall:methodCall
161 result:^(id _Nullable result) {
162 XCTAssertNil(result);
163 }];
164 XCTAssertEqualObjects([restorationPlugin restorationData], data);
165 }
166
167 - (void)testRespondsWithDataSetByFramework {
168 FlutterRestorationPlugin* restorationPlugin =
169 [[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];
170 [restorationPlugin markRestorationComplete];
171
172 NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding];
173 FlutterMethodCall* methodCall = [FlutterMethodCall
174 methodCallWithMethodName:@"put"
175 arguments:[FlutterStandardTypedData typedDataWithBytes:data]];
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];
199 FlutterMethodCall* methodCall = [FlutterMethodCall
200 methodCallWithMethodName:@"put"
201 arguments:[FlutterStandardTypedData typedDataWithBytes:data]];
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
FlutterEngine
Definition: FlutterEngine.h:61
+[FlutterMethodCall methodCallWithMethodName:arguments:]
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
FlutterViewController
Definition: FlutterViewController.h:57
FlutterMethodChannel
Definition: FlutterChannels.h:220
FlutterRestorationPlugin.h
FlutterRestorationPlugin
Definition: FlutterRestorationPlugin.h:12
-[FlutterRestorationPlugin reset]
void reset()
Definition: FlutterRestorationPlugin.mm:75
FlutterChannels.h
+[FlutterStandardTypedData typedDataWithBytes:]
instancetype typedDataWithBytes:(NSData *data)
Definition: FlutterStandardCodec.mm:146
FlutterMacros.h
FlutterRestorationPluginTest
Definition: FlutterRestorationPluginTest.mm:21
FlutterMethodCall
Definition: FlutterCodecs.h:220
-[FlutterRestorationPlugin markRestorationComplete]
void markRestorationComplete()
Definition: FlutterRestorationPlugin.mm:66
FlutterResult
void(^ FlutterResult)(id _Nullable result)
Definition: FlutterChannels.h:194
engine
id engine
Definition: FlutterTextInputPluginTest.mm:89
FlutterViewController_Internal.h
FlutterStandardTypedData
Definition: FlutterCodecs.h:300
-[FlutterEngine run]
BOOL run()
Definition: FlutterEngine.mm:940
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13
FlutterViewController.h