XCUIElement+FBTVFocuse.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Copyright (c) 2018-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 "XCUIElement+FBTVFocuse.h"
  10. #import <XCTest/XCUIRemote.h>
  11. #import "FBConfiguration.h"
  12. #import "FBErrorBuilder.h"
  13. #import <FBTVNavigationTracker.h>
  14. #import "XCUIApplication+FBHelpers.h"
  15. #import "XCUIElement+FBUtilities.h"
  16. #import "XCUIElement+FBWebDriverAttributes.h"
  17. #if TARGET_OS_TV
  18. int const MAX_ITERATIONS_COUNT = 100;
  19. @implementation XCUIElement (FBTVFocuse)
  20. - (BOOL)fb_setFocusWithError:(NSError**) error
  21. {
  22. [XCUIApplication.fb_activeApplication fb_waitUntilStableWithTimeout:FBConfiguration.animationCoolOffTimeout];
  23. if (!self.wdEnabled) {
  24. if (error) {
  25. *error = [[FBErrorBuilder.builder withDescription:
  26. [NSString stringWithFormat:@"'%@' element cannot be focused because it is disabled", self.description]] build];
  27. }
  28. return NO;
  29. }
  30. FBTVNavigationTracker *tracker = [FBTVNavigationTracker trackerWithTargetElement:self];
  31. for (int i = 0; i < MAX_ITERATIONS_COUNT; i++) {
  32. // Here hasFocus works so far. Maybe, it is because it is handled by `XCUIRemote`...
  33. if (self.hasFocus) {
  34. return YES;
  35. }
  36. if (!self.exists) {
  37. if (error) {
  38. *error = [[FBErrorBuilder.builder withDescription:
  39. [NSString stringWithFormat:@"'%@' element is not reachable because it does not exist. Try to use XCUIRemote commands.", self.description]] build];
  40. }
  41. return NO;
  42. }
  43. FBTVDirection direction = tracker.directionToFocusedElement;
  44. if (direction != FBTVDirectionNone) {
  45. [[XCUIRemote sharedRemote] pressButton: (XCUIRemoteButton)direction];
  46. }
  47. }
  48. return NO;
  49. }
  50. - (BOOL)fb_selectWithError:(NSError**) error
  51. {
  52. BOOL result = [self fb_setFocusWithError: error];
  53. if (result) {
  54. [[XCUIRemote sharedRemote] pressButton:XCUIRemoteButtonSelect];
  55. }
  56. return result;
  57. }
  58. @end
  59. #endif