Skip to content

Commit

Permalink
iOS: Exposes the RCTFont size overrides so unit tests can use the sam…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
fkgozali authored and facebook-github-bot committed May 8, 2018
1 parent 820673e commit 6611fef
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
1 change: 1 addition & 0 deletions RNTester/RNTesterUnitTests/RCTFontTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <XCTest/XCTest.h>

#import <React/RCTFont.h>
#import <React/RCTFontConstantsOverride.h>

@interface RCTFontTests : XCTestCase

Expand Down
6 changes: 6 additions & 0 deletions React/React.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,8 @@
59EDBCC61FDF4E55003573DE /* (null) in Copy Headers */ = {isa = PBXBuildFile; };
59EDBCC71FDF4E55003573DE /* RCTScrollView.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 59EDBCA31FDF4E0C003573DE /* RCTScrollView.h */; };
59EDBCC81FDF4E55003573DE /* RCTScrollViewManager.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 59EDBCA51FDF4E0C003573DE /* RCTScrollViewManager.h */; };
5C70426020A1004800FBEEF7 /* RCTFontConstantsOverride.h in Headers */ = {isa = PBXBuildFile; fileRef = 5C70425F20A1004800FBEEF7 /* RCTFontConstantsOverride.h */; };
5C70426120A1022C00FBEEF7 /* RCTFontConstantsOverride.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 5C70425F20A1004800FBEEF7 /* RCTFontConstantsOverride.h */; };
5CE2080220772F7D009A43B3 /* YGConfig.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5CE2080020772F7C009A43B3 /* YGConfig.cpp */; };
5CE2080320772F7D009A43B3 /* YGConfig.h in Headers */ = {isa = PBXBuildFile; fileRef = 5CE2080120772F7C009A43B3 /* YGConfig.h */; };
657734841EE834C900A0E9EA /* RCTInspectorDevServerHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 657734821EE834C900A0E9EA /* RCTInspectorDevServerHelper.h */; };
Expand Down Expand Up @@ -1599,6 +1601,7 @@
dstPath = include/React;
dstSubfolderSpec = 16;
files = (
5C70426120A1022C00FBEEF7 /* RCTFontConstantsOverride.h in Copy Headers */,
39C50FF92046EACF00CEE534 /* RCTVersion.h in Copy Headers */,
591F78DE202ADB8F004A668C /* RCTLayout.h in Copy Headers */,
59EDBCBD1FDF4E43003573DE /* RCTScrollableProtocol.h in Copy Headers */,
Expand Down Expand Up @@ -2240,6 +2243,7 @@
59EDBCA41FDF4E0C003573DE /* RCTScrollView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTScrollView.m; sourceTree = "<group>"; };
59EDBCA51FDF4E0C003573DE /* RCTScrollViewManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTScrollViewManager.h; sourceTree = "<group>"; };
59EDBCA61FDF4E0C003573DE /* RCTScrollViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTScrollViewManager.m; sourceTree = "<group>"; };
5C70425F20A1004800FBEEF7 /* RCTFontConstantsOverride.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTFontConstantsOverride.h; sourceTree = "<group>"; };
5CE2080020772F7C009A43B3 /* YGConfig.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGConfig.cpp; sourceTree = "<group>"; };
5CE2080120772F7C009A43B3 /* YGConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGConfig.h; sourceTree = "<group>"; };
657734821EE834C900A0E9EA /* RCTInspectorDevServerHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTInspectorDevServerHelper.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2561,6 +2565,7 @@
13B07FF31A6947C200A75B9A /* Views */ = {
isa = PBXGroup;
children = (
5C70425F20A1004800FBEEF7 /* RCTFontConstantsOverride.h */,
B95154301D1B34B200FE7B80 /* RCTActivityIndicatorView.h */,
B95154311D1B34B200FE7B80 /* RCTActivityIndicatorView.m */,
13B080181A69489C00A75B9A /* RCTActivityIndicatorViewManager.h */,
Expand Down Expand Up @@ -3568,6 +3573,7 @@
3D80DA621DF820620028D040 /* RCTAutoInsetsProtocol.h in Headers */,
C60128AB1F3D1258009DF9FF /* RCTCxxConvert.h in Headers */,
59EDBCAD1FDF4E0C003573DE /* RCTScrollContentView.h in Headers */,
5C70426020A1004800FBEEF7 /* RCTFontConstantsOverride.h in Headers */,
59EDBCA71FDF4E0C003573DE /* RCTScrollableProtocol.h in Headers */,
591F78DC202ADB22004A668C /* RCTLayout.h in Headers */,
5CE2080320772F7D009A43B3 /* YGConfig.h in Headers */,
Expand Down
20 changes: 1 addition & 19 deletions React/Views/RCTFont.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,13 @@

#import "RCTAssert.h"
#import "RCTFont.h"
#import "RCTFontConstantsOverride.h"
#import "RCTLog.h"

#import <CoreText/CoreText.h>

#import <mutex>

#if !defined(__IPHONE_8_2) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_2

// These constants are defined in iPhone SDK 8.2, but the app cannot run on
// iOS < 8.2 unless we redefine them here. If you target iOS 8.2 or above
// as a base target, the standard constants will be used instead.
// These constants can only be removed when React Native drops iOS8 support.

#define UIFontWeightUltraLight -0.8
#define UIFontWeightThin -0.6
#define UIFontWeightLight -0.4
#define UIFontWeightRegular 0
#define UIFontWeightMedium 0.23
#define UIFontWeightSemibold 0.3
#define UIFontWeightBold 0.4
#define UIFontWeightHeavy 0.56
#define UIFontWeightBlack 0.62

#endif

typedef CGFloat RCTFontWeight;
static RCTFontWeight weightOfFont(UIFont *font)
{
Expand Down
29 changes: 29 additions & 0 deletions React/Views/RCTFontConstantsOverride.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// NOTE: Include this header file to ensure consistency of the constants defined here.
// The values are the ones used in RCTFont.mm.
// Example: import it for testing RCTFont internals in a unit test.

#if !defined(__IPHONE_8_2) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_2

// These constants are defined in iPhone SDK 8.2, but the app cannot run on
// iOS < 8.2 unless we redefine them here. If you target iOS 8.2 or above
// as a base target, the standard constants will be used instead.
// These constants can only be removed when React Native drops iOS8 support.

#define UIFontWeightUltraLight -0.8
#define UIFontWeightThin -0.6
#define UIFontWeightLight -0.4
#define UIFontWeightRegular 0
#define UIFontWeightMedium 0.23
#define UIFontWeightSemibold 0.3
#define UIFontWeightBold 0.4
#define UIFontWeightHeavy 0.56
#define UIFontWeightBlack 0.62

#endif

0 comments on commit 6611fef

Please sign in to comment.