|
| 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/FlutterMacros.h" |
| 11 | +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h" |
| 12 | + |
| 13 | + |
| 14 | +FLUTTER_ASSERT_ARC |
| 15 | + |
| 16 | +@interface FlutterRestorationPlugin () |
| 17 | +- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result; |
| 18 | +@end |
| 19 | + |
| 20 | +@interface FlutterRestorationPluginTest : XCTestCase |
| 21 | +@end |
| 22 | + |
| 23 | +@implementation FlutterRestorationPluginTest { |
| 24 | + id restorationChannel; |
| 25 | +} |
| 26 | + |
| 27 | +- (void)setUp { |
| 28 | + [super setUp]; |
| 29 | + restorationChannel = OCMClassMock([FlutterMethodChannel class]); |
| 30 | +} |
| 31 | + |
| 32 | +- (void)tearDown { |
| 33 | + [restorationChannel stopMocking]; |
| 34 | + |
| 35 | + [super tearDown]; |
| 36 | +} |
| 37 | + |
| 38 | +#pragma mark - Tests |
| 39 | + |
| 40 | +- (void)testRestorationEnabledWaitsForData { |
| 41 | + FlutterRestorationPlugin* restorationPlugin = [[FlutterRestorationPlugin alloc] initWithChannel: restorationChannel restorationEnabled: YES]; |
| 42 | + |
| 43 | + FlutterMethodCall* methodCall =[FlutterMethodCall methodCallWithMethodName:@"get" |
| 44 | + arguments:nil]; |
| 45 | + __block id capturedResult; |
| 46 | + [restorationPlugin handleMethodCall:methodCall |
| 47 | + result:^(id _Nullable result){ |
| 48 | + capturedResult = result; |
| 49 | + }]; |
| 50 | + XCTAssertNil(capturedResult); |
| 51 | + |
| 52 | + NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding]; |
| 53 | + [restorationPlugin restorationData: data]; |
| 54 | + XCTAssertEqual([capturedResult count], 2u); |
| 55 | + XCTAssertEqual([capturedResult objectForKey: @"enabled"], @YES); |
| 56 | + XCTAssertEqual([[capturedResult objectForKey: @"data"] data], data); |
| 57 | +} |
| 58 | + |
| 59 | +- (void)testRestorationDisabledRespondsRightAway { |
| 60 | + FlutterRestorationPlugin* restorationPlugin = [[FlutterRestorationPlugin alloc] initWithChannel: restorationChannel restorationEnabled: NO]; |
| 61 | + |
| 62 | + FlutterMethodCall* methodCall =[FlutterMethodCall methodCallWithMethodName:@"get" |
| 63 | + arguments:nil]; |
| 64 | + __block id capturedResult; |
| 65 | + [restorationPlugin handleMethodCall:methodCall |
| 66 | + result:^(id _Nullable result){ |
| 67 | + capturedResult = result; |
| 68 | + }]; |
| 69 | + XCTAssertEqual([capturedResult count], 1u); |
| 70 | + XCTAssertEqual([capturedResult objectForKey: @"enabled"], @NO); |
| 71 | +} |
| 72 | + |
| 73 | +- (void)testRespondsRightAwayWhenDataIsSet { |
| 74 | + FlutterRestorationPlugin* restorationPlugin = [[FlutterRestorationPlugin alloc] initWithChannel: restorationChannel restorationEnabled: YES]; |
| 75 | + |
| 76 | + NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding]; |
| 77 | + [restorationPlugin restorationData: data]; |
| 78 | + |
| 79 | + __block id capturedResult; |
| 80 | + FlutterMethodCall* methodCall =[FlutterMethodCall methodCallWithMethodName:@"get" |
| 81 | + arguments:nil]; |
| 82 | + [restorationPlugin handleMethodCall:methodCall |
| 83 | + result:^(id _Nullable result){ |
| 84 | + capturedResult = result; |
| 85 | + }]; |
| 86 | + XCTAssertEqual([capturedResult count], 2u); |
| 87 | + XCTAssertEqual([capturedResult objectForKey: @"enabled"], @YES); |
| 88 | + XCTAssertEqual([[capturedResult objectForKey: @"data"] data], data); |
| 89 | +} |
| 90 | + |
| 91 | +- (void)testRespondsWithNoDataWhenRestorationIsCompletedWithoutData { |
| 92 | + FlutterRestorationPlugin* restorationPlugin = [[FlutterRestorationPlugin alloc] initWithChannel: restorationChannel restorationEnabled: YES]; |
| 93 | + |
| 94 | + FlutterMethodCall* methodCall =[FlutterMethodCall methodCallWithMethodName:@"get" |
| 95 | + arguments:nil]; |
| 96 | + __block id capturedResult; |
| 97 | + [restorationPlugin handleMethodCall:methodCall |
| 98 | + result:^(id _Nullable result){ |
| 99 | + capturedResult = result; |
| 100 | + }]; |
| 101 | + XCTAssertNil(capturedResult); |
| 102 | + |
| 103 | + [restorationPlugin restorationComplete]; |
| 104 | + XCTAssertEqual([capturedResult count], 1u); |
| 105 | + XCTAssertEqual([capturedResult objectForKey: @"enabled"], @YES); |
| 106 | +} |
| 107 | + |
| 108 | +- (void)testRespondsRightAwayWithNoDataWhenRestorationIsCompleted { |
| 109 | + FlutterRestorationPlugin* restorationPlugin = [[FlutterRestorationPlugin alloc] initWithChannel: restorationChannel restorationEnabled: YES]; |
| 110 | + |
| 111 | + [restorationPlugin restorationComplete]; |
| 112 | + |
| 113 | + __block id capturedResult; |
| 114 | + FlutterMethodCall* methodCall =[FlutterMethodCall methodCallWithMethodName:@"get" |
| 115 | + arguments:nil]; |
| 116 | + [restorationPlugin handleMethodCall:methodCall |
| 117 | + result:^(id _Nullable result){ |
| 118 | + capturedResult = result; |
| 119 | + }]; |
| 120 | + XCTAssertEqual([capturedResult count], 1u); |
| 121 | + XCTAssertEqual([capturedResult objectForKey: @"enabled"], @YES); |
| 122 | +} |
| 123 | + |
| 124 | +- (void)testReturnsDataSetByFramework { |
| 125 | + FlutterRestorationPlugin* restorationPlugin = [[FlutterRestorationPlugin alloc] initWithChannel: restorationChannel restorationEnabled: YES]; |
| 126 | + [restorationPlugin restorationComplete]; |
| 127 | + |
| 128 | + NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding]; |
| 129 | + FlutterMethodCall* methodCall =[FlutterMethodCall methodCallWithMethodName:@"put" |
| 130 | + arguments:[FlutterStandardTypedData typedDataWithBytes:data]]; |
| 131 | + [restorationPlugin handleMethodCall:methodCall |
| 132 | + result:^(id _Nullable result){ |
| 133 | + XCTAssertNil(result); |
| 134 | + }]; |
| 135 | + XCTAssertEqual([restorationPlugin restorationData], data); |
| 136 | +} |
| 137 | + |
| 138 | +- (void)testRespondsWithDataSetByFramework { |
| 139 | + FlutterRestorationPlugin* restorationPlugin = [[FlutterRestorationPlugin alloc] initWithChannel: restorationChannel restorationEnabled: YES]; |
| 140 | + [restorationPlugin restorationComplete]; |
| 141 | + |
| 142 | + NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding]; |
| 143 | + FlutterMethodCall* methodCall =[FlutterMethodCall methodCallWithMethodName:@"put" |
| 144 | + arguments:[FlutterStandardTypedData typedDataWithBytes:data]]; |
| 145 | + [restorationPlugin handleMethodCall:methodCall |
| 146 | + result:^(id _Nullable result){ |
| 147 | + XCTAssertNil(result); |
| 148 | + }]; |
| 149 | + XCTAssertEqual([restorationPlugin restorationData], data); |
| 150 | + |
| 151 | + __block id capturedResult; |
| 152 | + methodCall = [FlutterMethodCall methodCallWithMethodName:@"get" |
| 153 | + arguments:nil]; |
| 154 | + [restorationPlugin handleMethodCall:methodCall |
| 155 | + result:^(id _Nullable result){ |
| 156 | + capturedResult = result; |
| 157 | + }]; |
| 158 | + XCTAssertEqual([capturedResult count], 2u); |
| 159 | + XCTAssertEqual([capturedResult objectForKey: @"enabled"], @YES); |
| 160 | + XCTAssertEqual([[capturedResult objectForKey: @"data"] data], data); |
| 161 | +} |
| 162 | + |
| 163 | +- (void)testResetClearsData { |
| 164 | + FlutterRestorationPlugin* restorationPlugin = [[FlutterRestorationPlugin alloc] initWithChannel: restorationChannel restorationEnabled: YES]; |
| 165 | + [restorationPlugin restorationComplete]; |
| 166 | + |
| 167 | + NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding]; |
| 168 | + FlutterMethodCall* methodCall =[FlutterMethodCall methodCallWithMethodName:@"put" |
| 169 | + arguments:[FlutterStandardTypedData typedDataWithBytes:data]]; |
| 170 | + [restorationPlugin handleMethodCall:methodCall |
| 171 | + result:^(id _Nullable result){ |
| 172 | + XCTAssertNil(result); |
| 173 | + }]; |
| 174 | + XCTAssertEqual([restorationPlugin restorationData], data); |
| 175 | + |
| 176 | + [restorationPlugin reset]; |
| 177 | + XCTAssertNil([restorationPlugin restorationData]); |
| 178 | + |
| 179 | + |
| 180 | + __block id capturedResult; |
| 181 | + methodCall = [FlutterMethodCall methodCallWithMethodName:@"get" |
| 182 | + arguments:nil]; |
| 183 | + [restorationPlugin handleMethodCall:methodCall |
| 184 | + result:^(id _Nullable result){ |
| 185 | + capturedResult = result; |
| 186 | + }]; |
| 187 | + XCTAssertEqual([capturedResult count], 1u); |
| 188 | + XCTAssertEqual([capturedResult objectForKey: @"enabled"], @YES); |
| 189 | +} |
| 190 | + |
| 191 | +@end |
0 commit comments