12a38a11eSExponent GitHub Bot// Copyright 2015-present 650 Industries. All rights reserved.
22a38a11eSExponent GitHub Bot
3d5e9e21bSBen Roth#import "ExpoKit.h"
4652b7dd7SBen Roth#import "EXKernel.h"
5652b7dd7SBen Roth#import "EXKernelLinkingManager.h"
6d5e9e21bSBen Roth#import "EXRootViewController.h"
7652b7dd7SBen Roth#import "EXShellManager.h"
89aa11ec8SBen Roth#import "EXTest.h"
9d5e9e21bSBen Roth
10d5e9e21bSBen Roth#import <React/RCTAssert.h>
11d5e9e21bSBen Roth#import <React/RCTUtils.h>
122a38a11eSExponent GitHub Bot
132a38a11eSExponent GitHub Bot#import <XCTest/XCTest.h>
142a38a11eSExponent GitHub Bot
152a38a11eSExponent GitHub Bot@interface ExponentIntegrationTests : XCTestCase
162a38a11eSExponent GitHub Bot
17d5e9e21bSBen Roth@property (nonatomic, strong) EXRootViewController *rootViewController;
189aa11ec8SBen Roth@property (nonatomic, strong) NSDictionary *jsTestSuiteResult;
1967a01999SBen Roth@property (nonatomic, strong) NSString *testSuiteUrl;
20d5e9e21bSBen Roth
212a38a11eSExponent GitHub Bot@end
222a38a11eSExponent GitHub Bot
232a38a11eSExponent GitHub Bot@implementation ExponentIntegrationTests
242a38a11eSExponent GitHub Bot
252a38a11eSExponent GitHub Bot- (void)setUp
262a38a11eSExponent GitHub Bot{
272a38a11eSExponent GitHub Bot  [super setUp];
2867a01999SBen Roth  [self _loadConfig];
299aa11ec8SBen Roth
309aa11ec8SBen Roth  _jsTestSuiteResult = nil;
31d5e9e21bSBen Roth  _rootViewController = (EXRootViewController *)[ExpoKit sharedInstance].rootViewController;
32652b7dd7SBen Roth  [[NSNotificationCenter defaultCenter] addObserver:self
33652b7dd7SBen Roth                                           selector:@selector(_onKernelJSLoaded)
34652b7dd7SBen Roth                                               name:kEXKernelJSIsLoadedNotification
35652b7dd7SBen Roth                                             object:nil];
36652b7dd7SBen Roth  [[NSNotificationCenter defaultCenter] addObserver:self
37652b7dd7SBen Roth                                           selector:@selector(_onTestSuiteCompleted:)
38652b7dd7SBen Roth                                               name:EXTestSuiteCompletedNotification object:nil];
392a38a11eSExponent GitHub Bot}
402a38a11eSExponent GitHub Bot
41d5e9e21bSBen Roth- (void)testDoesTestSuiteAppPassAllJSTests
422a38a11eSExponent GitHub Bot{
4367a01999SBen Roth  XCTAssert((_testSuiteUrl), @"No url configured for JS test-suite. Make sure EXTestEnvironment.plist exists and contains a url to test-suite.");
44d5e9e21bSBen Roth  [_rootViewController applicationWillEnterForeground];
452a38a11eSExponent GitHub Bot
469aa11ec8SBen Roth  // wait for JS
479aa11ec8SBen Roth  NSDate *dateToTimeOut = [NSDate dateWithTimeIntervalSinceNow:60];
489aa11ec8SBen Roth  while (dateToTimeOut.timeIntervalSinceNow > 0 && _jsTestSuiteResult == nil) {
49d5e9e21bSBen Roth    [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
50d5e9e21bSBen Roth    [[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
51d5e9e21bSBen Roth  }
529aa11ec8SBen Roth  XCTAssert((_jsTestSuiteResult), @"Test suite timed out");
539aa11ec8SBen Roth  XCTAssert(([_jsTestSuiteResult[@"failed"] integerValue] == 0), @"Test suite failed: %@", _jsTestSuiteResult);
549aa11ec8SBen Roth}
559aa11ec8SBen Roth
569aa11ec8SBen Roth#pragma mark - internal
579aa11ec8SBen Roth
5867a01999SBen Roth- (void)_loadConfig
5967a01999SBen Roth{
60*95925a0aSJames Ide  // This plist is generated with `powertools configure-ios-test-suite-url`
6167a01999SBen Roth  NSString *configPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"EXTestEnvironment" ofType:@"plist"];
6267a01999SBen Roth  NSDictionary *testConfig = (configPath) ? [NSDictionary dictionaryWithContentsOfFile:configPath] : [NSDictionary dictionary];
6367a01999SBen Roth  if (testConfig) {
6467a01999SBen Roth    _testSuiteUrl = testConfig[@"testSuiteUrl"];
6567a01999SBen Roth  }
6667a01999SBen Roth}
6767a01999SBen Roth
68652b7dd7SBen Roth- (void)_onKernelJSLoaded
69652b7dd7SBen Roth{
70652b7dd7SBen Roth  // if test environment isn't configured for a shell app, override here
71652b7dd7SBen Roth  // since clearly we're running tests
72652b7dd7SBen Roth  if ([EXShellManager sharedInstance].testEnvironment == EXTestEnvironmentNone) {
73652b7dd7SBen Roth    [EXShellManager sharedInstance].testEnvironment = EXTestEnvironmentLocal;
74652b7dd7SBen Roth  }
7567a01999SBen Roth  [[EXKernel sharedInstance].serviceRegistry.linkingManager openUrl:_testSuiteUrl isUniversalLink:NO];
76652b7dd7SBen Roth}
77652b7dd7SBen Roth
789aa11ec8SBen Roth- (void)_onTestSuiteCompleted:(NSNotification *)notif
799aa11ec8SBen Roth{
809aa11ec8SBen Roth  _jsTestSuiteResult = notif.userInfo;
812a38a11eSExponent GitHub Bot}
822a38a11eSExponent GitHub Bot
832a38a11eSExponent GitHub Bot@end
84