1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/font.h"
14 class CanvasTest
: public testing::Test
{
16 int GetStringWidth(const char *text
) {
17 return Canvas::GetStringWidth(UTF8ToUTF16(text
), font_
);
20 gfx::Size
SizeStringInt(const char *text
, int width
, int line_height
) {
21 base::string16 text16
= UTF8ToUTF16(text
);
24 (text16
.find('\n') != base::string16::npos
) ? Canvas::MULTI_LINE
: 0;
25 Canvas::SizeStringInt(text16
, font_
, &width
, &height
, line_height
, flags
);
26 return gfx::Size(width
, height
);
33 TEST_F(CanvasTest
, StringWidth
) {
34 EXPECT_GT(GetStringWidth("Test"), 0);
37 TEST_F(CanvasTest
, StringWidthEmptyString
) {
38 EXPECT_EQ(0, GetStringWidth(""));
41 TEST_F(CanvasTest
, StringSizeEmptyString
) {
42 gfx::Size size
= SizeStringInt("", 0, 0);
43 EXPECT_EQ(0, size
.width());
44 EXPECT_GT(size
.height(), 0);
47 // Line height is only supported on Skia.
48 #if defined(OS_MACOSX) || defined(OS_ANDROID)
49 #define MAYBE_StringSizeWithLineHeight DISABLED_StringSizeWithLineHeight
51 #define MAYBE_StringSizeWithLineHeight StringSizeWithLineHeight
54 TEST_F(CanvasTest
, MAYBE_StringSizeWithLineHeight
) {
55 gfx::Size one_line_size
= SizeStringInt("Q", 0, 0);
56 gfx::Size four_line_size
= SizeStringInt("Q\nQ\nQ\nQ", 1000000, 1000);
57 EXPECT_EQ(one_line_size
.width(), four_line_size
.width());
58 EXPECT_EQ(3 * 1000 + one_line_size
.height(), four_line_size
.height());