From 81f59dfdc249c2192df635530d752cb819760646 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Mon, 11 Jul 2016 13:16:09 -0700 Subject: [PATCH] Remove code for testing without JS bundle Reviewed By: mmmulani Differential Revision: D3542347 fbshipit-source-id: d0dcfb65645faf515e6e48e6ff733646e1bcf907 --- Examples/UIExplorer/TestBundle.js | 9 +++++ .../UIExplorer.xcodeproj/project.pbxproj | 32 +++--------------- .../UIExplorerUnitTests/RCTBridgeTests.m | 8 ++++- .../RCTComponentPropsTests.m | 3 +- .../UIExplorerUnitTests/RCTImageLoaderTests.m | 18 +++++++--- .../RCTModuleInitNotificationRaceTests.m | 3 +- .../UIExplorerUnitTests/RCTModuleInitTests.m | 3 +- React/Base/RCTBatchedBridge.m | 33 ++++++------------- 8 files changed, 49 insertions(+), 60 deletions(-) create mode 100644 Examples/UIExplorer/TestBundle.js diff --git a/Examples/UIExplorer/TestBundle.js b/Examples/UIExplorer/TestBundle.js new file mode 100644 index 000000000..5919f0f4f --- /dev/null +++ b/Examples/UIExplorer/TestBundle.js @@ -0,0 +1,9 @@ +/** + * Copyright (c) 2013-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + diff --git a/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj b/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj index f73a4214d..a23d3d532 100644 --- a/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj +++ b/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj @@ -64,6 +64,7 @@ 27F441EC1BEBE5030039B79C /* FlexibleSizeExampleView.m in Sources */ = {isa = PBXBuildFile; fileRef = 27F441E81BEBE5030039B79C /* FlexibleSizeExampleView.m */; }; 3578590A1B28D2CF00341EDB /* libRCTLinking.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 357859011B28D2C500341EDB /* libRCTLinking.a */; }; 3DB99D0C1BA0340600302749 /* UIExplorerIntegrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3DB99D0B1BA0340600302749 /* UIExplorerIntegrationTests.m */; }; + 3DEFCE371D33A67200256E76 /* TestBundle.js in Resources */ = {isa = PBXBuildFile; fileRef = 3DEFCE351D33A61500256E76 /* TestBundle.js */; }; 68FF44381CF6111500720EFD /* RCTBundleURLProviderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 68FF44371CF6111500720EFD /* RCTBundleURLProviderTests.m */; }; 834C36EC1AF8DED70019C93C /* libRCTSettings.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 834C36D21AF8DA610019C93C /* libRCTSettings.a */; }; 83636F8F1B53F22C009F943E /* RCTUIManagerScenarioTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 83636F8E1B53F22C009F943E /* RCTUIManagerScenarioTests.m */; }; @@ -220,12 +221,6 @@ 13DF61B51B67A45000EDB188 /* RCTMethodArgumentTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTMethodArgumentTests.m; sourceTree = ""; }; 13E5019C1D07A502005F35D8 /* RCTAnimation.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTAnimation.xcodeproj; path = ../../Libraries/NativeAnimation/RCTAnimation.xcodeproj; sourceTree = ""; }; 143BC57E1B21E18100462512 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 143BC5811B21E18100462512 /* testLayoutExampleSnapshot_1@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "testLayoutExampleSnapshot_1@2x.png"; sourceTree = ""; }; - 143BC5821B21E18100462512 /* testSliderExampleSnapshot_1@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "testSliderExampleSnapshot_1@2x.png"; sourceTree = ""; }; - 143BC5831B21E18100462512 /* testSwitchExampleSnapshot_1@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "testSwitchExampleSnapshot_1@2x.png"; sourceTree = ""; }; - 143BC5841B21E18100462512 /* testTabBarExampleSnapshot_1@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "testTabBarExampleSnapshot_1@2x.png"; sourceTree = ""; }; - 143BC5851B21E18100462512 /* testTextExampleSnapshot_1@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "testTextExampleSnapshot_1@2x.png"; sourceTree = ""; }; - 143BC5861B21E18100462512 /* testViewExampleSnapshot_1@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "testViewExampleSnapshot_1@2x.png"; sourceTree = ""; }; 143BC5951B21E3E100462512 /* UIExplorerIntegrationTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = UIExplorerIntegrationTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 143BC5981B21E3E100462512 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 143BC5A01B21E45C00462512 /* UIExplorerSnapshotTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UIExplorerSnapshotTests.m; sourceTree = ""; }; @@ -257,6 +252,7 @@ 27F441EA1BEBE5030039B79C /* FlexibleSizeExampleView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FlexibleSizeExampleView.h; path = UIExplorer/NativeExampleViews/FlexibleSizeExampleView.h; sourceTree = ""; }; 357858F81B28D2C400341EDB /* RCTLinking.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTLinking.xcodeproj; path = ../../Libraries/LinkingIOS/RCTLinking.xcodeproj; sourceTree = ""; }; 3DB99D0B1BA0340600302749 /* UIExplorerIntegrationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UIExplorerIntegrationTests.m; sourceTree = ""; }; + 3DEFCE351D33A61500256E76 /* TestBundle.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = TestBundle.js; sourceTree = ""; }; 58005BE41ABA80530062E044 /* RCTTest.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTTest.xcodeproj; path = ../../Libraries/RCTTest/RCTTest.xcodeproj; sourceTree = ""; }; 68FF44371CF6111500720EFD /* RCTBundleURLProviderTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTBundleURLProviderTests.m; sourceTree = ""; }; 83636F8E1B53F22C009F943E /* RCTUIManagerScenarioTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTUIManagerScenarioTests.m; sourceTree = ""; }; @@ -344,6 +340,7 @@ 1323F18D1C04ABAC0091BED0 /* Supporting Files */ = { isa = PBXGroup; children = ( + 3DEFCE351D33A61500256E76 /* TestBundle.js */, 13B07FB61A68108700A75B9A /* Info.plist */, 1323F1851C04AB9F0091BED0 /* bunny.png */, 1323F1861C04AB9F0091BED0 /* flux@3x.png */, @@ -459,32 +456,10 @@ 143BC57E1B21E18100462512 /* Info.plist */, 14D6D7101B220EB3001FB087 /* libOCMock.a */, 14D6D7011B220AE3001FB087 /* OCMock */, - 143BC57F1B21E18100462512 /* ReferenceImages */, ); path = UIExplorerUnitTests; sourceTree = ""; }; - 143BC57F1B21E18100462512 /* ReferenceImages */ = { - isa = PBXGroup; - children = ( - 143BC5801B21E18100462512 /* Examples-UIExplorer-UIExplorerApp */, - ); - path = ReferenceImages; - sourceTree = ""; - }; - 143BC5801B21E18100462512 /* Examples-UIExplorer-UIExplorerApp */ = { - isa = PBXGroup; - children = ( - 143BC5811B21E18100462512 /* testLayoutExampleSnapshot_1@2x.png */, - 143BC5821B21E18100462512 /* testSliderExampleSnapshot_1@2x.png */, - 143BC5831B21E18100462512 /* testSwitchExampleSnapshot_1@2x.png */, - 143BC5841B21E18100462512 /* testTabBarExampleSnapshot_1@2x.png */, - 143BC5851B21E18100462512 /* testTextExampleSnapshot_1@2x.png */, - 143BC5861B21E18100462512 /* testViewExampleSnapshot_1@2x.png */, - ); - path = "Examples-UIExplorer-UIExplorerApp"; - sourceTree = ""; - }; 143BC5961B21E3E100462512 /* UIExplorerIntegrationTests */ = { isa = PBXGroup; children = ( @@ -887,6 +862,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 3DEFCE371D33A67200256E76 /* TestBundle.js in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Examples/UIExplorer/UIExplorerUnitTests/RCTBridgeTests.m b/Examples/UIExplorer/UIExplorerUnitTests/RCTBridgeTests.m index 15744669b..8a6ae4212 100644 --- a/Examples/UIExplorer/UIExplorerUnitTests/RCTBridgeTests.m +++ b/Examples/UIExplorer/UIExplorerUnitTests/RCTBridgeTests.m @@ -92,6 +92,11 @@ RCT_EXPORT_MODULE() block(); } +- (void)executeAsyncBlockOnJavaScriptQueue:(dispatch_block_t)block +{ + block(); +} + - (void)injectJSONText:(NSString *)script asGlobalObjectNamed:(NSString *)objectName callback:(RCTJavaScriptCompleteBlock)onComplete @@ -151,7 +156,8 @@ RCT_EXPORT_MODULE(TestModule) [super setUp]; _unregisteredTestModule = [UnregisteredTestModule new]; - _bridge = [[RCTBridge alloc] initWithBundleURL:nil + NSBundle *bundle = [NSBundle bundleForClass:[self class]]; + _bridge = [[RCTBridge alloc] initWithBundleURL:[bundle URLForResource:@"TestBundle" withExtension:@"js"] moduleProvider:^{ return @[self, self->_unregisteredTestModule]; } launchOptions:nil]; diff --git a/Examples/UIExplorer/UIExplorerUnitTests/RCTComponentPropsTests.m b/Examples/UIExplorer/UIExplorerUnitTests/RCTComponentPropsTests.m index 75a32bdac..248a42455 100644 --- a/Examples/UIExplorer/UIExplorerUnitTests/RCTComponentPropsTests.m +++ b/Examples/UIExplorer/UIExplorerUnitTests/RCTComponentPropsTests.m @@ -95,7 +95,8 @@ RCT_CUSTOM_VIEW_PROPERTY(customProp, NSString, RCTPropsTestView) { [super setUp]; - _bridge = [[RCTBridge alloc] initWithBundleURL:nil + NSBundle *bundle = [NSBundle bundleForClass:[self class]]; + _bridge = [[RCTBridge alloc] initWithBundleURL:[bundle URLForResource:@"TestBundle" withExtension:@"js"] moduleProvider:nil launchOptions:nil]; } diff --git a/Examples/UIExplorer/UIExplorerUnitTests/RCTImageLoaderTests.m b/Examples/UIExplorer/UIExplorerUnitTests/RCTImageLoaderTests.m index 8de60a199..e403daf54 100644 --- a/Examples/UIExplorer/UIExplorerUnitTests/RCTImageLoaderTests.m +++ b/Examples/UIExplorer/UIExplorerUnitTests/RCTImageLoaderTests.m @@ -33,7 +33,15 @@ RCTDefineImageDecoder(RCTImageLoaderTestsDecoder2) @end -@implementation RCTImageLoaderTests +@implementation RCTImageLoaderTests { + NSURL *_bundleURL; +} + +- (void)setUp +{ + NSBundle *bundle = [NSBundle bundleForClass:[self class]]; + _bundleURL = [bundle URLForResource:@"TestBundle" withExtension:@"js"]; +} - (void)testImageLoading { @@ -47,7 +55,7 @@ RCTDefineImageDecoder(RCTImageLoaderTestsDecoder2) return nil; }]; - NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil moduleProvider:^{ return @[loader]; } launchOptions:nil]; + NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL moduleProvider:^{ return @[loader]; } launchOptions:nil]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://facebook.github.io/react/img/logo_og.png"]]; [bridge.imageLoader loadImageWithURLRequest:urlRequest size:CGSizeMake(100, 100) scale:1.0 clipped:YES resizeMode:RCTResizeModeContain progressBlock:^(int64_t progress, int64_t total) { @@ -78,7 +86,7 @@ RCTDefineImageDecoder(RCTImageLoaderTestsDecoder2) return nil; }]; - NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil moduleProvider:^{ return @[loader1, loader2]; } launchOptions:nil]; + NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL moduleProvider:^{ return @[loader1, loader2]; } launchOptions:nil]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://facebook.github.io/react/img/logo_og.png"]]; [bridge.imageLoader loadImageWithURLRequest:urlRequest size:CGSizeMake(100, 100) scale:1.0 clipped:YES resizeMode:RCTResizeModeContain progressBlock:^(int64_t progress, int64_t total) { @@ -103,7 +111,7 @@ RCTDefineImageDecoder(RCTImageLoaderTestsDecoder2) return nil; }]; - NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil moduleProvider:^{ return @[decoder]; } launchOptions:nil]; + NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL moduleProvider:^{ return @[decoder]; } launchOptions:nil]; RCTImageLoaderCancellationBlock cancelBlock = [bridge.imageLoader decodeImageData:data size:CGSizeMake(1, 1) scale:1.0 clipped:NO resizeMode:RCTResizeModeStretch completionBlock:^(NSError *decodeError, id decodedImage) { XCTAssertEqualObjects(decodedImage, image); @@ -132,7 +140,7 @@ RCTDefineImageDecoder(RCTImageLoaderTestsDecoder2) return nil; }]; - NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil moduleProvider:^{ return @[decoder1, decoder2]; } launchOptions:nil]; + NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL moduleProvider:^{ return @[decoder1, decoder2]; } launchOptions:nil]; RCTImageLoaderCancellationBlock cancelBlock = [bridge.imageLoader decodeImageData:data size:CGSizeMake(1, 1) scale:1.0 clipped:NO resizeMode:RCTResizeModeStretch completionBlock:^(NSError *decodeError, id decodedImage) { XCTAssertEqualObjects(decodedImage, image); diff --git a/Examples/UIExplorer/UIExplorerUnitTests/RCTModuleInitNotificationRaceTests.m b/Examples/UIExplorer/UIExplorerUnitTests/RCTModuleInitNotificationRaceTests.m index 2e9220d2d..82542de06 100644 --- a/Examples/UIExplorer/UIExplorerUnitTests/RCTModuleInitNotificationRaceTests.m +++ b/Examples/UIExplorer/UIExplorerUnitTests/RCTModuleInitNotificationRaceTests.m @@ -99,7 +99,8 @@ RCT_EXPORT_MODULE() - (NSURL *)sourceURLForBridge:(__unused RCTBridge *)bridge { - return nil; + NSBundle *bundle = [NSBundle bundleForClass:[self class]]; + return [bundle URLForResource:@"TestBundle" withExtension:@"js"]; } - (NSArray *)extraModulesForBridge:(__unused RCTBridge *)bridge diff --git a/Examples/UIExplorer/UIExplorerUnitTests/RCTModuleInitTests.m b/Examples/UIExplorer/UIExplorerUnitTests/RCTModuleInitTests.m index 0de479392..215a5a202 100644 --- a/Examples/UIExplorer/UIExplorerUnitTests/RCTModuleInitTests.m +++ b/Examples/UIExplorer/UIExplorerUnitTests/RCTModuleInitTests.m @@ -147,7 +147,8 @@ RCT_EXPORT_MODULE() - (NSURL *)sourceURLForBridge:(__unused RCTBridge *)bridge { - return nil; + NSBundle *bundle = [NSBundle bundleForClass:[self class]]; + return [bundle URLForResource:@"TestBundle" withExtension:@"js"]; } - (NSArray *)extraModulesForBridge:(__unused RCTBridge *)bridge diff --git a/React/Base/RCTBatchedBridge.m b/React/Base/RCTBatchedBridge.m index 1f80fa390..2e589b6e3 100644 --- a/React/Base/RCTBatchedBridge.m +++ b/React/Base/RCTBatchedBridge.m @@ -171,7 +171,9 @@ RCT_EXTERN NSArray *RCTGetModuleClasses(void); if ([self.delegate respondsToSelector:@selector(loadSourceForBridge:withBlock:)]) { [self.delegate loadSourceForBridge:_parentBridge withBlock:onSourceLoad]; - } else if (self.bundleURL) { + } else { + RCTAssert(self.bundleURL, @"bundleURL must be non-nil when not implementing loadSourceForBridge"); + [RCTJavaScriptLoader loadBundleAtURL:self.bundleURL onComplete:^(NSError *error, NSData *source, int64_t sourceLength) { if (error && [self.delegate respondsToSelector:@selector(fallbackSourceURLForBridge:)]) { NSURL *fallbackURL = [self.delegate fallbackSourceURLForBridge:self->_parentBridge]; @@ -184,17 +186,6 @@ RCT_EXTERN NSArray *RCTGetModuleClasses(void); } onSourceLoad(error, source, sourceLength); }]; - } else { - // Allow testing without a script - dispatch_async(dispatch_get_main_queue(), ^{ - [[NSNotificationCenter defaultCenter] - postNotificationName:RCTJavaScriptDidLoadNotification - object:self->_parentBridge userInfo:@{@"bridge": self}]; - }); - - [self flushPendingCalls]; - - onSourceLoad(nil, nil, 0); } } @@ -487,11 +478,10 @@ RCT_EXTERN NSArray *RCTGetModuleClasses(void); object:self->_parentBridge userInfo:@{@"bridge": self}]; }); - [self flushPendingCalls]; + [self _flushPendingCalls]; }]; #if RCT_DEV - if ([RCTGetURLQueryParam(self.bundleURL, @"hot") boolValue]) { NSString *path = [self.bundleURL.path substringFromIndex:1]; // strip initial slash NSString *host = self.bundleURL.host; @@ -501,18 +491,15 @@ RCT_EXTERN NSArray *RCTGetModuleClasses(void); #endif } -- (void)flushPendingCalls +- (void)_flushPendingCalls { + RCTAssertJSThread(); [_performanceLogger markStopForTag:RCTPLBridgeStartup]; - [_javaScriptExecutor executeBlockOnJavaScriptQueue:^{ - for (dispatch_block_t call in self->_pendingCalls) { - _loading = NO; - - for (dispatch_block_t call in _pendingCalls) { - call(); - } - }]; + _loading = NO; + for (dispatch_block_t call in self->_pendingCalls) { + call(); + } } - (void)stopLoadingWithError:(NSError *)error