• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/gfx/canvas.h"
6 
7 #include <limits>
8 
9 #include "base/strings/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/gfx/font_list.h"
12 
13 namespace gfx {
14 
15 class CanvasTest : public testing::Test {
16  protected:
GetStringWidth(const char * text)17   int GetStringWidth(const char *text) {
18     return Canvas::GetStringWidth(base::UTF8ToUTF16(text), font_list_);
19   }
20 
SizeStringInt(const char * text,int width,int line_height)21   gfx::Size SizeStringInt(const char *text, int width, int line_height) {
22     base::string16 text16 = base::UTF8ToUTF16(text);
23     int height = 0;
24     int flags =
25         (text16.find('\n') != base::string16::npos) ? Canvas::MULTI_LINE : 0;
26     Canvas::SizeStringInt(text16, font_list_, &width, &height, line_height,
27                           flags);
28     return gfx::Size(width, height);
29   }
30 
31  private:
32   FontList font_list_;
33 };
34 
TEST_F(CanvasTest,StringWidth)35 TEST_F(CanvasTest, StringWidth) {
36   EXPECT_GT(GetStringWidth("Test"), 0);
37 }
38 
TEST_F(CanvasTest,StringWidthEmptyString)39 TEST_F(CanvasTest, StringWidthEmptyString) {
40   EXPECT_EQ(0, GetStringWidth(""));
41 }
42 
TEST_F(CanvasTest,StringSizeEmptyString)43 TEST_F(CanvasTest, StringSizeEmptyString) {
44   gfx::Size size = SizeStringInt("", 0, 0);
45   EXPECT_EQ(0, size.width());
46   EXPECT_GT(size.height(), 0);
47 }
48 
49 // Line height is only supported on Skia.
50 #if defined(OS_MACOSX) || defined(OS_ANDROID)
51 #define MAYBE_StringSizeWithLineHeight DISABLED_StringSizeWithLineHeight
52 #else
53 #define MAYBE_StringSizeWithLineHeight StringSizeWithLineHeight
54 #endif
55 
TEST_F(CanvasTest,MAYBE_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());
61 }
62 
63 }  // namespace gfx
64