FBSnapshotTestController.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2015, 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. */
  10. #import <Foundation/Foundation.h>
  11. #import <UIKit/UIKit.h>
  12. typedef NS_ENUM(NSInteger, FBSnapshotTestControllerErrorCode) {
  13. FBSnapshotTestControllerErrorCodeUnknown,
  14. FBSnapshotTestControllerErrorCodeNeedsRecord,
  15. FBSnapshotTestControllerErrorCodePNGCreationFailed,
  16. FBSnapshotTestControllerErrorCodeImagesDifferentSizes,
  17. FBSnapshotTestControllerErrorCodeImagesDifferent,
  18. };
  19. /**
  20. Errors returned by the methods of FBSnapshotTestController use this domain.
  21. */
  22. extern NSString *const FBSnapshotTestControllerErrorDomain;
  23. /**
  24. Errors returned by the methods of FBSnapshotTestController sometimes contain this key in the `userInfo` dictionary.
  25. */
  26. extern NSString *const FBReferenceImageFilePathKey;
  27. /**
  28. Errors returned by the methods of FBSnapshotTestController sometimes contain this key in the `userInfo` dictionary.
  29. */
  30. extern NSString *const FBReferenceImageKey;
  31. /**
  32. Errors returned by the methods of FBSnapshotTestController sometimes contain this key in the `userInfo` dictionary.
  33. */
  34. extern NSString *const FBCapturedImageKey;
  35. /**
  36. Errors returned by the methods of FBSnapshotTestController sometimes contain this key in the `userInfo` dictionary.
  37. */
  38. extern NSString *const FBDiffedImageKey;
  39. /**
  40. Provides the heavy-lifting for FBSnapshotTestCase. It loads and saves images, along with performing the actual pixel-
  41. by-pixel comparison of images.
  42. Instances are initialized with the test class, and directories to read and write to.
  43. */
  44. @interface FBSnapshotTestController : NSObject
  45. /**
  46. Record snapshots.
  47. */
  48. @property (readwrite, nonatomic, assign) BOOL recordMode;
  49. /**
  50. When @c YES appends the name of the device model and OS to the snapshot file name.
  51. The default value is @c NO.
  52. */
  53. @property (readwrite, nonatomic, assign, getter=isDeviceAgnostic) BOOL deviceAgnostic;
  54. /**
  55. Uses drawViewHierarchyInRect:afterScreenUpdates: to draw the image instead of renderInContext:
  56. */
  57. @property (readwrite, nonatomic, assign) BOOL usesDrawViewHierarchyInRect;
  58. /**
  59. The directory in which referfence images are stored.
  60. */
  61. @property (readwrite, nonatomic, copy) NSString *referenceImagesDirectory;
  62. /**
  63. @param testClass The subclass of FBSnapshotTestCase that is using this controller.
  64. @returns An instance of FBSnapshotTestController.
  65. */
  66. - (instancetype)initWithTestClass:(Class)testClass;
  67. /**
  68. Designated initializer.
  69. @param testName The name of the tests.
  70. @returns An instance of FBSnapshotTestController.
  71. */
  72. - (instancetype)initWithTestName:(NSString *)testName;
  73. /**
  74. Performs the comparison of the layer.
  75. @param layer The Layer to snapshot.
  76. @param selector The test method being run.
  77. @param identifier An optional identifier, used is there are muliptle snapshot tests in a given -test method.
  78. @param error An error to log in an XCTAssert() macro if the method fails (missing reference image, images differ, etc).
  79. @returns YES if the comparison (or saving of the reference image) succeeded.
  80. */
  81. - (BOOL)compareSnapshotOfLayer:(CALayer *)layer
  82. selector:(SEL)selector
  83. identifier:(NSString *)identifier
  84. error:(NSError **)errorPtr;
  85. /**
  86. Performs the comparison of the view.
  87. @param view The view to snapshot.
  88. @param selector The test method being run.
  89. @param identifier An optional identifier, used is there are muliptle snapshot tests in a given -test method.
  90. @param error An error to log in an XCTAssert() macro if the method fails (missing reference image, images differ, etc).
  91. @returns YES if the comparison (or saving of the reference image) succeeded.
  92. */
  93. - (BOOL)compareSnapshotOfView:(UIView *)view
  94. selector:(SEL)selector
  95. identifier:(NSString *)identifier
  96. error:(NSError **)errorPtr;
  97. /**
  98. Performs the comparison of a view or layer.
  99. @param view The view or layer to snapshot.
  100. @param selector The test method being run.
  101. @param identifier An optional identifier, used is there are muliptle snapshot tests in a given -test method.
  102. @param tolerance The percentage of pixels that can differ and still be considered 'identical'
  103. @param error An error to log in an XCTAssert() macro if the method fails (missing reference image, images differ, etc).
  104. @returns YES if the comparison (or saving of the reference image) succeeded.
  105. */
  106. - (BOOL)compareSnapshotOfViewOrLayer:(id)viewOrLayer
  107. selector:(SEL)selector
  108. identifier:(NSString *)identifier
  109. tolerance:(CGFloat)tolerance
  110. error:(NSError **)errorPtr;
  111. /**
  112. Loads a reference image.
  113. @param selector The test method being run.
  114. @param identifier The optional identifier, used when multiple images are tested in a single -test method.
  115. @param errorPtr An error, if this methods returns nil, the error will be something useful.
  116. @returns An image.
  117. */
  118. - (UIImage *)referenceImageForSelector:(SEL)selector
  119. identifier:(NSString *)identifier
  120. error:(NSError **)errorPtr;
  121. /**
  122. Performs a pixel-by-pixel comparison of the two images with an allowable margin of error.
  123. @param referenceImage The reference (correct) image.
  124. @param image The image to test against the reference.
  125. @param tolerance The percentage of pixels that can differ and still be considered 'identical'
  126. @param errorPtr An error that indicates why the comparison failed if it does.
  127. @returns YES if the comparison succeeded and the images are the same(ish).
  128. */
  129. - (BOOL)compareReferenceImage:(UIImage *)referenceImage
  130. toImage:(UIImage *)image
  131. tolerance:(CGFloat)tolerance
  132. error:(NSError **)errorPtr;
  133. /**
  134. Saves the reference image and the test image to `failedOutputDirectory`.
  135. @param referenceImage The reference (correct) image.
  136. @param testImage The image to test against the reference.
  137. @param selector The test method being run.
  138. @param identifier The optional identifier, used when multiple images are tested in a single -test method.
  139. @param errorPtr An error that indicates why the comparison failed if it does.
  140. @returns YES if the save succeeded.
  141. */
  142. - (BOOL)saveFailedReferenceImage:(UIImage *)referenceImage
  143. testImage:(UIImage *)testImage
  144. selector:(SEL)selector
  145. identifier:(NSString *)identifier
  146. error:(NSError **)errorPtr;
  147. @end