FBElementTypeTransformer.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "FBElementTypeTransformer.h"
  10. #import "FBExceptions.h"
  11. @implementation FBElementTypeTransformer
  12. static NSDictionary *ElementTypeToStringMapping;
  13. static NSDictionary *StringToElementTypeMapping;
  14. static NSString const *FB_ELEMENT_TYPE_PREFIX = @"XCUIElementType";
  15. + (void)createMapping
  16. {
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. ElementTypeToStringMapping =
  20. @{
  21. @0 : @"XCUIElementTypeAny",
  22. @1 : @"XCUIElementTypeOther",
  23. @2 : @"XCUIElementTypeApplication",
  24. @3 : @"XCUIElementTypeGroup",
  25. @4 : @"XCUIElementTypeWindow",
  26. @5 : @"XCUIElementTypeSheet",
  27. @6 : @"XCUIElementTypeDrawer",
  28. @7 : @"XCUIElementTypeAlert",
  29. @8 : @"XCUIElementTypeDialog",
  30. @9 : @"XCUIElementTypeButton",
  31. @10 : @"XCUIElementTypeRadioButton",
  32. @11 : @"XCUIElementTypeRadioGroup",
  33. @12 : @"XCUIElementTypeCheckBox",
  34. @13 : @"XCUIElementTypeDisclosureTriangle",
  35. @14 : @"XCUIElementTypePopUpButton",
  36. @15 : @"XCUIElementTypeComboBox",
  37. @16 : @"XCUIElementTypeMenuButton",
  38. @17 : @"XCUIElementTypeToolbarButton",
  39. @18 : @"XCUIElementTypePopover",
  40. @19 : @"XCUIElementTypeKeyboard",
  41. @20 : @"XCUIElementTypeKey",
  42. @21 : @"XCUIElementTypeNavigationBar",
  43. @22 : @"XCUIElementTypeTabBar",
  44. @23 : @"XCUIElementTypeTabGroup",
  45. @24 : @"XCUIElementTypeToolbar",
  46. @25 : @"XCUIElementTypeStatusBar",
  47. @26 : @"XCUIElementTypeTable",
  48. @27 : @"XCUIElementTypeTableRow",
  49. @28 : @"XCUIElementTypeTableColumn",
  50. @29 : @"XCUIElementTypeOutline",
  51. @30 : @"XCUIElementTypeOutlineRow",
  52. @31 : @"XCUIElementTypeBrowser",
  53. @32 : @"XCUIElementTypeCollectionView",
  54. @33 : @"XCUIElementTypeSlider",
  55. @34 : @"XCUIElementTypePageIndicator",
  56. @35 : @"XCUIElementTypeProgressIndicator",
  57. @36 : @"XCUIElementTypeActivityIndicator",
  58. @37 : @"XCUIElementTypeSegmentedControl",
  59. @38 : @"XCUIElementTypePicker",
  60. @39 : @"XCUIElementTypePickerWheel",
  61. @40 : @"XCUIElementTypeSwitch",
  62. @41 : @"XCUIElementTypeToggle",
  63. @42 : @"XCUIElementTypeLink",
  64. @43 : @"XCUIElementTypeImage",
  65. @44 : @"XCUIElementTypeIcon",
  66. @45 : @"XCUIElementTypeSearchField",
  67. @46 : @"XCUIElementTypeScrollView",
  68. @47 : @"XCUIElementTypeScrollBar",
  69. @48 : @"XCUIElementTypeStaticText",
  70. @49 : @"XCUIElementTypeTextField",
  71. @50 : @"XCUIElementTypeSecureTextField",
  72. @51 : @"XCUIElementTypeDatePicker",
  73. @52 : @"XCUIElementTypeTextView",
  74. @53 : @"XCUIElementTypeMenu",
  75. @54 : @"XCUIElementTypeMenuItem",
  76. @55 : @"XCUIElementTypeMenuBar",
  77. @56 : @"XCUIElementTypeMenuBarItem",
  78. @57 : @"XCUIElementTypeMap",
  79. @58 : @"XCUIElementTypeWebView",
  80. @59 : @"XCUIElementTypeIncrementArrow",
  81. @60 : @"XCUIElementTypeDecrementArrow",
  82. @61 : @"XCUIElementTypeTimeline",
  83. @62 : @"XCUIElementTypeRatingIndicator",
  84. @63 : @"XCUIElementTypeValueIndicator",
  85. @64 : @"XCUIElementTypeSplitGroup",
  86. @65 : @"XCUIElementTypeSplitter",
  87. @66 : @"XCUIElementTypeRelevanceIndicator",
  88. @67 : @"XCUIElementTypeColorWell",
  89. @68 : @"XCUIElementTypeHelpTag",
  90. @69 : @"XCUIElementTypeMatte",
  91. @70 : @"XCUIElementTypeDockItem",
  92. @71 : @"XCUIElementTypeRuler",
  93. @72 : @"XCUIElementTypeRulerMarker",
  94. @73 : @"XCUIElementTypeGrid",
  95. @74 : @"XCUIElementTypeLevelIndicator",
  96. @75 : @"XCUIElementTypeCell",
  97. @76 : @"XCUIElementTypeLayoutArea",
  98. @77 : @"XCUIElementTypeLayoutItem",
  99. @78 : @"XCUIElementTypeHandle",
  100. @79 : @"XCUIElementTypeStepper",
  101. @80 : @"XCUIElementTypeTab",
  102. @81 : @"XCUIElementTypeTouchBar",
  103. @82 : @"XCUIElementTypeStatusItem",
  104. // !!! This mapping should be updated if there are changes after each new XCTest release
  105. };
  106. NSMutableDictionary *swappedMapping = [NSMutableDictionary dictionary];
  107. [ElementTypeToStringMapping enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  108. swappedMapping[obj] = key;
  109. }];
  110. StringToElementTypeMapping = swappedMapping.copy;
  111. });
  112. }
  113. + (XCUIElementType)elementTypeWithTypeName:(NSString *)typeName
  114. {
  115. [self createMapping];
  116. NSNumber *type = StringToElementTypeMapping[typeName];
  117. if (nil == type) {
  118. if ([typeName hasPrefix:(NSString *)FB_ELEMENT_TYPE_PREFIX] && typeName.length > FB_ELEMENT_TYPE_PREFIX.length) {
  119. // Consider the element type is something new and has to be added into ElementTypeToStringMapping
  120. return XCUIElementTypeOther;
  121. }
  122. NSString *reason = [NSString stringWithFormat:@"Invalid argument for class used '%@'. Did you mean %@%@?", typeName, FB_ELEMENT_TYPE_PREFIX, typeName];
  123. @throw [NSException exceptionWithName:FBInvalidArgumentException reason:reason userInfo:@{}];
  124. }
  125. return (XCUIElementType) type.unsignedIntegerValue;
  126. }
  127. + (NSString *)stringWithElementType:(XCUIElementType)type
  128. {
  129. [self createMapping];
  130. NSString *typeName = ElementTypeToStringMapping[@(type)];
  131. return nil == typeName
  132. // Consider the type name is something new and has to be added into ElementTypeToStringMapping
  133. ? [NSString stringWithFormat:@"%@Other", FB_ELEMENT_TYPE_PREFIX]
  134. : typeName;
  135. }
  136. + (NSString *)shortStringWithElementType:(XCUIElementType)type
  137. {
  138. return [[self stringWithElementType:type] stringByReplacingOccurrencesOfString:(NSString *)FB_ELEMENT_TYPE_PREFIX withString:@""];
  139. }
  140. @end