diff --git a/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj b/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj index 05416d424..49a27dc61 100644 --- a/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj +++ b/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj @@ -50,8 +50,9 @@ 14D6D7281B2222EF001FB087 /* libRCTWebSocket.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 139FDED91B0651EA00C62182 /* libRCTWebSocket.a */; }; 14D6D7291B2222EF001FB087 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 14AADF041AC3DB95002390C9 /* libReact.a */; }; 14DC67F41AB71881001358AB /* libRCTPushNotification.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 14DC67F11AB71876001358AB /* libRCTPushNotification.a */; }; - 272E6B3F1BEA849E001FCF37 /* UpdatePropertiesExampleView.m in Sources */ = {isa = PBXBuildFile; fileRef = 272E6B3C1BEA849E001FCF37 /* UpdatePropertiesExampleView.m */; }; - 27F441EC1BEBE5030039B79C /* FlexibleSizeExampleView.m in Sources */ = {isa = PBXBuildFile; fileRef = 27F441E81BEBE5030039B79C /* FlexibleSizeExampleView.m */; }; + 272E6B3F1BEA849E001FCF37 /* UpdatePropertiesExampleView.m in Sources */ = {isa = PBXBuildFile; fileRef = 272E6B3C1BEA849E001FCF37 /* UpdatePropertiesExampleView.m */; settings = {ASSET_TAGS = (); }; }; + 27F441EC1BEBE5030039B79C /* FlexibleSizeExampleView.m in Sources */ = {isa = PBXBuildFile; fileRef = 27F441E81BEBE5030039B79C /* FlexibleSizeExampleView.m */; settings = {ASSET_TAGS = (); }; }; + 27B885561BED29AF00008352 /* RCTRootViewIntegrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 27B885551BED29AF00008352 /* RCTRootViewIntegrationTests.m */; settings = {ASSET_TAGS = (); }; }; 3578590A1B28D2CF00341EDB /* libRCTLinking.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 357859011B28D2C500341EDB /* libRCTLinking.a */; }; 3D36915B1BDA8CBB007B22D8 /* uie_thumb_big.png in Resources */ = {isa = PBXBuildFile; fileRef = 3D36915A1BDA8CBB007B22D8 /* uie_thumb_big.png */; }; 3DB99D0C1BA0340600302749 /* UIExplorerIntegrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3DB99D0B1BA0340600302749 /* UIExplorerIntegrationTests.m */; }; @@ -228,6 +229,7 @@ 272E6B3C1BEA849E001FCF37 /* UpdatePropertiesExampleView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = UpdatePropertiesExampleView.m; path = UIExplorer/NativeExampleViews/UpdatePropertiesExampleView.m; sourceTree = ""; }; 27F441E81BEBE5030039B79C /* FlexibleSizeExampleView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FlexibleSizeExampleView.m; path = UIExplorer/NativeExampleViews/FlexibleSizeExampleView.m; sourceTree = ""; }; 27F441EA1BEBE5030039B79C /* FlexibleSizeExampleView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FlexibleSizeExampleView.h; path = UIExplorer/NativeExampleViews/FlexibleSizeExampleView.h; sourceTree = ""; }; + 27B885551BED29AF00008352 /* RCTRootViewIntegrationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTRootViewIntegrationTests.m; sourceTree = ""; }; 357858F81B28D2C400341EDB /* RCTLinking.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTLinking.xcodeproj; path = ../../Libraries/LinkingIOS/RCTLinking.xcodeproj; sourceTree = ""; }; 3D36915A1BDA8CBB007B22D8 /* uie_thumb_big.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = uie_thumb_big.png; path = UIExplorer/Images.xcassets/uie_thumb_big.imageset/uie_thumb_big.png; sourceTree = SOURCE_ROOT; }; 3DB99D0B1BA0340600302749 /* UIExplorerIntegrationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UIExplorerIntegrationTests.m; sourceTree = ""; }; @@ -434,6 +436,7 @@ 143BC5961B21E3E100462512 /* UIExplorerIntegrationTests */ = { isa = PBXGroup; children = ( + 27B885551BED29AF00008352 /* RCTRootViewIntegrationTests.m */, 3DB99D0B1BA0340600302749 /* UIExplorerIntegrationTests.m */, 143BC5A01B21E45C00462512 /* UIExplorerSnapshotTests.m */, 83636F8E1B53F22C009F943E /* RCTUIManagerScenarioTests.m */, @@ -886,6 +889,7 @@ 3DB99D0C1BA0340600302749 /* UIExplorerIntegrationTests.m in Sources */, 83636F8F1B53F22C009F943E /* RCTUIManagerScenarioTests.m in Sources */, 143BC5A11B21E45C00462512 /* UIExplorerSnapshotTests.m in Sources */, + 27B885561BED29AF00008352 /* RCTRootViewIntegrationTests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Examples/UIExplorer/UIExplorerIntegrationTests/RCTRootViewIntegrationTests.m b/Examples/UIExplorer/UIExplorerIntegrationTests/RCTRootViewIntegrationTests.m new file mode 100644 index 000000000..9e665b4e1 --- /dev/null +++ b/Examples/UIExplorer/UIExplorerIntegrationTests/RCTRootViewIntegrationTests.m @@ -0,0 +1,172 @@ +/** + * Copyright (c) 2015-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. + */ + +//vs + +/** + * The examples provided by Facebook are for non-commercial testing and + * evaluation purposes only. + * + * Facebook reserves all rights not expressly granted. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL + * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#import +#import + +#import "RCTAssert.h" + +#import "RCTEventDispatcher.h" +#import "RCTRootView.h" +#import "RCTRootViewDelegate.h" + +#import + +#define RCT_TEST_DATA_CONFIGURATION_BLOCK(appName, testType, input, block) \ +- (void)test##appName##_##testType##_##input \ +{ \ + [_runner runTest:_cmd \ + module:@#appName \ + initialProps:@{@#input:@YES} \ +configurationBlock:block]; \ +} + +#define RCT_TEST_CONFIGURATION_BLOCK(appName, block) \ +- (void)test##appName \ +{ \ + [_runner runTest:_cmd \ + module:@#appName \ + initialProps:nil \ +configurationBlock:block]; \ +} + +#define RCTNone RCTRootViewSizeFlexibilityNone +#define RCTHeight RCTRootViewSizeFlexibilityHeight +#define RCTWidth RCTRootViewSizeFlexibilityWidth +#define RCTBoth RCTRootViewSizeFlexibilityWidthAndHeight + +typedef void (^ControlBlock)(RCTRootView*); + +@interface SizeFlexibilityTestDelegate : NSObject +@end + +@implementation SizeFlexibilityTestDelegate + +- (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView +{ + [rootView.bridge.eventDispatcher sendAppEventWithName:@"rootViewDidChangeIntrinsicSize" + body:@{@"width": @(rootView.intrinsicSize.width), + @"height": @(rootView.intrinsicSize.height)}]; +} + +@end + +static SizeFlexibilityTestDelegate *sizeFlexibilityDelegate() +{ + static SizeFlexibilityTestDelegate *delegate; + if (delegate == nil) { + delegate = [SizeFlexibilityTestDelegate new]; + } + + return delegate; +} + +static ControlBlock simpleSizeFlexibilityBlock(RCTRootViewSizeFlexibility sizeFlexibility) +{ + return ^(RCTRootView *rootView){ + rootView.delegate = sizeFlexibilityDelegate(); + rootView.sizeFlexibility = sizeFlexibility; + }; +} + +static ControlBlock multipleSizeFlexibilityUpdatesBlock(RCTRootViewSizeFlexibility finalSizeFlexibility) +{ + return ^(RCTRootView *rootView){ + + NSInteger arr[4] = {RCTNone, + RCTHeight, + RCTWidth, + RCTBoth}; + + rootView.delegate = sizeFlexibilityDelegate(); + + for (int i = 0; i < 4; ++i) { + if (arr[i] != finalSizeFlexibility) { + rootView.sizeFlexibility = arr[i]; + } + } + + rootView.sizeFlexibility = finalSizeFlexibility; + }; +} + +static ControlBlock reactContentSizeUpdateBlock(RCTRootViewSizeFlexibility sizeFlexibility) +{ + return ^(RCTRootView *rootView){ + rootView.delegate = sizeFlexibilityDelegate(); + rootView.sizeFlexibility = sizeFlexibility; + }; +} + +static ControlBlock propertiesUpdateBlock() +{ + return ^(RCTRootView *rootView){ + rootView.appProperties = @{@"markTestPassed":@YES}; + }; +} + +@interface RCTRootViewIntegrationTests : XCTestCase + +@end + +@implementation RCTRootViewIntegrationTests +{ + RCTTestRunner *_runner; +} + +- (void)setUp +{ +#if __LP64__ + RCTAssert(NO, @"Tests should be run on 32-bit device simulators (e.g. iPhone 5)"); +#endif + + NSOperatingSystemVersion version = [NSProcessInfo processInfo].operatingSystemVersion; + RCTAssert((version.majorVersion == 8 && version.minorVersion >= 3) || version.majorVersion >= 9, @"Tests should be run on iOS 8.3+, found %zd.%zd.%zd", version.majorVersion, version.minorVersion, version.patchVersion); + _runner = RCTInitRunnerForApp(@"IntegrationTests/RCTRootViewIntegrationTestApp", nil); +} + +#pragma mark Logic Tests + +// This list should be kept in sync with RCTRootViewIntegrationTestApp.js + +// Simple size flexibility tests - test if the content is measured properly +RCT_TEST_DATA_CONFIGURATION_BLOCK(SizeFlexibilityUpdateTest, SingleUpdate, none, simpleSizeFlexibilityBlock(RCTNone)); +RCT_TEST_DATA_CONFIGURATION_BLOCK(SizeFlexibilityUpdateTest, SingleUpdate, height, simpleSizeFlexibilityBlock(RCTHeight)); +RCT_TEST_DATA_CONFIGURATION_BLOCK(SizeFlexibilityUpdateTest, SingleUpdate, width, simpleSizeFlexibilityBlock(RCTWidth)); +RCT_TEST_DATA_CONFIGURATION_BLOCK(SizeFlexibilityUpdateTest, SingleUpdate, both, simpleSizeFlexibilityBlock(RCTBoth)); + +// Consider multiple size flexibility updates in a row. Test if the view's flexibility mode eventually is set to the expected value +RCT_TEST_DATA_CONFIGURATION_BLOCK(SizeFlexibilityUpdateTest, MultipleUpdates, none, multipleSizeFlexibilityUpdatesBlock(RCTNone)); +RCT_TEST_DATA_CONFIGURATION_BLOCK(SizeFlexibilityUpdateTest, MultipleUpdates, height, multipleSizeFlexibilityUpdatesBlock(RCTHeight)); +RCT_TEST_DATA_CONFIGURATION_BLOCK(SizeFlexibilityUpdateTest, MultipleUpdates, width, multipleSizeFlexibilityUpdatesBlock(RCTWidth)); +RCT_TEST_DATA_CONFIGURATION_BLOCK(SizeFlexibilityUpdateTest, MultipleUpdates, both, multipleSizeFlexibilityUpdatesBlock(RCTBoth)); + +// Test if the 'rootViewDidChangeIntrinsicSize' delegate method is called after the RN app decides internally to resize +RCT_TEST_CONFIGURATION_BLOCK(ReactContentSizeUpdateTest, reactContentSizeUpdateBlock(RCTBoth)) + +// Test if setting 'appProperties' property updates the RN app +RCT_TEST_CONFIGURATION_BLOCK(PropertiesUpdateTest, propertiesUpdateBlock()) + +@end diff --git a/IntegrationTests/PropertiesUpdateTest.js b/IntegrationTests/PropertiesUpdateTest.js new file mode 100644 index 000000000..da563ec94 --- /dev/null +++ b/IntegrationTests/PropertiesUpdateTest.js @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2015-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. + */ +'use strict'; + +var React = require('react-native'); +var { + View, +} = React; + +var { TestModule } = React.addons; + +var PropertiesUpdateTest = React.createClass({ + + render() { + if (this.props.markTestPassed) { + TestModule.markTestPassed(true); + } + return ( + + ); + } +}); + +PropertiesUpdateTest.displayName = 'PropertiesUpdateTest'; + +module.exports = PropertiesUpdateTest; diff --git a/IntegrationTests/RCTRootViewIntegrationTestApp.js b/IntegrationTests/RCTRootViewIntegrationTestApp.js new file mode 100644 index 000000000..5a126a601 --- /dev/null +++ b/IntegrationTests/RCTRootViewIntegrationTestApp.js @@ -0,0 +1,94 @@ +/** + * Copyright (c) 2015-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. + * + * @providesModule RCTRootViewIntegrationTestsApp + */ +'use strict'; + +require('regenerator/runtime'); + +var React = require('react-native'); + +var { + AppRegistry, + ScrollView, + StyleSheet, + Text, + TouchableOpacity, + View, +} = React; + +/* Keep this list in sync with RCTRootViewIntegrationTests.m */ +var TESTS = [ + require('./PropertiesUpdateTest'), + require('./ReactContentSizeUpdateTest'), + require('./SizeFlexibilityUpdateTest'), +]; + +TESTS.forEach( + (test) => AppRegistry.registerComponent(test.displayName, () => test) +); + +var RCTRootViewIntegrationTestsApp = React.createClass({ + getInitialState: function() { + return { + test: null, + }; + }, + render: function() { + if (this.state.test) { + return ( + + + + ); + } + return ( + + + Click on a test to run it in this shell for easier debugging and + development. Run all tests in the testing environment with cmd+U in + Xcode. + + + + {TESTS.map((test) => [ + this.setState({test})} + style={styles.row}> + + {test.displayName} + + , + + ])} + + + ); + } +}); + +var styles = StyleSheet.create({ + container: { + backgroundColor: 'white', + marginTop: 40, + margin: 15, + }, + row: { + padding: 10, + }, + testName: { + fontWeight: '500', + }, + separator: { + height: 1, + backgroundColor: '#bbbbbb', + }, +}); + +AppRegistry.registerComponent('RCTRootViewIntegrationTestsApp', () => RCTRootViewIntegrationTestsApp); diff --git a/IntegrationTests/ReactContentSizeUpdateTest.js b/IntegrationTests/ReactContentSizeUpdateTest.js new file mode 100644 index 000000000..73e1c5f5d --- /dev/null +++ b/IntegrationTests/ReactContentSizeUpdateTest.js @@ -0,0 +1,73 @@ +/** + * Copyright (c) 2015-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. + */ +'use strict'; + +var React = require('react-native'); +var RCTNativeAppEventEmitter = require('RCTNativeAppEventEmitter'); +var Subscribable = require('Subscribable'); +var TimerMixin = require('react-timer-mixin'); + +var { View } = React; + +var { TestModule } = React.addons; + +var reactViewWidth = 101; +var reactViewHeight = 102; +var newReactViewWidth = 201; +var newReactViewHeight = 202; + +var ReactContentSizeUpdateTest = React.createClass({ + mixins: [Subscribable.Mixin, + TimerMixin], + + componentWillMount: function() { + this.addListenerOn( + RCTNativeAppEventEmitter, + 'rootViewDidChangeIntrinsicSize', + this.rootViewDidChangeIntrinsicSize + ); + }, + + getInitialState: function() { + return { + height: reactViewHeight, + width: reactViewWidth, + }; + }, + + updateViewSize: function() { + this.setState({ + height: newReactViewHeight, + width: newReactViewWidth, + }); + }, + + componentDidMount: function() { + this.setTimeout( + () => { this.updateViewSize(); }, + 1000 + ); + }, + + rootViewDidChangeIntrinsicSize: function(intrinsicSize) { + if (intrinsicSize.height === newReactViewHeight && intrinsicSize.width === newReactViewWidth) { + TestModule.markTestPassed(true); + } + }, + + render() { + return ( + + ); + } +}); + +ReactContentSizeUpdateTest.displayName = 'ReactContentSizeUpdateTest'; + +module.exports = ReactContentSizeUpdateTest; diff --git a/IntegrationTests/SizeFlexibilityUpdateTest.js b/IntegrationTests/SizeFlexibilityUpdateTest.js new file mode 100644 index 000000000..ddc016b70 --- /dev/null +++ b/IntegrationTests/SizeFlexibilityUpdateTest.js @@ -0,0 +1,82 @@ +/** + * Copyright (c) 2015-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. + */ +'use strict'; + +var React = require('react-native'); +var RCTNativeAppEventEmitter = require('RCTNativeAppEventEmitter'); +var Subscribable = require('Subscribable'); +var { View } = React; + +var { TestModule } = React.addons; + +var reactViewWidth = 111; +var reactViewHeight = 222; + +var finalState = false; + +var SizeFlexibilityUpdateTest = React.createClass({ + mixins: [Subscribable.Mixin], + + componentWillMount: function() { + this.addListenerOn( + RCTNativeAppEventEmitter, + 'rootViewDidChangeIntrinsicSize', + this.rootViewDidChangeIntrinsicSize + ); + }, + + markPassed: function() { + TestModule.markTestPassed(true); + finalState = true; + }, + + rootViewDidChangeIntrinsicSize: function(intrinsicSize) { + + if (finalState) { + // If a test reaches its final state, it is not expected to do anything more + TestModule.markTestPassed(false); + return; + } + + if (this.props.both) { + if (intrinsicSize.width === reactViewWidth && intrinsicSize.height === reactViewHeight) { + this.markPassed(); + return; + } + } + if (this.props.height) { + if (intrinsicSize.width !== reactViewWidth && intrinsicSize.height === reactViewHeight) { + this.markPassed(); + return; + } + } + if (this.props.width) { + if (intrinsicSize.width === reactViewWidth && intrinsicSize.height !== reactViewHeight) { + this.markPassed(); + return; + } + } + if (this.props.none) { + if (intrinsicSize.width !== reactViewWidth && intrinsicSize.height !== reactViewHeight) { + this.markPassed(); + return; + } + } + }, + + render() { + return ( + + ); + } +}); + +SizeFlexibilityUpdateTest.displayName = 'SizeFlexibilityUpdateTest'; + +module.exports = SizeFlexibilityUpdateTest;