FBImageProcessorTests.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import <XCTest/XCTest.h>
  10. #import "FBImageProcessor.h"
  11. #import "FBIntegrationTestCase.h"
  12. @interface FBImageProcessorTests : FBIntegrationTestCase
  13. @property (nonatomic) NSData *originalImage;
  14. @property (nonatomic) CGSize originalSize;
  15. @end
  16. @implementation FBImageProcessorTests
  17. - (void)setUp {
  18. XCUIApplication *app = [[XCUIApplication alloc] init];
  19. [app launch];
  20. XCUIScreenshot *screenshot = app.screenshot;
  21. self.originalImage = UIImageJPEGRepresentation(screenshot.image, 1.0);
  22. self.originalSize = [FBImageProcessorTests scaledSizeFromImage:screenshot.image];
  23. }
  24. - (void)testScaling {
  25. CGFloat halfScale = 0.5;
  26. CGSize expectedHalfScaleSize = [FBImageProcessorTests sizeFromSize:self.originalSize scalingFactor:0.5];
  27. [self scaleImageWithFactor:halfScale
  28. expectedSize:expectedHalfScaleSize];
  29. // 0 is the smalles scaling factor we accept
  30. CGFloat minScale = 0.0;
  31. CGSize expectedMinScaleSize = [FBImageProcessorTests sizeFromSize:self.originalSize scalingFactor:0.01];
  32. [self scaleImageWithFactor:minScale
  33. expectedSize:expectedMinScaleSize];
  34. // For scaling factors above 100 we don't perform any scaling and just return the unmodified image
  35. [self scaleImageWithFactor:1.0
  36. expectedSize:self.originalSize];
  37. [self scaleImageWithFactor:2.0
  38. expectedSize:self.originalSize];
  39. }
  40. - (void)scaleImageWithFactor:(CGFloat)scalingFactor expectedSize:(CGSize)excpectedSize {
  41. FBImageProcessor *scaler = [[FBImageProcessor alloc] init];
  42. id expScaled = [self expectationWithDescription:@"Receive scaled image"];
  43. [scaler submitImageData:self.originalImage
  44. scalingFactor:scalingFactor
  45. completionHandler:^(NSData *scaled) {
  46. UIImage *scaledImage = [UIImage imageWithData:scaled];
  47. CGSize scaledSize = [FBImageProcessorTests scaledSizeFromImage:scaledImage];
  48. XCTAssertEqualWithAccuracy(scaledSize.width, excpectedSize.width, 1.0);
  49. XCTAssertEqualWithAccuracy(scaledSize.height, excpectedSize.height, 1.0);
  50. [expScaled fulfill];
  51. }];
  52. [self waitForExpectations:@[expScaled]
  53. timeout:0.5];
  54. }
  55. + (CGSize)scaledSizeFromImage:(UIImage *)image {
  56. return CGSizeMake(image.size.width * image.scale, image.size.height * image.scale);
  57. }
  58. + (CGSize)sizeFromSize:(CGSize)size scalingFactor:(CGFloat)scalingFactor {
  59. return CGSizeMake(round(size.width * scalingFactor), round(size.height * scalingFactor));
  60. }
  61. @end