Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / GlyphBufferTest.cpp
1 // Copyright 2014 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 "config.h"
6 #include "platform/fonts/GlyphBuffer.h"
7
8 #include "platform/fonts/SimpleFontData.h"
9 #include "wtf/PassRefPtr.h"
10 #include "wtf/RefPtr.h"
11 #include <gtest/gtest.h>
12
13 using namespace blink;
14
15 namespace {
16
17 // Minimal TestSimpleFontData implementation.
18 // Font has no glyphs, but that's okay.
19 class TestSimpleFontData : public SimpleFontData {
20 public:
21     static PassRefPtr<TestSimpleFontData> create()
22     {
23         return adoptRef(new TestSimpleFontData);
24     }
25
26 private:
27     TestSimpleFontData() : SimpleFontData(nullptr, 10, false, false) { }
28
29     bool fillGlyphPage(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength) const override
30     {
31         return false;
32     }
33 };
34
35 TEST(GlyphBufferTest, StartsEmpty)
36 {
37     GlyphBuffer glyphBuffer;
38     EXPECT_TRUE(glyphBuffer.isEmpty());
39     EXPECT_EQ(0u, glyphBuffer.size());
40 }
41
42 TEST(GlyphBufferTest, StoresGlyphs)
43 {
44     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
45     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
46
47     GlyphBuffer glyphBuffer;
48     glyphBuffer.add(42, font1.get(), 10);
49     glyphBuffer.add(43, font1.get(), 15);
50     glyphBuffer.add(44, font2.get(), 12);
51
52     EXPECT_FALSE(glyphBuffer.isEmpty());
53     EXPECT_FALSE(glyphBuffer.hasOffsets());
54     EXPECT_EQ(3u, glyphBuffer.size());
55
56     EXPECT_EQ(42, glyphBuffer.glyphAt(0));
57     EXPECT_EQ(43, glyphBuffer.glyphAt(1));
58     EXPECT_EQ(44, glyphBuffer.glyphAt(2));
59
60     const Glyph* glyphs = glyphBuffer.glyphs(0);
61     EXPECT_EQ(42, glyphs[0]);
62     EXPECT_EQ(43, glyphs[1]);
63     EXPECT_EQ(44, glyphs[2]);
64 }
65
66 TEST(GlyphBufferTest, StoresOffsets)
67 {
68     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
69     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
70
71     GlyphBuffer glyphBuffer;
72     glyphBuffer.add(42, font1.get(), FloatSize(10, 0), 0);
73     glyphBuffer.add(43, font1.get(), FloatSize(15, 0), 0);
74     glyphBuffer.add(44, font2.get(), FloatSize(12, 2), 0);
75
76     EXPECT_FALSE(glyphBuffer.isEmpty());
77     EXPECT_TRUE(glyphBuffer.hasOffsets());
78     EXPECT_EQ(3u, glyphBuffer.size());
79
80     const FloatSize* offsets = glyphBuffer.offsets(0);
81     EXPECT_EQ(FloatSize(10, 0), offsets[0]);
82     EXPECT_EQ(FloatSize(15, 0), offsets[1]);
83     EXPECT_EQ(FloatSize(12, 2), offsets[2]);
84 }
85
86 TEST(GlyphBufferTest, StoresAdvances)
87 {
88     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
89     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
90
91     GlyphBuffer glyphBuffer;
92     glyphBuffer.add(42, font1.get(), 10);
93     glyphBuffer.add(43, font1.get(), 15);
94     glyphBuffer.add(44, font2.get(), 20);
95
96     EXPECT_FALSE(glyphBuffer.isEmpty());
97     EXPECT_EQ(3u, glyphBuffer.size());
98
99     EXPECT_EQ(10, glyphBuffer.advanceAt(0));
100     EXPECT_EQ(15, glyphBuffer.advanceAt(1));
101     EXPECT_EQ(20, glyphBuffer.advanceAt(2));
102
103     const float* advances = glyphBuffer.advances(0);
104     EXPECT_EQ(10, advances[0]);
105     EXPECT_EQ(15, advances[1]);
106     EXPECT_EQ(20, advances[2]);
107 }
108
109 TEST(GlyphBufferTest, StoresSimpleFontData)
110 {
111     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
112     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
113
114     GlyphBuffer glyphBuffer;
115     glyphBuffer.add(42, font1.get(), 10);
116     glyphBuffer.add(43, font1.get(), 15);
117     glyphBuffer.add(44, font2.get(), 12);
118
119     EXPECT_FALSE(glyphBuffer.isEmpty());
120     EXPECT_EQ(3u, glyphBuffer.size());
121
122     EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(0));
123     EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(1));
124     EXPECT_EQ(font2.get(), glyphBuffer.fontDataAt(2));
125 }
126
127 TEST(GlyphBufferTest, GlyphArrayWithOffset)
128 {
129     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
130     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
131
132     GlyphBuffer glyphBuffer;
133     glyphBuffer.add(42, font1.get(), 10);
134     glyphBuffer.add(43, font1.get(), 15);
135     glyphBuffer.add(44, font2.get(), 12);
136
137     EXPECT_FALSE(glyphBuffer.isEmpty());
138     EXPECT_EQ(3u, glyphBuffer.size());
139
140     const Glyph* glyphs = glyphBuffer.glyphs(1);
141     EXPECT_EQ(43, glyphs[0]);
142     EXPECT_EQ(44, glyphs[1]);
143 }
144
145 TEST(GlyphBufferTest, AdvanceArrayWithOffset)
146 {
147     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
148     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
149
150     GlyphBuffer glyphBuffer;
151     glyphBuffer.add(42, font1.get(), 10);
152     glyphBuffer.add(43, font1.get(), 15);
153     glyphBuffer.add(43, font1.get(), 12);
154
155     EXPECT_FALSE(glyphBuffer.isEmpty());
156     EXPECT_EQ(3u, glyphBuffer.size());
157
158     const float* advances = glyphBuffer.advances(1);
159     EXPECT_EQ(15, advances[0]);
160     EXPECT_EQ(12, advances[1]);
161 }
162
163 TEST(GlyphBufferTest, Reverse)
164 {
165     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
166     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
167
168     GlyphBuffer glyphBuffer;
169     glyphBuffer.add(42, font1.get(), 10);
170     glyphBuffer.add(43, font1.get(), 15);
171     glyphBuffer.add(44, font2.get(), 20);
172
173     EXPECT_FALSE(glyphBuffer.isEmpty());
174     EXPECT_EQ(3u, glyphBuffer.size());
175
176     glyphBuffer.reverse();
177
178     EXPECT_FALSE(glyphBuffer.isEmpty());
179     EXPECT_EQ(3u, glyphBuffer.size());
180     EXPECT_EQ(44, glyphBuffer.glyphAt(0));
181     EXPECT_EQ(43, glyphBuffer.glyphAt(1));
182     EXPECT_EQ(42, glyphBuffer.glyphAt(2));
183     EXPECT_EQ(20, glyphBuffer.advanceAt(0));
184     EXPECT_EQ(15, glyphBuffer.advanceAt(1));
185     EXPECT_EQ(10, glyphBuffer.advanceAt(2));
186     EXPECT_EQ(font2.get(), glyphBuffer.fontDataAt(0));
187     EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(1));
188     EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(2));
189 }
190
191 TEST(GlyphBufferTest, ExpandLastAdvance)
192 {
193     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
194     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
195
196     GlyphBuffer glyphBuffer;
197     glyphBuffer.add(42, font1.get(), 10);
198     glyphBuffer.add(43, font1.get(), 15);
199     glyphBuffer.add(44, font2.get(), 12);
200
201     glyphBuffer.expandLastAdvance(20);
202     EXPECT_EQ(32, glyphBuffer.advanceAt(2));
203 }
204
205 } // namespace