The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-12 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../JuceDemoHeader.h"
  19. struct UnitTestClasses
  20. {
  21. class UnitTestsDemo;
  22. class TestRunnerThread;
  23. //==============================================================================
  24. // This subclass of UnitTestRunner is used to redirect the test output to our
  25. // TextBox, and to interrupt the running tests when our thread is asked to stop..
  26. class CustomTestRunner : public UnitTestRunner
  27. {
  28. public:
  29. CustomTestRunner (TestRunnerThread& trt) : owner (trt)
  30. {
  31. }
  32. void logMessage (const String& message) override
  33. {
  34. owner.logMessage (message);
  35. }
  36. bool shouldAbortTests() override
  37. {
  38. return owner.threadShouldExit();
  39. }
  40. private:
  41. TestRunnerThread& owner;
  42. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomTestRunner);
  43. };
  44. //==============================================================================
  45. class TestRunnerThread : public Thread,
  46. private Timer
  47. {
  48. public:
  49. TestRunnerThread (UnitTestsDemo& utd)
  50. : Thread ("Unit Tests"),
  51. owner (utd)
  52. {
  53. }
  54. void run() override
  55. {
  56. CustomTestRunner runner (*this);
  57. runner.runAllTests();
  58. startTimer (50); // when finished, start the timer which will
  59. // wait for the thread to end, then tell our component.
  60. }
  61. void logMessage (const String& message)
  62. {
  63. MessageManagerLock mm (this);
  64. if (mm.lockWasGained()) // this lock may fail if this thread has been told to stop
  65. owner.logMessage (message);
  66. }
  67. void timerCallback() override
  68. {
  69. if (! isThreadRunning())
  70. owner.testFinished(); // inform the demo page when done, so it can delete this thread.
  71. }
  72. private:
  73. UnitTestsDemo& owner;
  74. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TestRunnerThread);
  75. };
  76. //==============================================================================
  77. class UnitTestsDemo : public Component,
  78. public Button::Listener
  79. {
  80. public:
  81. UnitTestsDemo()
  82. : startTestButton ("Run Unit Tests...")
  83. {
  84. setOpaque (true);
  85. addAndMakeVisible (startTestButton);
  86. addAndMakeVisible (testResultsBox);
  87. testResultsBox.setMultiLine (true);
  88. testResultsBox.setFont (Font (Font::getDefaultMonospacedFontName(), 12.0f, Font::plain));
  89. startTestButton.addListener (this);
  90. logMessage ("This panel runs all the built-in JUCE unit-tests.\n");
  91. logMessage ("To add your own unit-tests, see the JUCE_UNIT_TESTS macro.");
  92. }
  93. ~UnitTestsDemo()
  94. {
  95. stopTest();
  96. }
  97. //==============================================================================
  98. void paint (Graphics& g) override
  99. {
  100. g.fillAll (Colours::grey);
  101. }
  102. void resized() override
  103. {
  104. Rectangle<int> r (getLocalBounds().reduced (6));
  105. startTestButton.setBounds (r.removeFromTop (25).removeFromLeft (200));
  106. testResultsBox.setBounds (r.withTrimmedTop (5));
  107. }
  108. void buttonClicked (Button* buttonThatWasClicked) override
  109. {
  110. if (buttonThatWasClicked == &startTestButton)
  111. {
  112. startTest();
  113. }
  114. }
  115. void startTest()
  116. {
  117. testResultsBox.clear();
  118. startTestButton.setEnabled (false);
  119. currentTestThread = new TestRunnerThread (*this);
  120. currentTestThread->startThread();
  121. }
  122. void stopTest()
  123. {
  124. if (currentTestThread != nullptr)
  125. {
  126. currentTestThread->stopThread (15000);
  127. currentTestThread = nullptr;
  128. }
  129. }
  130. void logMessage (const String& message)
  131. {
  132. testResultsBox.moveCaretToEnd();
  133. testResultsBox.insertTextAtCaret (message + newLine);
  134. testResultsBox.moveCaretToEnd();
  135. }
  136. void testFinished()
  137. {
  138. stopTest();
  139. startTestButton.setEnabled (true);
  140. logMessage (newLine + "*** Tests finished ***");
  141. }
  142. private:
  143. ScopedPointer<TestRunnerThread> currentTestThread;
  144. TextButton startTestButton;
  145. TextEditor testResultsBox;
  146. Label label;
  147. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UnitTestsDemo);
  148. };
  149. };
  150. // This static object will register this demo type in a global list of demos..
  151. static JuceDemoType<UnitTestClasses::UnitTestsDemo> demo ("40 Unit Tests");