TextOverflowTests.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/UnitTest/TestTypes.h>
  9. #include <TextOverflowWidget.h>
  10. namespace O3DE::ProjectManager
  11. {
  12. class TextOverflowWidgetTests
  13. : public ::UnitTest::LeakDetectionFixture
  14. {
  15. public:
  16. static bool EndsWithOverflowLink(const QString& str)
  17. {
  18. return str.endsWith(" <a href=\"OverflowLink\">Read More...</a>");
  19. }
  20. static const int s_testLength = 10;
  21. };
  22. TEST_F(TextOverflowWidgetTests, ElideText_UnderMaxLength_NoOverflow)
  23. {
  24. QString testStr = "";
  25. QString resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  26. EXPECT_FALSE(EndsWithOverflowLink(resultStr));
  27. testStr = "1234567890";
  28. resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  29. EXPECT_FALSE(EndsWithOverflowLink(resultStr));
  30. testStr = "1234<a href='https://www.o3de.org/'>56</a>7890";
  31. resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  32. EXPECT_FALSE(EndsWithOverflowLink(resultStr));
  33. }
  34. TEST_F(TextOverflowWidgetTests, ElideText_UnderMaxLength_NoTruncation)
  35. {
  36. QString testStr = "";
  37. QString resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  38. EXPECT_TRUE(testStr == resultStr);
  39. testStr = "1234567890";
  40. resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  41. EXPECT_TRUE(testStr == resultStr);
  42. testStr = "1234<a href='https://www.o3de.org/'>56</a>7890";
  43. resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  44. EXPECT_TRUE(testStr == resultStr);
  45. }
  46. TEST_F(TextOverflowWidgetTests, ElideText_OverMaxLength_ShowOverflow)
  47. {
  48. QString testStr = "12345678901";
  49. QString resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  50. EXPECT_TRUE(EndsWithOverflowLink(resultStr));
  51. testStr = "1234<a href='https://www.o3de.org/'>56</a>78901234<a href='https://www.o3de.org/'>56</a>7890";
  52. resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  53. EXPECT_TRUE(EndsWithOverflowLink(resultStr));
  54. }
  55. TEST_F(TextOverflowWidgetTests, ElideText_OverMaxLength_CorrectTruncation)
  56. {
  57. QString testStr = "12345678901234567890";
  58. QString resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  59. EXPECT_TRUE(resultStr.startsWith(QString("1234567890 ")));
  60. testStr = "1234<a href='https://www.o3de.org/'>56</a>78901234567890";
  61. resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  62. EXPECT_TRUE(resultStr.startsWith(QString("1234<a href='https://www.o3de.org/'>56</a>7890 ")));
  63. }
  64. TEST_F(TextOverflowWidgetTests, ElideText_OverMaxLengthAtOpeningTag_OpeningTagNotIncluded)
  65. {
  66. QString testStr = "1234567890<a href='https://www.o3de.org/'>1</a>";
  67. QString resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  68. EXPECT_FALSE(resultStr.startsWith(QString("1234567890<a ")));
  69. }
  70. TEST_F(TextOverflowWidgetTests, ElideText_OverMaxLengthAtLinkName_LinkNameTruncatedAndClosingTagIncluded)
  71. {
  72. QString testStr = "12345678<a href='https://www.o3de.org/'>901234567890</a>";
  73. QString resultStr = TextOverflowLabel::ElideLinkedText(testStr, s_testLength);
  74. EXPECT_TRUE(resultStr.startsWith(QString("12345678<a href='https://www.o3de.org/'>90</a> ")));
  75. }
  76. } // namespace O3DE::ProjectManager