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.
5 #include "ui/gfx/canvas.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/gfx/font_list.h"
15 class CanvasTest
: public testing::Test
{
17 int GetStringWidth(const char *text
) {
18 return Canvas::GetStringWidth(base::UTF8ToUTF16(text
), font_list_
);
21 gfx::Size
SizeStringInt(const char *text
, int width
, int line_height
) {
22 base::string16 text16
= base::UTF8ToUTF16(text
);
25 (text16
.find('\n') != base::string16::npos
) ? Canvas::MULTI_LINE
: 0;
26 Canvas::SizeStringInt(text16
, font_list_
, &width
, &height
, line_height
,
28 return gfx::Size(width
, height
);
35 TEST_F(CanvasTest
, StringWidth
) {
36 EXPECT_GT(GetStringWidth("Test"), 0);
39 TEST_F(CanvasTest
, StringWidthEmptyString
) {
40 EXPECT_EQ(0, GetStringWidth(""));
43 TEST_F(CanvasTest
, StringSizeEmptyString
) {
44 gfx::Size size
= SizeStringInt("", 0, 0);
45 EXPECT_EQ(0, size
.width());
46 EXPECT_GT(size
.height(), 0);
49 // Line height is only supported on Skia.
50 #if defined(OS_MACOSX) || defined(OS_ANDROID)
51 #define MAYBE_StringSizeWithLineHeight DISABLED_StringSizeWithLineHeight
53 #define MAYBE_StringSizeWithLineHeight StringSizeWithLineHeight
56 TEST_F(CanvasTest
, MAYBE_StringSizeWithLineHeight
) {
57 gfx::Size one_line_size
= SizeStringInt("Q", 0, 0);
58 gfx::Size four_line_size
= SizeStringInt("Q\nQ\nQ\nQ", 1000000, 1000);
59 EXPECT_EQ(one_line_size
.width(), four_line_size
.width());
60 EXPECT_EQ(3 * 1000 + one_line_size
.height(), four_line_size
.height());