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.

234 lines
7.5KB

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