|
6 | 6 |
|
7 | 7 | @import image_picker_ios;
|
8 | 8 | @import image_picker_ios.Test;
|
| 9 | +@import UniformTypeIdentifiers; |
9 | 10 | @import XCTest;
|
| 11 | + |
10 | 12 | #import <OCMock/OCMock.h>
|
11 | 13 |
|
12 | 14 | @interface MockViewController : UIViewController
|
@@ -269,37 +271,130 @@ - (void)testViewController {
|
269 | 271 | - (void)testPluginMultiImagePathHasNullItem {
|
270 | 272 | FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc] init];
|
271 | 273 |
|
272 |
| - dispatch_semaphore_t resultSemaphore = dispatch_semaphore_create(0); |
273 |
| - __block FlutterError *pickImageResult = nil; |
| 274 | + XCTestExpectation *resultExpectation = [self expectationWithDescription:@"result"]; |
274 | 275 | plugin.callContext = [[FLTImagePickerMethodCallContext alloc]
|
275 | 276 | initWithResult:^(NSArray<NSString *> *_Nullable result, FlutterError *_Nullable error) {
|
276 |
| - pickImageResult = error; |
277 |
| - dispatch_semaphore_signal(resultSemaphore); |
| 277 | + XCTAssertEqualObjects(error.code, @"create_error"); |
| 278 | + [resultExpectation fulfill]; |
278 | 279 | }];
|
279 | 280 | [plugin sendCallResultWithSavedPathList:@[ [NSNull null] ]];
|
280 | 281 |
|
281 |
| - dispatch_semaphore_wait(resultSemaphore, DISPATCH_TIME_FOREVER); |
282 |
| - |
283 |
| - XCTAssertEqualObjects(pickImageResult.code, @"create_error"); |
| 282 | + [self waitForExpectationsWithTimeout:30 handler:nil]; |
284 | 283 | }
|
285 | 284 |
|
286 | 285 | - (void)testPluginMultiImagePathHasItem {
|
287 | 286 | FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc] init];
|
288 | 287 | NSArray *pathList = @[ @"test" ];
|
289 | 288 |
|
290 |
| - dispatch_semaphore_t resultSemaphore = dispatch_semaphore_create(0); |
291 |
| - __block id pickImageResult = nil; |
| 289 | + XCTestExpectation *resultExpectation = [self expectationWithDescription:@"result"]; |
292 | 290 |
|
293 | 291 | plugin.callContext = [[FLTImagePickerMethodCallContext alloc]
|
294 | 292 | initWithResult:^(NSArray<NSString *> *_Nullable result, FlutterError *_Nullable error) {
|
295 |
| - pickImageResult = result; |
296 |
| - dispatch_semaphore_signal(resultSemaphore); |
| 293 | + XCTAssertEqualObjects(result, pathList); |
| 294 | + [resultExpectation fulfill]; |
297 | 295 | }];
|
298 | 296 | [plugin sendCallResultWithSavedPathList:pathList];
|
299 | 297 |
|
300 |
| - dispatch_semaphore_wait(resultSemaphore, DISPATCH_TIME_FOREVER); |
| 298 | + [self waitForExpectationsWithTimeout:30 handler:nil]; |
| 299 | +} |
| 300 | + |
| 301 | +- (void)testSendsImageInvalidSourceError API_AVAILABLE(ios(14)) { |
| 302 | + id mockPickerViewController = OCMClassMock([PHPickerViewController class]); |
| 303 | + |
| 304 | + id mockItemProvider = OCMClassMock([NSItemProvider class]); |
| 305 | + // Does not conform to image, invalid source. |
| 306 | + OCMStub([mockItemProvider hasItemConformingToTypeIdentifier:OCMOCK_ANY]).andReturn(NO); |
| 307 | + |
| 308 | + PHPickerResult *failResult1 = OCMClassMock([PHPickerResult class]); |
| 309 | + OCMStub([failResult1 itemProvider]).andReturn(mockItemProvider); |
| 310 | + |
| 311 | + PHPickerResult *failResult2 = OCMClassMock([PHPickerResult class]); |
| 312 | + OCMStub([failResult2 itemProvider]).andReturn(mockItemProvider); |
| 313 | + |
| 314 | + FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc] init]; |
| 315 | + |
| 316 | + XCTestExpectation *resultExpectation = [self expectationWithDescription:@"result"]; |
| 317 | + |
| 318 | + plugin.callContext = [[FLTImagePickerMethodCallContext alloc] |
| 319 | + initWithResult:^(NSArray<NSString *> *result, FlutterError *error) { |
| 320 | + XCTAssertTrue(NSThread.isMainThread); |
| 321 | + XCTAssertNil(result); |
| 322 | + XCTAssertEqualObjects(error.code, @"invalid_source"); |
| 323 | + [resultExpectation fulfill]; |
| 324 | + }]; |
| 325 | + |
| 326 | + [plugin picker:mockPickerViewController didFinishPicking:@[ failResult1, failResult2 ]]; |
| 327 | + |
| 328 | + [self waitForExpectationsWithTimeout:30 handler:nil]; |
| 329 | +} |
| 330 | + |
| 331 | +- (void)testSendsImageInvalidErrorWhenOneFails API_AVAILABLE(ios(14)) { |
| 332 | + id mockPickerViewController = OCMClassMock([PHPickerViewController class]); |
| 333 | + NSError *loadDataError = [NSError errorWithDomain:@"PHPickerDomain" code:1234 userInfo:nil]; |
| 334 | + |
| 335 | + id mockFailItemProvider = OCMClassMock([NSItemProvider class]); |
| 336 | + OCMStub([mockFailItemProvider hasItemConformingToTypeIdentifier:OCMOCK_ANY]).andReturn(YES); |
| 337 | + [[mockFailItemProvider stub] |
| 338 | + loadDataRepresentationForTypeIdentifier:OCMOCK_ANY |
| 339 | + completionHandler:[OCMArg invokeBlockWithArgs:[NSNull null], |
| 340 | + loadDataError, nil]]; |
| 341 | + |
| 342 | + PHPickerResult *failResult = OCMClassMock([PHPickerResult class]); |
| 343 | + OCMStub([failResult itemProvider]).andReturn(mockFailItemProvider); |
| 344 | + |
| 345 | + NSURL *tiffURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"tiffImage" |
| 346 | + withExtension:@"tiff"]; |
| 347 | + NSItemProvider *tiffItemProvider = [[NSItemProvider alloc] initWithContentsOfURL:tiffURL]; |
| 348 | + PHPickerResult *tiffResult = OCMClassMock([PHPickerResult class]); |
| 349 | + OCMStub([tiffResult itemProvider]).andReturn(tiffItemProvider); |
| 350 | + |
| 351 | + FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc] init]; |
| 352 | + |
| 353 | + XCTestExpectation *resultExpectation = [self expectationWithDescription:@"result"]; |
| 354 | + |
| 355 | + plugin.callContext = [[FLTImagePickerMethodCallContext alloc] |
| 356 | + initWithResult:^(NSArray<NSString *> *result, FlutterError *error) { |
| 357 | + XCTAssertTrue(NSThread.isMainThread); |
| 358 | + XCTAssertNil(result); |
| 359 | + XCTAssertEqualObjects(error.code, @"invalid_image"); |
| 360 | + [resultExpectation fulfill]; |
| 361 | + }]; |
| 362 | + |
| 363 | + [plugin picker:mockPickerViewController didFinishPicking:@[ failResult, tiffResult ]]; |
| 364 | + |
| 365 | + [self waitForExpectationsWithTimeout:30 handler:nil]; |
| 366 | +} |
| 367 | + |
| 368 | +- (void)testSavesImages API_AVAILABLE(ios(14)) { |
| 369 | + id mockPickerViewController = OCMClassMock([PHPickerViewController class]); |
| 370 | + |
| 371 | + NSURL *tiffURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"tiffImage" |
| 372 | + withExtension:@"tiff"]; |
| 373 | + NSItemProvider *tiffItemProvider = [[NSItemProvider alloc] initWithContentsOfURL:tiffURL]; |
| 374 | + PHPickerResult *tiffResult = OCMClassMock([PHPickerResult class]); |
| 375 | + OCMStub([tiffResult itemProvider]).andReturn(tiffItemProvider); |
| 376 | + |
| 377 | + NSURL *pngURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"pngImage" |
| 378 | + withExtension:@"png"]; |
| 379 | + NSItemProvider *pngItemProvider = [[NSItemProvider alloc] initWithContentsOfURL:pngURL]; |
| 380 | + PHPickerResult *pngResult = OCMClassMock([PHPickerResult class]); |
| 381 | + OCMStub([pngResult itemProvider]).andReturn(pngItemProvider); |
| 382 | + |
| 383 | + FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc] init]; |
| 384 | + |
| 385 | + XCTestExpectation *resultExpectation = [self expectationWithDescription:@"result"]; |
| 386 | + |
| 387 | + plugin.callContext = [[FLTImagePickerMethodCallContext alloc] |
| 388 | + initWithResult:^(NSArray<NSString *> *result, FlutterError *error) { |
| 389 | + XCTAssertTrue(NSThread.isMainThread); |
| 390 | + XCTAssertEqual(result.count, 2); |
| 391 | + XCTAssertNil(error); |
| 392 | + [resultExpectation fulfill]; |
| 393 | + }]; |
| 394 | + |
| 395 | + [plugin picker:mockPickerViewController didFinishPicking:@[ tiffResult, pngResult ]]; |
301 | 396 |
|
302 |
| - XCTAssertEqual(pickImageResult, pathList); |
| 397 | + [self waitForExpectationsWithTimeout:30 handler:nil]; |
303 | 398 | }
|
304 | 399 |
|
305 | 400 | @end
|
0 commit comments