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.

233 lines
7.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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, vs2019, linux_make, androidstudio, xcode_iphone
  30. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  31. defines: JUCE_UNIT_TESTS=1
  32. type: Component
  33. mainClass: UnitTestsDemo
  34. useLocalCopy: 1
  35. END_JUCE_PIP_METADATA
  36. *******************************************************************************/
  37. #pragma once
  38. #include "../Assets/DemoUtilities.h"
  39. //==============================================================================
  40. class UnitTestsDemo : public Component
  41. {
  42. public:
  43. UnitTestsDemo()
  44. {
  45. setOpaque (true);
  46. addAndMakeVisible (startTestButton);
  47. startTestButton.onClick = [this] { start(); };
  48. addAndMakeVisible (testResultsBox);
  49. testResultsBox.setMultiLine (true);
  50. testResultsBox.setFont (Font (Font::getDefaultMonospacedFontName(), 12.0f, Font::plain));
  51. addAndMakeVisible (categoriesBox);
  52. categoriesBox.addItem ("All Tests", 1);
  53. auto categories = UnitTest::getAllCategories();
  54. categories.sort (true);
  55. categoriesBox.addItemList (categories, 2);
  56. categoriesBox.setSelectedId (1);
  57. logMessage ("This panel runs the built-in JUCE unit-tests from the selected category.\n");
  58. logMessage ("To add your own unit-tests, see the JUCE_UNIT_TESTS macro.");
  59. setSize (500, 500);
  60. }
  61. ~UnitTestsDemo() override
  62. {
  63. stopTest();
  64. }
  65. //==============================================================================
  66. void paint (Graphics& g) override
  67. {
  68. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  69. Colours::grey));
  70. }
  71. void resized() override
  72. {
  73. auto bounds = getLocalBounds().reduced (6);
  74. auto topSlice = bounds.removeFromTop (25);
  75. startTestButton.setBounds (topSlice.removeFromLeft (200));
  76. topSlice.removeFromLeft (10);
  77. categoriesBox .setBounds (topSlice.removeFromLeft (250));
  78. bounds.removeFromTop (5);
  79. testResultsBox.setBounds (bounds);
  80. }
  81. void start()
  82. {
  83. startTest (categoriesBox.getText());
  84. }
  85. void startTest (const String& category)
  86. {
  87. testResultsBox.clear();
  88. startTestButton.setEnabled (false);
  89. currentTestThread.reset (new TestRunnerThread (*this, category));
  90. currentTestThread->startThread();
  91. }
  92. void stopTest()
  93. {
  94. if (currentTestThread.get() != nullptr)
  95. {
  96. currentTestThread->stopThread (15000);
  97. currentTestThread.reset();
  98. }
  99. }
  100. void logMessage (const String& message)
  101. {
  102. testResultsBox.moveCaretToEnd();
  103. testResultsBox.insertTextAtCaret (message + newLine);
  104. testResultsBox.moveCaretToEnd();
  105. }
  106. void testFinished()
  107. {
  108. stopTest();
  109. startTestButton.setEnabled (true);
  110. logMessage (newLine + "*** Tests finished ***");
  111. }
  112. private:
  113. //==============================================================================
  114. class TestRunnerThread : public Thread,
  115. private Timer
  116. {
  117. public:
  118. TestRunnerThread (UnitTestsDemo& utd, const String& ctg)
  119. : Thread ("Unit Tests"),
  120. owner (utd),
  121. category (ctg)
  122. {}
  123. void run() override
  124. {
  125. CustomTestRunner runner (*this);
  126. if (category == "All Tests")
  127. runner.runAllTests();
  128. else
  129. runner.runTestsInCategory (category);
  130. startTimer (50); // when finished, start the timer which will
  131. // wait for the thread to end, then tell our component.
  132. }
  133. void logMessage (const String& message)
  134. {
  135. WeakReference<UnitTestsDemo> safeOwner (&owner);
  136. MessageManager::callAsync ([=]
  137. {
  138. if (auto* o = safeOwner.get())
  139. o->logMessage (message);
  140. });
  141. }
  142. void timerCallback() override
  143. {
  144. if (! isThreadRunning())
  145. owner.testFinished(); // inform the demo page when done, so it can delete this thread.
  146. }
  147. private:
  148. //==============================================================================
  149. // This subclass of UnitTestRunner is used to redirect the test output to our
  150. // TextBox, and to interrupt the running tests when our thread is asked to stop..
  151. class CustomTestRunner : public UnitTestRunner
  152. {
  153. public:
  154. CustomTestRunner (TestRunnerThread& trt) : owner (trt) {}
  155. void logMessage (const String& message) override
  156. {
  157. owner.logMessage (message);
  158. }
  159. bool shouldAbortTests() override
  160. {
  161. return owner.threadShouldExit();
  162. }
  163. private:
  164. TestRunnerThread& owner;
  165. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomTestRunner)
  166. };
  167. UnitTestsDemo& owner;
  168. const String category;
  169. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TestRunnerThread)
  170. };
  171. std::unique_ptr<TestRunnerThread> currentTestThread;
  172. TextButton startTestButton { "Run Unit Tests..." };
  173. ComboBox categoriesBox;
  174. TextEditor testResultsBox;
  175. void lookAndFeelChanged() override
  176. {
  177. testResultsBox.applyFontToAllText (testResultsBox.getFont());
  178. }
  179. JUCE_DECLARE_WEAK_REFERENCEABLE (UnitTestsDemo)
  180. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UnitTestsDemo)
  181. };