diff options
author | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-17 17:47:16 +0000 |
---|---|---|
committer | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-17 17:47:16 +0000 |
commit | 2167b493d5f9b4d4c274683dcdf2b6635bfd6470 (patch) | |
tree | 64d8f1ac829452a5e4a40f1fcad993fbc2b201f5 /ui/base/text | |
parent | 538f956734244278d1d82e959bfd2a54a5537cd5 (diff) | |
download | chromium_src-2167b493d5f9b4d4c274683dcdf2b6635bfd6470.zip chromium_src-2167b493d5f9b4d4c274683dcdf2b6635bfd6470.tar.gz chromium_src-2167b493d5f9b4d4c274683dcdf2b6635bfd6470.tar.bz2 |
Some style cleanups for text_elider.
These were brought up during my C++ readability review.
BUG=none
TEST=existing unit tests
Review URL: http://codereview.chromium.org/9226009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117912 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/text')
-rw-r--r-- | ui/base/text/text_elider.cc | 150 | ||||
-rw-r--r-- | ui/base/text/text_elider.h | 6 | ||||
-rw-r--r-- | ui/base/text/text_elider_unittest.cc | 217 |
3 files changed, 197 insertions, 176 deletions
diff --git a/ui/base/text/text_elider.cc b/ui/base/text/text_elider.cc index dd39e48..525cea0 100644 --- a/ui/base/text/text_elider.cc +++ b/ui/base/text/text_elider.cc @@ -1,6 +1,11 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// +// This file implements utility functions for eliding and formatting UI text. +// +// Note that several of the functions declared in text_elider.h are implemented +// in this file using helper classes in the anonymous namespace. #include "ui/base/text/text_elider.h" @@ -49,24 +54,25 @@ class StringSlicer { // removed and only the beginning remains. If |insert_ellipsis| is true, // then an ellipsis character will by inserted at the cut point. string16 CutString(size_t length, bool insert_ellipsis) { - const string16 kInsert = insert_ellipsis ? ellipsis_ : string16(); + const string16 ellipsis_text = insert_ellipsis ? ellipsis_ : string16(); if (!elide_in_middle_) - return text_.substr(0, FindValidBoundaryBefore(length)) + kInsert; + return text_.substr(0, FindValidBoundaryBefore(length)) + ellipsis_text; // We put the extra character, if any, before the cut. - size_t half_length = length / 2; - size_t prefix_length = FindValidBoundaryBefore(length - half_length); - size_t suffix_start_guess = text_.length() - half_length; - size_t suffix_start = FindValidBoundaryAfter(suffix_start_guess); - size_t suffix_length = half_length - (suffix_start_guess - suffix_start); - return text_.substr(0, prefix_length) + kInsert + + const size_t half_length = length / 2; + const size_t prefix_length = FindValidBoundaryBefore(length - half_length); + const size_t suffix_start_guess = text_.length() - half_length; + const size_t suffix_start = FindValidBoundaryAfter(suffix_start_guess); + const size_t suffix_length = + half_length - (suffix_start_guess - suffix_start); + return text_.substr(0, prefix_length) + ellipsis_text + text_.substr(suffix_start, suffix_length); } private: // Returns a valid cut boundary at or before |index|. - size_t FindValidBoundaryBefore(size_t index) { + size_t FindValidBoundaryBefore(size_t index) const { DCHECK_LE(index, text_.length()); if (index != text_.length()) U16_SET_CP_START(text_.data(), 0, index); @@ -74,7 +80,7 @@ class StringSlicer { } // Returns a valid cut boundary at or after |index|. - size_t FindValidBoundaryAfter(size_t index) { + size_t FindValidBoundaryAfter(size_t index) const { DCHECK_LE(index, text_.length()); if (index != text_.length()) U16_SET_CP_LIMIT(text_.data(), 0, index, text_.length()); @@ -125,16 +131,17 @@ string16 ElideComponentizedPath(const string16& url_path_prefix, const string16& url_query, const gfx::Font& font, int available_pixel_width) { - size_t url_path_number_of_elements = url_path_elements.size(); + const size_t url_path_number_of_elements = url_path_elements.size(); const string16 kEllipsisAndSlash = UTF8ToUTF16(kEllipsis) + kForwardSlash; + CHECK(url_path_number_of_elements); for (size_t i = url_path_number_of_elements - 1; i > 0; --i) { string16 elided_path = BuildPathFromComponents(url_path_prefix, url_path_elements, url_filename, i); if (available_pixel_width >= font.GetStringWidth(elided_path)) return ElideText(elided_path + url_query, - font, available_pixel_width, ui::ELIDE_AT_END); + font, available_pixel_width, ELIDE_AT_END); } return string16(); @@ -158,31 +165,33 @@ string16 ElideUrl(const GURL& url, const std::string& languages) { // Get a formatted string and corresponding parsing of the url. url_parse::Parsed parsed; - string16 url_string = net::FormatUrl(url, languages, net::kFormatUrlOmitAll, - net::UnescapeRule::SPACES, &parsed, NULL, NULL); + const string16 url_string = + net::FormatUrl(url, languages, net::kFormatUrlOmitAll, + net::UnescapeRule::SPACES, &parsed, NULL, NULL); if (available_pixel_width <= 0) return url_string; // If non-standard or not file type, return plain eliding. if (!(url.SchemeIsFile() || url.IsStandard())) - return ElideText(url_string, font, available_pixel_width, ui::ELIDE_AT_END); + return ElideText(url_string, font, available_pixel_width, ELIDE_AT_END); // Now start eliding url_string to fit within available pixel width. // Fist pass - check to see whether entire url_string fits. - int pixel_width_url_string = font.GetStringWidth(url_string); + const int pixel_width_url_string = font.GetStringWidth(url_string); if (available_pixel_width >= pixel_width_url_string) return url_string; // Get the path substring, including query and reference. - size_t path_start_index = parsed.path.begin; - size_t path_len = parsed.path.len; + const size_t path_start_index = parsed.path.begin; + const size_t path_len = parsed.path.len; string16 url_path_query_etc = url_string.substr(path_start_index); string16 url_path = url_string.substr(path_start_index, path_len); // Return general elided text if url minus the query fits. - string16 url_minus_query = url_string.substr(0, path_start_index + path_len); + const string16 url_minus_query = + url_string.substr(0, path_start_index + path_len); if (available_pixel_width >= font.GetStringWidth(url_minus_query)) - return ElideText(url_string, font, available_pixel_width, ui::ELIDE_AT_END); + return ElideText(url_string, font, available_pixel_width, ELIDE_AT_END); // Get Host. string16 url_host = UTF8ToUTF16(url.host()); @@ -201,8 +210,8 @@ string16 ElideUrl(const GURL& url, // Get sub domain. string16 url_subdomain; - size_t domain_start_index = url_host.find(url_domain); - if (domain_start_index > 0) + const size_t domain_start_index = url_host.find(url_domain); + if (domain_start_index != string16::npos) url_subdomain = url_host.substr(0, domain_start_index); const string16 kWwwPrefix = UTF8ToUTF16("www."); if ((url_subdomain == kWwwPrefix || url_subdomain.empty() || @@ -229,15 +238,15 @@ string16 ElideUrl(const GURL& url, } // Second Pass - remove scheme - the rest fits. - int pixel_width_url_host = font.GetStringWidth(url_host); - int pixel_width_url_path = font.GetStringWidth(url_path_query_etc); + const int pixel_width_url_host = font.GetStringWidth(url_host); + const int pixel_width_url_path = font.GetStringWidth(url_path_query_etc); if (available_pixel_width >= pixel_width_url_host + pixel_width_url_path) return url_host + url_path_query_etc; // Third Pass: Subdomain, domain and entire path fits. - int pixel_width_url_domain = font.GetStringWidth(url_domain); - int pixel_width_url_subdomain = font.GetStringWidth(url_subdomain); + const int pixel_width_url_domain = font.GetStringWidth(url_domain); + const int pixel_width_url_subdomain = font.GetStringWidth(url_subdomain); if (available_pixel_width >= pixel_width_url_subdomain + pixel_width_url_domain + pixel_width_url_path) @@ -253,7 +262,7 @@ string16 ElideUrl(const GURL& url, pixel_width_url_domain + pixel_width_url_path - font.GetStringWidth(url_query))) { return ElideText(url_subdomain + url_domain + url_path_query_etc, - font, available_pixel_width, ui::ELIDE_AT_END); + font, available_pixel_width, ELIDE_AT_END); } } @@ -281,16 +290,18 @@ string16 ElideUrl(const GURL& url, // No path to elide, or too long of a path (could overflow in loop below) // Just elide this as a text string. return ElideText(url_subdomain + url_domain + url_path_query_etc, font, - available_pixel_width, ui::ELIDE_AT_END); + available_pixel_width, ELIDE_AT_END); } // Start eliding the path and replacing elements by ".../". const string16 kEllipsisAndSlash = UTF8ToUTF16(kEllipsis) + kForwardSlash; - int pixel_width_ellipsis_slash = font.GetStringWidth(kEllipsisAndSlash); + const int pixel_width_ellipsis_slash = font.GetStringWidth(kEllipsisAndSlash); // Check with both subdomain and domain. - string16 elided_path = ElideComponentizedPath(url_subdomain + url_domain, - url_path_elements, url_filename, url_query, font, available_pixel_width); + string16 elided_path = + ElideComponentizedPath(url_subdomain + url_domain, url_path_elements, + url_filename, url_query, font, + available_pixel_width); if (!elided_path.empty()) return elided_path; @@ -308,7 +319,8 @@ string16 ElideUrl(const GURL& url, } elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, - url_filename, url_query, font, available_pixel_width); + url_filename, url_query, font, + available_pixel_width); if (!elided_path.empty()) return elided_path; @@ -316,7 +328,7 @@ string16 ElideUrl(const GURL& url, // Return elided domain/.../filename anyway. string16 final_elided_url_string(url_elided_domain); - int url_elided_domain_width = font.GetStringWidth(url_elided_domain); + const int url_elided_domain_width = font.GetStringWidth(url_elided_domain); // A hack to prevent trailing ".../...". if ((available_pixel_width - url_elided_domain_width) > @@ -329,7 +341,7 @@ string16 ElideUrl(const GURL& url, } return ElideText(final_elided_url_string, font, available_pixel_width, - ui::ELIDE_AT_END); + ELIDE_AT_END); } string16 ElideFilename(const FilePath& filename, @@ -348,35 +360,35 @@ string16 ElideFilename(const FilePath& filename, filename.BaseName().RemoveExtension().value())); #endif - int full_width = font.GetStringWidth(filename_utf16); + const int full_width = font.GetStringWidth(filename_utf16); if (full_width <= available_pixel_width) return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16); if (rootname.empty() || extension.empty()) { - string16 elided_name = ElideText(filename_utf16, font, - available_pixel_width, ui::ELIDE_AT_END); + const string16 elided_name = ElideText(filename_utf16, font, + available_pixel_width, ELIDE_AT_END); return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); } - int ext_width = font.GetStringWidth(extension); - int root_width = font.GetStringWidth(rootname); + const int ext_width = font.GetStringWidth(extension); + const int root_width = font.GetStringWidth(rootname); // We may have trimmed the path. if (root_width + ext_width <= available_pixel_width) { - string16 elided_name = rootname + extension; + const string16 elided_name = rootname + extension; return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); } if (ext_width >= available_pixel_width) { - string16 elided_name = ElideText(rootname + extension, font, - available_pixel_width, - ui::ELIDE_IN_MIDDLE); + const string16 elided_name = ElideText(rootname + extension, font, + available_pixel_width, + ELIDE_IN_MIDDLE); return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); } int available_root_width = available_pixel_width - ext_width; string16 elided_name = - ElideText(rootname, font, available_root_width, ui::ELIDE_AT_END); + ElideText(rootname, font, available_root_width, ELIDE_AT_END); elided_name += extension; return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); } @@ -392,9 +404,9 @@ string16 ElideText(const string16& text, const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); - int current_text_pixel_width = font.GetStringWidth(text); - bool elide_in_middle = (elide_behavior == ui::ELIDE_IN_MIDDLE); - bool insert_ellipsis = (elide_behavior != ui::TRUNCATE_AT_END); + const int current_text_pixel_width = font.GetStringWidth(text); + const bool elide_in_middle = (elide_behavior == ELIDE_IN_MIDDLE); + const bool insert_ellipsis = (elide_behavior != TRUNCATE_AT_END); StringSlicer slicer(text, kEllipsisUTF16, elide_in_middle); @@ -407,7 +419,7 @@ string16 ElideText(const string16& text, // (eliding way too much from a ridiculous string is probably still // ridiculous), but we should check other widths for bogus values as well. if (current_text_pixel_width <= 0 && !text.empty()) { - string16 cut = slicer.CutString(text.length() / 2, false); + const string16 cut = slicer.CutString(text.length() / 2, false); return ElideText(cut, font, available_pixel_width, elide_behavior); } @@ -424,8 +436,8 @@ string16 ElideText(const string16& text, for (guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { // We check the length of the whole desired string at once to ensure we // handle kerning/ligatures/etc. correctly. - string16 cut = slicer.CutString(guess, insert_ellipsis); - int guess_length = font.GetStringWidth(cut); + const string16 cut = slicer.CutString(guess, insert_ellipsis); + const int guess_length = font.GetStringWidth(cut); // Check again that we didn't hit a Pango width overflow. If so, cut the // current string in half and start over. if (guess_length <= 0) { @@ -446,9 +458,9 @@ SortedDisplayURL::SortedDisplayURL(const GURL& url, net::AppendFormattedHost(url, languages, &sort_host_); string16 host_minus_www = net::StripWWW(sort_host_); url_parse::Parsed parsed; - display_url_ = net::FormatUrl(url, languages, - net::kFormatUrlOmitAll, net::UnescapeRule::SPACES, &parsed, &prefix_end_, - NULL); + display_url_ = + net::FormatUrl(url, languages, net::kFormatUrlOmitAll, + net::UnescapeRule::SPACES, &parsed, &prefix_end_, NULL); if (sort_host_.length() > host_minus_www.length()) { prefix_end_ += sort_host_.length() - host_minus_www.length(); sort_host_.swap(host_minus_www); @@ -503,7 +515,7 @@ int SortedDisplayURL::Compare(const SortedDisplayURL& other, } string16 SortedDisplayURL::AfterHost() const { - size_t slash_index = display_url_.find(sort_host_, prefix_end_); + const size_t slash_index = display_url_.find(sort_host_, prefix_end_); if (slash_index == string16::npos) { NOTREACHED(); return string16(); @@ -832,7 +844,7 @@ bool RectangleText::Finalize() { if (!full_ && !lines_->empty()) { TrimWhitespace(lines_->back(), TRIM_TRAILING, &lines_->back()); if (lines_->back().empty()) - lines_->resize(lines_->size() - 1); + lines_->pop_back(); } return full_; } @@ -842,7 +854,7 @@ bool RectangleText::IsWhitespaceString(const string16& text) const { } void RectangleText::AddLine(const string16& line) { - int line_width = font_.GetStringWidth(line); + const int line_width = font_.GetStringWidth(line); if (line_width < available_pixel_width_) { AddToCurrentLineWithWidth(line, line_width); } else { @@ -852,13 +864,13 @@ void RectangleText::AddLine(const string16& line) { base::i18n::BreakIterator::BREAK_LINE); if (words.Init()) { while (words.Advance()) { - bool truncate = !current_line_.empty(); + const bool truncate = !current_line_.empty(); const string16& word = words.GetString(); - int lines_added = AddWord(word); + const int lines_added = AddWord(word); if (lines_added) { if (truncate) { // Trim trailing whitespace from the line that was added. - int line = lines_->size() - lines_added; + const int line = lines_->size() - lines_added; TrimWhitespace(lines_->at(line), TRIM_TRAILING, &lines_->at(line)); } if (IsWhitespaceString(word)) { @@ -882,7 +894,7 @@ int RectangleText::WrapWord(const string16& word) { int lines_added = 0; bool first_fragment = true; while (!full_ && !text.empty()) { - string16 fragment = + const string16 fragment = ui::ElideText(text, font_, available_pixel_width_, ui::TRUNCATE_AT_END); if (!first_fragment && NewLine()) lines_added++; @@ -909,9 +921,10 @@ int RectangleText::AddWordOverflow(const string16& word) { } else if (wrap_behavior_ == ui::WRAP_LONG_WORDS) { lines_added += WrapWord(word); } else { - ui::ElideBehavior elide_behavior = (wrap_behavior_ == ui::ELIDE_LONG_WORDS ? - ui::ELIDE_AT_END : ui::TRUNCATE_AT_END); - string16 elided_word = + const ui::ElideBehavior elide_behavior = + (wrap_behavior_ == ui::ELIDE_LONG_WORDS ? ui::ELIDE_AT_END : + ui::TRUNCATE_AT_END); + const string16 elided_word = ui::ElideText(word, font_, available_pixel_width_, elide_behavior); AddToCurrentLine(elided_word); } @@ -923,7 +936,7 @@ int RectangleText::AddWord(const string16& word) { int lines_added = 0; string16 trimmed; TrimWhitespace(word, TRIM_TRAILING, &trimmed); - int trimmed_width = font_.GetStringWidth(trimmed); + const int trimmed_width = font_.GetStringWidth(trimmed); if (trimmed_width <= available_pixel_width_) { // Word can be made to fit, no need to fragment it. if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) @@ -997,19 +1010,18 @@ string16 TruncateString(const string16& string, size_t length) { // String fits, return it. return string; - if (length == 0) { + if (length == 0) // No room for the elide string, return an empty string. return string16(); - } + size_t max = length - 1; // Added to the end of strings that are too big. static const char16 kElideString[] = { 0x2026, 0 }; - if (max == 0) { + if (max == 0) // Just enough room for the elide string. return kElideString; - } // Use a line iterator to find the first boundary. UErrorCode status = U_ZERO_ERROR; diff --git a/ui/base/text/text_elider.h b/ui/base/text/text_elider.h index f422698..b2ec600 100644 --- a/ui/base/text/text_elider.h +++ b/ui/base/text/text_elider.h @@ -1,6 +1,8 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// +// This file defines utility functions for eliding and formatting UI text. #ifndef UI_BASE_TEXT_TEXT_ELIDER_H_ #define UI_BASE_TEXT_TEXT_ELIDER_H_ @@ -98,6 +100,8 @@ class UI_EXPORT SortedDisplayURL { size_t prefix_end_; string16 display_url_; + + DISALLOW_COPY_AND_ASSIGN(SortedDisplayURL); }; // Functions to elide strings when the font information is unknown. As diff --git a/ui/base/text/text_elider_unittest.cc b/ui/base/text/text_elider_unittest.cc index b6d5b87..b342b4c 100644 --- a/ui/base/text/text_elider_unittest.cc +++ b/ui/base/text/text_elider_unittest.cc @@ -1,6 +1,10 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// +// Unit tests for eliding and formatting utility functions. + +#include "ui/base/text/text_elider.h" #include "base/file_path.h" #include "base/i18n/rtl.h" @@ -9,7 +13,6 @@ #include "base/utf_string_conversions.h" #include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/font.h" namespace ui { @@ -231,8 +234,8 @@ TEST(TextEliderTest, ElideTextTruncate) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { - string16 result = ui::ElideText(UTF8ToUTF16(cases[i].input), font, - cases[i].width, ui::TRUNCATE_AT_END); + string16 result = ElideText(UTF8ToUTF16(cases[i].input), font, + cases[i].width, TRUNCATE_AT_END); EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); } } @@ -271,13 +274,13 @@ TEST(TextEliderTest, ElideTextSurrogatePairs) { // Elide |kTextString| to all possible widths and check that no instance of // |kSurrogate| was split in two. for (int width = 0; width <= kTestStringWidth; width++) { - result = ui::ElideText(kTestString, font, width, ui::TRUNCATE_AT_END); + result = ElideText(kTestString, font, width, TRUNCATE_AT_END); CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); - result = ui::ElideText(kTestString, font, width, ui::ELIDE_AT_END); + result = ElideText(kTestString, font, width, ELIDE_AT_END); CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); - result = ui::ElideText(kTestString, font, width, ui::ELIDE_IN_MIDDLE); + result = ElideText(kTestString, font, width, ELIDE_IN_MIDDLE); CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); } } @@ -314,10 +317,10 @@ TEST(TextEliderTest, ElideTextLongStrings) { EXPECT_EQ(testcases_end[i].output.size(), ElideText(testcases_end[i].input, font, font.GetStringWidth(testcases_end[i].output), - ui::ELIDE_AT_END).size()); + ELIDE_AT_END).size()); EXPECT_EQ(kEllipsisStr, ElideText(testcases_end[i].input, font, ellipsis_width, - ui::ELIDE_AT_END)); + ELIDE_AT_END)); } size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2; @@ -339,16 +342,16 @@ TEST(TextEliderTest, ElideTextLongStrings) { EXPECT_EQ(testcases_middle[i].output.size(), ElideText(testcases_middle[i].input, font, font.GetStringWidth(testcases_middle[i].output), - ui::ELIDE_AT_END).size()); + ELIDE_AT_END).size()); EXPECT_EQ(kEllipsisStr, ElideText(testcases_middle[i].input, font, ellipsis_width, - ui::ELIDE_AT_END)); + ELIDE_AT_END)); } } // Verifies display_url is set correctly. TEST(TextEliderTest, SortedDisplayURL) { - ui::SortedDisplayURL d_url(GURL("http://www.google.com"), std::string()); + SortedDisplayURL d_url(GURL("http://www.google.com"), std::string()); EXPECT_EQ("www.google.com", UTF16ToASCII(d_url.display_url())); } @@ -383,8 +386,8 @@ TEST(TextEliderTest, SortedDisplayURLCompare) { }; for (size_t i = 0; i < arraysize(tests); ++i) { - ui::SortedDisplayURL url1(GURL(tests[i].a), std::string()); - ui::SortedDisplayURL url2(GURL(tests[i].b), std::string()); + SortedDisplayURL url1(GURL(tests[i].a), std::string()); + SortedDisplayURL url2(GURL(tests[i].b), std::string()); EXPECT_EQ(tests[i].compare_result, url1.Compare(url2, collator.get())); EXPECT_EQ(-tests[i].compare_result, url2.Compare(url1, collator.get())); } @@ -412,16 +415,17 @@ TEST(TextEliderTest, ElideString) { for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { string16 output; EXPECT_EQ(cases[i].result, - ui::ElideString(UTF8ToUTF16(cases[i].input), - cases[i].max_len, &output)); + ElideString(UTF8ToUTF16(cases[i].input), + cases[i].max_len, &output)); EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); } } TEST(TextEliderTest, ElideRectangleText) { const gfx::Font font; - const int kLineHeight = font.GetHeight(); - const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); + const int line_height = font.GetHeight(); + const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); + struct TestData { const char* input; int available_pixel_width; @@ -431,32 +435,32 @@ TEST(TextEliderTest, ElideRectangleText) { } cases[] = { { "", 0, 0, false, NULL }, { "", 1, 1, false, NULL }, - { "Test", kTestWidth, 0, true, NULL }, - { "Test", kTestWidth, 1, false, "Test" }, - { "Test", kTestWidth, kLineHeight, false, "Test" }, - { "Test Test", kTestWidth, kLineHeight, true, "Test" }, - { "Test Test", kTestWidth, kLineHeight + 1, false, "Test|Test" }, - { "Test Test", kTestWidth, kLineHeight * 2, false, "Test|Test" }, - { "Test Test", kTestWidth, kLineHeight * 3, false, "Test|Test" }, - { "Test Test", kTestWidth * 2, kLineHeight * 2, false, "Test|Test" }, - { "Test Test", kTestWidth * 3, kLineHeight, false, "Test Test" }, - { "Test\nTest", kTestWidth * 3, kLineHeight * 2, false, "Test|Test" }, - { "Te\nst Te", kTestWidth, kLineHeight * 3, false, "Te|st|Te" }, - { "\nTest", kTestWidth, kLineHeight * 2, false, "|Test" }, - { "\nTest", kTestWidth, kLineHeight, true, "" }, - { "\n\nTest", kTestWidth, kLineHeight * 3, false, "||Test" }, - { "\n\nTest", kTestWidth, kLineHeight * 2, true, "|" }, + { "Test", test_width, 0, true, NULL }, + { "Test", test_width, 1, false, "Test" }, + { "Test", test_width, line_height, false, "Test" }, + { "Test Test", test_width, line_height, true, "Test" }, + { "Test Test", test_width, line_height + 1, false, "Test|Test" }, + { "Test Test", test_width, line_height * 2, false, "Test|Test" }, + { "Test Test", test_width, line_height * 3, false, "Test|Test" }, + { "Test Test", test_width * 2, line_height * 2, false, "Test|Test" }, + { "Test Test", test_width * 3, line_height, false, "Test Test" }, + { "Test\nTest", test_width * 3, line_height * 2, false, "Test|Test" }, + { "Te\nst Te", test_width, line_height * 3, false, "Te|st|Te" }, + { "\nTest", test_width, line_height * 2, false, "|Test" }, + { "\nTest", test_width, line_height, true, "" }, + { "\n\nTest", test_width, line_height * 3, false, "||Test" }, + { "\n\nTest", test_width, line_height * 2, true, "|" }, }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { std::vector<string16> lines; EXPECT_EQ(cases[i].truncated, - ui::ElideRectangleText(UTF8ToUTF16(cases[i].input), - font, - cases[i].available_pixel_width, - cases[i].available_pixel_height, - ui::TRUNCATE_LONG_WORDS, - &lines)); + ElideRectangleText(UTF8ToUTF16(cases[i].input), + font, + cases[i].available_pixel_width, + cases[i].available_pixel_height, + TRUNCATE_LONG_WORDS, + &lines)); if (cases[i].output) EXPECT_EQ(cases[i].output, UTF16ToUTF8(JoinString(lines, '|'))); else @@ -466,9 +470,10 @@ TEST(TextEliderTest, ElideRectangleText) { TEST(TextEliderTest, ElideRectangleTextPunctuation) { const gfx::Font font; - const int kLineHeight = font.GetHeight(); - const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); - const int kTestTWidth = font.GetStringWidth(ASCIIToUTF16("Test T")); + const int line_height = font.GetHeight(); + const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); + const int test_t_width = font.GetStringWidth(ASCIIToUTF16("Test T")); + struct TestData { const char* input; int available_pixel_width; @@ -477,23 +482,23 @@ TEST(TextEliderTest, ElideRectangleTextPunctuation) { bool truncated; const char* output; } cases[] = { - { "Test T.", kTestTWidth, kLineHeight * 2, false, false, "Test|T." }, - { "Test T ?", kTestTWidth, kLineHeight * 2, false, false, "Test|T ?" }, - { "Test. Test", kTestWidth, kLineHeight * 3, false, false, "Test|Test" }, - { "Test. Test", kTestWidth, kLineHeight * 3, true, false, "Test|.|Test" }, + { "Test T.", test_t_width, line_height * 2, false, false, "Test|T." }, + { "Test T ?", test_t_width, line_height * 2, false, false, "Test|T ?" }, + { "Test. Test", test_width, line_height * 3, false, false, "Test|Test" }, + { "Test. Test", test_width, line_height * 3, true, false, "Test|.|Test" }, }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { std::vector<string16> lines; - ui::WordWrapBehavior wrap_behavior = - (cases[i].wrap_words ? ui::WRAP_LONG_WORDS : ui::TRUNCATE_LONG_WORDS); + const WordWrapBehavior wrap_behavior = + (cases[i].wrap_words ? WRAP_LONG_WORDS : TRUNCATE_LONG_WORDS); EXPECT_EQ(cases[i].truncated, - ui::ElideRectangleText(UTF8ToUTF16(cases[i].input), - font, - cases[i].available_pixel_width, - cases[i].available_pixel_height, - wrap_behavior, - &lines)); + ElideRectangleText(UTF8ToUTF16(cases[i].input), + font, + cases[i].available_pixel_width, + cases[i].available_pixel_height, + wrap_behavior, + &lines)); if (cases[i].output) EXPECT_EQ(cases[i].output, UTF16ToUTF8(JoinString(lines, '|'))); else @@ -505,52 +510,52 @@ TEST(TextEliderTest, ElideRectangleTextLongWords) { const gfx::Font font; const int kAvailableHeight = 1000; const string16 kElidedTesting = UTF8ToUTF16(std::string("Tes") + kEllipsis); - const int kElidedWidth = font.GetStringWidth(kElidedTesting); - const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); + const int elided_width = font.GetStringWidth(kElidedTesting); + const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); struct TestData { const char* input; int available_pixel_width; - ui::WordWrapBehavior wrap_behavior; + WordWrapBehavior wrap_behavior; const char* output; } cases[] = { - { "Testing", kTestWidth, ui::IGNORE_LONG_WORDS, "Testing" }, - { "X Testing", kTestWidth, ui::IGNORE_LONG_WORDS, "X|Testing" }, - { "Test Testing", kTestWidth, ui::IGNORE_LONG_WORDS, "Test|Testing" }, - { "Test\nTesting", kTestWidth, ui::IGNORE_LONG_WORDS, "Test|Testing" }, - { "Test Tests ", kTestWidth, ui::IGNORE_LONG_WORDS, "Test|Tests" }, - { "Test Tests T", kTestWidth, ui::IGNORE_LONG_WORDS, "Test|Tests|T" }, - - { "Testing", kElidedWidth, ui::ELIDE_LONG_WORDS, "Tes..." }, - { "X Testing", kElidedWidth, ui::ELIDE_LONG_WORDS, "X|Tes..." }, - { "Test Testing", kElidedWidth, ui::ELIDE_LONG_WORDS, "Test|Tes..." }, - { "Test\nTesting", kElidedWidth, ui::ELIDE_LONG_WORDS, "Test|Tes..." }, - - { "Testing", kTestWidth, ui::TRUNCATE_LONG_WORDS, "Test" }, - { "X Testing", kTestWidth, ui::TRUNCATE_LONG_WORDS, "X|Test" }, - { "Test Testing", kTestWidth, ui::TRUNCATE_LONG_WORDS, "Test|Test" }, - { "Test\nTesting", kTestWidth, ui::TRUNCATE_LONG_WORDS, "Test|Test" }, - { "Test Tests ", kTestWidth, ui::TRUNCATE_LONG_WORDS, "Test|Test" }, - { "Test Tests T", kTestWidth, ui::TRUNCATE_LONG_WORDS, "Test|Test|T" }, - - { "Testing", kTestWidth, ui::WRAP_LONG_WORDS, "Test|ing" }, - { "X Testing", kTestWidth, ui::WRAP_LONG_WORDS, "X|Test|ing" }, - { "Test Testing", kTestWidth, ui::WRAP_LONG_WORDS, "Test|Test|ing" }, - { "Test\nTesting", kTestWidth, ui::WRAP_LONG_WORDS, "Test|Test|ing" }, - { "Test Tests ", kTestWidth, ui::WRAP_LONG_WORDS, "Test|Test|s" }, - { "Test Tests T", kTestWidth, ui::WRAP_LONG_WORDS, "Test|Test|s T" }, - { "TestTestTest", kTestWidth, ui::WRAP_LONG_WORDS, "Test|Test|Test" }, - { "TestTestTestT", kTestWidth, ui::WRAP_LONG_WORDS, "Test|Test|Test|T" }, + { "Testing", test_width, IGNORE_LONG_WORDS, "Testing" }, + { "X Testing", test_width, IGNORE_LONG_WORDS, "X|Testing" }, + { "Test Testing", test_width, IGNORE_LONG_WORDS, "Test|Testing" }, + { "Test\nTesting", test_width, IGNORE_LONG_WORDS, "Test|Testing" }, + { "Test Tests ", test_width, IGNORE_LONG_WORDS, "Test|Tests" }, + { "Test Tests T", test_width, IGNORE_LONG_WORDS, "Test|Tests|T" }, + + { "Testing", elided_width, ELIDE_LONG_WORDS, "Tes..." }, + { "X Testing", elided_width, ELIDE_LONG_WORDS, "X|Tes..." }, + { "Test Testing", elided_width, ELIDE_LONG_WORDS, "Test|Tes..." }, + { "Test\nTesting", elided_width, ELIDE_LONG_WORDS, "Test|Tes..." }, + + { "Testing", test_width, TRUNCATE_LONG_WORDS, "Test" }, + { "X Testing", test_width, TRUNCATE_LONG_WORDS, "X|Test" }, + { "Test Testing", test_width, TRUNCATE_LONG_WORDS, "Test|Test" }, + { "Test\nTesting", test_width, TRUNCATE_LONG_WORDS, "Test|Test" }, + { "Test Tests ", test_width, TRUNCATE_LONG_WORDS, "Test|Test" }, + { "Test Tests T", test_width, TRUNCATE_LONG_WORDS, "Test|Test|T" }, + + { "Testing", test_width, WRAP_LONG_WORDS, "Test|ing" }, + { "X Testing", test_width, WRAP_LONG_WORDS, "X|Test|ing" }, + { "Test Testing", test_width, WRAP_LONG_WORDS, "Test|Test|ing" }, + { "Test\nTesting", test_width, WRAP_LONG_WORDS, "Test|Test|ing" }, + { "Test Tests ", test_width, WRAP_LONG_WORDS, "Test|Test|s" }, + { "Test Tests T", test_width, WRAP_LONG_WORDS, "Test|Test|s T" }, + { "TestTestTest", test_width, WRAP_LONG_WORDS, "Test|Test|Test" }, + { "TestTestTestT", test_width, WRAP_LONG_WORDS, "Test|Test|Test|T" }, }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { std::vector<string16> lines; - ui::ElideRectangleText(UTF8ToUTF16(cases[i].input), - font, - cases[i].available_pixel_width, - kAvailableHeight, - cases[i].wrap_behavior, - &lines); + ElideRectangleText(UTF8ToUTF16(cases[i].input), + font, + cases[i].available_pixel_width, + kAvailableHeight, + cases[i].wrap_behavior, + &lines); std::string expected_output(cases[i].output); ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis); EXPECT_EQ(expected_output, UTF16ToUTF8(JoinString(lines, '|'))); @@ -633,9 +638,9 @@ TEST(TextEliderTest, ElideRectangleString) { string16 output; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { EXPECT_EQ(cases[i].result, - ui::ElideRectangleString(UTF8ToUTF16(cases[i].input), - cases[i].max_rows, cases[i].max_cols, - true, &output)); + ElideRectangleString(UTF8ToUTF16(cases[i].input), + cases[i].max_rows, cases[i].max_cols, + true, &output)); EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); } } @@ -715,9 +720,9 @@ TEST(TextEliderTest, ElideRectangleStringNotStrict) { string16 output; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { EXPECT_EQ(cases[i].result, - ui::ElideRectangleString(UTF8ToUTF16(cases[i].input), - cases[i].max_rows, cases[i].max_cols, - false, &output)); + ElideRectangleString(UTF8ToUTF16(cases[i].input), + cases[i].max_rows, cases[i].max_cols, + false, &output)); EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); } } @@ -735,9 +740,9 @@ TEST(TextEliderTest, ElideRectangleWide16) { L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9\x03bf\x03c2\x0020\n" L"\x0399\x03c3\x03c4\x03cc\x03c2")); string16 output; - EXPECT_TRUE(ui::ElideRectangleString(str, 2, 4, true, &output)); + EXPECT_TRUE(ElideRectangleString(str, 2, 4, true, &output)); EXPECT_EQ(out1, output); - EXPECT_FALSE(ui::ElideRectangleString(str, 2, 12, true, &output)); + EXPECT_FALSE(ElideRectangleString(str, 2, 12, true, &output)); EXPECT_EQ(out2, output); } @@ -750,7 +755,7 @@ TEST(TextEliderTest, ElideRectangleWide32) { "\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\n" "\xF0\x9D\x92\x9C \naaa\n...")); string16 output; - EXPECT_TRUE(ui::ElideRectangleString(str, 3, 3, true, &output)); + EXPECT_TRUE(ElideRectangleString(str, 3, 3, true, &output)); EXPECT_EQ(out, output); } @@ -758,27 +763,27 @@ TEST(TextEliderTest, TruncateString) { string16 string = ASCIIToUTF16("foooooey bxxxar baz"); // Make sure it doesn't modify the string if length > string length. - EXPECT_EQ(string, ui::TruncateString(string, 100)); + EXPECT_EQ(string, TruncateString(string, 100)); // Test no characters. - EXPECT_EQ(L"", UTF16ToWide(ui::TruncateString(string, 0))); + EXPECT_EQ(L"", UTF16ToWide(TruncateString(string, 0))); // Test 1 character. - EXPECT_EQ(L"\x2026", UTF16ToWide(ui::TruncateString(string, 1))); + EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(string, 1))); // Test adds ... at right spot when there is enough room to break at a // word boundary. - EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(ui::TruncateString(string, 14))); + EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 14))); // Test adds ... at right spot when there is not enough space in first word. - EXPECT_EQ(L"f\x2026", UTF16ToWide(ui::TruncateString(string, 2))); + EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(string, 2))); // Test adds ... at right spot when there is not enough room to break at a // word boundary. - EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(ui::TruncateString(string, 11))); + EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); // Test completely truncates string if break is on initial whitespace. - EXPECT_EQ(L"\x2026", UTF16ToWide(ui::TruncateString(ASCIIToUTF16(" "), 2))); + EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); } } // namespace ui |