OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/tests/test_browser_font.h" | 5 #include "ppapi/tests/test_browser_font.h" |
6 | 6 |
7 #include <stdio.h>// ERASEME | |
8 | |
9 #include "ppapi/tests/test_utils.h" | 7 #include "ppapi/tests/test_utils.h" |
10 #include "ppapi/tests/testing_instance.h" | 8 #include "ppapi/tests/testing_instance.h" |
11 #include "ppapi/cpp/image_data.h" | 9 #include "ppapi/cpp/image_data.h" |
12 #include "ppapi/cpp/trusted/browser_font_trusted.h" | 10 #include "ppapi/cpp/trusted/browser_font_trusted.h" |
13 | 11 |
14 REGISTER_TEST_CASE(BrowserFont); | 12 REGISTER_TEST_CASE(BrowserFont); |
15 | 13 |
16 bool TestBrowserFont::Init() { | 14 bool TestBrowserFont::Init() { |
17 return true; | 15 return true; |
18 } | 16 } |
19 | 17 |
20 void TestBrowserFont::RunTests(const std::string& filter) { | 18 void TestBrowserFont::RunTests(const std::string& filter) { |
21 RUN_TEST(FontFamilies, filter); | 19 RUN_TEST(FontFamilies, filter); |
22 RUN_TEST(Measure, filter); | 20 RUN_TEST(Measure, filter); |
| 21 RUN_TEST(MeasureRTL, filter); |
23 RUN_TEST(CharPos, 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); |
24 RUN_TEST(Draw, filter); | 26 RUN_TEST(Draw, filter); |
25 } | 27 } |
26 | 28 |
27 // Just tests that GetFontFamilies is hooked up & returns something. | 29 // Just tests that GetFontFamilies is hooked up & returns something. |
28 std::string TestBrowserFont::TestFontFamilies() { | 30 std::string TestBrowserFont::TestFontFamilies() { |
29 // This function is only supported out-of-process. | 31 // This function is only supported out-of-process. |
30 const PPB_Testing_Dev* testing_interface = GetTestingInterface(); | 32 const PPB_Testing_Dev* testing_interface = GetTestingInterface(); |
31 if (testing_interface && !testing_interface->IsOutOfProcess()) | 33 if (testing_interface && !testing_interface->IsOutOfProcess()) |
32 PASS(); | 34 PASS(); |
33 | 35 |
(...skipping 13 matching lines...) Expand all Loading... |
47 pp::BrowserFont_Trusted font(instance_, desc); | 49 pp::BrowserFont_Trusted font(instance_, desc); |
48 | 50 |
49 int32_t length1 = font.MeasureText(pp::BrowserFontTextRun("WWW")); | 51 int32_t length1 = font.MeasureText(pp::BrowserFontTextRun("WWW")); |
50 ASSERT_TRUE(length1 > 0); | 52 ASSERT_TRUE(length1 > 0); |
51 int32_t length2 = font.MeasureText(pp::BrowserFontTextRun("WWWWWWWW")); | 53 int32_t length2 = font.MeasureText(pp::BrowserFontTextRun("WWWWWWWW")); |
52 | 54 |
53 ASSERT_TRUE(length2 >= length1 * 2); | 55 ASSERT_TRUE(length2 >= length1 * 2); |
54 PASS(); | 56 PASS(); |
55 } | 57 } |
56 | 58 |
| 59 std::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 |
57 // Tests that the character/pixel offset functions correctly round-trip. | 101 // Tests that the character/pixel offset functions correctly round-trip. |
58 std::string TestBrowserFont::TestCharPos() { | 102 std::string TestBrowserFont::TestCharPos() { |
59 pp::BrowserFontDescription desc; | 103 pp::BrowserFontDescription desc; |
60 pp::BrowserFont_Trusted font(instance_, desc); | 104 pp::BrowserFont_Trusted font(instance_, desc); |
61 | 105 |
62 pp::BrowserFontTextRun run("Hello, world"); | 106 pp::BrowserFontTextRun run("Hello, world"); |
63 uint32_t original_char = 3; | 107 uint32_t original_char = 3; |
64 uint32_t pixel_offset = font.PixelOffsetForCharacter(run, original_char); | 108 uint32_t pixel_offset = font.PixelOffsetForCharacter(run, original_char); |
65 ASSERT_TRUE(pixel_offset > 0); | 109 ASSERT_TRUE(pixel_offset > 0); |
66 | 110 |
67 uint32_t computed_char = font.CharacterOffsetForPixel( | 111 uint32_t computed_char = font.CharacterOffsetForPixel( |
68 run, static_cast<int32_t>(pixel_offset)); | 112 run, static_cast<int32_t>(pixel_offset)); |
69 ASSERT_TRUE(computed_char == original_char); | 113 ASSERT_TRUE(computed_char == original_char); |
70 | 114 |
71 PASS(); | 115 PASS(); |
72 } | 116 } |
73 | 117 |
| 118 // Tests that we can get character positions in a mixed LTR/RTL run. |
| 119 std::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 |
74 // Tests that drawing some text produces "some" output. | 160 // Tests that drawing some text produces "some" output. |
75 std::string TestBrowserFont::TestDraw() { | 161 std::string TestBrowserFont::TestDraw() { |
76 pp::BrowserFontDescription desc; | 162 pp::BrowserFontDescription desc; |
77 desc.set_size(100); | 163 desc.set_size(100); |
78 pp::BrowserFont_Trusted font(instance_, desc); | 164 pp::BrowserFont_Trusted font(instance_, desc); |
79 | 165 |
80 const int kSize = 256; | 166 const int kSize = 256; |
81 pp::ImageData image(instance_, pp::ImageData::GetNativeImageDataFormat(), | 167 pp::ImageData image(instance_, pp::ImageData::GetNativeImageDataFormat(), |
82 pp::Size(kSize, kSize), true); | 168 pp::Size(kSize, kSize), true); |
83 ASSERT_TRUE(!image.is_null()); | 169 ASSERT_TRUE(!image.is_null()); |
84 | 170 |
85 const uint32_t kColor = 0xFFFFFFFF; | 171 const uint32_t kColor = 0xFFFFFFFF; |
86 font.DrawSimpleText(&image, "Hello", pp::Point(0, 0), kColor, false); | 172 font.DrawSimpleText(&image, "Hello", pp::Point(0, 0), kColor, false); |
87 | 173 |
88 // Expect that some pixel is nonzero. Due to blending, there may be rounding | 174 // Expect that some pixel is nonzero. Due to blending, there may be rounding |
89 // errors and checking for exact white may not be correct. | 175 // errors and checking for exact white may not be correct. |
90 bool found = false; | 176 bool found = false; |
91 for (int y = 0; y < kSize; y++) { | 177 for (int y = 0; y < kSize; y++) { |
92 for (int x = 0; x < kSize; x++) { | 178 for (int x = 0; x < kSize; x++) { |
93 if (*image.GetAddr32(pp::Point(x, y)) != 0) { | 179 if (*image.GetAddr32(pp::Point(x, y)) != 0) { |
94 found = true; | 180 found = true; |
95 break; | 181 break; |
96 } | 182 } |
97 } | 183 } |
98 } | 184 } |
99 ASSERT_TRUE(found); | 185 ASSERT_TRUE(found); |
100 PASS(); | 186 PASS(); |
101 } | 187 } |
OLD | NEW |