File indexing completed on 2024-04-21 04:44:03

0001 #include <libsnore/snore.h>
0002 #include <libsnore/snore_p.h>
0003 #include <libsnore/utils.h>
0004 #include <libsnore/snoreconstants.h>
0005 
0006 #include <QTextDocument>
0007 
0008 #include <QtTest>
0009 
0010 using namespace Snore;
0011 
0012 class SnoreBenchmark : public QObject
0013 {
0014     Q_OBJECT
0015 public:
0016     SnoreBenchmark()
0017     {
0018         SnoreCore &instance = SnoreCore::instance();
0019         instance.loadPlugins(SnorePlugin::Backend);
0020         instance.setSettingsValue(Snore::Constants::SettingsKeys::Timeout, 1);
0021     }
0022 
0023     // clazy is complaining about this string but QStringLiteral won't work for the multiline string, so use QStringBuilder to silence it.
0024     QString htmlTestString = QLatin1String("<i>Italic A</i><br>"
0025                                            "<i>Italic B</i><br>"
0026                                            "<b>Bold</b><br>"
0027                                            "<u>Underline</u><br>"
0028                                            "<font color=\"blue\">Font</font><br>"
0029                                            "&lt;&amp;&gt;<br>"
0030                                            "<a href=\"https://github.com/Snorenotify/Snorenotify\">Website</a><br>") + QLatin1String("");
0031 
0032 private Q_SLOTS:
0033     void benchmarkUtilsToHtml();
0034     void benchmarkUtilsToHtmlAllMarkup();
0035     void benchmarkUtilsToPlain();
0036     void benchmarkNotifications();
0037 
0038 };
0039 
0040 void SnoreBenchmark::benchmarkUtilsToHtml()
0041 {
0042 
0043     QCOMPARE(Utils::normalizeMarkup(htmlTestString, Utils::NoMarkup), QLatin1String("Italic A\n"
0044              "Italic B\n"
0045              "Bold\n"
0046              "Underline\n"
0047              "Font\n"
0048              "<&>\n"
0049              "Website\n"));
0050     QCOMPARE(Utils::normalizeMarkup(htmlTestString, Utils::Href), QLatin1String("Italic A\n"
0051              "Italic B\n"
0052              "Bold\n"
0053              "Underline\n"
0054              "Font\n"
0055              "&lt;&amp;&gt;\n"
0056              "<a href=\"https://github.com/Snorenotify/Snorenotify\">Website</a>\n"));
0057     QCOMPARE(Utils::normalizeMarkup(htmlTestString, Utils::Href | Utils::Bold | Utils::Break |
0058                                     Utils::Underline | Utils::Font | Utils::Italic), htmlTestString);
0059     QBENCHMARK {
0060         Utils::normalizeMarkup(htmlTestString, Utils::Href);
0061     }
0062 }
0063 
0064 void SnoreBenchmark::benchmarkUtilsToHtmlAllMarkup()
0065 {
0066     QCOMPARE(Utils::normalizeMarkup(htmlTestString, Utils::AllMarkup), htmlTestString);
0067 
0068     QBENCHMARK {
0069         Utils::normalizeMarkup(htmlTestString, Utils::AllMarkup);
0070     }
0071 }
0072 
0073 void SnoreBenchmark::benchmarkUtilsToPlain()
0074 {
0075     QBENCHMARK {
0076         Utils::normalizeMarkup(htmlTestString, Utils::NoMarkup);
0077     }
0078 }
0079 
0080 void SnoreBenchmark::benchmarkNotifications()
0081 {
0082     SnoreCore &instance = SnoreCore::instance();
0083     int closed = 0;
0084     connect(&instance, &SnoreCore::notificationClosed, [&closed](Notification) {
0085         closed++;
0086     });
0087 
0088     Application app = SnoreCorePrivate::instance()->defaultApplication();
0089     //   QBENCHMARK_ONCE{
0090     for (int i = 0; i < 100; ++i) {
0091         QString number = QString::number(i);
0092         Notification n(app, app.defaultAlert(), QLatin1String("Test ") + number, QLatin1String("Message ") + number, app.icon());
0093         instance.broadcastNotification(n);
0094     }
0095     // }
0096     while (closed < 100) {
0097         QTest::qWait(100);
0098     }
0099 
0100 }
0101 
0102 QTEST_MAIN(SnoreBenchmark)
0103 
0104 #include "snorebenchmark.moc"