PickerSaveImageToPathOperationTests.m
13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <OCMock/OCMock.h>
@import image_picker_ios;
@import image_picker_ios.Test;
@import UniformTypeIdentifiers;
@import XCTest;
@interface PickerSaveImageToPathOperationTests : XCTestCase
@end
@implementation PickerSaveImageToPathOperationTests
- (void)testSaveWebPImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"webpImage"
withExtension:@"webp"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
[self verifySavingImageWithPickerResult:result fullMetadata:YES withExtension:@"jpg"];
}
- (void)testSavePNGImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"pngImage"
withExtension:@"png"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
[self verifySavingImageWithPickerResult:result fullMetadata:YES withExtension:@"png"];
}
- (void)testSaveJPGImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"jpgImage"
withExtension:@"jpg"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
[self verifySavingImageWithPickerResult:result fullMetadata:YES withExtension:@"jpg"];
}
- (void)testSaveGIFImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"gifImage"
withExtension:@"gif"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
NSData *dataGIF = [NSData dataWithContentsOfURL:imageURL];
CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)dataGIF, nil);
size_t numberOfFrames = CGImageSourceGetCount(imageSource);
XCTestExpectation *pathExpectation = [self expectationWithDescription:@"Path was created"];
XCTestExpectation *operationExpectation =
[self expectationWithDescription:@"Operation completed"];
FLTPHPickerSaveImageToPathOperation *operation = [[FLTPHPickerSaveImageToPathOperation alloc]
initWithResult:result
maxHeight:@100
maxWidth:@100
desiredImageQuality:@100
fullMetadata:NO
savedPathBlock:^(NSString *savedPath, FlutterError *error) {
XCTAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:savedPath]);
// Ensure gif is animated.
XCTAssertEqualObjects([NSURL URLWithString:savedPath].pathExtension, @"gif");
NSData *newDataGIF = [NSData dataWithContentsOfFile:savedPath];
CGImageSourceRef newImageSource =
CGImageSourceCreateWithData((__bridge CFDataRef)newDataGIF, nil);
size_t newNumberOfFrames = CGImageSourceGetCount(newImageSource);
XCTAssertEqual(numberOfFrames, newNumberOfFrames);
[pathExpectation fulfill];
}];
operation.completionBlock = ^{
[operationExpectation fulfill];
};
[operation start];
[self waitForExpectationsWithTimeout:30 handler:nil];
XCTAssertTrue(operation.isFinished);
}
- (void)testSaveBMPImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"bmpImage"
withExtension:@"bmp"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
[self verifySavingImageWithPickerResult:result fullMetadata:YES withExtension:@"jpg"];
}
- (void)testSaveHEICImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"heicImage"
withExtension:@"heic"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
[self verifySavingImageWithPickerResult:result fullMetadata:YES withExtension:@"jpg"];
}
- (void)testSaveWithOrientation API_AVAILABLE(ios(14)) {
NSURL *imageURL =
[[NSBundle bundleForClass:[self class]] URLForResource:@"jpgImageWithRightOrientation"
withExtension:@"jpg"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
XCTestExpectation *pathExpectation = [self expectationWithDescription:@"Path was created"];
XCTestExpectation *operationExpectation =
[self expectationWithDescription:@"Operation completed"];
FLTPHPickerSaveImageToPathOperation *operation = [[FLTPHPickerSaveImageToPathOperation alloc]
initWithResult:result
maxHeight:@10
maxWidth:@10
desiredImageQuality:@100
fullMetadata:NO
savedPathBlock:^(NSString *savedPath, FlutterError *error) {
XCTAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:savedPath]);
// Ensure image retained it's orientation data.
XCTAssertEqualObjects([NSURL URLWithString:savedPath].pathExtension, @"jpg");
UIImage *image = [UIImage imageWithContentsOfFile:savedPath];
XCTAssertEqual(image.imageOrientation, UIImageOrientationRight);
XCTAssertEqual(image.size.width, 7);
XCTAssertEqual(image.size.height, 10);
[pathExpectation fulfill];
}];
operation.completionBlock = ^{
[operationExpectation fulfill];
};
[operation start];
[self waitForExpectationsWithTimeout:30 handler:nil];
XCTAssertTrue(operation.isFinished);
}
- (void)testSaveICNSImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"icnsImage"
withExtension:@"icns"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
[self verifySavingImageWithPickerResult:result fullMetadata:YES withExtension:@"jpg"];
}
- (void)testSaveICOImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"icoImage"
withExtension:@"ico"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
[self verifySavingImageWithPickerResult:result fullMetadata:YES withExtension:@"jpg"];
}
- (void)testSaveProRAWImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"proRawImage"
withExtension:@"dng"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
[self verifySavingImageWithPickerResult:result fullMetadata:YES withExtension:@"jpg"];
}
- (void)testSaveSVGImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"svgImage"
withExtension:@"svg"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
[self verifySavingImageWithPickerResult:result fullMetadata:YES withExtension:@"jpg"];
}
- (void)testSaveTIFFImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"tiffImage"
withExtension:@"tiff"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
[self verifySavingImageWithPickerResult:result fullMetadata:YES withExtension:@"jpg"];
}
- (void)testNonexistentImage API_AVAILABLE(ios(14)) {
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"bogus"
withExtension:@"png"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
XCTestExpectation *errorExpectation = [self expectationWithDescription:@"invalid source error"];
FLTPHPickerSaveImageToPathOperation *operation = [[FLTPHPickerSaveImageToPathOperation alloc]
initWithResult:result
maxHeight:@100
maxWidth:@100
desiredImageQuality:@100
fullMetadata:YES
savedPathBlock:^(NSString *savedPath, FlutterError *error) {
XCTAssertEqualObjects(error.code, @"invalid_source");
[errorExpectation fulfill];
}];
[operation start];
[self waitForExpectationsWithTimeout:30 handler:nil];
}
- (void)testFailingImageLoad API_AVAILABLE(ios(14)) {
NSError *loadDataError = [NSError errorWithDomain:@"PHPickerDomain" code:1234 userInfo:nil];
id mockItemProvider = OCMClassMock([NSItemProvider class]);
OCMStub([mockItemProvider hasItemConformingToTypeIdentifier:OCMOCK_ANY]).andReturn(YES);
[[mockItemProvider stub]
loadDataRepresentationForTypeIdentifier:OCMOCK_ANY
completionHandler:[OCMArg invokeBlockWithArgs:[NSNull null],
loadDataError, nil]];
id pickerResult = OCMClassMock([PHPickerResult class]);
OCMStub([pickerResult itemProvider]).andReturn(mockItemProvider);
XCTestExpectation *errorExpectation = [self expectationWithDescription:@"invalid image error"];
FLTPHPickerSaveImageToPathOperation *operation = [[FLTPHPickerSaveImageToPathOperation alloc]
initWithResult:pickerResult
maxHeight:@100
maxWidth:@100
desiredImageQuality:@100
fullMetadata:YES
savedPathBlock:^(NSString *savedPath, FlutterError *error) {
XCTAssertEqualObjects(error.code, @"invalid_image");
XCTAssertEqualObjects(error.message, loadDataError.localizedDescription);
XCTAssertEqualObjects(error.details, @"PHPickerDomain");
[errorExpectation fulfill];
}];
[operation start];
[self waitForExpectationsWithTimeout:30 handler:nil];
}
- (void)testSavePNGImageWithoutFullMetadata API_AVAILABLE(ios(14)) {
id photoAssetUtil = OCMClassMock([PHAsset class]);
NSURL *imageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"pngImage"
withExtension:@"png"];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithContentsOfURL:imageURL];
PHPickerResult *result = [self createPickerResultWithProvider:itemProvider];
OCMReject([photoAssetUtil fetchAssetsWithLocalIdentifiers:OCMOCK_ANY options:OCMOCK_ANY]);
[self verifySavingImageWithPickerResult:result fullMetadata:NO withExtension:@"png"];
OCMVerifyAll(photoAssetUtil);
}
/**
* Creates a mock picker result using NSItemProvider.
*
* @param itemProvider an item provider that will be used as picker result
*/
- (PHPickerResult *)createPickerResultWithProvider:(NSItemProvider *)itemProvider
API_AVAILABLE(ios(14)) {
PHPickerResult *result = OCMClassMock([PHPickerResult class]);
OCMStub([result itemProvider]).andReturn(itemProvider);
OCMStub([result assetIdentifier]).andReturn(itemProvider.registeredTypeIdentifiers.firstObject);
return result;
}
/**
* Validates a saving process of FLTPHPickerSaveImageToPathOperation.
*
* FLTPHPickerSaveImageToPathOperation is responsible for saving a picked image to the disk for
* later use. It is expected that the saving is always successful.
*
* @param result the picker result
*/
- (void)verifySavingImageWithPickerResult:(PHPickerResult *)result
fullMetadata:(BOOL)fullMetadata
withExtension:(NSString *)extension API_AVAILABLE(ios(14)) {
XCTestExpectation *pathExpectation = [self expectationWithDescription:@"Path was created"];
XCTestExpectation *operationExpectation =
[self expectationWithDescription:@"Operation completed"];
FLTPHPickerSaveImageToPathOperation *operation = [[FLTPHPickerSaveImageToPathOperation alloc]
initWithResult:result
maxHeight:@100
maxWidth:@100
desiredImageQuality:@100
fullMetadata:fullMetadata
savedPathBlock:^(NSString *savedPath, FlutterError *error) {
XCTAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:savedPath]);
XCTAssertEqualObjects([NSURL URLWithString:savedPath].pathExtension, extension);
[pathExpectation fulfill];
}];
operation.completionBlock = ^{
[operationExpectation fulfill];
};
[operation start];
[self waitForExpectationsWithTimeout:30 handler:nil];
XCTAssertTrue(operation.isFinished);
}
@end