PageRenderTime 115ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/third_party/WebKit/Source/platform/fonts/GlyphPageTreeNodeTest.cpp

https://gitlab.com/jonnialva90/iridium-browser
C++ | 253 lines | 219 code | 31 blank | 3 comment | 3 complexity | 1bc70488eac0c80741aa9a61293441a5 MD5 | raw file
  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. #include "config.h"
  5. #include "platform/fonts/GlyphPageTreeNode.h"
  6. #include "platform/fonts/SegmentedFontData.h"
  7. #include "platform/fonts/SimpleFontData.h"
  8. #include <gtest/gtest.h>
  9. namespace blink {
  10. namespace {
  11. class TestCustomFontData : public CustomFontData {
  12. public:
  13. static PassRefPtr<TestCustomFontData> create() { return adoptRef(new TestCustomFontData()); }
  14. private:
  15. TestCustomFontData() { }
  16. bool isLoadingFallback() const override { return true; }
  17. };
  18. class TestSimpleFontData : public SimpleFontData {
  19. public:
  20. static PassRefPtr<TestSimpleFontData> create(UChar32 from, UChar32 to)
  21. {
  22. return adoptRef(new TestSimpleFontData(nullptr, from, to));
  23. }
  24. static PassRefPtr<TestSimpleFontData> createUnloaded(UChar32 from, UChar32 to)
  25. {
  26. return adoptRef(new TestSimpleFontData(TestCustomFontData::create(), from, to));
  27. }
  28. private:
  29. TestSimpleFontData(PassRefPtr<CustomFontData> customData, UChar32 from, UChar32 to)
  30. : SimpleFontData(customData, 10, false, false)
  31. , m_from(from)
  32. , m_to(to)
  33. {
  34. }
  35. bool fillGlyphPage(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength) const override
  36. {
  37. const Glyph kGlyph = 1;
  38. String bufferString(buffer, bufferLength);
  39. unsigned bufferIndex = 0;
  40. bool hasGlyphs = false;
  41. for (unsigned i = 0; i < length; i++) {
  42. UChar32 c = bufferString.characterStartingAt(bufferIndex);
  43. bufferIndex += U16_LENGTH(c);
  44. if (m_from <= c && c <= m_to) {
  45. pageToFill->setGlyphDataForIndex(offset + i, kGlyph, this);
  46. hasGlyphs = true;
  47. }
  48. }
  49. return hasGlyphs;
  50. }
  51. UChar32 m_from;
  52. UChar32 m_to;
  53. };
  54. } // anonymous namespace
  55. TEST(GlyphPageTreeNodeTest, rootChild)
  56. {
  57. const unsigned kPageNumber = 0;
  58. size_t pageCountBeforeTest = GlyphPageTreeNode::treeGlyphPageCount();
  59. {
  60. RefPtr<TestSimpleFontData> data = TestSimpleFontData::create('a', 'z');
  61. GlyphPageTreeNode* node = GlyphPageTreeNode::getNormalRootChild(data.get(), kPageNumber);
  62. EXPECT_EQ(pageCountBeforeTest + 1, GlyphPageTreeNode::treeGlyphPageCount());
  63. EXPECT_TRUE(node->page()->glyphAt('a'));
  64. EXPECT_FALSE(node->page()->glyphAt('A'));
  65. EXPECT_FALSE(node->isSystemFallback());
  66. EXPECT_EQ(1u, node->level());
  67. EXPECT_EQ(node, node->page()->owner());
  68. }
  69. EXPECT_EQ(pageCountBeforeTest, GlyphPageTreeNode::treeGlyphPageCount());
  70. }
  71. TEST(GlyphPageTreeNodeTest, level2)
  72. {
  73. const unsigned kPageNumber = 0;
  74. size_t pageCountBeforeTest = GlyphPageTreeNode::treeGlyphPageCount();
  75. {
  76. RefPtr<TestSimpleFontData> dataAtoC = TestSimpleFontData::create('A', 'C');
  77. RefPtr<TestSimpleFontData> dataCtoE = TestSimpleFontData::create('C', 'E');
  78. GlyphPageTreeNode* node1 = GlyphPageTreeNode::getNormalRootChild(dataAtoC.get(), kPageNumber);
  79. GlyphPageTreeNode* node2 = node1->getNormalChild(dataCtoE.get(), kPageNumber);
  80. EXPECT_EQ(pageCountBeforeTest + 3, GlyphPageTreeNode::treeGlyphPageCount());
  81. EXPECT_EQ(2u, node2->level());
  82. EXPECT_EQ(dataAtoC, node2->page()->glyphDataForCharacter('A').fontData);
  83. EXPECT_EQ(dataAtoC, node2->page()->glyphDataForCharacter('C').fontData);
  84. EXPECT_EQ(dataCtoE, node2->page()->glyphDataForCharacter('E').fontData);
  85. }
  86. EXPECT_EQ(pageCountBeforeTest, GlyphPageTreeNode::treeGlyphPageCount());
  87. }
  88. TEST(GlyphPageTreeNodeTest, segmentedData)
  89. {
  90. const unsigned kPageNumber = 0;
  91. size_t pageCountBeforeTest = GlyphPageTreeNode::treeGlyphPageCount();
  92. {
  93. RefPtr<TestSimpleFontData> dataBtoC = TestSimpleFontData::create('B', 'C');
  94. RefPtr<TestSimpleFontData> dataCtoE = TestSimpleFontData::create('C', 'E');
  95. RefPtr<SegmentedFontData> segmentedData = SegmentedFontData::create();
  96. segmentedData->appendRange(FontDataRange('A', 'C', dataBtoC));
  97. segmentedData->appendRange(FontDataRange('C', 'D', dataCtoE));
  98. GlyphPageTreeNode* node = GlyphPageTreeNode::getNormalRootChild(segmentedData.get(), kPageNumber);
  99. EXPECT_EQ(0, node->page()->glyphDataForCharacter('A').fontData);
  100. EXPECT_EQ(dataBtoC, node->page()->glyphDataForCharacter('B').fontData);
  101. EXPECT_EQ(dataBtoC, node->page()->glyphDataForCharacter('C').fontData);
  102. EXPECT_EQ(dataCtoE, node->page()->glyphDataForCharacter('D').fontData);
  103. EXPECT_EQ(0, node->page()->glyphDataForCharacter('E').fontData);
  104. }
  105. EXPECT_EQ(pageCountBeforeTest, GlyphPageTreeNode::treeGlyphPageCount());
  106. }
  107. TEST(GlyphPageTreeNodeTest, outsideBMP)
  108. {
  109. const unsigned kPageNumber = 0x1f300 / GlyphPage::size;
  110. size_t pageCountBeforeTest = GlyphPageTreeNode::treeGlyphPageCount();
  111. {
  112. RefPtr<TestSimpleFontData> data = TestSimpleFontData::create(0x1f310, 0x1f320);
  113. GlyphPageTreeNode* node = GlyphPageTreeNode::getNormalRootChild(data.get(), kPageNumber);
  114. EXPECT_EQ(pageCountBeforeTest + 1, GlyphPageTreeNode::treeGlyphPageCount());
  115. EXPECT_FALSE(node->page()->glyphForCharacter(0x1f30f));
  116. EXPECT_TRUE(node->page()->glyphForCharacter(0x1f310));
  117. EXPECT_TRUE(node->page()->glyphForCharacter(0x1f320));
  118. EXPECT_FALSE(node->page()->glyphForCharacter(0x1f321));
  119. }
  120. EXPECT_EQ(pageCountBeforeTest, GlyphPageTreeNode::treeGlyphPageCount());
  121. }
  122. TEST(GlyphPageTreeNodeTest, customData)
  123. {
  124. const unsigned kPageNumber = 0;
  125. size_t pageCountBeforeTest = GlyphPageTreeNode::treeGlyphPageCount();
  126. {
  127. RefPtr<TestSimpleFontData> dataAtoC = TestSimpleFontData::createUnloaded('A', 'C');
  128. RefPtr<TestSimpleFontData> dataBtoD = TestSimpleFontData::create('B', 'D');
  129. RefPtr<TestSimpleFontData> dataCtoE = TestSimpleFontData::createUnloaded('C', 'E');
  130. RefPtr<SegmentedFontData> segmentedData = SegmentedFontData::create();
  131. segmentedData->appendRange(FontDataRange('A', 'C', dataAtoC));
  132. segmentedData->appendRange(FontDataRange('B', 'D', dataBtoD));
  133. segmentedData->appendRange(FontDataRange('C', 'E', dataCtoE));
  134. GlyphPageTreeNode* node = GlyphPageTreeNode::getNormalRootChild(segmentedData.get(), kPageNumber);
  135. EXPECT_EQ(0, node->page()->glyphDataForCharacter('A').fontData);
  136. EXPECT_EQ(dataBtoD, node->page()->glyphDataForCharacter('B').fontData);
  137. EXPECT_EQ(dataBtoD, node->page()->glyphDataForCharacter('C').fontData);
  138. EXPECT_EQ(dataBtoD, node->page()->glyphDataForCharacter('D').fontData);
  139. EXPECT_EQ(0, node->page()->glyphDataForCharacter('E').fontData);
  140. EXPECT_EQ(dataAtoC->customFontData(), node->page()->customFontToLoadAt('A'));
  141. EXPECT_EQ(dataAtoC->customFontData(), node->page()->customFontToLoadAt('B'));
  142. EXPECT_EQ(dataAtoC->customFontData(), node->page()->customFontToLoadAt('C'));
  143. EXPECT_EQ(0, node->page()->customFontToLoadAt('D'));
  144. EXPECT_EQ(dataCtoE->customFontData(), node->page()->customFontToLoadAt('E'));
  145. }
  146. EXPECT_EQ(pageCountBeforeTest, GlyphPageTreeNode::treeGlyphPageCount());
  147. }
  148. TEST(GlyphPageTreeNodeTest, customDataWithMultiplePages)
  149. {
  150. const unsigned kPageNumber = 0;
  151. size_t pageCountBeforeTest = GlyphPageTreeNode::treeGlyphPageCount();
  152. {
  153. RefPtr<TestSimpleFontData> dataAtoC = TestSimpleFontData::createUnloaded('A', 'C');
  154. RefPtr<TestSimpleFontData> dataBtoD = TestSimpleFontData::create('B', 'D');
  155. RefPtr<TestSimpleFontData> dataCtoE = TestSimpleFontData::createUnloaded('C', 'E');
  156. RefPtr<SegmentedFontData> segmentedData1 = SegmentedFontData::create();
  157. RefPtr<SegmentedFontData> segmentedData2 = SegmentedFontData::create();
  158. RefPtr<SegmentedFontData> segmentedData3 = SegmentedFontData::create();
  159. segmentedData1->appendRange(FontDataRange('A', 'C', dataAtoC));
  160. segmentedData2->appendRange(FontDataRange('B', 'D', dataBtoD));
  161. segmentedData3->appendRange(FontDataRange('C', 'E', dataCtoE));
  162. GlyphPageTreeNode* node1 = GlyphPageTreeNode::getNormalRootChild(segmentedData1.get(), kPageNumber);
  163. GlyphPageTreeNode* node2 = node1->getNormalChild(segmentedData2.get(), kPageNumber);
  164. GlyphPageTreeNode* node3 = node2->getNormalChild(segmentedData3.get(), kPageNumber);
  165. EXPECT_EQ(0, node3->page()->glyphDataForCharacter('A').fontData);
  166. EXPECT_EQ(dataBtoD, node3->page()->glyphDataForCharacter('B').fontData);
  167. EXPECT_EQ(dataBtoD, node3->page()->glyphDataForCharacter('C').fontData);
  168. EXPECT_EQ(dataBtoD, node3->page()->glyphDataForCharacter('D').fontData);
  169. EXPECT_EQ(0, node3->page()->glyphDataForCharacter('E').fontData);
  170. EXPECT_EQ(dataAtoC->customFontData(), node3->page()->customFontToLoadAt('A'));
  171. EXPECT_EQ(dataAtoC->customFontData(), node3->page()->customFontToLoadAt('B'));
  172. EXPECT_EQ(dataAtoC->customFontData(), node3->page()->customFontToLoadAt('C'));
  173. EXPECT_EQ(0, node3->page()->customFontToLoadAt('D'));
  174. EXPECT_EQ(dataCtoE->customFontData(), node3->page()->customFontToLoadAt('E'));
  175. }
  176. EXPECT_EQ(pageCountBeforeTest, GlyphPageTreeNode::treeGlyphPageCount());
  177. }
  178. TEST(GlyphPageTreeNodeTest, systemFallback)
  179. {
  180. const unsigned kPageNumber = 0;
  181. size_t pageCountBeforeTest = GlyphPageTreeNode::treeGlyphPageCount();
  182. {
  183. RefPtr<TestSimpleFontData> dataAtoC = TestSimpleFontData::createUnloaded('A', 'C');
  184. RefPtr<TestSimpleFontData> dataBtoD = TestSimpleFontData::create('B', 'D');
  185. RefPtr<SegmentedFontData> segmentedData = SegmentedFontData::create();
  186. segmentedData->appendRange(FontDataRange('A', 'C', dataAtoC));
  187. GlyphPageTreeNode* node1 = GlyphPageTreeNode::getNormalRootChild(segmentedData.get(), kPageNumber);
  188. GlyphPageTreeNode* node2 = node1->getNormalChild(dataBtoD.get(), kPageNumber);
  189. SystemFallbackGlyphPageTreeNode* node3 = node2->getSystemFallbackChild(kPageNumber);
  190. EXPECT_TRUE(node3->isSystemFallback());
  191. EXPECT_EQ(0, node3->page()->glyphDataForCharacter('A').fontData);
  192. EXPECT_EQ(dataBtoD, node3->page()->glyphDataForCharacter('B').fontData);
  193. EXPECT_EQ(dataBtoD, node3->page()->glyphDataForCharacter('C').fontData);
  194. EXPECT_EQ(dataBtoD, node3->page()->glyphDataForCharacter('D').fontData);
  195. EXPECT_EQ(dataAtoC->customFontData(), node3->page()->customFontToLoadAt('A'));
  196. EXPECT_EQ(dataAtoC->customFontData(), node3->page()->customFontToLoadAt('B'));
  197. EXPECT_EQ(dataAtoC->customFontData(), node3->page()->customFontToLoadAt('C'));
  198. EXPECT_EQ(0, node3->page()->customFontToLoadAt('D'));
  199. }
  200. EXPECT_EQ(pageCountBeforeTest, GlyphPageTreeNode::treeGlyphPageCount());
  201. }
  202. TEST(GlyphPageTreeNodeTest, systemFallbackScriptIsolation)
  203. {
  204. const unsigned kPageNumber = 0;
  205. RefPtr<TestSimpleFontData> defaultData = TestSimpleFontData::create('A', 'B');
  206. GlyphPageTreeNode* node1 = GlyphPageTreeNode::getNormalRootChild(defaultData.get(), kPageNumber);
  207. SystemFallbackGlyphPageTreeNode* node2 = node1->getSystemFallbackChild(kPageNumber);
  208. EXPECT_TRUE(node2->isSystemFallback());
  209. GlyphPage* commonPage = node2->page(USCRIPT_COMMON);
  210. GlyphPage* latinPage = node2->page(USCRIPT_LATIN);
  211. GlyphPage* simplifiedChinesePage = node2->page(USCRIPT_SIMPLIFIED_HAN);
  212. GlyphPage* traditionalChinesePage = node2->page(USCRIPT_TRADITIONAL_HAN);
  213. GlyphPage* japanesePage = node2->page(USCRIPT_KATAKANA_OR_HIRAGANA);
  214. EXPECT_NE(commonPage, latinPage);
  215. EXPECT_NE(commonPage, simplifiedChinesePage);
  216. EXPECT_NE(commonPage, traditionalChinesePage);
  217. EXPECT_NE(simplifiedChinesePage, traditionalChinesePage);
  218. EXPECT_NE(commonPage, japanesePage);
  219. EXPECT_NE(japanesePage, simplifiedChinesePage);
  220. EXPECT_NE(japanesePage, traditionalChinesePage);
  221. }
  222. } // namespace blink