XCUIElement+FBMinMax.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "FBLogger.h"
  10. #import "XCUIElement+FBMinMax.h"
  11. #import "FBXCElementSnapshotWrapper+Helpers.h"
  12. #import "XCUIElement+FBUtilities.h"
  13. #import "XCTestPrivateSymbols.h"
  14. @interface FBXCElementSnapshotWrapper (FBMinMaxInternal)
  15. - (NSNumber *)fb_numericAttribute:(NSString *)attributeName symbol:(NSNumber *)symbol;
  16. @end
  17. @implementation XCUIElement (FBMinMax)
  18. - (NSNumber *)fb_minValue
  19. {
  20. @autoreleasepool {
  21. id<FBXCElementSnapshot> snapshot = [self fb_standardSnapshot];
  22. return [[FBXCElementSnapshotWrapper ensureWrapped:snapshot] fb_minValue];
  23. }
  24. }
  25. - (NSNumber *)fb_maxValue
  26. {
  27. @autoreleasepool {
  28. id<FBXCElementSnapshot> snapshot = [self fb_standardSnapshot];
  29. return [[FBXCElementSnapshotWrapper ensureWrapped:snapshot] fb_maxValue];
  30. }
  31. }
  32. @end
  33. @implementation FBXCElementSnapshotWrapper (FBMinMax)
  34. - (NSNumber *)fb_minValue
  35. {
  36. return [self fb_numericAttribute:FB_XCAXACustomMinValueAttributeName
  37. symbol:FB_XCAXACustomMinValueAttribute];
  38. }
  39. - (NSNumber *)fb_maxValue
  40. {
  41. return [self fb_numericAttribute:FB_XCAXACustomMaxValueAttributeName
  42. symbol:FB_XCAXACustomMaxValueAttribute];
  43. }
  44. - (NSNumber *)fb_numericAttribute:(NSString *)attributeName symbol:(NSNumber *)symbol
  45. {
  46. NSNumber *cached = (self.snapshot.additionalAttributes ?: @{})[symbol];
  47. if (cached) {
  48. return cached;
  49. }
  50. NSError *error = nil;
  51. NSNumber *raw = [self fb_attributeValue:attributeName error:&error];
  52. if (nil != raw) {
  53. NSMutableDictionary *updated = [NSMutableDictionary dictionaryWithDictionary:self.additionalAttributes ?: @{}];
  54. updated[symbol] = raw;
  55. self.snapshot.additionalAttributes = updated.copy;
  56. return raw;
  57. }
  58. [FBLogger logFmt:@"[FBMinMax] Cannot determine %@ for %@: %@", attributeName, self.fb_description, error.localizedDescription];
  59. return nil;
  60. }
  61. @end