Make certificate viewer a tab-modal dialog.
[chromium-blink-merge.git] / ppapi / tests / test_browser_font.cc
blob8fc4104e6a1549c670794e8b199cea10d18247e0
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.
5 #include "ppapi/tests/test_browser_font.h"
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"
12 REGISTER_TEST_CASE(BrowserFont);
14 bool TestBrowserFont::Init() {
15 return true;
18 void 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);
29 // Just tests that GetFontFamilies is hooked up & returns something.
30 std::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();
36 pp::Var families = pp::BrowserFont_Trusted::GetFontFamilies(instance_);
38 ASSERT_TRUE(families.is_string());
39 ASSERT_TRUE(!families.AsString().empty());
40 PASS();
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.
47 std::string TestBrowserFont::TestMeasure() {
48 pp::BrowserFontDescription desc;
49 pp::BrowserFont_Trusted font(instance_, desc);
51 int32_t length1 = font.MeasureText(pp::BrowserFontTextRun("WWW"));
52 ASSERT_TRUE(length1 > 0);
53 int32_t length2 = font.MeasureText(pp::BrowserFontTextRun("WWWWWWWW"));
55 ASSERT_TRUE(length2 >= length1 * 2);
56 PASS();
59 std::string TestBrowserFont::TestMeasureRTL() {
60 pp::BrowserFontDescription desc;
61 pp::BrowserFont_Trusted font(instance_, desc);
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);
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);
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]);
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]);
98 PASS();
101 // Tests that the character/pixel offset functions correctly round-trip.
102 std::string TestBrowserFont::TestCharPos() {
103 pp::BrowserFontDescription desc;
104 pp::BrowserFont_Trusted font(instance_, desc);
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);
111 uint32_t computed_char = font.CharacterOffsetForPixel(
112 run, static_cast<int32_t>(pixel_offset));
113 ASSERT_TRUE(computed_char == original_char);
115 PASS();
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);
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");
127 pp::BrowserFontTextRun run(mixed);
128 static const int kNumChars = 6;
129 int expected_char_sequence[kNumChars] = { 0, 1, 3, 2, 4, 5 };
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]);
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);
157 PASS();
160 // Tests that drawing some text produces "some" output.
161 std::string TestBrowserFont::TestDraw() {
162 pp::BrowserFontDescription desc;
163 desc.set_size(100);
164 pp::BrowserFont_Trusted font(instance_, desc);
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());
171 const uint32_t kColor = 0xFFFFFFFF;
172 font.DrawSimpleText(&image, "Hello", pp::Point(0, 0), kColor, false);
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;
185 ASSERT_TRUE(found);
186 PASS();