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); |
24 RUN_TEST(Draw, filter); | 23 RUN_TEST(Draw, filter); |
viettrungluu
2012/06/27 20:34:28
You forgot CharPosRTL.
| |
25 } | 24 } |
26 | 25 |
27 // Just tests that GetFontFamilies is hooked up & returns something. | 26 // Just tests that GetFontFamilies is hooked up & returns something. |
28 std::string TestBrowserFont::TestFontFamilies() { | 27 std::string TestBrowserFont::TestFontFamilies() { |
29 // This function is only supported out-of-process. | 28 // This function is only supported out-of-process. |
30 const PPB_Testing_Dev* testing_interface = GetTestingInterface(); | 29 const PPB_Testing_Dev* testing_interface = GetTestingInterface(); |
31 if (testing_interface && !testing_interface->IsOutOfProcess()) | 30 if (testing_interface && !testing_interface->IsOutOfProcess()) |
32 PASS(); | 31 PASS(); |
33 | 32 |
34 pp::Var families = pp::BrowserFont_Trusted::GetFontFamilies(instance_); | 33 pp::Var families = pp::BrowserFont_Trusted::GetFontFamilies(instance_); |
(...skipping 12 matching lines...) Expand all Loading... | |
47 pp::BrowserFont_Trusted font(instance_, desc); | 46 pp::BrowserFont_Trusted font(instance_, desc); |
48 | 47 |
49 int32_t length1 = font.MeasureText(pp::BrowserFontTextRun("WWW")); | 48 int32_t length1 = font.MeasureText(pp::BrowserFontTextRun("WWW")); |
50 ASSERT_TRUE(length1 > 0); | 49 ASSERT_TRUE(length1 > 0); |
51 int32_t length2 = font.MeasureText(pp::BrowserFontTextRun("WWWWWWWW")); | 50 int32_t length2 = font.MeasureText(pp::BrowserFontTextRun("WWWWWWWW")); |
52 | 51 |
53 ASSERT_TRUE(length2 >= length1 * 2); | 52 ASSERT_TRUE(length2 >= length1 * 2); |
54 PASS(); | 53 PASS(); |
55 } | 54 } |
56 | 55 |
56 std::string TestBrowserFont::TestMeasureRTL() { | |
57 pp::BrowserFontDescription desc; | |
58 pp::BrowserFont_Trusted font(instance_, desc); | |
59 | |
60 // Mixed string, two chars of LTR, two of RTL, than two of LTR. | |
viettrungluu
2012/06/27 20:33:58
s/than/then/
| |
61 // Note this is in UTF-8 so has more than 6 bytes. | |
62 std::string mixed("AB\xd7\x94\xd7\x97ZZ"); | |
63 const int kNumChars = 6; | |
64 pp::BrowserFontTextRun run(mixed); | |
65 | |
66 // Note that since this is UTF8, the two RTL chars are two bytes each. | |
viettrungluu
2012/06/27 20:33:58
Nit: "UTF8" or "UTF-8"?
| |
67 int32_t len[kNumChars]; | |
68 len[0] = font.PixelOffsetForCharacter(run, 0); | |
69 len[1] = font.PixelOffsetForCharacter(run, 1); | |
70 len[2] = font.PixelOffsetForCharacter(run, 2); | |
71 len[3] = font.PixelOffsetForCharacter(run, 3); | |
72 len[4] = font.PixelOffsetForCharacter(run, 4); | |
73 len[5] = font.PixelOffsetForCharacter(run, 5); | |
74 | |
75 // First three chars should be increasing. | |
76 ASSERT_TRUE(len[0] >= 0); | |
77 ASSERT_TRUE(len[1] > len[0]); | |
78 ASSERT_TRUE(len[2] > len[1]); | |
viettrungluu
2012/06/27 20:33:58
Instead of this convoluted logic, I don't see why
| |
79 | |
80 // Next char should be between the previous two (since it's going RTL). | |
81 ASSERT_TRUE(len[3] < len[2] && len[3] > len[1]); | |
82 | |
83 // Last two chars should be increasing again. | |
84 ASSERT_TRUE(len[4] > len[2] && len[4] > len[3]); | |
85 ASSERT_TRUE(len[4] > len[3]); | |
86 | |
87 // Test the same sequence with force LTR. The offsets should appear in | |
88 // sequence. | |
89 pp::BrowserFontTextRun forced_run(mixed, false, true); | |
90 len[0] = font.PixelOffsetForCharacter(forced_run, 0); | |
91 len[1] = font.PixelOffsetForCharacter(forced_run, 1); | |
92 len[2] = font.PixelOffsetForCharacter(forced_run, 2); | |
93 len[3] = font.PixelOffsetForCharacter(forced_run, 3); | |
94 len[4] = font.PixelOffsetForCharacter(forced_run, 4); | |
95 len[5] = font.PixelOffsetForCharacter(forced_run, 5); | |
96 for (int i = 1; i < kNumChars; i++) | |
97 ASSERT_TRUE(len[i] > len[i - 1]); | |
98 | |
99 PASS(); | |
100 } | |
101 | |
57 // Tests that the character/pixel offset functions correctly round-trip. | 102 // Tests that the character/pixel offset functions correctly round-trip. |
58 std::string TestBrowserFont::TestCharPos() { | 103 std::string TestBrowserFont::TestCharPos() { |
59 pp::BrowserFontDescription desc; | 104 pp::BrowserFontDescription desc; |
60 pp::BrowserFont_Trusted font(instance_, desc); | 105 pp::BrowserFont_Trusted font(instance_, desc); |
61 | 106 |
62 pp::BrowserFontTextRun run("Hello, world"); | 107 pp::BrowserFontTextRun run("Hello, world"); |
63 uint32_t original_char = 3; | 108 uint32_t original_char = 3; |
64 uint32_t pixel_offset = font.PixelOffsetForCharacter(run, original_char); | 109 uint32_t pixel_offset = font.PixelOffsetForCharacter(run, original_char); |
65 ASSERT_TRUE(pixel_offset > 0); | 110 ASSERT_TRUE(pixel_offset > 0); |
66 | 111 |
67 uint32_t computed_char = font.CharacterOffsetForPixel( | 112 uint32_t computed_char = font.CharacterOffsetForPixel( |
68 run, static_cast<int32_t>(pixel_offset)); | 113 run, static_cast<int32_t>(pixel_offset)); |
69 ASSERT_TRUE(computed_char == original_char); | 114 ASSERT_TRUE(computed_char == original_char); |
70 | 115 |
71 PASS(); | 116 PASS(); |
72 } | 117 } |
73 | 118 |
119 // Tests that we can get character positions in a mixed LTR/RTL run. | |
120 std::string TestBrowserFont::TestCharPosRTL() { | |
121 pp::BrowserFontDescription desc; | |
122 pp::BrowserFont_Trusted font(instance_, desc); | |
123 | |
124 // Mixed string, two chars of LTR, two of RTL, than two of LTR. | |
125 // Note this is in UTF-8 so has more than 6 bytes. | |
126 std::string mixed("AB\xd7\x94\xd7\x97ZZ"); | |
127 | |
128 pp::BrowserFontTextRun run(mixed); | |
129 static const int kNumChars = 6; | |
130 int expected_char_sequence[kNumChars] = { 0, 1, 3, 2, 4, 5 }; | |
131 | |
132 // Check that the characters appear in the order we expect. | |
133 int pixel_width = font.MeasureText(pp::BrowserFontTextRun(mixed)); | |
134 int last_sequence = 0; // Index into expected_char_sequence. | |
135 for (int x = 0; x < pixel_width; x++) { | |
136 int cur_char = font.CharacterOffsetForPixel(run, x); | |
137 if (cur_char != expected_char_sequence[cur_char]) { | |
138 // This pixel has a different character. It should be the next one in | |
139 // the sequence for it to be correct. | |
140 last_sequence++; | |
141 ASSERT_TRUE(last_sequence < kNumChars); | |
142 ASSERT_TRUE(cur_char == expected_char_sequence[last_sequence]); | |
143 } | |
144 } | |
145 | |
146 // Try the same string with force LTR. The characters should all appear in | |
147 // sequence. | |
148 pp::BrowserFontTextRun forced_run(mixed, false, true); | |
149 int last_forced_char = 0; // Char index into the forced sequence. | |
150 for (int x = 0; x < pixel_width; x++) { | |
151 int cur_char = font.CharacterOffsetForPixel(forced_run, x); | |
152 if (cur_char != last_forced_char) { | |
153 last_forced_char++; | |
154 ASSERT_TRUE(cur_char == last_forced_char); | |
155 } | |
156 } | |
157 | |
158 PASS(); | |
159 } | |
160 | |
74 // Tests that drawing some text produces "some" output. | 161 // Tests that drawing some text produces "some" output. |
75 std::string TestBrowserFont::TestDraw() { | 162 std::string TestBrowserFont::TestDraw() { |
76 pp::BrowserFontDescription desc; | 163 pp::BrowserFontDescription desc; |
77 desc.set_size(100); | 164 desc.set_size(100); |
78 pp::BrowserFont_Trusted font(instance_, desc); | 165 pp::BrowserFont_Trusted font(instance_, desc); |
79 | 166 |
80 const int kSize = 256; | 167 const int kSize = 256; |
81 pp::ImageData image(instance_, pp::ImageData::GetNativeImageDataFormat(), | 168 pp::ImageData image(instance_, pp::ImageData::GetNativeImageDataFormat(), |
82 pp::Size(kSize, kSize), true); | 169 pp::Size(kSize, kSize), true); |
83 ASSERT_TRUE(!image.is_null()); | 170 ASSERT_TRUE(!image.is_null()); |
84 | 171 |
85 const uint32_t kColor = 0xFFFFFFFF; | 172 const uint32_t kColor = 0xFFFFFFFF; |
86 font.DrawSimpleText(&image, "Hello", pp::Point(0, 0), kColor, false); | 173 font.DrawSimpleText(&image, "Hello", pp::Point(0, 0), kColor, false); |
87 | 174 |
88 // Expect that some pixel is nonzero. Due to blending, there may be rounding | 175 // Expect that some pixel is nonzero. Due to blending, there may be rounding |
89 // errors and checking for exact white may not be correct. | 176 // errors and checking for exact white may not be correct. |
90 bool found = false; | 177 bool found = false; |
91 for (int y = 0; y < kSize; y++) { | 178 for (int y = 0; y < kSize; y++) { |
92 for (int x = 0; x < kSize; x++) { | 179 for (int x = 0; x < kSize; x++) { |
93 if (*image.GetAddr32(pp::Point(x, y)) != 0) { | 180 if (*image.GetAddr32(pp::Point(x, y)) != 0) { |
94 found = true; | 181 found = true; |
95 break; | 182 break; |
96 } | 183 } |
97 } | 184 } |
98 } | 185 } |
99 ASSERT_TRUE(found); | 186 ASSERT_TRUE(found); |
100 PASS(); | 187 PASS(); |
101 } | 188 } |
OLD | NEW |