FBAlertViewCommands.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "FBAlertViewCommands.h"
  10. #import "FBAlert.h"
  11. #import "FBRouteRequest.h"
  12. #import "FBSession.h"
  13. #import "XCUIApplication+FBHelpers.h"
  14. @implementation FBAlertViewCommands
  15. #pragma mark - <FBCommandHandler>
  16. + (NSArray *)routes
  17. {
  18. return
  19. @[
  20. [[FBRoute GET:@"/alert/text"] respondWithTarget:self action:@selector(handleAlertGetTextCommand:)],
  21. [[FBRoute GET:@"/alert/text"].withoutSession respondWithTarget:self action:@selector(handleAlertGetTextCommand:)],
  22. [[FBRoute POST:@"/alert/text"] respondWithTarget:self action:@selector(handleAlertSetTextCommand:)],
  23. [[FBRoute POST:@"/alert/accept"] respondWithTarget:self action:@selector(handleAlertAcceptCommand:)],
  24. [[FBRoute POST:@"/alert/accept"].withoutSession respondWithTarget:self action:@selector(handleAlertAcceptCommand:)],
  25. [[FBRoute POST:@"/alert/dismiss"] respondWithTarget:self action:@selector(handleAlertDismissCommand:)],
  26. [[FBRoute POST:@"/alert/dismiss"].withoutSession respondWithTarget:self action:@selector(handleAlertDismissCommand:)],
  27. [[FBRoute GET:@"/wda/alert/buttons"] respondWithTarget:self action:@selector(handleGetAlertButtonsCommand:)],
  28. ];
  29. }
  30. #pragma mark - Commands
  31. + (id<FBResponsePayload>)handleAlertGetTextCommand:(FBRouteRequest *)request
  32. {
  33. XCUIApplication *application = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
  34. NSString *alertText = [FBAlert alertWithApplication:application].text;
  35. if (!alertText) {
  36. return FBResponseWithStatus([FBCommandStatus noAlertOpenErrorWithMessage:nil
  37. traceback:nil]);
  38. }
  39. return FBResponseWithObject(alertText);
  40. }
  41. + (id<FBResponsePayload>)handleAlertSetTextCommand:(FBRouteRequest *)request
  42. {
  43. FBSession *session = request.session;
  44. id value = request.arguments[@"value"];
  45. if (!value) {
  46. return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:@"Missing 'value' parameter" traceback:nil]);
  47. }
  48. FBAlert *alert = [FBAlert alertWithApplication:session.activeApplication];
  49. if (!alert.isPresent) {
  50. return FBResponseWithStatus([FBCommandStatus noAlertOpenErrorWithMessage:nil
  51. traceback:nil]);
  52. }
  53. NSString *textToType = value;
  54. if ([value isKindOfClass:[NSArray class]]) {
  55. textToType = [value componentsJoinedByString:@""];
  56. }
  57. NSError *error;
  58. if (![alert typeText:textToType error:&error]) {
  59. return FBResponseWithStatus([FBCommandStatus unsupportedOperationErrorWithMessage:error.description
  60. traceback:[NSString stringWithFormat:@"%@", NSThread.callStackSymbols]]);
  61. }
  62. return FBResponseWithOK();
  63. }
  64. + (id<FBResponsePayload>)handleAlertAcceptCommand:(FBRouteRequest *)request
  65. {
  66. XCUIApplication *application = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
  67. NSString *name = request.arguments[@"name"];
  68. FBAlert *alert = [FBAlert alertWithApplication:application];
  69. NSError *error;
  70. if (!alert.isPresent) {
  71. return FBResponseWithStatus([FBCommandStatus noAlertOpenErrorWithMessage:nil
  72. traceback:nil]);
  73. }
  74. if (name) {
  75. if (![alert clickAlertButton:name error:&error]) {
  76. return FBResponseWithStatus([FBCommandStatus invalidElementStateErrorWithMessage:error.description
  77. traceback:[NSString stringWithFormat:@"%@", NSThread.callStackSymbols]]);
  78. }
  79. } else if (![alert acceptWithError:&error]) {
  80. return FBResponseWithStatus([FBCommandStatus invalidElementStateErrorWithMessage:error.description
  81. traceback:[NSString stringWithFormat:@"%@", NSThread.callStackSymbols]]);
  82. }
  83. return FBResponseWithOK();
  84. }
  85. + (id<FBResponsePayload>)handleAlertDismissCommand:(FBRouteRequest *)request
  86. {
  87. XCUIApplication *application = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
  88. NSString *name = request.arguments[@"name"];
  89. FBAlert *alert = [FBAlert alertWithApplication:application];
  90. NSError *error;
  91. if (!alert.isPresent) {
  92. return FBResponseWithStatus([FBCommandStatus noAlertOpenErrorWithMessage:nil
  93. traceback:nil]);
  94. }
  95. if (name) {
  96. if (![alert clickAlertButton:name error:&error]) {
  97. return FBResponseWithStatus([FBCommandStatus invalidElementStateErrorWithMessage:error.description
  98. traceback:[NSString stringWithFormat:@"%@", NSThread.callStackSymbols]]);
  99. }
  100. } else if (![alert dismissWithError:&error]) {
  101. return FBResponseWithStatus([FBCommandStatus invalidElementStateErrorWithMessage:error.description
  102. traceback:[NSString stringWithFormat:@"%@", NSThread.callStackSymbols]]);
  103. }
  104. return FBResponseWithOK();
  105. }
  106. + (id<FBResponsePayload>)handleGetAlertButtonsCommand:(FBRouteRequest *)request {
  107. FBSession *session = request.session;
  108. FBAlert *alert = [FBAlert alertWithApplication:session.activeApplication];
  109. if (!alert.isPresent) {
  110. return FBResponseWithStatus([FBCommandStatus noAlertOpenErrorWithMessage:nil
  111. traceback:nil]);
  112. }
  113. NSArray *labels = alert.buttonLabels;
  114. return FBResponseWithObject(labels);
  115. }
  116. @end