PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/specrunner/specrunner.cpp

https://github.com/joseanpg/Cuore-Framework
C++ | 139 lines | 98 code | 20 blank | 21 comment | 11 complexity | 74d0e8d210368d5520c04636d5025cd1 MD5 | raw file
  1. /*
  2. Copyright (c) 2010 Sencha Inc.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <QtGui>
  20. #include <QtWebKit>
  21. #include <iostream>
  22. #if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
  23. #error Use Qt 4.7 or later version
  24. #endif
  25. class HeadlessSpecRunner: public QObject
  26. {
  27. Q_OBJECT
  28. public:
  29. HeadlessSpecRunner();
  30. void load(const QString &spec);
  31. public slots:
  32. void log(int indent, const QString &msg);
  33. private slots:
  34. void watch(bool ok);
  35. protected:
  36. bool hasElement(const char *select);
  37. void timerEvent(QTimerEvent *event);
  38. private:
  39. QWebPage m_page;
  40. QBasicTimer m_ticker;
  41. int m_runs;
  42. };
  43. HeadlessSpecRunner::HeadlessSpecRunner()
  44. : QObject()
  45. , m_runs(0)
  46. {
  47. m_page.settings()->enablePersistentStorage();
  48. connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(watch(bool)));
  49. }
  50. void HeadlessSpecRunner::load(const QString &spec)
  51. {
  52. m_ticker.stop();
  53. m_page.mainFrame()->load(spec);
  54. m_page.setPreferredContentsSize(QSize(1024, 600));
  55. }
  56. void HeadlessSpecRunner::watch(bool ok)
  57. {
  58. if (!ok) {
  59. std::cerr << "Can't load' " << qPrintable(m_page.mainFrame()->url().toString()) << std::endl;
  60. QApplication::instance()->exit(1);
  61. return;
  62. }
  63. m_ticker.start(200, this);
  64. }
  65. bool HeadlessSpecRunner::hasElement(const char *select)
  66. {
  67. return !m_page.mainFrame()->findFirstElement(select).isNull();
  68. }
  69. void HeadlessSpecRunner::log(int indent, const QString &msg)
  70. {
  71. for (int i = 0; i < indent; ++i)
  72. std::cout << " ";
  73. std::cout << qPrintable(msg);
  74. std::cout << std::endl;
  75. }
  76. #define DUMP_MSG "(function(n, i) { if (n.toString() === '[object NodeList]') { for (var c = 0; c < n.length; ++c) arguments.callee(n[c], i); return } if (n.className === 'description' || n.className == 'resultMessage fail') debug.log(i, n.textContent); var e = n.firstElementChild; while (e) { arguments.callee(e, i + 1); e = e.nextElementSibling; } })(document.getElementsByClassName('suite failed'), 1);"
  77. void HeadlessSpecRunner::timerEvent(QTimerEvent *event)
  78. {
  79. if (event->timerId() != m_ticker.timerId())
  80. return;
  81. if (!hasElement(".jasmine_reporter") && !hasElement(".runner.running"))
  82. return;
  83. if (hasElement(".runner.passed")) {
  84. QWebElement desc = m_page.mainFrame()->findFirstElement(".description");
  85. std::cout << qPrintable(desc.toPlainText()) << std::endl;
  86. QApplication::instance()->exit(0);
  87. return;
  88. }
  89. if (hasElement(".runner.failed")) {
  90. QWebElement desc = m_page.mainFrame()->findFirstElement(".description");
  91. std::cout << "FAIL: " << qPrintable(desc.toPlainText()) << std::endl;
  92. m_page.mainFrame()->addToJavaScriptWindowObject("debug", this);
  93. m_page.mainFrame()->evaluateJavaScript(DUMP_MSG);
  94. QDesktopServices::openUrl(m_page.mainFrame()->url());
  95. QApplication::instance()->exit(1);
  96. return;
  97. }
  98. ++m_runs;
  99. if (m_runs > 20) {
  100. std::cout << "WARNING: too many runs and the test is still not finished!" << std::endl;
  101. QApplication::instance()->exit(1);
  102. }
  103. }
  104. #include "specrunner.moc"
  105. int main(int argc, char** argv)
  106. {
  107. if (argc != 2) {
  108. std::cerr << "Run Jasmine's SpecRunner headlessly" << std::endl << std::endl;
  109. std::cerr << " specrunner SpecRunner.html" << std::endl;
  110. return 1;
  111. }
  112. QApplication app(argc, argv);
  113. HeadlessSpecRunner runner;
  114. runner.load(QString::fromLocal8Bit(argv[1]));
  115. return app.exec();
  116. }