TestCommon.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef TestCommon_h__
  5. #define TestCommon_h__
  6. #include <stdlib.h>
  7. #include "nsThreadUtils.h"
  8. #include "mozilla/Attributes.h"
  9. inline int test_common_init(int *argc, char ***argv)
  10. {
  11. return 0;
  12. }
  13. //-----------------------------------------------------------------------------
  14. static bool gKeepPumpingEvents = false;
  15. class nsQuitPumpingEvent final : public nsIRunnable {
  16. ~nsQuitPumpingEvent() {}
  17. public:
  18. NS_DECL_THREADSAFE_ISUPPORTS
  19. NS_IMETHOD Run() override {
  20. gKeepPumpingEvents = false;
  21. return NS_OK;
  22. }
  23. };
  24. NS_IMPL_ISUPPORTS(nsQuitPumpingEvent, nsIRunnable)
  25. static inline void PumpEvents()
  26. {
  27. nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
  28. gKeepPumpingEvents = true;
  29. while (gKeepPumpingEvents)
  30. NS_ProcessNextEvent(thread);
  31. NS_ProcessPendingEvents(thread);
  32. }
  33. static inline void QuitPumpingEvents()
  34. {
  35. // Dispatch a task that toggles gKeepPumpingEvents so that we flush all
  36. // of the pending tasks before exiting from PumpEvents.
  37. nsCOMPtr<nsIRunnable> event = new nsQuitPumpingEvent();
  38. NS_DispatchToMainThread(event);
  39. }
  40. #endif