Skip to content

Commit 6611fef

Browse files
fkgozalifacebook-github-bot
authored andcommitted
iOS: Exposes the RCTFont size overrides so unit tests can use the same values
Summary: `RCTFontTests` test in RNTester is broken if the target deployment is <= OS 8.2. This is because RCTFont.mm overrides the OS-defined values, but the override is only visible to RCTFont.mm internals. As the result, when the Unit test tries to create UIFont of the "same" weight, it got a different font - most likely due to internal floating rounding errors. To mitigate, code that wants to test out internals of RCTFont should import RCTFontConstantsOverride.h Reviewed By: mmmulani Differential Revision: D7900954 fbshipit-source-id: e5814ef059a8afdfb5205ca1af46c41b3cfd4318
1 parent 820673e commit 6611fef

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

RNTester/RNTesterUnitTests/RCTFontTests.m

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import <XCTest/XCTest.h>
1111

1212
#import <React/RCTFont.h>
13+
#import <React/RCTFontConstantsOverride.h>
1314

1415
@interface RCTFontTests : XCTestCase
1516

React/React.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,8 @@
11471147
59EDBCC61FDF4E55003573DE /* (null) in Copy Headers */ = {isa = PBXBuildFile; };
11481148
59EDBCC71FDF4E55003573DE /* RCTScrollView.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 59EDBCA31FDF4E0C003573DE /* RCTScrollView.h */; };
11491149
59EDBCC81FDF4E55003573DE /* RCTScrollViewManager.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 59EDBCA51FDF4E0C003573DE /* RCTScrollViewManager.h */; };
1150+
5C70426020A1004800FBEEF7 /* RCTFontConstantsOverride.h in Headers */ = {isa = PBXBuildFile; fileRef = 5C70425F20A1004800FBEEF7 /* RCTFontConstantsOverride.h */; };
1151+
5C70426120A1022C00FBEEF7 /* RCTFontConstantsOverride.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 5C70425F20A1004800FBEEF7 /* RCTFontConstantsOverride.h */; };
11501152
5CE2080220772F7D009A43B3 /* YGConfig.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5CE2080020772F7C009A43B3 /* YGConfig.cpp */; };
11511153
5CE2080320772F7D009A43B3 /* YGConfig.h in Headers */ = {isa = PBXBuildFile; fileRef = 5CE2080120772F7C009A43B3 /* YGConfig.h */; };
11521154
657734841EE834C900A0E9EA /* RCTInspectorDevServerHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 657734821EE834C900A0E9EA /* RCTInspectorDevServerHelper.h */; };
@@ -1599,6 +1601,7 @@
15991601
dstPath = include/React;
16001602
dstSubfolderSpec = 16;
16011603
files = (
1604+
5C70426120A1022C00FBEEF7 /* RCTFontConstantsOverride.h in Copy Headers */,
16021605
39C50FF92046EACF00CEE534 /* RCTVersion.h in Copy Headers */,
16031606
591F78DE202ADB8F004A668C /* RCTLayout.h in Copy Headers */,
16041607
59EDBCBD1FDF4E43003573DE /* RCTScrollableProtocol.h in Copy Headers */,
@@ -2240,6 +2243,7 @@
22402243
59EDBCA41FDF4E0C003573DE /* RCTScrollView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTScrollView.m; sourceTree = "<group>"; };
22412244
59EDBCA51FDF4E0C003573DE /* RCTScrollViewManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTScrollViewManager.h; sourceTree = "<group>"; };
22422245
59EDBCA61FDF4E0C003573DE /* RCTScrollViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTScrollViewManager.m; sourceTree = "<group>"; };
2246+
5C70425F20A1004800FBEEF7 /* RCTFontConstantsOverride.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTFontConstantsOverride.h; sourceTree = "<group>"; };
22432247
5CE2080020772F7C009A43B3 /* YGConfig.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGConfig.cpp; sourceTree = "<group>"; };
22442248
5CE2080120772F7C009A43B3 /* YGConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGConfig.h; sourceTree = "<group>"; };
22452249
657734821EE834C900A0E9EA /* RCTInspectorDevServerHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTInspectorDevServerHelper.h; sourceTree = "<group>"; };
@@ -2561,6 +2565,7 @@
25612565
13B07FF31A6947C200A75B9A /* Views */ = {
25622566
isa = PBXGroup;
25632567
children = (
2568+
5C70425F20A1004800FBEEF7 /* RCTFontConstantsOverride.h */,
25642569
B95154301D1B34B200FE7B80 /* RCTActivityIndicatorView.h */,
25652570
B95154311D1B34B200FE7B80 /* RCTActivityIndicatorView.m */,
25662571
13B080181A69489C00A75B9A /* RCTActivityIndicatorViewManager.h */,
@@ -3568,6 +3573,7 @@
35683573
3D80DA621DF820620028D040 /* RCTAutoInsetsProtocol.h in Headers */,
35693574
C60128AB1F3D1258009DF9FF /* RCTCxxConvert.h in Headers */,
35703575
59EDBCAD1FDF4E0C003573DE /* RCTScrollContentView.h in Headers */,
3576+
5C70426020A1004800FBEEF7 /* RCTFontConstantsOverride.h in Headers */,
35713577
59EDBCA71FDF4E0C003573DE /* RCTScrollableProtocol.h in Headers */,
35723578
591F78DC202ADB22004A668C /* RCTLayout.h in Headers */,
35733579
5CE2080320772F7D009A43B3 /* YGConfig.h in Headers */,

React/Views/RCTFont.mm

+1-19
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,13 @@
77

88
#import "RCTAssert.h"
99
#import "RCTFont.h"
10+
#import "RCTFontConstantsOverride.h"
1011
#import "RCTLog.h"
1112

1213
#import <CoreText/CoreText.h>
1314

1415
#import <mutex>
1516

16-
#if !defined(__IPHONE_8_2) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_2
17-
18-
// These constants are defined in iPhone SDK 8.2, but the app cannot run on
19-
// iOS < 8.2 unless we redefine them here. If you target iOS 8.2 or above
20-
// as a base target, the standard constants will be used instead.
21-
// These constants can only be removed when React Native drops iOS8 support.
22-
23-
#define UIFontWeightUltraLight -0.8
24-
#define UIFontWeightThin -0.6
25-
#define UIFontWeightLight -0.4
26-
#define UIFontWeightRegular 0
27-
#define UIFontWeightMedium 0.23
28-
#define UIFontWeightSemibold 0.3
29-
#define UIFontWeightBold 0.4
30-
#define UIFontWeightHeavy 0.56
31-
#define UIFontWeightBlack 0.62
32-
33-
#endif
34-
3517
typedef CGFloat RCTFontWeight;
3618
static RCTFontWeight weightOfFont(UIFont *font)
3719
{
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// NOTE: Include this header file to ensure consistency of the constants defined here.
9+
// The values are the ones used in RCTFont.mm.
10+
// Example: import it for testing RCTFont internals in a unit test.
11+
12+
#if !defined(__IPHONE_8_2) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_2
13+
14+
// These constants are defined in iPhone SDK 8.2, but the app cannot run on
15+
// iOS < 8.2 unless we redefine them here. If you target iOS 8.2 or above
16+
// as a base target, the standard constants will be used instead.
17+
// These constants can only be removed when React Native drops iOS8 support.
18+
19+
#define UIFontWeightUltraLight -0.8
20+
#define UIFontWeightThin -0.6
21+
#define UIFontWeightLight -0.4
22+
#define UIFontWeightRegular 0
23+
#define UIFontWeightMedium 0.23
24+
#define UIFontWeightSemibold 0.3
25+
#define UIFontWeightBold 0.4
26+
#define UIFontWeightHeavy 0.56
27+
#define UIFontWeightBlack 0.62
28+
29+
#endif

0 commit comments

Comments
 (0)