From 7cece342336fdbb40b45505965d8d0ddfcbd81fe Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Fri, 11 Feb 2022 13:44:33 -0800 Subject: [PATCH] test RCTView with c swizzling example Summary: writing a test that has an example of c swizzling in oss. testing this: https://www.internalfb.com/code/fbsource/[c58818169205f1e0fa816968efdb4c3fac8333e9]/xplat/js/react-native-github/React/Views/RCTView.m?lines=452-454%2C460-468 Changelog: [Internal] Reviewed By: RSNara Differential Revision: D31949237 fbshipit-source-id: f16c98ec1a736f3f2152d1e411b693083519a7b9 --- React/Views/RCTView.m | 5 ++ packages/rn-tester/Podfile.lock | 6 +-- .../RNTesterPods.xcodeproj/project.pbxproj | 4 ++ .../RNTesterUnitTests/RCTViewTests.m | 48 +++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 packages/rn-tester/RNTesterUnitTests/RCTViewTests.m diff --git a/React/Views/RCTView.m b/React/Views/RCTView.m index a18da5c92389d0..03dcc94d9310dc 100644 --- a/React/Views/RCTView.m +++ b/React/Views/RCTView.m @@ -7,6 +7,8 @@ #import "RCTView.h" +#import + #import "RCTAutoInsetsProtocol.h" #import "RCTBorderDrawing.h" #import "RCTI18nUtil.h" @@ -14,6 +16,9 @@ #import "RCTViewUtils.h" #import "UIView+React.h" +RCT_MOCK_DEF(RCTView, RCTContentInsets); +#define RCTContentInsets RCT_MOCK_USE(RCTView, RCTContentInsets) + UIAccessibilityTraits const SwitchAccessibilityTrait = 0x20000000000001; @implementation UIView (RCTViewUnmounting) diff --git a/packages/rn-tester/Podfile.lock b/packages/rn-tester/Podfile.lock index 7e31588013f9be..abe32ab3475116 100644 --- a/packages/rn-tester/Podfile.lock +++ b/packages/rn-tester/Podfile.lock @@ -884,7 +884,7 @@ SPEC CHECKSUMS: CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99 DoubleConversion: 831926d9b8bf8166fd87886c4abab286c2422662 FBLazyVector: b81a2b70c72d8b0aefb652cea22c11e9ffd02949 - FBReactNativeSpec: 277e2460e8a1a20883306c886d5d57a361e32a71 + FBReactNativeSpec: fdfe4008ff58c7c6e3ffd5ad938b9dfa2cc7e7a2 Flipper: 26fc4b7382499f1281eb8cb921e5c3ad6de91fe0 Flipper-Boost-iOSX: fd1e2b8cbef7e662a122412d7ac5f5bea715403c Flipper-DoubleConversion: 57ffbe81ef95306cc9e69c4aa3aeeeeb58a6a28c @@ -926,10 +926,10 @@ SPEC CHECKSUMS: React-RCTTest: 12bbd7fc2e72bd9920dc7286c5b8ef96639582b6 React-RCTText: e9146b2c0550a83d1335bfe2553760070a2d75c7 React-RCTVibration: 50be9c390f2da76045ef0dfdefa18b9cf9f35cfa - React-rncore: 252dd9c510174ba718e358d332a43ad7056e9cb0 + React-rncore: 9d392094961ad6270bd39d5ea0237cccdb1cc081 React-runtimeexecutor: 4b0c6eb341c7d3ceb5e2385cb0fdb9bf701024f3 ReactCommon: 7a2714d1128f965392b6f99a8b390e3aa38c9569 - ScreenshotManager: 79ae4255728f14773097f05accaa53949bdd526c + ScreenshotManager: 646257771aab2075223caf1316e4b089001d3cb9 SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608 Yoga: c0d06f5380d34e939f55420669a60fe08b79bd75 YogaKit: f782866e155069a2cca2517aafea43200b01fd5a diff --git a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj index 668d70803b32a6..7db639b53b2647 100644 --- a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj +++ b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj @@ -48,6 +48,7 @@ E7DB20EB22B2BAA6005AC45F /* RCTConvert_YGValueTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20CE22B2BAA5005AC45F /* RCTConvert_YGValueTests.m */; }; E7DB20EC22B2BAA6005AC45F /* RCTMultipartStreamReaderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20CF22B2BAA5005AC45F /* RCTMultipartStreamReaderTests.m */; }; E7DB20ED22B2BAA6005AC45F /* RCTURLUtilsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20D022B2BAA5005AC45F /* RCTURLUtilsTests.m */; }; + E7DB20EE22B2BAA6005AC45F /* RCTViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20E022B2BAA5005AC45F /* RCTViewTests.m */; }; E7DB213122B2C649005AC45F /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E7DB213022B2C649005AC45F /* JavaScriptCore.framework */; }; E7DB213222B2C67D005AC45F /* libOCMock.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E7DB212422B2C342005AC45F /* libOCMock.a */; }; E7DB216222B2F3EC005AC45F /* RNTesterTestModule.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB215D22B2F3EC005AC45F /* RNTesterTestModule.m */; }; @@ -134,6 +135,7 @@ E7DB20CE22B2BAA5005AC45F /* RCTConvert_YGValueTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTConvert_YGValueTests.m; sourceTree = ""; }; E7DB20CF22B2BAA5005AC45F /* RCTMultipartStreamReaderTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTMultipartStreamReaderTests.m; sourceTree = ""; }; E7DB20D022B2BAA5005AC45F /* RCTURLUtilsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTURLUtilsTests.m; sourceTree = ""; }; + E7DB20E022B2BAA5005AC45F /* RCTViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTViewTests.m; sourceTree = ""; }; E7DB20F022B2BD53005AC45F /* libDoubleConversion.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libDoubleConversion.a; sourceTree = BUILT_PRODUCTS_DIR; }; E7DB20F222B2BD53005AC45F /* libFolly.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libFolly.a; sourceTree = BUILT_PRODUCTS_DIR; }; E7DB20F422B2BD53005AC45F /* libglog.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libglog.a; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -357,6 +359,7 @@ E7DB20BF22B2BAA4005AC45F /* RCTUIManagerTests.m */, E7DB20C122B2BAA4005AC45F /* RCTUnicodeDecodeTests.m */, E7DB20D022B2BAA5005AC45F /* RCTURLUtilsTests.m */, + E7DB20E022B2BAA5005AC45F /* RCTViewTests.m */, E7DB212422B2C342005AC45F /* libOCMock.a */, E7DB212522B2C342005AC45F /* OCMock */, E7DB20B322B2BAA4005AC45F /* RNTesterUnitTestsBundle.js */, @@ -718,6 +721,7 @@ E7DB20E822B2BAA6005AC45F /* RCTModuleMethodTests.mm in Sources */, E7DB20E222B2BAA6005AC45F /* RCTGzipTests.m in Sources */, E7DB20ED22B2BAA6005AC45F /* RCTURLUtilsTests.m in Sources */, + E7DB20EE22B2BAA6005AC45F /* RCTViewTests.m in Sources */, E7DB20D322B2BAA6005AC45F /* RCTBlobManagerTests.m in Sources */, E7DB20DC22B2BAA6005AC45F /* RCTUIManagerTests.m in Sources */, E7DB20E322B2BAA6005AC45F /* RCTAllocationTests.m in Sources */, diff --git a/packages/rn-tester/RNTesterUnitTests/RCTViewTests.m b/packages/rn-tester/RNTesterUnitTests/RCTViewTests.m new file mode 100644 index 00000000000000..1049ca511a59e7 --- /dev/null +++ b/packages/rn-tester/RNTesterUnitTests/RCTViewTests.m @@ -0,0 +1,48 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import +#import "OCMock/OCMock.h" + +#import +#import +#import +#import +#import + +RCT_MOCK_REF(RCTView, RCTContentInsets); + +UIEdgeInsets gContentInsets; +static UIEdgeInsets RCTContentInsetsMock(UIView *view) +{ + return gContentInsets; +} + +@interface RCTViewTests : XCTestCase +@end + +@implementation RCTViewTests + +- (void)testAutoAdjustInsetsUpdateOffsetNo +{ + RCT_MOCK_SET(RCTView, RCTContentInsets, RCTContentInsetsMock); + + RCTScrollView *parentView = OCMClassMock([RCTScrollView class]); + OCMStub([parentView contentInset]).andReturn(UIEdgeInsetsMake(1, 1, 1, 1)); + OCMStub([parentView automaticallyAdjustContentInsets]).andReturn(YES); + UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; + + gContentInsets = UIEdgeInsetsMake(1, 2, 3, 4); + [RCTView autoAdjustInsetsForView:parentView withScrollView:scrollView updateOffset:NO]; + + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(scrollView.contentInset, UIEdgeInsetsMake(2, 3, 4, 5))); + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(scrollView.scrollIndicatorInsets, UIEdgeInsetsMake(2, 3, 4, 5))); + + RCT_MOCK_RESET(RCTView, RCTContentInsets); +} + +@end