From 6611fefef7559c4cd3d1824235d263bff210d5e2 Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Mon, 7 May 2018 17:22:26 -0700 Subject: [PATCH] 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 --- RNTester/RNTesterUnitTests/RCTFontTests.m | 1 + React/React.xcodeproj/project.pbxproj | 6 +++++ React/Views/RCTFont.mm | 20 +--------------- React/Views/RCTFontConstantsOverride.h | 29 +++++++++++++++++++++++ 4 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 React/Views/RCTFontConstantsOverride.h diff --git a/RNTester/RNTesterUnitTests/RCTFontTests.m b/RNTester/RNTesterUnitTests/RCTFontTests.m index 1d9573b48..5d6a3bd1b 100644 --- a/RNTester/RNTesterUnitTests/RCTFontTests.m +++ b/RNTester/RNTesterUnitTests/RCTFontTests.m @@ -10,6 +10,7 @@ #import #import +#import @interface RCTFontTests : XCTestCase diff --git a/React/React.xcodeproj/project.pbxproj b/React/React.xcodeproj/project.pbxproj index 8c8941947..ab260bb49 100644 --- a/React/React.xcodeproj/project.pbxproj +++ b/React/React.xcodeproj/project.pbxproj @@ -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 */; }; @@ -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 */, @@ -2240,6 +2243,7 @@ 59EDBCA41FDF4E0C003573DE /* RCTScrollView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTScrollView.m; sourceTree = ""; }; 59EDBCA51FDF4E0C003573DE /* RCTScrollViewManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTScrollViewManager.h; sourceTree = ""; }; 59EDBCA61FDF4E0C003573DE /* RCTScrollViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTScrollViewManager.m; sourceTree = ""; }; + 5C70425F20A1004800FBEEF7 /* RCTFontConstantsOverride.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTFontConstantsOverride.h; sourceTree = ""; }; 5CE2080020772F7C009A43B3 /* YGConfig.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGConfig.cpp; sourceTree = ""; }; 5CE2080120772F7C009A43B3 /* YGConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGConfig.h; sourceTree = ""; }; 657734821EE834C900A0E9EA /* RCTInspectorDevServerHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTInspectorDevServerHelper.h; sourceTree = ""; }; @@ -2561,6 +2565,7 @@ 13B07FF31A6947C200A75B9A /* Views */ = { isa = PBXGroup; children = ( + 5C70425F20A1004800FBEEF7 /* RCTFontConstantsOverride.h */, B95154301D1B34B200FE7B80 /* RCTActivityIndicatorView.h */, B95154311D1B34B200FE7B80 /* RCTActivityIndicatorView.m */, 13B080181A69489C00A75B9A /* RCTActivityIndicatorViewManager.h */, @@ -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 */, diff --git a/React/Views/RCTFont.mm b/React/Views/RCTFont.mm index 3fb3e3617..8753510d9 100644 --- a/React/Views/RCTFont.mm +++ b/React/Views/RCTFont.mm @@ -7,31 +7,13 @@ #import "RCTAssert.h" #import "RCTFont.h" +#import "RCTFontConstantsOverride.h" #import "RCTLog.h" #import #import -#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) { diff --git a/React/Views/RCTFontConstantsOverride.h b/React/Views/RCTFontConstantsOverride.h new file mode 100644 index 000000000..1563e92e7 --- /dev/null +++ b/React/Views/RCTFontConstantsOverride.h @@ -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