FBXCElementSnapshotWrapper.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "FBXCElementSnapshotWrapper.h"
  10. #import "FBElementUtils.h"
  11. #pragma clang diagnostic push
  12. #pragma clang diagnostic ignored "-Wobjc-protocol-property-synthesis"
  13. #pragma clang diagnostic ignored "-Wprotocol"
  14. @implementation FBXCElementSnapshotWrapper
  15. - (instancetype)initWithSnapshot:(id<FBXCElementSnapshot>)snapshot;
  16. {
  17. self->_snapshot = snapshot;
  18. return self;
  19. }
  20. + (instancetype)ensureWrapped:(id<FBXCElementSnapshot>)snapshot
  21. {
  22. if (nil == snapshot) {
  23. return nil;
  24. }
  25. return [(NSObject *)snapshot isKindOfClass:self.class]
  26. ? (FBXCElementSnapshotWrapper *)snapshot
  27. : [[FBXCElementSnapshotWrapper alloc] initWithSnapshot:snapshot];
  28. }
  29. // Attributes are queried most often,
  30. // so we prefer them to have direct accessors defined here
  31. // rather than to use message forwarding via forwardingTargetForSelector,
  32. // which is slow
  33. - (NSString *)identifier
  34. {
  35. return self.snapshot.identifier;
  36. }
  37. - (CGRect)frame
  38. {
  39. return self.snapshot.frame;
  40. }
  41. - (id)value
  42. {
  43. return self.snapshot.value;
  44. }
  45. - (NSString *)title
  46. {
  47. return self.snapshot.title;
  48. }
  49. - (NSString *)label
  50. {
  51. return self.snapshot.label;
  52. }
  53. - (XCUIElementType)elementType
  54. {
  55. return self.snapshot.elementType;
  56. }
  57. - (BOOL)isEnabled
  58. {
  59. return self.snapshot.enabled;
  60. }
  61. - (XCUIUserInterfaceSizeClass)horizontalSizeClass
  62. {
  63. return self.snapshot.horizontalSizeClass;
  64. }
  65. - (XCUIUserInterfaceSizeClass)verticalSizeClass
  66. {
  67. return self.snapshot.verticalSizeClass;
  68. }
  69. - (NSString *)placeholderValue
  70. {
  71. return self.snapshot.placeholderValue;
  72. }
  73. - (BOOL)isSelected
  74. {
  75. return self.snapshot.selected;
  76. }
  77. #if !TARGET_OS_OSX
  78. - (BOOL)hasFocus
  79. {
  80. return self.snapshot.hasFocus;
  81. }
  82. #endif
  83. - (id)forwardingTargetForSelector:(SEL)aSelector
  84. {
  85. static dispatch_once_t onceToken;
  86. static NSSet<NSString *> *names;
  87. dispatch_once(&onceToken, ^{
  88. names = [FBElementUtils selectorNamesWithProtocol:@protocol(FBXCElementSnapshot)];
  89. });
  90. return [names containsObject:NSStringFromSelector(aSelector)] ? self.snapshot : nil;
  91. }
  92. @end
  93. #pragma clang diagnostic pop