Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(659)

Unified Diff: ui/gfx/text_elider_unittest.cc

Issue 24883002: Uses and returns the fractional width in text eliding (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix win bots Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/gfx/text_elider_unittest.cc
diff --git a/ui/gfx/text_elider_unittest.cc b/ui/gfx/text_elider_unittest.cc
index 2a90e5d73cbcca9366f4fa2eeab1a8246fb97284..b4989406278c33a38175a3ce621b5188f1c5e632 100644
--- a/ui/gfx/text_elider_unittest.cc
+++ b/ui/gfx/text_elider_unittest.cc
@@ -13,6 +13,8 @@
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/font.h"
+#include "ui/gfx/font_list.h"
+#include "ui/gfx/text_utils.h"
#include "url/gurl.h"
namespace gfx {
@@ -53,24 +55,10 @@ void RunUrlTest(Testcase* testcases, size_t num_testcases) {
}
}
-gfx::Font GetTestingFont() {
- gfx::Font font;
-#if defined(OS_MACOSX)
- // Use a specific font for certain tests on Mac.
- // 1) Different Mac machines might be configured with different default font.
- // The number of extra pixels needed to make ElideEmail/TestFilenameEliding
- // tests work might vary per the default font.
- // 2) This specific font helps expose the line width exceeding problem as in
- // ElideRectangleTextCheckLineWidth.
- font = gfx::Font("LucidaGrande", 12);
-#endif
- return font;
-}
-
} // namespace
-// TODO(ios): Complex eliding is off by one for some of those tests on iOS.
-// See crbug.com/154019
+// TODO(ios): This test fails on iOS because iOS version of gfx::GetStringWidth
+// that calls [NSString sizeWithFont] returns the rounded string width.
#if defined(OS_IOS)
#define MAYBE_ElideEmail DISABLED_ElideEmail
#else
@@ -121,22 +109,13 @@ TEST(TextEliderTest, MAYBE_ElideEmail) {
{"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr},
};
- const gfx::Font font = GetTestingFont();
+ const gfx::Font font;
for (size_t i = 0; i < arraysize(testcases); ++i) {
const string16 expected_output = UTF8ToUTF16(testcases[i].output);
- int available_width = font.GetStringWidth(expected_output);
-#if defined(OS_MACOSX)
- // Give two extra pixels to offset the ceiling width returned by
- // GetStringWidth on Mac. This workaround will no longer be needed once
- // the floating point width is adopted (http://crbug.com/288987).
- // Note that we need one more pixel than TestFilenameEliding because
- // multiple strings are elided and we need to offset more.
- available_width += 2;
-#endif
EXPECT_EQ(expected_output,
ElideEmail(UTF8ToUTF16(testcases[i].input),
font,
- available_width));
+ font.GetStringWidth(expected_output)));
}
}
@@ -201,7 +180,7 @@ TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) {
// Very little space, would cause double ellipsis.
gfx::Font font;
GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
- int available_width = font.GetStringWidth(
+ float available_width = font.GetStringWidth(
UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr));
// Create the expected string, after elision. Depending on font size, the
@@ -288,7 +267,8 @@ TEST(TextEliderTest, TestFileURLEliding) {
RunUrlTest(testcases, arraysize(testcases));
}
-// TODO(ios): Complex eliding is off by one for some of those tests on iOS.
+// TODO(ios): This test fails on iOS because iOS version of gfx::GetStringWidth
+// that calls [NSString sizeWithFont] returns the rounded string width.
// See crbug.com/154019
#if defined(OS_IOS)
#define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding
@@ -334,28 +314,22 @@ TEST(TextEliderTest, MAYBE_TestFilenameEliding) {
"file.name.re" + kEllipsisStr + "emelylongext"}
};
- static const gfx::Font font = GetTestingFont();
+ static const gfx::Font font;
for (size_t i = 0; i < arraysize(testcases); ++i) {
base::FilePath filepath(testcases[i].input);
string16 expected = UTF8ToUTF16(testcases[i].output);
expected = base::i18n::GetDisplayStringInLTRDirectionality(expected);
- int available_width = font.GetStringWidth(UTF8ToUTF16(testcases[i].output));
-#if defined(OS_MACOSX)
- // Give one extra pixel to offset the ceiling width returned by
- // GetStringWidth on Mac. This workaround will no longer be needed once
- // the floating point width is adopted (http://crbug.com/288987).
- available_width += 1;
-#endif
- EXPECT_EQ(expected, ElideFilename(filepath, font, available_width));
+ EXPECT_EQ(expected, ElideFilename(filepath, font,
+ font.GetStringWidth(UTF8ToUTF16(testcases[i].output))));
}
}
TEST(TextEliderTest, ElideTextTruncate) {
const gfx::Font font;
- const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test"));
+ const float kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test"));
struct TestData {
const char* input;
- int width;
+ float width;
const char* output;
} cases[] = {
{ "", 0, "" },
@@ -375,12 +349,12 @@ TEST(TextEliderTest, ElideTextTruncate) {
TEST(TextEliderTest, ElideTextEllipsis) {
const gfx::Font font;
- const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test"));
+ const float kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test"));
const char* kEllipsis = "\xE2\x80\xA6";
- const int kEllipsisWidth = font.GetStringWidth(UTF8ToUTF16(kEllipsis));
+ const float kEllipsisWidth = font.GetStringWidth(UTF8ToUTF16(kEllipsis));
struct TestData {
const char* input;
- int width;
+ float width;
const char* output;
} cases[] = {
{ "", 0, "" },
@@ -424,14 +398,14 @@ TEST(TextEliderTest, ElideTextSurrogatePairs) {
const std::string kSurrogate = "\xF0\x9D\x84\x9E";
const string16 kTestString =
UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd");
- const int kTestStringWidth = font.GetStringWidth(kTestString);
+ const float kTestStringWidth = font.GetStringWidth(kTestString);
const char16 kSurrogateFirstChar = kTestString[0];
const char16 kSurrogateSecondChar = kTestString[1];
string16 result;
// Elide |kTextString| to all possible widths and check that no instance of
// |kSurrogate| was split in two.
- for (int width = 0; width <= kTestStringWidth; width++) {
+ for (float width = 0; width <= kTestStringWidth; width++) {
result = ElideText(kTestString, font, width, TRUNCATE_AT_END);
CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar);
@@ -468,7 +442,7 @@ TEST(TextEliderTest, ElideTextLongStrings) {
};
const gfx::Font font;
- int ellipsis_width = font.GetStringWidth(kEllipsisStr);
+ float ellipsis_width = font.GetStringWidth(kEllipsisStr);
for (size_t i = 0; i < arraysize(testcases_end); ++i) {
// Compare sizes rather than actual contents because if the test fails,
// output is rather long.
@@ -581,13 +555,13 @@ TEST(TextEliderTest, ElideString) {
TEST(TextEliderTest, ElideRectangleText) {
const gfx::Font font;
- const int line_height = font.GetHeight();
- const int test_width = font.GetStringWidth(ASCIIToUTF16("Test"));
+ const float line_height = font.GetHeight();
+ const float test_width = font.GetStringWidth(ASCIIToUTF16("Test"));
struct TestData {
const char* input;
- int available_pixel_width;
- int available_pixel_height;
+ float available_pixel_width;
+ float available_pixel_height;
bool truncated_y;
const char* output;
} cases[] = {
@@ -638,14 +612,14 @@ TEST(TextEliderTest, ElideRectangleText) {
TEST(TextEliderTest, ElideRectangleTextPunctuation) {
const gfx::Font font;
- const int line_height = font.GetHeight();
- const int test_width = font.GetStringWidth(ASCIIToUTF16("Test"));
- const int test_t_width = font.GetStringWidth(ASCIIToUTF16("Test T"));
+ const float line_height = font.GetHeight();
+ const float test_width = font.GetStringWidth(ASCIIToUTF16("Test"));
+ const float test_t_width = font.GetStringWidth(ASCIIToUTF16("Test T"));
struct TestData {
const char* input;
- int available_pixel_width;
- int available_pixel_height;
+ float available_pixel_width;
+ float available_pixel_height;
bool wrap_words;
bool truncated_x;
const char* output;
@@ -678,14 +652,14 @@ TEST(TextEliderTest, ElideRectangleTextPunctuation) {
TEST(TextEliderTest, ElideRectangleTextLongWords) {
const gfx::Font font;
- const int kAvailableHeight = 1000;
+ const float kAvailableHeight = 1000;
const string16 kElidedTesting = UTF8ToUTF16(std::string("Tes") + kEllipsis);
- const int elided_width = font.GetStringWidth(kElidedTesting);
- const int test_width = font.GetStringWidth(ASCIIToUTF16("Test"));
+ const float elided_width = font.GetStringWidth(kElidedTesting);
+ const float test_width = font.GetStringWidth(ASCIIToUTF16("Test"));
struct TestData {
const char* input;
- int available_pixel_width;
+ float available_pixel_width;
WordWrapBehavior wrap_behavior;
bool truncated_x;
const char* output;
@@ -735,24 +709,19 @@ TEST(TextEliderTest, ElideRectangleTextLongWords) {
}
}
-// TODO(ios): Complex eliding is off by one for some of those tests on iOS.
-// See crbug.com/154019
-#if defined(OS_IOS)
-#define MAYBE_ElideRectangleTextCheckLineWidth \
- DISABLED_ElideRectangleTextCheckLineWidth
-#else
-#define MAYBE_ElideRectangleTextCheckLineWidth ElideRectangleTextCheckLineWidth
-#endif
-
// This test is to make sure that the width of each wrapped line does not
// exceed the available width. On some platform like Mac, this test used to
// fail because the truncated integer width is returned for the string
// and the accumulation of the truncated values causes the elide function
// to wrap incorrectly.
-TEST(TextEliderTest, MAYBE_ElideRectangleTextCheckLineWidth) {
- gfx::Font font = GetTestingFont();
- const int kAvailableWidth = 235;
- const int kAvailableHeight = 1000;
+TEST(TextEliderTest, ElideRectangleTextCheckLineWidth) {
+ gfx::Font font;
+#if defined(OS_MACOSX) && !defined(OS_IOS)
+ // Use a specific font to expose the line width exceeding problem.
+ font = gfx::Font("LucidaGrande", 12);
+#endif
+ const float kAvailableWidth = 235;
+ const float kAvailableHeight = 1000;
const char text[] = "that Russian place we used to go to after fencing";
std::vector<string16> lines;
EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text),

Powered by Google App Engine
This is Rietveld 408576698