tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestCommon.h (1095B)


      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 "nsThreadUtils.h"
      9 #include "mozilla/SpinEventLoopUntil.h"
     10 
     11 //-----------------------------------------------------------------------------
     12 
     13 class WaitForCondition final : public nsIRunnable {
     14 public:
     15  NS_DECL_THREADSAFE_ISUPPORTS
     16 
     17  void Wait(int pending) {
     18    MOZ_RELEASE_ASSERT(NS_IsMainThread());
     19    MOZ_RELEASE_ASSERT(mPending == 0);
     20 
     21    mPending = pending;
     22    mozilla::SpinEventLoopUntil("TestCommon.h:WaitForCondition::Wait"_ns,
     23                                [&]() { return !mPending; });
     24    NS_ProcessPendingEvents(nullptr);
     25  }
     26 
     27  void Notify() { NS_DispatchToMainThread(this); }
     28 
     29 private:
     30  virtual ~WaitForCondition() = default;
     31 
     32  NS_IMETHOD Run() override {
     33    MOZ_RELEASE_ASSERT(NS_IsMainThread());
     34    MOZ_RELEASE_ASSERT(mPending);
     35 
     36    --mPending;
     37    return NS_OK;
     38  }
     39 
     40  uint32_t mPending = 0;
     41 };
     42 
     43 #endif