ctkFontButtonTest.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QSignalSpy>
  17. #include <QTimer>
  18. #include <QVBoxLayout>
  19. // CTK includes
  20. #include "ctkFontButton.h"
  21. #include "ctkTest.h"
  22. // STD includes
  23. #include <cstdlib>
  24. #include <iostream>
  25. // ----------------------------------------------------------------------------
  26. class ctkFontButtonTester: public QObject
  27. {
  28. Q_OBJECT
  29. private slots:
  30. void testDefaults();
  31. void testDefaultFont();
  32. void testSetFont();
  33. void testSetFont_data();
  34. void testSetFontTextFormat();
  35. void testSetFontTextFormat_data();
  36. void testUpdateText();
  37. void testBrowseFont();
  38. };
  39. // ----------------------------------------------------------------------------
  40. void ctkFontButtonTester::testDefaults()
  41. {
  42. ctkFontButton fontButton;
  43. QCOMPARE(fontButton.currentFont(), qApp->font());
  44. QCOMPARE(fontButton.fontTextFormat(), QString("fff-sss"));
  45. }
  46. // ----------------------------------------------------------------------------
  47. void ctkFontButtonTester::testDefaultFont()
  48. {
  49. QWidget parentWidget;
  50. parentWidget.setFont(QFont("Helvetica", 6));
  51. ctkFontButton fontButton(&parentWidget);
  52. QCOMPARE(fontButton.currentFont(), qApp->font());
  53. fontButton.setFont(QFont("Arial", 9));
  54. QCOMPARE(fontButton.currentFont(), qApp->font());
  55. }
  56. // ----------------------------------------------------------------------------
  57. void ctkFontButtonTester::testSetFont()
  58. {
  59. ctkFontButton fontButton;
  60. fontButton.setCurrentFont(QFont("Helvetica", 8));
  61. QSignalSpy spyFontChanged(&fontButton, SIGNAL(currentFontChanged(QFont)));
  62. QFETCH(QFont, currentFont);
  63. fontButton.setCurrentFont(currentFont);
  64. QFETCH(QFont, expectedCurrentFont);
  65. QFETCH(int, expectedFontChangedSignalCount);
  66. QCOMPARE(fontButton.currentFont(), expectedCurrentFont);
  67. QCOMPARE(spyFontChanged.count(), expectedFontChangedSignalCount);
  68. }
  69. // ----------------------------------------------------------------------------
  70. void ctkFontButtonTester::testSetFont_data()
  71. {
  72. QTest::addColumn<QFont>("currentFont");
  73. QTest::addColumn<QFont>("expectedCurrentFont");
  74. QTest::addColumn<int>("expectedFontChangedSignalCount");
  75. QTest::newRow("invalid") << QFont() << QFont() << 1;
  76. QTest::newRow("new family") << QFont("Arial", 8) << QFont("Arial", 8) << 1;
  77. QTest::newRow("new size") << QFont("Helvetica", 12) << QFont("Helvetica", 12) << 1;
  78. QTest::newRow("new weight") << QFont("Helvetica", 8, QFont::Bold) << QFont("Helvetica", 8, QFont::Bold) << 1;
  79. QTest::newRow("new family+size+weight") << QFont("Arial", 12, QFont::Bold) << QFont("Arial", 12, QFont::Bold) << 1;
  80. }
  81. // ----------------------------------------------------------------------------
  82. void ctkFontButtonTester::testSetFontTextFormat()
  83. {
  84. ctkFontButton fontButton;
  85. fontButton.setCurrentFont(QFont("Helvetica", 8, QFont::Normal, true));
  86. QFETCH(QString, fontTextFormat);
  87. fontButton.setFontTextFormat(fontTextFormat);
  88. QCOMPARE(fontButton.fontTextFormat(), fontTextFormat);
  89. QFETCH(QString, expectedText);
  90. QCOMPARE(fontButton.text(), expectedText);
  91. }
  92. // ----------------------------------------------------------------------------
  93. void ctkFontButtonTester::testSetFontTextFormat_data()
  94. {
  95. QTest::addColumn<QString>("fontTextFormat");
  96. QTest::addColumn<QString>("expectedText");
  97. QTest::newRow("null") << QString() << QString("");
  98. QTest::newRow("empty") << QString("") << QString("");
  99. QTest::newRow("fff") << QString("fff") << QString("Helvetica");
  100. QTest::newRow("ffffff") << QString("ffffff") << QString("HelveticaHelvetica");
  101. QTest::newRow("fff-ss-sss-ww-www-bb-bbb-ii-iii-uu-uuu-biu")
  102. << QString("fff-ss-sss-ww-www-bb-bbb-ii-iii-uu-uuu-biu")
  103. << QString("Helvetica-8-8pt-50-Normal---i-italic----i-");
  104. QTest::newRow("biuu") << QString("biuu") << QString("-i-u");
  105. }
  106. //-----------------------------------------------------------------------------
  107. void ctkFontButtonTester::testUpdateText()
  108. {
  109. ctkFontButton fontButton(QFont("Helvetica", 8));
  110. QCOMPARE(fontButton.text(), QString("Helvetica-8pt"));
  111. fontButton.setText("Custom text");
  112. QCOMPARE(fontButton.text(), QString("Custom text"));
  113. fontButton.setCurrentFont(QFont("Helvetica", 9));
  114. QCOMPARE(fontButton.text(), QString("Helvetica-9pt"));
  115. fontButton.setText("Custom text");
  116. QCOMPARE(fontButton.text(), QString("Custom text"));
  117. fontButton.setFontTextFormat("fff-ss");
  118. QCOMPARE(fontButton.text(), QString("Helvetica-9"));
  119. }
  120. //-----------------------------------------------------------------------------
  121. void ctkFontButtonTester::testBrowseFont()
  122. {
  123. ctkFontButton fontButton;
  124. QTimer::singleShot(200, qApp, SLOT(quit()));
  125. QTimer::singleShot(100, &fontButton, SLOT(browseFont()));
  126. fontButton.show();
  127. qApp->exec();
  128. }
  129. // ----------------------------------------------------------------------------
  130. CTK_TEST_MAIN(ctkFontButtonTest)
  131. #include "moc_ctkFontButtonTest.cpp"