Skip to content

Commit

Permalink
test RCTView with c swizzling example
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philIip authored and facebook-github-bot committed Feb 11, 2022
1 parent 8b24ce6 commit 7cece34
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
5 changes: 5 additions & 0 deletions React/Views/RCTView.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@

#import "RCTView.h"

#import <React/RCTMockDef.h>

#import "RCTAutoInsetsProtocol.h"
#import "RCTBorderDrawing.h"
#import "RCTI18nUtil.h"
#import "RCTLog.h"
#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)
Expand Down
6 changes: 3 additions & 3 deletions packages/rn-tester/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -134,6 +135,7 @@
E7DB20CE22B2BAA5005AC45F /* RCTConvert_YGValueTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTConvert_YGValueTests.m; sourceTree = "<group>"; };
E7DB20CF22B2BAA5005AC45F /* RCTMultipartStreamReaderTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTMultipartStreamReaderTests.m; sourceTree = "<group>"; };
E7DB20D022B2BAA5005AC45F /* RCTURLUtilsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTURLUtilsTests.m; sourceTree = "<group>"; };
E7DB20E022B2BAA5005AC45F /* RCTViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTViewTests.m; sourceTree = "<group>"; };
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; };
Expand Down Expand Up @@ -357,6 +359,7 @@
E7DB20BF22B2BAA4005AC45F /* RCTUIManagerTests.m */,
E7DB20C122B2BAA4005AC45F /* RCTUnicodeDecodeTests.m */,
E7DB20D022B2BAA5005AC45F /* RCTURLUtilsTests.m */,
E7DB20E022B2BAA5005AC45F /* RCTViewTests.m */,
E7DB212422B2C342005AC45F /* libOCMock.a */,
E7DB212522B2C342005AC45F /* OCMock */,
E7DB20B322B2BAA4005AC45F /* RNTesterUnitTestsBundle.js */,
Expand Down Expand Up @@ -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 */,
Expand Down
48 changes: 48 additions & 0 deletions packages/rn-tester/RNTesterUnitTests/RCTViewTests.m
Original file line number Diff line number Diff line change
@@ -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 <XCTest/XCTest.h>
#import "OCMock/OCMock.h"

#import <React/RCTAutoInsetsProtocol.h>
#import <React/RCTMockDef.h>
#import <React/RCTScrollView.h>
#import <React/RCTView.h>
#import <React/RCTViewUtils.h>

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

0 comments on commit 7cece34

Please sign in to comment.