TMP_RuntimeTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using UnityEngine;
  2. using UnityEngine.TestTools;
  3. using NUnit.Framework;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. namespace TMPro
  8. {
  9. [Category("Text Parsing & Layout")]
  10. class TMP_RuntimeTests
  11. {
  12. private TextMeshPro m_TextComponent;
  13. // Characters: 22 Spaces: 4 Words: 5 Lines:
  14. private const string m_TextBlock_00 = "A simple line of text.";
  15. // Characters: 104 Spaces: 14 Words: 15 Lines:
  16. private const string m_TextBlock_01 = "Unity 2017 introduces new features that help teams of artists and developers build experiences together.";
  17. // Characters: 1500 Spaces: 228 Words: 241
  18. private const string m_TextBlock_02 = "The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words." +
  19. "Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.To achieve this, it would be necessary to have uniform grammar, pronunciation and more common words.If several languages coalesce, the grammar of the resulting language is more simple and regular than that of the individual languages." +
  20. "The new common language will be more simple and regular than the existing European languages.It will be as simple as Occidental; in fact, it will be Occidental.To an English person, it will seem like simplified English, as a skeptical Cambridge friend of mine told me what Occidental is. The European languages are members of the same family." +
  21. "Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary.The languages only differ in their grammar, their pronunciation and their most common words.Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.To achieve this, it would be necessary to" +
  22. "have uniform grammar, pronunciation and more common words.If several languages coalesce, the grammar of the resulting language is more simple and regular than that of the individual languages.The new common language will be";
  23. // Characters: 2500 Spaces: 343 Words: 370
  24. private const string m_TextBlock_03 = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. " +
  25. "Nullam dictum felis eu pede mollis pretium.Integer tincidunt.Cras dapibus.Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim.Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.Phasellus viverra nulla ut metus varius laoreet.Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue.Curabitur ullamcorper ultricies nisi. " +
  26. "Nam eget dui.Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem.Maecenas nec odio et ante tincidunt tempus.Donec vitae sapien ut libero venenatis faucibus.Nullam quis ante.Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. " +
  27. "Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus.Nullam accumsan lorem in dui.Cras ultricies mi eu turpis hendrerit fringilla.Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu. " +
  28. "Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris.Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris.Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc.Nunc nonummy metus.Vestibulum volutpat pretium libero. Cras id dui.Aenean ut eros et nisl sagittis vestibulum.Nullam nulla eros, ultricies sit amet, nonummy id, imperdiet feugiat, pede.Sed lectus. Donec mollis hendrerit risus. Phasellus nec sem in justo pellentesque facilisis. " +
  29. "Etiam imperdiet imperdiet orci. Nunc nec neque.Phasellus leo dolor, tempus non, auctor et, hendrerit quis, nisi.Curabitur ligula sapien, tincidunt non, euismod vitae, posuere imperdiet, leo.Maecenas malesuada. Praesent nan. The end of this of this long block of text.";
  30. // Characters: 3423 Spaces: 453 Words: 500
  31. private const string m_TextBlock_04 = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Aenean commodo ligula eget dolor.Aenean massa.Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.Nulla consequat massa quis enim.Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.Nullam dictum felis eu pede mollis pretium.Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus." +
  32. "Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim.Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.Phasellus viverra nulla ut metus varius laoreet.Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue.Curabitur ullamcorper ultricies nisi. Nam eget dui.Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem.Maecenas nec odio et ante tincidunt tempus.Donec vitae sapien ut libero venenatis faucibus.Nullam quis ante." +
  33. "Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus.Nullam accumsan lorem in dui.Cras ultricies mi eu turpis hendrerit fringilla.Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia.Nam pretium turpis et arcu." +
  34. "Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris.Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris.Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc.Nunc nonummy metus.Vestibulum volutpat pretium libero. Cras id dui.Aenean ut eros et nisl sagittis vestibulum.Nullam nulla eros, ultricies sit amet, nonummy id, imperdiet feugiat, pede.Sed lectus. Donec mollis hendrerit risus. Phasellus nec sem in justo pellentesque facilisis.Etiam imperdiet imperdiet orci. Nunc nec neque." +
  35. "Phasellus leo dolor, tempus non, auctor et, hendrerit quis, nisi.Curabitur ligula sapien, tincidunt non, euismod vitae, posuere imperdiet, leo.Maecenas malesuada. Praesent congue erat at massa.Sed cursus turpis vitae tortor.Donec posuere vulputate arcu. Phasellus accumsan cursus velit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed aliquam, nisi quis porttitor congue, elit erat euismod orci, ac placerat dolor lectus quis orci.Phasellus consectetuer vestibulum elit.Aenean tellus metus, bibendum sed, posuere ac, mattis non, nunc.Vestibulum fringilla pede sit amet augue." +
  36. "In turpis. Pellentesque posuere. Praesent turpis. Aenean posuere, tortor sed cursus feugiat, nunc augue blandit nunc, eu sollicitudin urna dolor sagittis lacus. Donec elit libero, sodales nec, volutpat a, suscipit non, turpis.Nullam sagittis. Suspendisse pulvinar, augue ac venenatis condimentum, sem libero volutpat nibh, nec pellentesque velit pede quis nunc. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Fusce id purus.Ut varius tincidunt libero.Phasellus dolor.Maecenas vestibulum mollis";
  37. //
  38. private const string m_TextBlock_05 = "This block of text contains <b>bold</b> and <i>italicized</i> characters.";
  39. private const string m_TextBlock_06 = "<align=center><style=H1><#ffffff><u>Multiple<#80f0ff> Alignment</color> per text object</u></color></style></align><line-height=2em>\n" +
  40. "</line-height> The <<#ffffa0>align</color>> tag in TextMesh<#40a0ff>Pro</color> provides the ability to control the alignment of lines and paragraphs which is essential when working with text.\n" +
  41. "<align=left> You may want some block of text to be<#80f0ff>left aligned</color> <<#ffffa0>align=<#80f0ff>left</color></color>> which is sort of the standard.</align>\n" +
  42. "<style=Quote><#ffffa0>\"Using <#80f0ff>Center Alignment</color> <<#ffffa0>align=<#80f0ff>center</color></color>> for a title or displaying a quote is another good example of text alignment.\"</color></style>\n" +
  43. "<align=right><#80f0ff>Right Alignment</color> <<#ffffa0>align=<#80f0ff>right</color></color>> can be useful to create contrast between lines and paragraphs of text.\n" +
  44. "<align=justified><#80f0ff>Justified Alignment</color> <<#ffffa0>align=<#80f0ff>justified</color></color>> results in text that is flush on both the left and right margins. Used well, justified type can look clean and classy.\n" +
  45. "<style=Quote><align=left><#ffffa0>\"Text formatting and alignment has a huge impact on how people will read and perceive your text.\"</color>\n" +
  46. "<size=65%><align=right> -Stephan Bouchard</style>";
  47. private readonly string[] testStrings = new string[] { m_TextBlock_00, m_TextBlock_01, m_TextBlock_02, m_TextBlock_03, m_TextBlock_04, m_TextBlock_05, m_TextBlock_06 };
  48. [OneTimeSetUp]
  49. public void Setup()
  50. {
  51. if (Directory.Exists(Path.GetFullPath("Assets/TextMesh Pro")) || Directory.Exists(Path.GetFullPath("Packages/com.unity.textmeshpro.tests/TextMesh Pro")))
  52. {
  53. GameObject textObject = new GameObject("Text Object");
  54. m_TextComponent = textObject.AddComponent<TextMeshPro>();
  55. m_TextComponent.fontSize = 18;
  56. }
  57. else
  58. {
  59. Debug.Log("Skipping over Editor tests as TMP Essential Resources are missing from the current test project.");
  60. Assert.Ignore();
  61. return;
  62. }
  63. }
  64. public static IEnumerable<object[]> TestCases_Parsing_TextInfo_WordWrapDisabled()
  65. {
  66. yield return new object[] { 0, 22, 4, 5, 1 };
  67. yield return new object[] { 1, 104, 14, 15, 1 };
  68. yield return new object[] { 2, 1500, 228, 241, 1 };
  69. yield return new object[] { 3, 2500, 343, 370, 1 };
  70. yield return new object[] { 4, 3423, 453, 500, 1 };
  71. }
  72. [Test, TestCaseSource("TestCases_Parsing_TextInfo_WordWrapDisabled")]
  73. public void Parsing_TextInfo_WordWrapDisabled(int sourceTextIndex, int characterCount, int spaceCount, int wordCount, int lineCount)
  74. {
  75. m_TextComponent.text = testStrings[sourceTextIndex];
  76. m_TextComponent.enableWordWrapping = false;
  77. m_TextComponent.alignment = TextAlignmentOptions.TopLeft;
  78. // Size the RectTransform
  79. m_TextComponent.rectTransform.sizeDelta = new Vector2(50, 5);
  80. // Force text generation to populate the TextInfo data structure.
  81. m_TextComponent.ForceMeshUpdate();
  82. Assert.AreEqual(m_TextComponent.textInfo.characterCount, characterCount);
  83. Assert.AreEqual(m_TextComponent.textInfo.spaceCount, spaceCount);
  84. Assert.AreEqual(m_TextComponent.textInfo.wordCount, wordCount);
  85. Assert.AreEqual(m_TextComponent.textInfo.lineCount, lineCount);
  86. }
  87. public static IEnumerable<object[]> TestCases_Parsing_TextInfo_WordWrapEnabled()
  88. {
  89. yield return new object[] { 0, 22, 4, 5, 1 };
  90. yield return new object[] { 1, 104, 14, 15, 1 };
  91. yield return new object[] { 2, 1500, 228, 241, 13 };
  92. yield return new object[] { 3, 2500, 343, 370, 21 };
  93. yield return new object[] { 4, 3423, 453, 500, 29 };
  94. }
  95. [Test, TestCaseSource("TestCases_Parsing_TextInfo_WordWrapEnabled")]
  96. public void Parsing_TextInfo_WordWrapEnabled(int sourceTextIndex, int characterCount, int spaceCount, int wordCount, int lineCount)
  97. {
  98. m_TextComponent.text = testStrings[sourceTextIndex];
  99. m_TextComponent.enableWordWrapping = true;
  100. m_TextComponent.alignment = TextAlignmentOptions.TopLeft;
  101. // Size the RectTransform
  102. m_TextComponent.rectTransform.sizeDelta = new Vector2(100, 50);
  103. // Force text generation to populate the TextInfo data structure.
  104. m_TextComponent.ForceMeshUpdate();
  105. Assert.AreEqual(m_TextComponent.textInfo.characterCount, characterCount);
  106. Assert.AreEqual(m_TextComponent.textInfo.spaceCount, spaceCount);
  107. Assert.AreEqual(m_TextComponent.textInfo.wordCount, wordCount);
  108. Assert.AreEqual(m_TextComponent.textInfo.lineCount, lineCount);
  109. }
  110. public static IEnumerable<object[]> TestCases_Parsing_TextInfo_AlignmentTopJustified()
  111. {
  112. yield return new object[] { 2, 1500, 228, 241, 13 };
  113. yield return new object[] { 3, 2500, 343, 370, 20 };
  114. yield return new object[] { 4, 3423, 453, 500, 27 };
  115. }
  116. [Test, TestCaseSource("TestCases_Parsing_TextInfo_AlignmentTopJustified")]
  117. public void Parsing_TextInfo_AlignmentTopJustified(int sourceTextIndex, int characterCount, int spaceCount, int wordCount, int lineCount)
  118. {
  119. m_TextComponent.text = testStrings[sourceTextIndex];
  120. m_TextComponent.enableWordWrapping = true;
  121. m_TextComponent.alignment = TextAlignmentOptions.TopJustified;
  122. // Size the RectTransform
  123. m_TextComponent.rectTransform.sizeDelta = new Vector2(100, 50);
  124. // Force text generation to populate the TextInfo data structure.
  125. m_TextComponent.ForceMeshUpdate();
  126. Assert.AreEqual(m_TextComponent.textInfo.characterCount, characterCount);
  127. Assert.AreEqual(m_TextComponent.textInfo.spaceCount, spaceCount);
  128. Assert.AreEqual(m_TextComponent.textInfo.wordCount, wordCount);
  129. Assert.AreEqual(m_TextComponent.textInfo.lineCount, lineCount);
  130. }
  131. public static IEnumerable<object[]> TestCases_Parsing_TextInfo_RichText()
  132. {
  133. yield return new object[] { 5, 59, 8, 9, 1 };
  134. yield return new object[] { 6, 768, 124, 126, 14 };
  135. }
  136. [Test, TestCaseSource("TestCases_Parsing_TextInfo_RichText")]
  137. public void Parsing_TextInfo_RichText(int sourceTextIndex, int characterCount, int spaceCount, int wordCount, int lineCount)
  138. {
  139. m_TextComponent.text = testStrings[sourceTextIndex];
  140. m_TextComponent.enableWordWrapping = true;
  141. m_TextComponent.alignment = TextAlignmentOptions.TopLeft;
  142. // Size the RectTransform
  143. m_TextComponent.rectTransform.sizeDelta = new Vector2(70, 35);
  144. // Force text generation to populate the TextInfo data structure.
  145. m_TextComponent.ForceMeshUpdate();
  146. Assert.AreEqual(m_TextComponent.textInfo.characterCount, characterCount);
  147. Assert.AreEqual(m_TextComponent.textInfo.spaceCount, spaceCount);
  148. Assert.AreEqual(m_TextComponent.textInfo.wordCount, wordCount);
  149. Assert.AreEqual(m_TextComponent.textInfo.lineCount, lineCount);
  150. }
  151. //[OneTimeTearDown]
  152. //public void Cleanup()
  153. //{
  154. // // Remove TMP Essential Resources if they were imported in the project as a result of running tests.
  155. // if (TMPro_EventManager.temporaryResourcesImported == true)
  156. // {
  157. // string testResourceFolderPath = Path.GetFullPath("Assets/TextMesh Pro");
  158. // if (Directory.Exists(testResourceFolderPath))
  159. // {
  160. // Directory.Delete(testResourceFolderPath);
  161. // File.Delete(Path.GetFullPath("Assets/TextMesh Pro.meta"));
  162. // }
  163. // TMPro_EventManager.temporaryResourcesImported = false;
  164. // }
  165. //}
  166. }
  167. }