FBW3CActionsHelpers.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "FBW3CActionsHelpers.h"
  10. #import "FBErrorBuilder.h"
  11. #import "XCUIElement.h"
  12. #import "FBLogger.h"
  13. static NSString *const FB_ACTION_ITEM_KEY_VALUE = @"value";
  14. static NSString *const FB_ACTION_ITEM_KEY_DURATION = @"duration";
  15. NSString *FBRequireValue(NSDictionary<NSString *, id> *actionItem, NSError **error)
  16. {
  17. id value = [actionItem objectForKey:FB_ACTION_ITEM_KEY_VALUE];
  18. if (![value isKindOfClass:NSString.class] || [value length] == 0) {
  19. NSString *description = [NSString stringWithFormat:@"Key value must be present and should be a valid non-empty string for '%@'", actionItem];
  20. if (error) {
  21. *error = [[FBErrorBuilder.builder withDescription:description] build];
  22. }
  23. return nil;
  24. }
  25. NSRange r = [(NSString *)value rangeOfComposedCharacterSequenceAtIndex:0];
  26. return [(NSString *)value substringWithRange:r];
  27. }
  28. NSNumber *_Nullable FBOptDuration(NSDictionary<NSString *, id> *actionItem, NSNumber *defaultValue, NSError **error)
  29. {
  30. NSNumber *durationObj = [actionItem objectForKey:FB_ACTION_ITEM_KEY_DURATION];
  31. if (nil == durationObj) {
  32. if (nil == defaultValue) {
  33. NSString *description = [NSString stringWithFormat:@"Duration must be present for '%@' action item", actionItem];
  34. if (error) {
  35. *error = [[FBErrorBuilder.builder withDescription:description] build];
  36. }
  37. return nil;
  38. }
  39. return defaultValue;
  40. }
  41. if ([durationObj doubleValue] < 0.0) {
  42. NSString *description = [NSString stringWithFormat:@"Duration must be a valid positive number for '%@' action item", actionItem];
  43. if (error) {
  44. *error = [[FBErrorBuilder.builder withDescription:description] build];
  45. }
  46. return nil;
  47. }
  48. return durationObj;
  49. }
  50. BOOL FBIsMetaModifier(NSString *value)
  51. {
  52. unichar charCode = [value characterAtIndex:0];
  53. return charCode >= 0xE000 && charCode <= 0xF8FF;
  54. }
  55. NSUInteger FBToMetaModifier(NSString *value)
  56. {
  57. if (!FBIsMetaModifier(value)) {
  58. return 0;
  59. }
  60. unichar charCode = [value characterAtIndex:0];
  61. switch (charCode) {
  62. case 0xE000:
  63. return XCUIKeyModifierNone;
  64. case 0xE03D:
  65. return XCUIKeyModifierCommand;
  66. case 0xE009:
  67. return XCUIKeyModifierControl;
  68. case 0xE00A:
  69. return XCUIKeyModifierOption;
  70. case 0xE008:
  71. return XCUIKeyModifierShift;
  72. default:
  73. [FBLogger logFmt:@"Skipping the unsupported meta modifier with code %@", @(charCode)];
  74. return 0;
  75. }
  76. }