Commit b9bda8f6 authored by Yi Su's avatar Yi Su Committed by Commit Bot

Move font-cap helper to dynamic_type_util.mm.

This CL moves helper function "PreferredFontForTextStyleWithMaxCategory"
from uikit_ui_util.mm to dynamic_type_util.mm, and adds some unittests.

Change-Id: I08620274827845d58a9a4cced1c91b849c5da91d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1496857
Commit-Queue: Yi Su <mrsuyi@chromium.org>
Auto-Submit: Yi Su <mrsuyi@chromium.org>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638094}
parent 6059819c
......@@ -9,6 +9,7 @@
#import "ios/chrome/browser/ui/location_bar/extended_touch_target_button.h"
#import "ios/chrome/browser/ui/omnibox/omnibox_constants.h"
#import "ios/chrome/browser/ui/toolbar/public/toolbar_constants.h"
#import "ios/chrome/browser/ui/util/dynamic_type_util.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
#import "ios/chrome/common/ui_util/constraints_ui_util.h"
#include "ios/chrome/grit/ios_strings.h"
......
......@@ -4,7 +4,7 @@
#import "ios/chrome/browser/ui/ntp_tile_views/ntp_tile_view.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
#import "ios/chrome/browser/ui/util/dynamic_type_util.h"
#import "ios/chrome/common/ui_util/constraints_ui_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
......
......@@ -21,6 +21,7 @@
#import "ios/chrome/browser/ui/toolbar/public/features.h"
#import "ios/chrome/browser/ui/toolbar/public/toolbar_constants.h"
#import "ios/chrome/browser/ui/util/animation_util.h"
#import "ios/chrome/browser/ui/util/dynamic_type_util.h"
#import "ios/chrome/browser/ui/util/reversed_animation.h"
#include "ios/chrome/browser/ui/util/rtl_geometry.h"
#include "ios/chrome/browser/ui/util/ui_util.h"
......
......@@ -22,4 +22,11 @@ float SystemSuggestedFontSizeMultiplier(UIContentSizeCategory category,
UIContentSizeCategory min_category,
UIContentSizeCategory max_category);
// Returns an UIFont* calculated by |style| and
// min(|currentCategory|,|maxCategory|).
UIFont* PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyle style,
UIContentSizeCategory currentCategory,
UIContentSizeCategory maxCategory);
#endif // IOS_CHROME_BROWSER_UI_UTIL_DYNAMIC_TYPE_UTIL_H_
......@@ -57,3 +57,17 @@ float SystemSuggestedFontSizeMultiplier(UIContentSizeCategory category,
max_multiplier,
std::max(min_multiplier, SystemSuggestedFontSizeMultiplier(category)));
}
UIFont* PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyle style,
UIContentSizeCategory currentCategory,
UIContentSizeCategory maxCategory) {
NSComparisonResult result =
UIContentSizeCategoryCompareToCategory(currentCategory, maxCategory);
UIContentSizeCategory category =
result == NSOrderedDescending ? maxCategory : currentCategory;
return [UIFont preferredFontForTextStyle:style
compatibleWithTraitCollection:
[UITraitCollection
traitCollectionWithPreferredContentSizeCategory:category]];
}
......@@ -6,6 +6,7 @@
#import <UIKit/UIKit.h>
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
......@@ -16,85 +17,66 @@
// Test fixture for DynamicTypeUtil class.
class DynamicTypeUtilTest : public PlatformTest {
public:
protected:
DynamicTypeUtilTest() {}
~DynamicTypeUtilTest() override {}
void SetPreferredContentSizeCategory(UILabel* testLabel,
UIViewController* viewController,
UIContentSizeCategory category) {
UITraitCollection* overrideTraitCollection = [UITraitCollection
traitCollectionWithPreferredContentSizeCategory:category];
[viewController.parentViewController
setOverrideTraitCollection:overrideTraitCollection
forChildViewController:viewController];
[testLabel removeFromSuperview];
[viewController.view addSubview:testLabel];
UIFont* PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyle style,
UIContentSizeCategory category) {
return
[UIFont preferredFontForTextStyle:style
compatibleWithTraitCollection:
[UITraitCollection
traitCollectionWithPreferredContentSizeCategory:category]];
}
};
// Checks that the font sizes associated with the "body"
// preferredContentSizeCategory aren't changing in new iOS releases.
TEST_F(DynamicTypeUtilTest, TestFontSize) {
UIViewController* parentViewController = [[UIViewController alloc] init];
UIViewController* viewController = [[UIViewController alloc] init];
[parentViewController addChildViewController:viewController];
[parentViewController.view addSubview:viewController.view];
[viewController didMoveToParentViewController:parentViewController];
UILabel* testLabel = [[UILabel alloc] init];
testLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
testLabel.adjustsFontForContentSizeCategory = YES;
SetPreferredContentSizeCategory(testLabel, viewController,
UIContentSizeCategoryExtraSmall);
EXPECT_EQ(14.f, testLabel.font.pointSize);
SetPreferredContentSizeCategory(testLabel, viewController,
UIContentSizeCategorySmall);
EXPECT_EQ(15.f, testLabel.font.pointSize);
SetPreferredContentSizeCategory(testLabel, viewController,
UIContentSizeCategoryMedium);
EXPECT_EQ(16.f, testLabel.font.pointSize);
SetPreferredContentSizeCategory(testLabel, viewController,
UIContentSizeCategoryLarge);
EXPECT_EQ(17.f, testLabel.font.pointSize);
SetPreferredContentSizeCategory(testLabel, viewController,
UIContentSizeCategoryExtraLarge);
EXPECT_EQ(19.f, testLabel.font.pointSize);
SetPreferredContentSizeCategory(testLabel, viewController,
UIContentSizeCategoryExtraExtraLarge);
EXPECT_EQ(21.f, testLabel.font.pointSize);
SetPreferredContentSizeCategory(testLabel, viewController,
UIContentSizeCategoryExtraExtraExtraLarge);
EXPECT_EQ(23.f, testLabel.font.pointSize);
SetPreferredContentSizeCategory(testLabel, viewController,
UIContentSizeCategoryAccessibilityMedium);
EXPECT_EQ(28.f, testLabel.font.pointSize);
SetPreferredContentSizeCategory(testLabel, viewController,
UIContentSizeCategoryAccessibilityLarge);
EXPECT_EQ(33.f, testLabel.font.pointSize);
SetPreferredContentSizeCategory(testLabel, viewController,
UIContentSizeCategoryAccessibilityExtraLarge);
EXPECT_EQ(40.f, testLabel.font.pointSize);
SetPreferredContentSizeCategory(
testLabel, viewController,
UIContentSizeCategoryAccessibilityExtraExtraLarge);
EXPECT_EQ(47.f, testLabel.font.pointSize);
SetPreferredContentSizeCategory(
testLabel, viewController,
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge);
EXPECT_EQ(53.f, testLabel.font.pointSize);
EXPECT_EQ(14.f, PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryExtraSmall)
.pointSize);
EXPECT_EQ(15.f, PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategorySmall)
.pointSize);
EXPECT_EQ(16.f, PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryMedium)
.pointSize);
EXPECT_EQ(17.f, PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryLarge)
.pointSize);
EXPECT_EQ(19.f, PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryExtraLarge)
.pointSize);
EXPECT_EQ(21.f, PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryExtraExtraLarge)
.pointSize);
EXPECT_EQ(23.f,
PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryExtraExtraExtraLarge)
.pointSize);
EXPECT_EQ(28.f,
PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryAccessibilityMedium)
.pointSize);
EXPECT_EQ(33.f,
PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryAccessibilityLarge)
.pointSize);
EXPECT_EQ(40.f, PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody,
UIContentSizeCategoryAccessibilityExtraLarge)
.pointSize);
EXPECT_EQ(47.f, PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody,
UIContentSizeCategoryAccessibilityExtraExtraLarge)
.pointSize);
EXPECT_EQ(53.f, PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody,
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge)
.pointSize);
}
// Tests that the clamped version of the font size multipler is working.
......@@ -136,3 +118,58 @@ TEST_F(DynamicTypeUtilTest, TestClampedFontSize) {
SystemSuggestedFontSizeMultiplier(UIContentSizeCategoryExtraExtraLarge),
multiplier);
}
// Tests that |PreferredFontForTextStyleWithMaxCategory| works well with various
// input scenarios.
TEST_F(DynamicTypeUtilTest, PreferredFontSize) {
// Use normal category as maxmium category.
EXPECT_NSEQ(PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryExtraSmall),
PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyleBody, UIContentSizeCategoryExtraSmall,
UIContentSizeCategoryMedium));
EXPECT_NSEQ(PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryMedium),
PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyleBody, UIContentSizeCategoryMedium,
UIContentSizeCategoryMedium));
EXPECT_NSEQ(PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryMedium),
PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyleBody, UIContentSizeCategoryExtraExtraLarge,
UIContentSizeCategoryMedium));
EXPECT_NSEQ(PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryMedium),
PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyleBody, UIContentSizeCategoryAccessibilityLarge,
UIContentSizeCategoryMedium));
// Use accessibility category as maxmium category.
EXPECT_NSEQ(PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryExtraSmall),
PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyleBody, UIContentSizeCategoryExtraSmall,
UIContentSizeCategoryAccessibilityLarge));
EXPECT_NSEQ(PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryMedium),
PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyleBody, UIContentSizeCategoryMedium,
UIContentSizeCategoryAccessibilityLarge));
EXPECT_NSEQ(PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryAccessibilityLarge),
PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyleBody, UIContentSizeCategoryAccessibilityLarge,
UIContentSizeCategoryAccessibilityLarge));
EXPECT_NSEQ(PreferredFontForTextStyleAndSizeCategory(
UIFontTextStyleBody, UIContentSizeCategoryAccessibilityLarge),
PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyleBody,
UIContentSizeCategoryAccessibilityExtraExtraLarge,
UIContentSizeCategoryAccessibilityLarge));
}
......@@ -255,9 +255,4 @@ void TriggerHapticFeedbackForNotification(UINotificationFeedbackType type);
// more than 99 tabs open.
NSString* TextForTabCount(long count);
UIFont* PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyle style,
UIContentSizeCategory currentCategory,
UIContentSizeCategory maxCategory);
#endif // IOS_CHROME_BROWSER_UI_UTIL_UIKIT_UI_UTIL_H_
......@@ -690,20 +690,3 @@ NSString* TextForTabCount(long count) {
return @":)";
return [NSString stringWithFormat:@"%ld", count];
}
UIFont* PreferredFontForTextStyleWithMaxCategory(
UIFontTextStyle style,
UIContentSizeCategory currentCategory,
UIContentSizeCategory maxCategory) {
CGFloat maxMultiplier = SystemSuggestedFontSizeMultiplier(maxCategory);
CGFloat currentMultiplier =
SystemSuggestedFontSizeMultiplier(currentCategory);
if (currentMultiplier > maxMultiplier) {
return [UIFont
preferredFontForTextStyle:style
compatibleWithTraitCollection:
[UITraitCollection
traitCollectionWithPreferredContentSizeCategory:maxCategory]];
}
return [UIFont preferredFontForTextStyle:style];
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment