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 
5 #ifndef TestCommon_h__
6 #define TestCommon_h__
7 
8 #include <stdlib.h>
9 #include "nsThreadUtils.h"
10 #include "mozilla/Attributes.h"
11 
test_common_init(int * argc,char *** argv)12 inline int test_common_init(int *argc, char ***argv)
13 {
14   return 0;
15 }
16 
17 //-----------------------------------------------------------------------------
18 
19 static bool gKeepPumpingEvents = false;
20 
21 class nsQuitPumpingEvent final : public nsIRunnable {
~nsQuitPumpingEvent()22   ~nsQuitPumpingEvent() {}
23 public:
24   NS_DECL_THREADSAFE_ISUPPORTS
Run()25   NS_IMETHOD Run() override {
26     gKeepPumpingEvents = false;
27     return NS_OK;
28   }
29 };
NS_IMPL_ISUPPORTS(nsQuitPumpingEvent,nsIRunnable)30 NS_IMPL_ISUPPORTS(nsQuitPumpingEvent, nsIRunnable)
31 
32 static inline void PumpEvents()
33 {
34   nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
35 
36   gKeepPumpingEvents = true;
37   while (gKeepPumpingEvents)
38     NS_ProcessNextEvent(thread);
39 
40   NS_ProcessPendingEvents(thread);
41 }
42 
QuitPumpingEvents()43 static inline void QuitPumpingEvents()
44 {
45   // Dispatch a task that toggles gKeepPumpingEvents so that we flush all
46   // of the pending tasks before exiting from PumpEvents.
47   nsCOMPtr<nsIRunnable> event = new nsQuitPumpingEvent();
48   NS_DispatchToMainThread(event);
49 }
50 
51 #endif
52