Commit d4a8bfc8 authored by Helen Yang's avatar Helen Yang Committed by Commit Bot

[ios] Remove locale overrides from bubble_util unit tests

Previously, unit tests for bubble_util overrode the ICU default
locale in order to test methods in LTR/RTL. This caused
CardStackLayoutManagerTest to fail on bots. This CL fixes the issue
by removing default locale overrides from bubble_util unit tests,
and instead adds an |isRTL| parameter to the relevant util method
to make it more testable.

Bug: 750865
Change-Id: Ia53f92c8a08636c8c56519306b9347e8b425a37f
Reviewed-on: https://chromium-review.googlesource.com/596673
Commit-Queue: Helen Yang <helenlyang@google.com>
Reviewed-by: default avatarRohit Rao (ping after 24h) <rohitrao@chromium.org>
Reviewed-by: default avatarGregory Chatzinoff <gchatz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491134}
parent 1dfd08c8
......@@ -31,8 +31,15 @@ CGFloat OriginY(CGRect targetFrame,
// Calculate the maximum width of the bubble such that it stays within its
// bounding coordinate space. |targetFrame| is the frame the target UI element
// in the coordinate system in which the bubble is drawn. |alignment| is the
// bubble's alignment, and |boundingWidth| is the width of the coordinate space
// in which the bubble is drawn.
// bubble's alignment, |boundingWidth| is the width of the coordinate space
// in which the bubble is drawn, and |isRTL| is true if the language is RTL.
CGFloat MaxWidth(CGRect targetFrame,
BubbleAlignment alignment,
CGFloat boundingWidth,
bool isRTL);
// Convenience method for calculating the maximum width of the bubble. Uses the
// ICU default locale of the device to determine whether the language is RTL.
CGFloat MaxWidth(CGRect targetFrame,
BubbleAlignment alignment,
CGFloat boundingWidth);
......
......@@ -65,12 +65,13 @@ CGFloat OriginY(CGRect targetFrame,
CGFloat MaxWidth(CGRect targetFrame,
BubbleAlignment alignment,
CGFloat boundingWidth) {
CGFloat boundingWidth,
bool isRTL) {
CGFloat anchorX = CGRectGetMidX(targetFrame);
CGFloat maxWidth;
switch (alignment) {
case BubbleAlignmentLeading:
if (base::i18n::IsRTL()) {
if (isRTL) {
// The bubble is aligned right, and can use space to the left of the
// anchor point and within |kBubbleAlignmentOffset| from the right.
maxWidth = anchorX + kBubbleAlignmentOffset;
......@@ -86,7 +87,7 @@ CGFloat MaxWidth(CGRect targetFrame,
maxWidth = MIN(anchorX, boundingWidth - anchorX) * 2.0f;
break;
case BubbleAlignmentTrailing:
if (base::i18n::IsRTL()) {
if (isRTL) {
// The bubble is aligned left, and can use space to the right of the
// anchor point and within |kBubbleAlignmentOffset| from the left.
maxWidth = boundingWidth - anchorX + kBubbleAlignmentOffset;
......@@ -103,4 +104,11 @@ CGFloat MaxWidth(CGRect targetFrame,
return maxWidth;
}
CGFloat MaxWidth(CGRect targetFrame,
BubbleAlignment alignment,
CGFloat boundingWidth) {
bool isRTL = base::i18n::IsRTL();
return MaxWidth(targetFrame, alignment, boundingWidth, isRTL);
}
} // namespace bubble_util
......@@ -4,7 +4,6 @@
#import "ios/chrome/browser/ui/bubble/bubble_util.h"
#include "base/i18n/rtl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
......@@ -77,179 +76,161 @@ TEST_F(BubbleUtilTest, LeadingDistanceTrailingAlignment) {
// Test the |MaxWidth| method when the bubble is leading aligned, the target is
// on the left side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnLeft) {
// Ensure that the language is LTR by setting the locale to English.
base::i18n::SetICUDefaultLocale("en");
CGFloat leftAlignedWidth = bubble_util::MaxWidth(
leftAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
CGFloat leftAlignedWidth =
bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentLeading,
containerWidth_, false /* isRTL */);
EXPECT_EQ(430.0f + kTestBubbleAlignmentOffset, leftAlignedWidth);
}
// Test the |MaxWidth| method when the bubble is leading aligned, the target is
// on the center of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnCenter) {
// Ensure that the language is LTR by setting the locale to English.
base::i18n::SetICUDefaultLocale("en");
CGFloat centerAlignedWidth = bubble_util::MaxWidth(
centerAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
CGFloat centerAlignedWidth =
bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentLeading,
containerWidth_, false /* isRTL */);
EXPECT_EQ(200.0f + kTestBubbleAlignmentOffset, centerAlignedWidth);
}
// Test the |MaxWidth| method when the bubble is leading aligned, the target is
// on the right side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnRight) {
// Ensure that the language is LTR by setting the locale to English.
base::i18n::SetICUDefaultLocale("en");
CGFloat rightAlignedWidth = bubble_util::MaxWidth(
rightAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
CGFloat rightAlignedWidth =
bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentLeading,
containerWidth_, false /* isRTL */);
EXPECT_EQ(50.0f + kTestBubbleAlignmentOffset, rightAlignedWidth);
}
// Test the |MaxWidth| method when the bubble is center aligned, the target is
// on the left side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnLeft) {
// Ensure that the language is LTR by setting the locale to English.
base::i18n::SetICUDefaultLocale("en");
CGFloat leftAlignedWidth = bubble_util::MaxWidth(
leftAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
CGFloat leftAlignedWidth =
bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentCenter,
containerWidth_, false /* isRTL */);
EXPECT_EQ(140.0f, leftAlignedWidth);
}
// Test the |MaxWidth| method when the bubble is center aligned, the target is
// on the center of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnCenter) {
// Ensure that the language is LTR by setting the locale to English.
base::i18n::SetICUDefaultLocale("en");
CGFloat centerAlignedWidth = bubble_util::MaxWidth(
centerAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
CGFloat centerAlignedWidth =
bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentCenter,
containerWidth_, false /* isRTL */);
EXPECT_EQ(400.0f, centerAlignedWidth);
}
// Test the |MaxWidth| method when the bubble is center aligned, the target is
// on the right side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnRight) {
// Ensure that the language is LTR by setting the locale to English.
base::i18n::SetICUDefaultLocale("en");
CGFloat rightAlignedWidth = bubble_util::MaxWidth(
rightAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
CGFloat rightAlignedWidth =
bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentCenter,
containerWidth_, false /* isRTL */);
EXPECT_EQ(100.0f, rightAlignedWidth);
}
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is
// on the left side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnLeft) {
// Ensure that the language is LTR by setting the locale to English.
base::i18n::SetICUDefaultLocale("en");
CGFloat leftAlignedWidth = bubble_util::MaxWidth(
leftAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
CGFloat leftAlignedWidth =
bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentTrailing,
containerWidth_, false /* isRTL */);
EXPECT_EQ(70.0f + kTestBubbleAlignmentOffset, leftAlignedWidth);
}
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is
// on the center of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnCenter) {
// Ensure that the language is LTR by setting the locale to English.
base::i18n::SetICUDefaultLocale("en");
CGFloat centerAlignedWidth = bubble_util::MaxWidth(
centerAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
CGFloat centerAlignedWidth =
bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentTrailing,
containerWidth_, false /* isRTL */);
EXPECT_EQ(300.0f + kTestBubbleAlignmentOffset, centerAlignedWidth);
}
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is
// on the right side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnRight) {
// Ensure that the language is LTR by setting the locale to English.
base::i18n::SetICUDefaultLocale("en");
CGFloat rightAlignedWidth = bubble_util::MaxWidth(
rightAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
CGFloat rightAlignedWidth =
bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentTrailing,
containerWidth_, false /* isRTL */);
EXPECT_EQ(450.0f + kTestBubbleAlignmentOffset, rightAlignedWidth);
}
// Test the |MaxWidth| method when the bubble is leading aligned, the target is
// on the left side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnLeftRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew.
base::i18n::SetICUDefaultLocale("he");
CGFloat leftAlignedWidthRTL = bubble_util::MaxWidth(
leftAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
CGFloat leftAlignedWidthRTL =
bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentLeading,
containerWidth_, true /* isRTL */);
EXPECT_EQ(70.0f + kTestBubbleAlignmentOffset, leftAlignedWidthRTL);
}
// Test the |MaxWidth| method when the bubble is leading aligned, the target is
// on the center of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnCenterRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew.
base::i18n::SetICUDefaultLocale("he");
CGFloat centerAlignedWidthRTL = bubble_util::MaxWidth(
centerAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
CGFloat centerAlignedWidthRTL =
bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentLeading,
containerWidth_, true /* isRTL */);
EXPECT_EQ(300.0f + kTestBubbleAlignmentOffset, centerAlignedWidthRTL);
}
// Test the |MaxWidth| method when the bubble is leading aligned, the target is
// on the right side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnRightRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew.
base::i18n::SetICUDefaultLocale("he");
CGFloat rightAlignedWidthRTL = bubble_util::MaxWidth(
rightAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
CGFloat rightAlignedWidthRTL =
bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentLeading,
containerWidth_, true /* isRTL */);
EXPECT_EQ(450.0f + kTestBubbleAlignmentOffset, rightAlignedWidthRTL);
}
// Test the |MaxWidth| method when the bubble is center aligned, the target is
// on the left side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnLeftRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew.
base::i18n::SetICUDefaultLocale("he");
CGFloat leftAlignedWidthRTL = bubble_util::MaxWidth(
leftAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
CGFloat leftAlignedWidthRTL =
bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentCenter,
containerWidth_, true /* isRTL */);
EXPECT_EQ(140.0f, leftAlignedWidthRTL);
}
// Test the |MaxWidth| method when the bubble is center aligned, the target is
// on the center of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnCenterRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew.
base::i18n::SetICUDefaultLocale("he");
CGFloat centerAlignedWidthRTL = bubble_util::MaxWidth(
centerAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
CGFloat centerAlignedWidthRTL =
bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentCenter,
containerWidth_, true /* isRTL */);
EXPECT_EQ(400.0f, centerAlignedWidthRTL);
}
// Test the |MaxWidth| method when the bubble is center aligned, the target is
// on the right side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnRightRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew.
base::i18n::SetICUDefaultLocale("he");
CGFloat rightAlignedWidthRTL = bubble_util::MaxWidth(
rightAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
CGFloat rightAlignedWidthRTL =
bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentCenter,
containerWidth_, true /* isRTL */);
EXPECT_EQ(100.0f, rightAlignedWidthRTL);
}
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is
// on the left side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnLeftRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew.
base::i18n::SetICUDefaultLocale("he");
CGFloat leftAlignedWidthRTL = bubble_util::MaxWidth(
leftAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
CGFloat leftAlignedWidthRTL =
bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentTrailing,
containerWidth_, true /* isRTL */);
EXPECT_EQ(430.0f + kTestBubbleAlignmentOffset, leftAlignedWidthRTL);
}
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is
// on the center of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnCenterRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew.
base::i18n::SetICUDefaultLocale("he");
CGFloat centerAlignedWidthRTL = bubble_util::MaxWidth(
centerAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
CGFloat centerAlignedWidthRTL =
bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentTrailing,
containerWidth_, true /* isRTL */);
EXPECT_EQ(200.0f + kTestBubbleAlignmentOffset, centerAlignedWidthRTL);
}
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is
// on the right side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnRightRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew.
base::i18n::SetICUDefaultLocale("he");
CGFloat rightAlignedWidthRTL = bubble_util::MaxWidth(
rightAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
CGFloat rightAlignedWidthRTL =
bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentTrailing,
containerWidth_, true /* isRTL */);
EXPECT_EQ(50.0f + kTestBubbleAlignmentOffset, rightAlignedWidthRTL);
}
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