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"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/geometry/rect_f.h"
14 #include "ui/gfx/skia_util.h"
18 class CanvasTest
: public testing::Test
{
20 int GetStringWidth(const char *text
) {
21 return Canvas::GetStringWidth(base::UTF8ToUTF16(text
), font_list_
);
24 gfx::Size
SizeStringInt(const char *text
, int width
, int line_height
) {
25 base::string16 text16
= base::UTF8ToUTF16(text
);
28 (text16
.find('\n') != base::string16::npos
) ? Canvas::MULTI_LINE
: 0;
29 Canvas::SizeStringInt(text16
, font_list_
, &width
, &height
, line_height
,
31 return gfx::Size(width
, height
);
38 #if defined(OS_ANDROID)
39 #define MAYBE_StringWidth DISABLED_StringWidth
41 #define MAYBE_StringWidth StringWidth
43 TEST_F(CanvasTest
, MAYBE_StringWidth
) {
44 EXPECT_GT(GetStringWidth("Test"), 0);
47 TEST_F(CanvasTest
, StringWidthEmptyString
) {
48 EXPECT_EQ(0, GetStringWidth(""));
51 #if defined(OS_ANDROID)
52 #define MAYBE_StringSizeEmptyString DISABLED_StringSizeEmptyString
54 #define MAYBE_StringSizeEmptyString StringSizeEmptyString
56 TEST_F(CanvasTest
, MAYBE_StringSizeEmptyString
) {
57 gfx::Size size
= SizeStringInt("", 0, 0);
58 EXPECT_EQ(0, size
.width());
59 EXPECT_GT(size
.height(), 0);
62 // Verifies GetClipBounds() returns the correct value.
63 TEST_F(CanvasTest
, ClipRectWithScaling
) {
64 Canvas
canvas(gfx::Size(200, 100), 2.25, true);
65 canvas
.sk_canvas()->clipRect(RectFToSkRect(gfx::RectF(100, 0, 20, 1.7f
)));
67 ASSERT_TRUE(canvas
.GetClipBounds(&clip_rect
));
68 // Use Contains() rather than Equals() as skia may extend the rect in certain
69 // directions. None-the-less the clip must contain the region we damaged.
70 EXPECT_TRUE(clip_rect
.Contains(gfx::Rect(100, 0, 20, 2)));
73 // Line height is only supported on Skia.
74 #if defined(OS_MACOSX) || defined(OS_ANDROID)
75 #define MAYBE_StringSizeWithLineHeight DISABLED_StringSizeWithLineHeight
77 #define MAYBE_StringSizeWithLineHeight StringSizeWithLineHeight
80 TEST_F(CanvasTest
, MAYBE_StringSizeWithLineHeight
) {
81 gfx::Size one_line_size
= SizeStringInt("Q", 0, 0);
82 gfx::Size four_line_size
= SizeStringInt("Q\nQ\nQ\nQ", 1000000, 1000);
83 EXPECT_EQ(one_line_size
.width(), four_line_size
.width());
84 EXPECT_EQ(3 * 1000 + one_line_size
.height(), four_line_size
.height());