DISTRHO Plugin Framework
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.

116 lines
3.9KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "tests.hpp"
  17. #define DPF_TEST_APPLICATION_CPP
  18. #include "dgl/src/pugl.cpp"
  19. #include "dgl/src/Application.cpp"
  20. #include "dgl/src/ApplicationPrivateData.cpp"
  21. #include "distrho/extra/Thread.hpp"
  22. START_NAMESPACE_DGL
  23. // --------------------------------------------------------------------------------------------------------------------
  24. class ApplicationQuitter : public Thread
  25. {
  26. Application& app;
  27. public:
  28. ApplicationQuitter(Application& a)
  29. : Thread("ApplicationQuitter"),
  30. app(a)
  31. {
  32. startThread();
  33. }
  34. private:
  35. void run() override
  36. {
  37. d_sleep(2);
  38. app.quit();
  39. }
  40. };
  41. // --------------------------------------------------------------------------------------------------------------------
  42. struct IdleCallbackCounter : IdleCallback
  43. {
  44. int counter;
  45. IdleCallbackCounter()
  46. : counter(0) {}
  47. void idleCallback() override
  48. {
  49. ++counter;
  50. }
  51. };
  52. // --------------------------------------------------------------------------------------------------------------------
  53. END_NAMESPACE_DGL
  54. int main()
  55. {
  56. USE_NAMESPACE_DGL;
  57. // regular usage
  58. {
  59. Application app(true);
  60. IdleCallbackCounter idleCounter;
  61. app.addIdleCallback(&idleCounter);
  62. DISTRHO_ASSERT_EQUAL(app.isQuiting(), false, "app MUST NOT be set as quitting during init");
  63. DISTRHO_ASSERT_EQUAL(idleCounter.counter, 0, "app MUST NOT have triggered idle callbacks yet");
  64. app.idle();
  65. DISTRHO_ASSERT_EQUAL(app.isQuiting(), false, "app MUST NOT be set as quitting after idle()");
  66. DISTRHO_ASSERT_EQUAL(idleCounter.counter, 1, "app MUST have triggered 1 idle callback");
  67. app.idle();
  68. DISTRHO_ASSERT_EQUAL(idleCounter.counter, 2, "app MUST have triggered 2 idle callbacks");
  69. app.quit();
  70. DISTRHO_ASSERT_EQUAL(app.isQuiting(), true, "app MUST be set as quitting after quit()");
  71. DISTRHO_ASSERT_EQUAL(idleCounter.counter, 2, "app MUST have triggered only 2 idle callbacks in its lifetime");
  72. }
  73. // standalone exec, must not block forever due to quit() called from another thread
  74. {
  75. Application app(true);
  76. ApplicationQuitter appQuitter(app);
  77. IdleCallbackCounter idleCounter;
  78. app.addIdleCallback(&idleCounter);
  79. app.exec();
  80. DISTRHO_ASSERT_EQUAL(appQuitter.isThreadRunning(), false, "app quit triggered because we told it so");
  81. DISTRHO_ASSERT_NOT_EQUAL(idleCounter.counter, 0, "app idle callbacks MUST have been triggered");
  82. }
  83. // standalone exec, but with 0 as timeout
  84. {
  85. Application app(true);
  86. ApplicationQuitter appQuitter(app);
  87. IdleCallbackCounter idleCounter;
  88. app.addIdleCallback(&idleCounter);
  89. app.exec(0);
  90. DISTRHO_ASSERT_EQUAL(appQuitter.isThreadRunning(), false, "app quit triggered because we told it so");
  91. DISTRHO_ASSERT_NOT_EQUAL(idleCounter.counter, 0, "app idle callbacks MUST have been triggered");
  92. }
  93. return 0;
  94. }
  95. // --------------------------------------------------------------------------------------------------------------------