test_browser_font.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "ppapi/tests/test_browser_font.h"
6
7#include "ppapi/tests/test_utils.h"
8#include "ppapi/tests/testing_instance.h"
9#include "ppapi/cpp/image_data.h"
10#include "ppapi/cpp/trusted/browser_font_trusted.h"
11
12REGISTER_TEST_CASE(BrowserFont);
13
14bool TestBrowserFont::Init() {
15  return true;
16}
17
18void TestBrowserFont::RunTests(const std::string& filter) {
19  RUN_TEST(FontFamilies, filter);
20  RUN_TEST(Measure, filter);
21  RUN_TEST(MeasureRTL, filter);
22  RUN_TEST(CharPos, filter);
23  // This test is disabled. It doesn't currently pass. See the
24  // CharacterOffsetForPixel API.
25  //RUN_TEST(CharPosRTL, filter);
26  RUN_TEST(Draw, filter);
27}
28
29// Just tests that GetFontFamilies is hooked up & returns something.
30std::string TestBrowserFont::TestFontFamilies() {
31  // This function is only supported out-of-process.
32  const PPB_Testing_Dev* testing_interface = GetTestingInterface();
33  if (testing_interface && !testing_interface->IsOutOfProcess())
34    PASS();
35
36  pp::Var families = pp::BrowserFont_Trusted::GetFontFamilies(instance_);
37
38  ASSERT_TRUE(families.is_string());
39  ASSERT_TRUE(!families.AsString().empty());
40  PASS();
41}
42
43// Tests that measuring text behaves reasonably. We aren't sure if the browser
44// will be doing kerning or something for the particular default font, so we
45// just make a string that we're pretty sure should be more than twice as long
46// as another one, and verify that condition.
47std::string TestBrowserFont::TestMeasure() {
48  pp::BrowserFontDescription desc;
49  pp::BrowserFont_Trusted font(instance_, desc);
50
51  int32_t length1 = font.MeasureText(pp::BrowserFontTextRun("WWW"));
52  ASSERT_TRUE(length1 > 0);
53  int32_t length2 = font.MeasureText(pp::BrowserFontTextRun("WWWWWWWW"));
54
55  ASSERT_TRUE(length2 >= length1 * 2);
56  PASS();
57}
58
59std::string TestBrowserFont::TestMeasureRTL() {
60  pp::BrowserFontDescription desc;
61  pp::BrowserFont_Trusted font(instance_, desc);
62
63  // Mixed string, two chars of LTR, two of RTL, then two of LTR.
64  // Note this is in UTF-8 so has more than 6 bytes.
65  std::string mixed("AB\xd7\x94\xd7\x97ZZ");
66  const int kNumChars = 6;
67  pp::BrowserFontTextRun run(mixed);
68
69  // Note that since this is UTF-8, the two RTL chars are two bytes each.
70  int32_t len[kNumChars];
71  len[0] = font.PixelOffsetForCharacter(run, 0);
72  len[1] = font.PixelOffsetForCharacter(run, 1);
73  len[2] = font.PixelOffsetForCharacter(run, 2);
74  len[3] = font.PixelOffsetForCharacter(run, 3);
75  len[4] = font.PixelOffsetForCharacter(run, 4);
76  len[5] = font.PixelOffsetForCharacter(run, 5);
77
78  // First three chars should be increasing.
79  ASSERT_TRUE(len[0] >= 0);
80  ASSERT_TRUE(len[1] > len[0]);
81  ASSERT_TRUE(len[3] > len[1]);
82  ASSERT_TRUE(len[2] > len[3]);
83  ASSERT_TRUE(len[4] > len[2]);
84  ASSERT_TRUE(len[5] > len[4]);
85
86  // Test the same sequence with force LTR. The offsets should appear in
87  // sequence.
88  pp::BrowserFontTextRun forced_run(mixed, false, true);
89  len[0] = font.PixelOffsetForCharacter(forced_run, 0);
90  len[1] = font.PixelOffsetForCharacter(forced_run, 1);
91  len[2] = font.PixelOffsetForCharacter(forced_run, 2);
92  len[3] = font.PixelOffsetForCharacter(forced_run, 3);
93  len[4] = font.PixelOffsetForCharacter(forced_run, 4);
94  len[5] = font.PixelOffsetForCharacter(forced_run, 5);
95  for (int i = 1; i < kNumChars; i++)
96    ASSERT_TRUE(len[i] > len[i - 1]);
97
98  PASS();
99}
100
101// Tests that the character/pixel offset functions correctly round-trip.
102std::string TestBrowserFont::TestCharPos() {
103  pp::BrowserFontDescription desc;
104  pp::BrowserFont_Trusted font(instance_, desc);
105
106  pp::BrowserFontTextRun run("Hello, world");
107  uint32_t original_char = 3;
108  uint32_t pixel_offset = font.PixelOffsetForCharacter(run, original_char);
109  ASSERT_TRUE(pixel_offset > 0);
110
111  uint32_t computed_char = font.CharacterOffsetForPixel(
112      run, static_cast<int32_t>(pixel_offset));
113  ASSERT_TRUE(computed_char == original_char);
114
115  PASS();
116}
117
118// Tests that we can get character positions in a mixed LTR/RTL run.
119std::string TestBrowserFont::TestCharPosRTL() {
120  pp::BrowserFontDescription desc;
121  pp::BrowserFont_Trusted font(instance_, desc);
122
123  // Mixed string, two chars of LTR, two of RTL, than two of LTR.
124  // Note this is in UTF-8 so has more than 6 bytes.
125  std::string mixed("AB\xd7\x94\xd7\x97ZZ");
126
127  pp::BrowserFontTextRun run(mixed);
128  static const int kNumChars = 6;
129  int expected_char_sequence[kNumChars] = { 0, 1, 3, 2, 4, 5 };
130
131  // Check that the characters appear in the order we expect.
132  int pixel_width = font.MeasureText(pp::BrowserFontTextRun(mixed));
133  int last_sequence = 0;  // Index into expected_char_sequence.
134  for (int x = 0; x < pixel_width; x++) {
135    int cur_char = font.CharacterOffsetForPixel(run, x);
136    if (cur_char != expected_char_sequence[last_sequence]) {
137      // This pixel has a different character. It should be the next one in
138      // the sequence for it to be correct.
139      last_sequence++;
140      ASSERT_TRUE(last_sequence < kNumChars);
141      ASSERT_TRUE(cur_char == expected_char_sequence[last_sequence]);
142    }
143  }
144
145  // Try the same string with force LTR. The characters should all appear in
146  // sequence.
147  pp::BrowserFontTextRun forced_run(mixed, false, true);
148  int last_forced_char = 0;  // Char index into the forced sequence.
149  for (int x = 0; x < pixel_width; x++) {
150    int cur_char = font.CharacterOffsetForPixel(forced_run, x);
151    if (cur_char != last_forced_char) {
152      last_forced_char++;
153      ASSERT_TRUE(cur_char == last_forced_char);
154    }
155  }
156
157  PASS();
158}
159
160// Tests that drawing some text produces "some" output.
161std::string TestBrowserFont::TestDraw() {
162  pp::BrowserFontDescription desc;
163  desc.set_size(100);
164  pp::BrowserFont_Trusted font(instance_, desc);
165
166  const int kSize = 256;
167  pp::ImageData image(instance_, pp::ImageData::GetNativeImageDataFormat(),
168                      pp::Size(kSize, kSize), true);
169  ASSERT_TRUE(!image.is_null());
170
171  const uint32_t kColor = 0xFFFFFFFF;
172  font.DrawSimpleText(&image, "Hello", pp::Point(0, 0), kColor, false);
173
174  // Expect that some pixel is nonzero. Due to blending, there may be rounding
175  // errors and checking for exact white may not be correct.
176  bool found = false;
177  for (int y = 0; y < kSize; y++) {
178    for (int x = 0; x < kSize; x++) {
179      if (*image.GetAddr32(pp::Point(x, y)) != 0) {
180        found = true;
181        break;
182      }
183    }
184  }
185  ASSERT_TRUE(found);
186  PASS();
187}
188