xref: /MusicFree/ios/MusicFreeTests/MusicFreeNewTests.m (revision 5589cdf32b2bb0f641e5ac7bf1f6152cd6b9b70e)
1*5589cdf3S猫头猫#import <UIKit/UIKit.h>
2*5589cdf3S猫头猫#import <XCTest/XCTest.h>
3*5589cdf3S猫头猫
4*5589cdf3S猫头猫#import <React/RCTLog.h>
5*5589cdf3S猫头猫#import <React/RCTRootView.h>
6*5589cdf3S猫头猫
7*5589cdf3S猫头猫#define TIMEOUT_SECONDS 600
8*5589cdf3S猫头猫#define TEXT_TO_LOOK_FOR @"Welcome to React"
9*5589cdf3S猫头猫
10*5589cdf3S猫头猫@interface MusicFreeTests : XCTestCase
11*5589cdf3S猫头猫
12*5589cdf3S猫头猫@end
13*5589cdf3S猫头猫
14*5589cdf3S猫头猫@implementation MusicFreeTests
15*5589cdf3S猫头猫
16*5589cdf3S猫头猫- (BOOL)findSubviewInView:(UIView *)view matching:(BOOL (^)(UIView *view))test
17*5589cdf3S猫头猫{
18*5589cdf3S猫头猫  if (test(view)) {
19*5589cdf3S猫头猫    return YES;
20*5589cdf3S猫头猫  }
21*5589cdf3S猫头猫  for (UIView *subview in [view subviews]) {
22*5589cdf3S猫头猫    if ([self findSubviewInView:subview matching:test]) {
23*5589cdf3S猫头猫      return YES;
24*5589cdf3S猫头猫    }
25*5589cdf3S猫头猫  }
26*5589cdf3S猫头猫  return NO;
27*5589cdf3S猫头猫}
28*5589cdf3S猫头猫
29*5589cdf3S猫头猫- (void)testRendersWelcomeScreen
30*5589cdf3S猫头猫{
31*5589cdf3S猫头猫  UIViewController *vc = [[[RCTSharedApplication() delegate] window] rootViewController];
32*5589cdf3S猫头猫  NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS];
33*5589cdf3S猫头猫  BOOL foundElement = NO;
34*5589cdf3S猫头猫
35*5589cdf3S猫头猫  __block NSString *redboxError = nil;
36*5589cdf3S猫头猫#ifdef DEBUG
37*5589cdf3S猫头猫  RCTSetLogFunction(
38*5589cdf3S猫头猫      ^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
39*5589cdf3S猫头猫        if (level >= RCTLogLevelError) {
40*5589cdf3S猫头猫          redboxError = message;
41*5589cdf3S猫头猫        }
42*5589cdf3S猫头猫      });
43*5589cdf3S猫头猫#endif
44*5589cdf3S猫头猫
45*5589cdf3S猫头猫  while ([date timeIntervalSinceNow] > 0 && !foundElement && !redboxError) {
46*5589cdf3S猫头猫    [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
47*5589cdf3S猫头猫    [[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
48*5589cdf3S猫头猫
49*5589cdf3S猫头猫    foundElement = [self findSubviewInView:vc.view
50*5589cdf3S猫头猫                                  matching:^BOOL(UIView *view) {
51*5589cdf3S猫头猫                                    if ([view.accessibilityLabel isEqualToString:TEXT_TO_LOOK_FOR]) {
52*5589cdf3S猫头猫                                      return YES;
53*5589cdf3S猫头猫                                    }
54*5589cdf3S猫头猫                                    return NO;
55*5589cdf3S猫头猫                                  }];
56*5589cdf3S猫头猫  }
57*5589cdf3S猫头猫
58*5589cdf3S猫头猫#ifdef DEBUG
59*5589cdf3S猫头猫  RCTSetLogFunction(RCTDefaultLogFunction);
60*5589cdf3S猫头猫#endif
61*5589cdf3S猫头猫
62*5589cdf3S猫头猫  XCTAssertNil(redboxError, @"RedBox error: %@", redboxError);
63*5589cdf3S猫头猫  XCTAssertTrue(foundElement, @"Couldn't find element with text '%@' in %d seconds", TEXT_TO_LOOK_FOR, TIMEOUT_SECONDS);
64*5589cdf3S猫头猫}
65*5589cdf3S猫头猫
66*5589cdf3S猫头猫@end
67