react-native-webview.git

exampleTests.m 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <UIKit/UIKit.h>
  8. #import <XCTest/XCTest.h>
  9. #import <React/RCTLog.h>
  10. #import <React/RCTRootView.h>
  11. #define TIMEOUT_SECONDS 600
  12. #define TEXT_TO_LOOK_FOR @"Welcome to React"
  13. @interface exampleTests : XCTestCase
  14. @end
  15. @implementation exampleTests
  16. - (BOOL)findSubviewInView:(UIView *)view matching:(BOOL(^)(UIView *view))test
  17. {
  18. if (test(view)) {
  19. return YES;
  20. }
  21. for (UIView *subview in [view subviews]) {
  22. if ([self findSubviewInView:subview matching:test]) {
  23. return YES;
  24. }
  25. }
  26. return NO;
  27. }
  28. - (void)testRendersWelcomeScreen
  29. {
  30. UIViewController *vc = [[[RCTSharedApplication() delegate] window] rootViewController];
  31. NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS];
  32. BOOL foundElement = NO;
  33. __block NSString *redboxError = nil;
  34. #ifdef DEBUG
  35. RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
  36. if (level >= RCTLogLevelError) {
  37. redboxError = message;
  38. }
  39. });
  40. #endif
  41. while ([date timeIntervalSinceNow] > 0 && !foundElement && !redboxError) {
  42. [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  43. [[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  44. foundElement = [self findSubviewInView:vc.view matching:^BOOL(UIView *view) {
  45. if ([view.accessibilityLabel isEqualToString:TEXT_TO_LOOK_FOR]) {
  46. return YES;
  47. }
  48. return NO;
  49. }];
  50. }
  51. #ifdef DEBUG
  52. RCTSetLogFunction(RCTDefaultLogFunction);
  53. #endif
  54. XCTAssertNil(redboxError, @"RedBox error: %@", redboxError);
  55. XCTAssertTrue(foundElement, @"Couldn't find element with text '%@' in %d seconds", TEXT_TO_LOOK_FOR, TIMEOUT_SECONDS);
  56. }
  57. @end