暫無描述

ViewShotExampleTests.m 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <UIKit/UIKit.h>
  10. #import <XCTest/XCTest.h>
  11. #import <React/RCTLog.h>
  12. #import <React/RCTRootView.h>
  13. #define TIMEOUT_SECONDS 600
  14. #define TEXT_TO_LOOK_FOR @"Welcome to React Native!"
  15. @interface ViewShotExampleTests : XCTestCase
  16. @end
  17. @implementation ViewShotExampleTests
  18. - (BOOL)findSubviewInView:(UIView *)view matching:(BOOL(^)(UIView *view))test
  19. {
  20. if (test(view)) {
  21. return YES;
  22. }
  23. for (UIView *subview in [view subviews]) {
  24. if ([self findSubviewInView:subview matching:test]) {
  25. return YES;
  26. }
  27. }
  28. return NO;
  29. }
  30. - (void)testRendersWelcomeScreen
  31. {
  32. UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
  33. NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS];
  34. BOOL foundElement = NO;
  35. __block NSString *redboxError = nil;
  36. RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
  37. if (level >= RCTLogLevelError) {
  38. redboxError = message;
  39. }
  40. });
  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. RCTSetLogFunction(RCTDefaultLogFunction);
  52. XCTAssertNil(redboxError, @"RedBox error: %@", redboxError);
  53. XCTAssertTrue(foundElement, @"Couldn't find element with text '%@' in %d seconds", TEXT_TO_LOOK_FOR, TIMEOUT_SECONDS);
  54. }
  55. @end