summaryrefslogtreecommitdiffstats
path: root/ui/gfx/canvas_unittest_mac.mm
blob: ad2214b7c7a3f6b3a68edee2e46f5b7dac445ca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/canvas.h"

#include <cmath>

#import <Cocoa/Cocoa.h>

#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_list.h"

namespace gfx {

namespace {

// Returns the pixel width of the string via calling the native method
// -sizeWithAttributes.
float GetStringNativeWidth(const base::string16& text,
                           const FontList& font_list) {
  NSFont* native_font = font_list.GetPrimaryFont().GetNativeFont();
  NSString* ns_string = base::SysUTF16ToNSString(text);
  NSDictionary* attributes =
      [NSDictionary dictionaryWithObject:native_font
                                  forKey:NSFontAttributeName];
  return [ns_string sizeWithAttributes:attributes].width;
}

}  // namespace

class CanvasTestMac : public testing::Test {
 protected:
  // Compare the size returned by Canvas::SizeStringInt to the size generated
  // by the platform-specific version in CanvasMac_SizeStringInt. Will generate
  // expectation failure on any mismatch. Only works for single-line text
  // without specified line height, since that is all the platform
  // implementation supports.
  void CompareSizes(const char* text) {
    const float kReallyLargeNumber = 12345678;
    FontList font_list(font_);
    base::string16 text16 = base::UTF8ToUTF16(text);

    float mac_width = GetStringNativeWidth(text16, font_list);
    int mac_height = font_list.GetHeight();

    float canvas_width = kReallyLargeNumber;
    float canvas_height = kReallyLargeNumber;
    Canvas::SizeStringFloat(
        text16, font_list, &canvas_width, &canvas_height, 0, 0);

    EXPECT_NE(kReallyLargeNumber, mac_width) << "no width for " << text;
    EXPECT_NE(kReallyLargeNumber, mac_height) << "no height for " << text;
    EXPECT_EQ(mac_width, canvas_width) << " width for " << text;
    // FontList::GetHeight returns a truncated height.
    EXPECT_EQ(mac_height,
              static_cast<int>(canvas_height)) << " height for " << text;
  }

 private:
  Font font_;
};

 // Tests that Canvas' SizeStringFloat yields result consistent with a native
 // implementation.
 TEST_F(CanvasTestMac, StringSizeIdenticalForSkia) {
  CompareSizes("");
  CompareSizes("Foo");
  CompareSizes("Longword");
  CompareSizes("This is a complete sentence.");
}

TEST_F(CanvasTestMac, FractionalWidth) {
  const float kReallyLargeNumber = 12345678;
  float width = kReallyLargeNumber;
  float height = kReallyLargeNumber;

  FontList font_list;
  Canvas::SizeStringFloat(
      base::UTF8ToUTF16("Test"), font_list, &width, &height, 0, 0);

  EXPECT_GT(width, static_cast<int>(width));
}

}  // namespace gfx