XCUIElement+FBResolve.m 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_stableInstance
  27. {
  28. if (![self.fb_isResolvedNatively boolValue]) {
  29. return self;
  30. }
  31. XCUIElementQuery *query = [self isKindOfClass:XCUIApplication.class]
  32. ? self.application.fb_query
  33. : [self.application.fb_query descendantsMatchingType:XCUIElementTypeAny];
  34. NSString *uid = nil == self.fb_cachedSnapshot
  35. ? self.fb_uid
  36. : [FBXCElementSnapshotWrapper wdUIDWithSnapshot:(id)self.fb_cachedSnapshot];
  37. if (nil == uid) {
  38. return self;
  39. }
  40. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@",FBStringify(FBXCElementSnapshotWrapper, fb_uid), uid];
  41. return [query matchingPredicate:predicate].allElementsBoundByIndex.firstObject ?: self;
  42. }
  43. @end