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.

238 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: UnitTestsDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Performs unit tests.
  24. dependencies: juce_analytics, juce_audio_basics, juce_audio_devices,
  25. juce_audio_formats, juce_audio_processors, juce_audio_utils,
  26. juce_core, juce_cryptography, juce_data_structures, juce_dsp,
  27. juce_events, juce_graphics, juce_gui_basics, juce_gui_extra,
  28. juce_opengl, juce_osc, juce_product_unlocking, juce_video
  29. exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
  30. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  31. defines: JUCE_UNIT_TESTS=1
  32. type: Component
  33. mainClass: UnitTestClasses::UnitTestsDemo
  34. useLocalCopy: 1
  35. END_JUCE_PIP_METADATA
  36. *******************************************************************************/
  37. #pragma once
  38. #include "../Assets/DemoUtilities.h"
  39. //==============================================================================
  40. struct UnitTestClasses
  41. {
  42. class UnitTestsDemo;
  43. class TestRunnerThread;
  44. //==============================================================================
  45. // This subclass of UnitTestRunner is used to redirect the test output to our
  46. // TextBox, and to interrupt the running tests when our thread is asked to stop..
  47. class CustomTestRunner : public UnitTestRunner
  48. {
  49. public:
  50. CustomTestRunner (TestRunnerThread& trt) : owner (trt) {}
  51. void logMessage (const String& message) override
  52. {
  53. owner.logMessage (message);
  54. }
  55. bool shouldAbortTests() override
  56. {
  57. return owner.threadShouldExit();
  58. }
  59. private:
  60. TestRunnerThread& owner;
  61. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomTestRunner)
  62. };
  63. //==============================================================================
  64. class TestRunnerThread : public Thread,
  65. private Timer
  66. {
  67. public:
  68. TestRunnerThread (UnitTestsDemo& utd, const String& ctg)
  69. : Thread ("Unit Tests"),
  70. owner (utd),
  71. category (ctg)
  72. {}
  73. void run() override
  74. {
  75. CustomTestRunner runner (*this);
  76. if (category == "All Tests")
  77. runner.runAllTests();
  78. else
  79. runner.runTestsInCategory (category);
  80. startTimer (50); // when finished, start the timer which will
  81. // wait for the thread to end, then tell our component.
  82. }
  83. void logMessage (const String& message)
  84. {
  85. MessageManagerLock mm (this);
  86. if (mm.lockWasGained()) // this lock may fail if this thread has been told to stop
  87. owner.logMessage (message);
  88. }
  89. void timerCallback() override
  90. {
  91. if (! isThreadRunning())
  92. owner.testFinished(); // inform the demo page when done, so it can delete this thread.
  93. }
  94. private:
  95. UnitTestsDemo& owner;
  96. const String category;
  97. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TestRunnerThread)
  98. };
  99. //==============================================================================
  100. class UnitTestsDemo : public Component
  101. {
  102. public:
  103. UnitTestsDemo()
  104. {
  105. setOpaque (true);
  106. addAndMakeVisible (startTestButton);
  107. startTestButton.onClick = [this] { start(); };
  108. addAndMakeVisible (testResultsBox);
  109. testResultsBox.setMultiLine (true);
  110. testResultsBox.setFont (Font (Font::getDefaultMonospacedFontName(), 12.0f, Font::plain));
  111. addAndMakeVisible (categoriesBox);
  112. categoriesBox.addItem ("All Tests", 1);
  113. auto categories = UnitTest::getAllCategories();
  114. categories.sort (true);
  115. categoriesBox.addItemList (categories, 2);
  116. categoriesBox.setSelectedId (1);
  117. logMessage ("This panel runs the built-in JUCE unit-tests from the selected category.\n");
  118. logMessage ("To add your own unit-tests, see the JUCE_UNIT_TESTS macro.");
  119. setSize (500, 500);
  120. }
  121. ~UnitTestsDemo()
  122. {
  123. stopTest();
  124. }
  125. //==============================================================================
  126. void paint (Graphics& g) override
  127. {
  128. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  129. Colours::grey));
  130. }
  131. void resized() override
  132. {
  133. auto bounds = getLocalBounds().reduced (6);
  134. auto topSlice = bounds.removeFromTop (25);
  135. startTestButton.setBounds (topSlice.removeFromLeft (200));
  136. topSlice.removeFromLeft (10);
  137. categoriesBox .setBounds (topSlice.removeFromLeft (250));
  138. bounds.removeFromTop (5);
  139. testResultsBox.setBounds (bounds);
  140. }
  141. void start()
  142. {
  143. startTest (categoriesBox.getText());
  144. }
  145. void startTest (const String& category)
  146. {
  147. testResultsBox.clear();
  148. startTestButton.setEnabled (false);
  149. currentTestThread.reset (new TestRunnerThread (*this, category));
  150. currentTestThread->startThread();
  151. }
  152. void stopTest()
  153. {
  154. if (currentTestThread.get() != nullptr)
  155. {
  156. currentTestThread->stopThread (15000);
  157. currentTestThread.reset();
  158. }
  159. }
  160. void logMessage (const String& message)
  161. {
  162. testResultsBox.moveCaretToEnd();
  163. testResultsBox.insertTextAtCaret (message + newLine);
  164. testResultsBox.moveCaretToEnd();
  165. }
  166. void testFinished()
  167. {
  168. stopTest();
  169. startTestButton.setEnabled (true);
  170. logMessage (newLine + "*** Tests finished ***");
  171. }
  172. private:
  173. std::unique_ptr<TestRunnerThread> currentTestThread;
  174. TextButton startTestButton { "Run Unit Tests..." };
  175. ComboBox categoriesBox;
  176. TextEditor testResultsBox;
  177. void lookAndFeelChanged() override
  178. {
  179. testResultsBox.applyFontToAllText (testResultsBox.getFont());
  180. }
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UnitTestsDemo)
  182. };
  183. };