XCAXClient_iOS+FBSnapshotReqParams.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "XCAXClient_iOS+FBSnapshotReqParams.h"
  10. #import <objc/runtime.h>
  11. /**
  12. Available parameters with their default values for XCTest:
  13. @"maxChildren" : (int)2147483647
  14. @"traverseFromParentsToChildren" : YES
  15. @"maxArrayCount" : (int)2147483647
  16. @"snapshotKeyHonorModalViews" : NO
  17. @"maxDepth" : (int)2147483647
  18. */
  19. NSString *const FBSnapshotMaxDepthKey = @"maxDepth";
  20. static id (*original_defaultParameters)(id, SEL);
  21. static id (*original_snapshotParameters)(id, SEL);
  22. static NSDictionary *defaultRequestParameters;
  23. static NSDictionary *defaultAdditionalRequestParameters;
  24. static NSMutableDictionary *customRequestParameters;
  25. void FBSetCustomParameterForElementSnapshot (NSString *name, id value)
  26. {
  27. static dispatch_once_t onceToken;
  28. dispatch_once(&onceToken, ^{
  29. customRequestParameters = [NSMutableDictionary new];
  30. });
  31. customRequestParameters[name] = value;
  32. }
  33. id FBGetCustomParameterForElementSnapshot (NSString *name)
  34. {
  35. return customRequestParameters[name];
  36. }
  37. static id swizzledDefaultParameters(id self, SEL _cmd)
  38. {
  39. static dispatch_once_t onceToken;
  40. dispatch_once(&onceToken, ^{
  41. defaultRequestParameters = original_defaultParameters(self, _cmd);
  42. });
  43. NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:defaultRequestParameters];
  44. [result addEntriesFromDictionary:defaultAdditionalRequestParameters ?: @{}];
  45. [result addEntriesFromDictionary:customRequestParameters ?: @{}];
  46. return result.copy;
  47. }
  48. static id swizzledSnapshotParameters(id self, SEL _cmd)
  49. {
  50. NSDictionary *result = original_snapshotParameters(self, _cmd);
  51. defaultAdditionalRequestParameters = result;
  52. return result;
  53. }
  54. @implementation XCAXClient_iOS (FBSnapshotReqParams)
  55. #pragma clang diagnostic push
  56. #pragma clang diagnostic ignored "-Wobjc-load-method"
  57. + (void)load
  58. {
  59. Method original_defaultParametersMethod = class_getInstanceMethod(self.class, @selector(defaultParameters));
  60. IMP swizzledDefaultParametersImp = (IMP)swizzledDefaultParameters;
  61. original_defaultParameters = (id (*)(id, SEL)) method_setImplementation(original_defaultParametersMethod, swizzledDefaultParametersImp);
  62. Method original_snapshotParametersMethod = class_getInstanceMethod(NSClassFromString(@"XCTElementQuery"), NSSelectorFromString(@"snapshotParameters"));
  63. IMP swizzledSnapshotParametersImp = (IMP)swizzledSnapshotParameters;
  64. original_snapshotParameters = (id (*)(id, SEL)) method_setImplementation(original_snapshotParametersMethod, swizzledSnapshotParametersImp);
  65. }
  66. #pragma clang diagnostic pop
  67. @end