mtransport_test_utils.h (1872B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 // Original author: ekr@rtfm.com 8 9 #ifndef mtransport_test_utils_h__ 10 #define mtransport_test_utils_h__ 11 12 #include "nsCOMPtr.h" 13 #include "nsISerialEventTarget.h" 14 #include "nsNetCID.h" 15 #include "nsPISocketTransportService.h" 16 #include "nsServiceManagerUtils.h" 17 #include "nsThreadUtils.h" 18 19 class MtransportTestUtils { 20 public: 21 MtransportTestUtils() { InitServices(); } 22 23 ~MtransportTestUtils() = default; 24 25 void InitServices() { 26 nsresult rv; 27 sts_target_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv); 28 MOZ_ASSERT(NS_SUCCEEDED(rv)); 29 sts_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv); 30 MOZ_ASSERT(NS_SUCCEEDED(rv)); 31 } 32 33 nsISerialEventTarget* sts_target() { return sts_target_; } 34 35 nsresult SyncDispatchToSTS(nsIRunnable* aRunnable) { 36 return SyncDispatchToSTS(do_AddRef(aRunnable)); 37 } 38 nsresult SyncDispatchToSTS(already_AddRefed<nsIRunnable>&& aRunnable) { 39 return NS_DispatchAndSpinEventLoopUntilComplete( 40 "MtransportTestUtils::SyncDispatchToSts"_ns, sts_target_, 41 std::move(aRunnable)); 42 } 43 44 private: 45 nsCOMPtr<nsISerialEventTarget> sts_target_; 46 nsCOMPtr<nsPISocketTransportService> sts_; 47 }; 48 49 #define CHECK_ENVIRONMENT_FLAG(envname) \ 50 char* test_flag = getenv(envname); \ 51 if (!test_flag || strcmp(test_flag, "1")) { \ 52 printf("To run this test set %s=1 in your environment\n", envname); \ 53 exit(0); \ 54 } 55 56 #endif