XCUIElement+FBResolve.m 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 "XCUIElement+FBResolve.h"
  10. #import <objc/runtime.h>
  11. #import "XCUIElement.h"
  12. #import "FBXCodeCompatibility.h"
  13. #import "XCUIElement+FBUID.h"
  14. @implementation XCUIElement (FBResolve)
  15. static char XCUIELEMENT_IS_RESOLVED_NATIVELY_KEY;
  16. @dynamic fb_isResolvedNatively;
  17. - (void)setFb_isResolvedNatively:(NSNumber *)isResolvedNatively
  18. {
  19. objc_setAssociatedObject(self, &XCUIELEMENT_IS_RESOLVED_NATIVELY_KEY, isResolvedNatively, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  20. }
  21. - (NSNumber *)fb_isResolvedNatively
  22. {
  23. NSNumber *result = objc_getAssociatedObject(self, &XCUIELEMENT_IS_RESOLVED_NATIVELY_KEY);
  24. return nil == result ? @YES : result;
  25. }
  26. - (XCUIElement *)fb_stableInstanceWithUid:(NSString *)uid
  27. {
  28. if (nil == uid || ![self.fb_isResolvedNatively boolValue] || [self isKindOfClass:XCUIApplication.class]) {
  29. return self;
  30. }
  31. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", FBStringify(FBXCElementSnapshotWrapper, fb_uid), uid];
  32. @autoreleasepool {
  33. XCUIElementQuery *query = [self.application.fb_query descendantsMatchingType:XCUIElementTypeAny];
  34. XCUIElement *result = [query matchingPredicate:predicate].allElementsBoundByIndex.firstObject;
  35. if (nil != result) {
  36. result.fb_isResolvedNatively = @NO;
  37. return result;
  38. }
  39. }
  40. return self;
  41. }
  42. @end