XCTestPrivateSymbols.m 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "XCTestPrivateSymbols.h"
  10. #import <objc/runtime.h>
  11. #import "FBRuntimeUtils.h"
  12. #import "FBXCodeCompatibility.h"
  13. NSNumber *FB_XCAXAIsVisibleAttribute;
  14. NSString *FB_XCAXAIsVisibleAttributeName = @"XC_kAXXCAttributeIsVisible";
  15. NSNumber *FB_XCAXAIsElementAttribute;
  16. NSString *FB_XCAXAIsElementAttributeName = @"XC_kAXXCAttributeIsElement";
  17. NSString *FB_XCAXAVisibleFrameAttributeName = @"XC_kAXXCAttributeVisibleFrame";
  18. NSNumber *FB_XCAXACustomMinValueAttribute;
  19. NSString *FB_XCAXACustomMinValueAttributeName = @"XC_kAXXCAttributeMinValue";
  20. NSNumber *FB_XCAXACustomMaxValueAttribute;
  21. NSString *FB_XCAXACustomMaxValueAttributeName = @"XC_kAXXCAttributeMaxValue";
  22. void (*XCSetDebugLogger)(id <XCDebugLogDelegate>);
  23. id<XCDebugLogDelegate> (*XCDebugLogger)(void);
  24. NSArray<NSNumber *> *(*XCAXAccessibilityAttributesForStringAttributes)(id);
  25. __attribute__((constructor)) void FBLoadXCTestSymbols(void)
  26. {
  27. NSString *XC_kAXXCAttributeIsVisible = *(NSString*__autoreleasing*)FBRetrieveXCTestSymbol([FB_XCAXAIsVisibleAttributeName UTF8String]);
  28. NSString *XC_kAXXCAttributeIsElement = *(NSString*__autoreleasing*)FBRetrieveXCTestSymbol([FB_XCAXAIsElementAttributeName UTF8String]);
  29. XCAXAccessibilityAttributesForStringAttributes =
  30. (NSArray<NSNumber *> *(*)(id))FBRetrieveXCTestSymbol("XCAXAccessibilityAttributesForStringAttributes");
  31. XCSetDebugLogger = (void (*)(id <XCDebugLogDelegate>))FBRetrieveXCTestSymbol("XCSetDebugLogger");
  32. XCDebugLogger = (id<XCDebugLogDelegate>(*)(void))FBRetrieveXCTestSymbol("XCDebugLogger");
  33. NSArray<NSNumber *> *accessibilityAttributes = XCAXAccessibilityAttributesForStringAttributes(@[XC_kAXXCAttributeIsVisible, XC_kAXXCAttributeIsElement]);
  34. FB_XCAXAIsVisibleAttribute = accessibilityAttributes[0];
  35. FB_XCAXAIsElementAttribute = accessibilityAttributes[1];
  36. NSCAssert(FB_XCAXAIsVisibleAttribute != nil , @"Failed to retrieve FB_XCAXAIsVisibleAttribute", FB_XCAXAIsVisibleAttribute);
  37. NSCAssert(FB_XCAXAIsElementAttribute != nil , @"Failed to retrieve FB_XCAXAIsElementAttribute", FB_XCAXAIsElementAttribute);
  38. NSString *XC_kAXXCAttributeMinValue = *(NSString *__autoreleasing *)FBRetrieveXCTestSymbol([FB_XCAXACustomMinValueAttributeName UTF8String]);
  39. NSString *XC_kAXXCAttributeMaxValue = *(NSString *__autoreleasing *)FBRetrieveXCTestSymbol([FB_XCAXACustomMaxValueAttributeName UTF8String]);
  40. NSArray<NSNumber *> *minMaxAttrs = XCAXAccessibilityAttributesForStringAttributes(@[XC_kAXXCAttributeMinValue, XC_kAXXCAttributeMaxValue]);
  41. FB_XCAXACustomMinValueAttribute = minMaxAttrs[0];
  42. FB_XCAXACustomMaxValueAttribute = minMaxAttrs[1];
  43. NSCAssert(FB_XCAXACustomMinValueAttribute != nil, @"Failed to retrieve FB_XCAXACustomMinValueAttribute", FB_XCAXACustomMinValueAttribute);
  44. NSCAssert(FB_XCAXACustomMaxValueAttribute != nil, @"Failed to retrieve FB_XCAXACustomMaxValueAttribute", FB_XCAXACustomMaxValueAttribute);
  45. }
  46. void *FBRetrieveXCTestSymbol(const char *name)
  47. {
  48. Class XCTestClass = objc_lookUpClass("XCTestCase");
  49. NSCAssert(XCTestClass != nil, @"XCTest should be already linked", XCTestClass);
  50. NSString *XCTestBinary = [NSBundle bundleForClass:XCTestClass].executablePath;
  51. const char *binaryPath = XCTestBinary.UTF8String;
  52. NSCAssert(binaryPath != nil, @"XCTest binary path should not be nil", binaryPath);
  53. return FBRetrieveSymbolFromBinary(binaryPath, name);
  54. }
  55. NSArray<NSString*> *FBStandardAttributeNames(void)
  56. {
  57. static NSArray<NSString *> *attributeNames;
  58. static dispatch_once_t onceToken;
  59. dispatch_once(&onceToken, ^{
  60. Class xcElementSnapshotClass = NSClassFromString(@"XCElementSnapshot");
  61. NSCAssert(nil != xcElementSnapshotClass, @"XCElementSnapshot class must be resolvable", xcElementSnapshotClass);
  62. attributeNames = [xcElementSnapshotClass sanitizedElementSnapshotHierarchyAttributesForAttributes:nil
  63. isMacOS:NO];
  64. });
  65. return attributeNames;
  66. }
  67. NSArray<NSString*> *FBCustomAttributeNames(void)
  68. {
  69. static NSArray<NSString *> *customNames;
  70. static dispatch_once_t onceCustomAttributeNamesToken;
  71. dispatch_once(&onceCustomAttributeNamesToken, ^{
  72. customNames = @[
  73. FB_XCAXAIsVisibleAttributeName,
  74. FB_XCAXAIsElementAttributeName,
  75. FB_XCAXACustomMinValueAttributeName,
  76. FB_XCAXACustomMaxValueAttributeName
  77. ];
  78. });
  79. return customNames;
  80. }