This source file includes following definitions.
- ToString
- ToPixels
- GetWrappedText
- GetLinesForWidth
- TEST_F
- TEST_F
#include "ui/message_center/views/bounded_label.h"
#include <limits>
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/text_utils.h"
#include "ui/views/controls/label.h"
namespace message_center {
namespace test {
class BoundedLabelTest : public testing::Test {
public:
BoundedLabelTest() {
digit_pixels_ = gfx::GetStringWidth(base::UTF8ToUTF16("0"), font_list_);
space_pixels_ = gfx::GetStringWidth(base::UTF8ToUTF16(" "), font_list_);
ellipsis_pixels_ = gfx::GetStringWidth(base::UTF8ToUTF16("\xE2\x80\xA6"),
font_list_);
}
virtual ~BoundedLabelTest() {}
base::string16 ToString(const char* string) {
const base::string16 periods = base::UTF8ToUTF16("...");
const base::string16 ellipses = base::UTF8ToUTF16("\xE2\x80\xA6");
base::string16 result = base::UTF8ToUTF16(string);
ReplaceSubstringsAfterOffset(&result, 0, periods, ellipses);
return result;
}
int ToPixels(int width) {
return digit_pixels_ * width / 100 +
space_pixels_ * (width % 100) / 10 +
ellipsis_pixels_ * (width % 10);
}
base::string16 GetWrappedText(int width) {
return label_->GetWrappedTextForTest(width, lines_);
}
int GetLinesForWidth(int width) {
label_->SetBounds(0, 0, width, font_list_.GetHeight() * lines_);
return label_->GetLinesForWidthAndLimit(width, lines_);
}
protected:
BoundedLabelTest& Label(base::string16 text, int lines) {
lines_ = lines;
label_.reset(new BoundedLabel(text, font_list_));
label_->SetLineLimit(lines_);
return *this;
}
private:
gfx::FontList font_list_;
int digit_pixels_;
int space_pixels_;
int ellipsis_pixels_;
scoped_ptr<BoundedLabel> label_;
int lines_;
};
#define TEST_WRAP(expected, text, width, lines) \
EXPECT_EQ(ToString(expected), \
Label(ToString(text), lines).GetWrappedText(ToPixels(width)))
#define TEST_LINES(expected, text, width, lines) \
EXPECT_EQ(expected, \
Label(ToString(text), lines).GetLinesForWidth(ToPixels(width)))
TEST_F(BoundedLabelTest, GetWrappedTextTest) {
TEST_WRAP("123", "123", 301, 1);
TEST_WRAP("123", "123", 301, 2);
TEST_WRAP("123", "123", 301, 3);
TEST_WRAP("123\n456", "123 456", 301, 2);
TEST_WRAP("123\n456", "123 456", 301, 3);
TEST_WRAP("123\n456\n789", "123 456 789", 301, 3);
TEST_WRAP("123...", "123 456", 301, 1);
TEST_WRAP("123...", "123 456 789", 301, 1);
TEST_WRAP("123\n456...", "123 456 789", 301, 2);
TEST_WRAP("123 456", "123 456", 621, 1);
TEST_WRAP("123 456", "123 456", 621, 2);
TEST_WRAP("123 456", "123 456", 621, 3);
TEST_WRAP("123 456\n789 012", "123 456 789 012", 621, 2);
TEST_WRAP("123 456\n789 012", "123 456 789 012", 621, 3);
TEST_WRAP("123 456\n789 012\n345 678", "123 456 789 012 345 678", 621, 3);
TEST_WRAP("123 456...", "123 456 789 012", 621, 1);
TEST_WRAP("123 456...", "123 456 789 012 345 678", 621, 1);
TEST_WRAP("123 456\n789 012...", "123 456 789 012 345 678", 621, 2);
TEST_WRAP("123", "123 ", 301, 1);
TEST_WRAP("123\n456", "123 456 ", 301, 2);
TEST_WRAP("123\n456\n789", "123 456 789 ", 301, 3);
TEST_WRAP("123 456", "123 456 ", 611, 1);
TEST_WRAP("123 456\n789 012", "123 456 789 012 ", 611, 2);
TEST_WRAP("123 456\n789 012\n345 678", "123 456 789 012 345 678 ", 611, 3);
TEST_WRAP("123", "123 ", 301, 1);
TEST_WRAP("123\n456", "123 456 ", 301, 2);
TEST_WRAP("123\n456\n789", "123 456 789 ", 301, 3);
TEST_WRAP("123 456", "123 456 ", 611, 1);
TEST_WRAP("123 456\n789 012", "123 456 789 012 ", 611, 2);
TEST_WRAP("123 456\n789 012\n345 678",
"123 456 789 012 345 678 ", 611, 3);
TEST_WRAP("123 456", "123 456", 621, 1);
TEST_WRAP("123 456...", "123 456 789 012", 631, 1);
TEST_WRAP("123 456\n789 012", "123 456 789 012", 631, 2);
TEST_WRAP("123 456...", "123 456 789 012 345 678", 621, 1);
TEST_WRAP("123 456\n789 012...", "123 456 789 012 345 678", 631, 2);
TEST_WRAP("123 456\n789 012\n345 678",
"123 456 789 012 345 678", 641, 3);
TEST_WRAP("123\n456", "123 456", 321, 2);
TEST_WRAP("123\n456", "123 456", 391, 2);
TEST_WRAP("123\n456...", "123 456 789", 321, 2);
TEST_WRAP("123\n456...", "123 456 789", 391, 2);
TEST_WRAP("123 456\n789 012", "123 456 789 012", 641, 2);
TEST_WRAP("123 456\n789 012...", "123 456 789 012 345 678", 641, 2);
TEST_WRAP("123\n456", "123456", 300, 9);
}
TEST_F(BoundedLabelTest, GetLinesTest) {
TEST_LINES(0, "123 456", 0, 1);
TEST_LINES(0, "123 456", -1, 1);
TEST_LINES(0, "123 456", -2, 1);
TEST_LINES(0, "123 456", std::numeric_limits<int>::min(), 1);
TEST_LINES(1, "123 456", 1, 1);
TEST_LINES(2, "123 456", 1, 2);
TEST_LINES(1, "123 456", 2, 1);
TEST_LINES(2, "123 456", 2, 2);
TEST_LINES(1, "123 456", 3, 1);
TEST_LINES(2, "123 456", 3, 2);
TEST_LINES(1, "123 456", 610, 1);
TEST_LINES(1, "123 456", std::numeric_limits<int>::max(), 1);
}
}
}