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, ...@@ -31,8 +31,15 @@ CGFloat OriginY(CGRect targetFrame,
// Calculate the maximum width of the bubble such that it stays within its // Calculate the maximum width of the bubble such that it stays within its
// bounding coordinate space. |targetFrame| is the frame the target UI element // bounding coordinate space. |targetFrame| is the frame the target UI element
// in the coordinate system in which the bubble is drawn. |alignment| is the // 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 // bubble's alignment, |boundingWidth| is the width of the coordinate space
// in which the bubble is drawn. // 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, CGFloat MaxWidth(CGRect targetFrame,
BubbleAlignment alignment, BubbleAlignment alignment,
CGFloat boundingWidth); CGFloat boundingWidth);
......
...@@ -65,12 +65,13 @@ CGFloat OriginY(CGRect targetFrame, ...@@ -65,12 +65,13 @@ CGFloat OriginY(CGRect targetFrame,
CGFloat MaxWidth(CGRect targetFrame, CGFloat MaxWidth(CGRect targetFrame,
BubbleAlignment alignment, BubbleAlignment alignment,
CGFloat boundingWidth) { CGFloat boundingWidth,
bool isRTL) {
CGFloat anchorX = CGRectGetMidX(targetFrame); CGFloat anchorX = CGRectGetMidX(targetFrame);
CGFloat maxWidth; CGFloat maxWidth;
switch (alignment) { switch (alignment) {
case BubbleAlignmentLeading: case BubbleAlignmentLeading:
if (base::i18n::IsRTL()) { if (isRTL) {
// The bubble is aligned right, and can use space to the left of the // The bubble is aligned right, and can use space to the left of the
// anchor point and within |kBubbleAlignmentOffset| from the right. // anchor point and within |kBubbleAlignmentOffset| from the right.
maxWidth = anchorX + kBubbleAlignmentOffset; maxWidth = anchorX + kBubbleAlignmentOffset;
...@@ -86,7 +87,7 @@ CGFloat MaxWidth(CGRect targetFrame, ...@@ -86,7 +87,7 @@ CGFloat MaxWidth(CGRect targetFrame,
maxWidth = MIN(anchorX, boundingWidth - anchorX) * 2.0f; maxWidth = MIN(anchorX, boundingWidth - anchorX) * 2.0f;
break; break;
case BubbleAlignmentTrailing: case BubbleAlignmentTrailing:
if (base::i18n::IsRTL()) { if (isRTL) {
// The bubble is aligned left, and can use space to the right of the // The bubble is aligned left, and can use space to the right of the
// anchor point and within |kBubbleAlignmentOffset| from the left. // anchor point and within |kBubbleAlignmentOffset| from the left.
maxWidth = boundingWidth - anchorX + kBubbleAlignmentOffset; maxWidth = boundingWidth - anchorX + kBubbleAlignmentOffset;
...@@ -103,4 +104,11 @@ CGFloat MaxWidth(CGRect targetFrame, ...@@ -103,4 +104,11 @@ CGFloat MaxWidth(CGRect targetFrame,
return maxWidth; return maxWidth;
} }
CGFloat MaxWidth(CGRect targetFrame,
BubbleAlignment alignment,
CGFloat boundingWidth) {
bool isRTL = base::i18n::IsRTL();
return MaxWidth(targetFrame, alignment, boundingWidth, isRTL);
}
} // namespace bubble_util } // namespace bubble_util
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#import "ios/chrome/browser/ui/bubble/bubble_util.h" #import "ios/chrome/browser/ui/bubble/bubble_util.h"
#include "base/i18n/rtl.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h" #include "testing/platform_test.h"
...@@ -77,179 +76,161 @@ TEST_F(BubbleUtilTest, LeadingDistanceTrailingAlignment) { ...@@ -77,179 +76,161 @@ TEST_F(BubbleUtilTest, LeadingDistanceTrailingAlignment) {
// Test the |MaxWidth| method when the bubble is leading aligned, the target is // 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. // on the left side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnLeft) { TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnLeft) {
// Ensure that the language is LTR by setting the locale to English. CGFloat leftAlignedWidth =
base::i18n::SetICUDefaultLocale("en"); bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentLeading,
CGFloat leftAlignedWidth = bubble_util::MaxWidth( containerWidth_, false /* isRTL */);
leftAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
EXPECT_EQ(430.0f + kTestBubbleAlignmentOffset, leftAlignedWidth); EXPECT_EQ(430.0f + kTestBubbleAlignmentOffset, leftAlignedWidth);
} }
// Test the |MaxWidth| method when the bubble is leading aligned, the target is // Test the |MaxWidth| method when the bubble is leading aligned, the target is
// on the center of the container, and the language is LTR. // on the center of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnCenter) { TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnCenter) {
// Ensure that the language is LTR by setting the locale to English. CGFloat centerAlignedWidth =
base::i18n::SetICUDefaultLocale("en"); bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentLeading,
CGFloat centerAlignedWidth = bubble_util::MaxWidth( containerWidth_, false /* isRTL */);
centerAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
EXPECT_EQ(200.0f + kTestBubbleAlignmentOffset, centerAlignedWidth); EXPECT_EQ(200.0f + kTestBubbleAlignmentOffset, centerAlignedWidth);
} }
// Test the |MaxWidth| method when the bubble is leading aligned, the target is // 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. // on the right side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnRight) { TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnRight) {
// Ensure that the language is LTR by setting the locale to English. CGFloat rightAlignedWidth =
base::i18n::SetICUDefaultLocale("en"); bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentLeading,
CGFloat rightAlignedWidth = bubble_util::MaxWidth( containerWidth_, false /* isRTL */);
rightAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
EXPECT_EQ(50.0f + kTestBubbleAlignmentOffset, rightAlignedWidth); EXPECT_EQ(50.0f + kTestBubbleAlignmentOffset, rightAlignedWidth);
} }
// Test the |MaxWidth| method when the bubble is center aligned, the target is // 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. // on the left side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnLeft) { TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnLeft) {
// Ensure that the language is LTR by setting the locale to English. CGFloat leftAlignedWidth =
base::i18n::SetICUDefaultLocale("en"); bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentCenter,
CGFloat leftAlignedWidth = bubble_util::MaxWidth( containerWidth_, false /* isRTL */);
leftAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
EXPECT_EQ(140.0f, leftAlignedWidth); EXPECT_EQ(140.0f, leftAlignedWidth);
} }
// Test the |MaxWidth| method when the bubble is center aligned, the target is // Test the |MaxWidth| method when the bubble is center aligned, the target is
// on the center of the container, and the language is LTR. // on the center of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnCenter) { TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnCenter) {
// Ensure that the language is LTR by setting the locale to English. CGFloat centerAlignedWidth =
base::i18n::SetICUDefaultLocale("en"); bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentCenter,
CGFloat centerAlignedWidth = bubble_util::MaxWidth( containerWidth_, false /* isRTL */);
centerAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
EXPECT_EQ(400.0f, centerAlignedWidth); EXPECT_EQ(400.0f, centerAlignedWidth);
} }
// Test the |MaxWidth| method when the bubble is center aligned, the target is // 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. // on the right side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnRight) { TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnRight) {
// Ensure that the language is LTR by setting the locale to English. CGFloat rightAlignedWidth =
base::i18n::SetICUDefaultLocale("en"); bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentCenter,
CGFloat rightAlignedWidth = bubble_util::MaxWidth( containerWidth_, false /* isRTL */);
rightAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
EXPECT_EQ(100.0f, rightAlignedWidth); EXPECT_EQ(100.0f, rightAlignedWidth);
} }
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is // 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. // on the left side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnLeft) { TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnLeft) {
// Ensure that the language is LTR by setting the locale to English. CGFloat leftAlignedWidth =
base::i18n::SetICUDefaultLocale("en"); bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentTrailing,
CGFloat leftAlignedWidth = bubble_util::MaxWidth( containerWidth_, false /* isRTL */);
leftAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
EXPECT_EQ(70.0f + kTestBubbleAlignmentOffset, leftAlignedWidth); EXPECT_EQ(70.0f + kTestBubbleAlignmentOffset, leftAlignedWidth);
} }
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is // Test the |MaxWidth| method when the bubble is trailing aligned, the target is
// on the center of the container, and the language is LTR. // on the center of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnCenter) { TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnCenter) {
// Ensure that the language is LTR by setting the locale to English. CGFloat centerAlignedWidth =
base::i18n::SetICUDefaultLocale("en"); bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentTrailing,
CGFloat centerAlignedWidth = bubble_util::MaxWidth( containerWidth_, false /* isRTL */);
centerAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
EXPECT_EQ(300.0f + kTestBubbleAlignmentOffset, centerAlignedWidth); EXPECT_EQ(300.0f + kTestBubbleAlignmentOffset, centerAlignedWidth);
} }
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is // 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. // on the right side of the container, and the language is LTR.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnRight) { TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnRight) {
// Ensure that the language is LTR by setting the locale to English. CGFloat rightAlignedWidth =
base::i18n::SetICUDefaultLocale("en"); bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentTrailing,
CGFloat rightAlignedWidth = bubble_util::MaxWidth( containerWidth_, false /* isRTL */);
rightAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
EXPECT_EQ(450.0f + kTestBubbleAlignmentOffset, rightAlignedWidth); EXPECT_EQ(450.0f + kTestBubbleAlignmentOffset, rightAlignedWidth);
} }
// Test the |MaxWidth| method when the bubble is leading aligned, the target is // 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. // on the left side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnLeftRTL) { TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnLeftRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew. CGFloat leftAlignedWidthRTL =
base::i18n::SetICUDefaultLocale("he"); bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentLeading,
CGFloat leftAlignedWidthRTL = bubble_util::MaxWidth( containerWidth_, true /* isRTL */);
leftAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
EXPECT_EQ(70.0f + kTestBubbleAlignmentOffset, leftAlignedWidthRTL); EXPECT_EQ(70.0f + kTestBubbleAlignmentOffset, leftAlignedWidthRTL);
} }
// Test the |MaxWidth| method when the bubble is leading aligned, the target is // Test the |MaxWidth| method when the bubble is leading aligned, the target is
// on the center of the container, and the language is RTL. // on the center of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnCenterRTL) { TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnCenterRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew. CGFloat centerAlignedWidthRTL =
base::i18n::SetICUDefaultLocale("he"); bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentLeading,
CGFloat centerAlignedWidthRTL = bubble_util::MaxWidth( containerWidth_, true /* isRTL */);
centerAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
EXPECT_EQ(300.0f + kTestBubbleAlignmentOffset, centerAlignedWidthRTL); EXPECT_EQ(300.0f + kTestBubbleAlignmentOffset, centerAlignedWidthRTL);
} }
// Test the |MaxWidth| method when the bubble is leading aligned, the target is // 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. // on the right side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnRightRTL) { TEST_F(BubbleUtilTest, MaxWidthLeadingWithTargetOnRightRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew. CGFloat rightAlignedWidthRTL =
base::i18n::SetICUDefaultLocale("he"); bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentLeading,
CGFloat rightAlignedWidthRTL = bubble_util::MaxWidth( containerWidth_, true /* isRTL */);
rightAlignedTarget_, BubbleAlignmentLeading, containerWidth_);
EXPECT_EQ(450.0f + kTestBubbleAlignmentOffset, rightAlignedWidthRTL); EXPECT_EQ(450.0f + kTestBubbleAlignmentOffset, rightAlignedWidthRTL);
} }
// Test the |MaxWidth| method when the bubble is center aligned, the target is // 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. // on the left side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnLeftRTL) { TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnLeftRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew. CGFloat leftAlignedWidthRTL =
base::i18n::SetICUDefaultLocale("he"); bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentCenter,
CGFloat leftAlignedWidthRTL = bubble_util::MaxWidth( containerWidth_, true /* isRTL */);
leftAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
EXPECT_EQ(140.0f, leftAlignedWidthRTL); EXPECT_EQ(140.0f, leftAlignedWidthRTL);
} }
// Test the |MaxWidth| method when the bubble is center aligned, the target is // Test the |MaxWidth| method when the bubble is center aligned, the target is
// on the center of the container, and the language is RTL. // on the center of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnCenterRTL) { TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnCenterRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew. CGFloat centerAlignedWidthRTL =
base::i18n::SetICUDefaultLocale("he"); bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentCenter,
CGFloat centerAlignedWidthRTL = bubble_util::MaxWidth( containerWidth_, true /* isRTL */);
centerAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
EXPECT_EQ(400.0f, centerAlignedWidthRTL); EXPECT_EQ(400.0f, centerAlignedWidthRTL);
} }
// Test the |MaxWidth| method when the bubble is center aligned, the target is // 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. // on the right side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnRightRTL) { TEST_F(BubbleUtilTest, MaxWidthCenterWithTargetOnRightRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew. CGFloat rightAlignedWidthRTL =
base::i18n::SetICUDefaultLocale("he"); bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentCenter,
CGFloat rightAlignedWidthRTL = bubble_util::MaxWidth( containerWidth_, true /* isRTL */);
rightAlignedTarget_, BubbleAlignmentCenter, containerWidth_);
EXPECT_EQ(100.0f, rightAlignedWidthRTL); EXPECT_EQ(100.0f, rightAlignedWidthRTL);
} }
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is // 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. // on the left side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnLeftRTL) { TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnLeftRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew. CGFloat leftAlignedWidthRTL =
base::i18n::SetICUDefaultLocale("he"); bubble_util::MaxWidth(leftAlignedTarget_, BubbleAlignmentTrailing,
CGFloat leftAlignedWidthRTL = bubble_util::MaxWidth( containerWidth_, true /* isRTL */);
leftAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
EXPECT_EQ(430.0f + kTestBubbleAlignmentOffset, leftAlignedWidthRTL); EXPECT_EQ(430.0f + kTestBubbleAlignmentOffset, leftAlignedWidthRTL);
} }
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is // Test the |MaxWidth| method when the bubble is trailing aligned, the target is
// on the center of the container, and the language is RTL. // on the center of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnCenterRTL) { TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnCenterRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew. CGFloat centerAlignedWidthRTL =
base::i18n::SetICUDefaultLocale("he"); bubble_util::MaxWidth(centerAlignedTarget_, BubbleAlignmentTrailing,
CGFloat centerAlignedWidthRTL = bubble_util::MaxWidth( containerWidth_, true /* isRTL */);
centerAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
EXPECT_EQ(200.0f + kTestBubbleAlignmentOffset, centerAlignedWidthRTL); EXPECT_EQ(200.0f + kTestBubbleAlignmentOffset, centerAlignedWidthRTL);
} }
// Test the |MaxWidth| method when the bubble is trailing aligned, the target is // 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. // on the right side of the container, and the language is RTL.
TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnRightRTL) { TEST_F(BubbleUtilTest, MaxWidthTrailingWithTargetOnRightRTL) {
// Ensure that the language is RTL by setting the locale to Hebrew. CGFloat rightAlignedWidthRTL =
base::i18n::SetICUDefaultLocale("he"); bubble_util::MaxWidth(rightAlignedTarget_, BubbleAlignmentTrailing,
CGFloat rightAlignedWidthRTL = bubble_util::MaxWidth( containerWidth_, true /* isRTL */);
rightAlignedTarget_, BubbleAlignmentTrailing, containerWidth_);
EXPECT_EQ(50.0f + kTestBubbleAlignmentOffset, rightAlignedWidthRTL); 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