XCTestPrivateSymbols.m 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. void (*XCSetDebugLogger)(id <XCDebugLogDelegate>);
  18. id<XCDebugLogDelegate> (*XCDebugLogger)(void);
  19. NSArray<NSNumber *> *(*XCAXAccessibilityAttributesForStringAttributes)(id);
  20. __attribute__((constructor)) void FBLoadXCTestSymbols(void)
  21. {
  22. NSString *XC_kAXXCAttributeIsVisible = *(NSString*__autoreleasing*)FBRetrieveXCTestSymbol([FB_XCAXAIsVisibleAttributeName UTF8String]);
  23. NSString *XC_kAXXCAttributeIsElement = *(NSString*__autoreleasing*)FBRetrieveXCTestSymbol([FB_XCAXAIsElementAttributeName UTF8String]);
  24. XCAXAccessibilityAttributesForStringAttributes =
  25. (NSArray<NSNumber *> *(*)(id))FBRetrieveXCTestSymbol("XCAXAccessibilityAttributesForStringAttributes");
  26. XCSetDebugLogger = (void (*)(id <XCDebugLogDelegate>))FBRetrieveXCTestSymbol("XCSetDebugLogger");
  27. XCDebugLogger = (id<XCDebugLogDelegate>(*)(void))FBRetrieveXCTestSymbol("XCDebugLogger");
  28. NSArray<NSNumber *> *accessibilityAttributes = XCAXAccessibilityAttributesForStringAttributes(@[XC_kAXXCAttributeIsVisible, XC_kAXXCAttributeIsElement]);
  29. FB_XCAXAIsVisibleAttribute = accessibilityAttributes[0];
  30. FB_XCAXAIsElementAttribute = accessibilityAttributes[1];
  31. NSCAssert(FB_XCAXAIsVisibleAttribute != nil , @"Failed to retrieve FB_XCAXAIsVisibleAttribute", FB_XCAXAIsVisibleAttribute);
  32. NSCAssert(FB_XCAXAIsElementAttribute != nil , @"Failed to retrieve FB_XCAXAIsElementAttribute", FB_XCAXAIsElementAttribute);
  33. }
  34. void *FBRetrieveXCTestSymbol(const char *name)
  35. {
  36. Class XCTestClass = objc_lookUpClass("XCTestCase");
  37. NSCAssert(XCTestClass != nil, @"XCTest should be already linked", XCTestClass);
  38. NSString *XCTestBinary = [NSBundle bundleForClass:XCTestClass].executablePath;
  39. const char *binaryPath = XCTestBinary.UTF8String;
  40. NSCAssert(binaryPath != nil, @"XCTest binary path should not be nil", binaryPath);
  41. return FBRetrieveSymbolFromBinary(binaryPath, name);
  42. }
  43. NSArray<NSString*> *FBStandardAttributeNames(void)
  44. {
  45. static NSArray<NSString *> *attributeNames;
  46. static dispatch_once_t onceToken;
  47. dispatch_once(&onceToken, ^{
  48. Class xcElementSnapshotClass = NSClassFromString(@"XCElementSnapshot");
  49. NSCAssert(nil != xcElementSnapshotClass, @"XCElementSnapshot class must be resolvable", xcElementSnapshotClass);
  50. attributeNames = [xcElementSnapshotClass sanitizedElementSnapshotHierarchyAttributesForAttributes:nil
  51. isMacOS:NO];
  52. });
  53. return attributeNames;
  54. }
  55. NSArray<NSString*> *FBCustomAttributeNames(void)
  56. {
  57. static NSArray<NSString *> *customNames;
  58. static dispatch_once_t onceCustomAttributeNamesToken;
  59. dispatch_once(&onceCustomAttributeNamesToken, ^{
  60. customNames = @[
  61. FB_XCAXAIsVisibleAttributeName,
  62. FB_XCAXAIsElementAttributeName
  63. ];
  64. });
  65. return customNames;
  66. }