tor-browser

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

ThenWithCycleCollectedArgsJS.cpp (5920B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=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
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "gtest/gtest.h"
      8 #include "mozilla/dom/Promise-inl.h"
      9 #include "mozilla/dom/Promise.h"
     10 #include "xpcpublic.h"
     11 
     12 using namespace mozilla;
     13 using namespace mozilla::dom;
     14 
     15 TEST(ThenWithCycleCollectedArgsJS, Empty)
     16 {
     17  nsCOMPtr<nsIGlobalObject> global =
     18      xpc::NativeGlobal(xpc::PrivilegedJunkScope());
     19 
     20  RefPtr<Promise> promise = Promise::Create(global, IgnoreErrors());
     21  auto result = promise->ThenWithCycleCollectedArgsJS(
     22      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&) { return nullptr; },
     23      std::make_tuple(), std::make_tuple());
     24 }
     25 
     26 TEST(ThenWithCycleCollectedArgsJS, nsCOMPtr)
     27 {
     28  nsCOMPtr<nsIGlobalObject> global =
     29      xpc::NativeGlobal(xpc::PrivilegedJunkScope());
     30 
     31  RefPtr<Promise> promise = Promise::Create(global, IgnoreErrors());
     32  auto result = promise->ThenWithCycleCollectedArgsJS(
     33      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&, nsIGlobalObject*) {
     34        return nullptr;
     35      },
     36      std::make_tuple(global), std::make_tuple());
     37 }
     38 
     39 TEST(ThenWithCycleCollectedArgsJS, RefPtr)
     40 {
     41  nsCOMPtr<nsIGlobalObject> global =
     42      xpc::NativeGlobal(xpc::PrivilegedJunkScope());
     43 
     44  RefPtr<Promise> promise = Promise::Create(global, IgnoreErrors());
     45  auto result = promise->ThenWithCycleCollectedArgsJS(
     46      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&, Promise*) {
     47        return nullptr;
     48      },
     49      std::make_tuple(promise), std::make_tuple());
     50 }
     51 
     52 TEST(ThenWithCycleCollectedArgsJS, RefPtrAndJSHandle)
     53 {
     54  nsCOMPtr<nsIGlobalObject> global =
     55      xpc::NativeGlobal(xpc::PrivilegedJunkScope());
     56 
     57  RefPtr<Promise> promise = Promise::Create(global, IgnoreErrors());
     58  auto result = promise->ThenWithCycleCollectedArgsJS(
     59      [](JSContext*, JS::Handle<JS::Value> v, ErrorResult&, Promise*,
     60         JS::Handle<JS::Value>) { return nullptr; },
     61      std::make_tuple(promise), std::make_tuple(JS::UndefinedHandleValue));
     62 }
     63 
     64 TEST(ThenWithCycleCollectedArgsJS, Mixed)
     65 {
     66  AutoJSAPI jsapi;
     67  MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope()));
     68  JSContext* cx = jsapi.cx();
     69  nsCOMPtr<nsIGlobalObject> global = xpc::CurrentNativeGlobal(cx);
     70  JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
     71 
     72  RefPtr<Promise> promise = Promise::Create(global, IgnoreErrors());
     73  auto result = promise->ThenWithCycleCollectedArgsJS(
     74      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&, nsIGlobalObject*,
     75         Promise*, JS::Handle<JS::Value>,
     76         JS::Handle<JSObject*>) { return nullptr; },
     77      std::make_tuple(global, promise),
     78      std::make_tuple(JS::UndefinedHandleValue, JS::HandleObject(obj)));
     79 }
     80 
     81 TEST(ThenCatchWithCycleCollectedArgsJS, Empty)
     82 {
     83  nsCOMPtr<nsIGlobalObject> global =
     84      xpc::NativeGlobal(xpc::PrivilegedJunkScope());
     85 
     86  RefPtr<Promise> promise = Promise::Create(global, IgnoreErrors());
     87  auto result = promise->ThenCatchWithCycleCollectedArgsJS(
     88      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&) { return nullptr; },
     89      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&) { return nullptr; },
     90      std::make_tuple(), std::make_tuple());
     91 }
     92 
     93 TEST(ThenCatchWithCycleCollectedArgsJS, nsCOMPtr)
     94 {
     95  nsCOMPtr<nsIGlobalObject> global =
     96      xpc::NativeGlobal(xpc::PrivilegedJunkScope());
     97 
     98  RefPtr<Promise> promise = Promise::Create(global, IgnoreErrors());
     99  auto result = promise->ThenCatchWithCycleCollectedArgsJS(
    100      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&, nsIGlobalObject*) {
    101        return nullptr;
    102      },
    103      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&, nsIGlobalObject*) {
    104        return nullptr;
    105      },
    106      std::make_tuple(global), std::make_tuple());
    107 }
    108 
    109 TEST(ThenCatchWithCycleCollectedArgsJS, RefPtr)
    110 {
    111  nsCOMPtr<nsIGlobalObject> global =
    112      xpc::NativeGlobal(xpc::PrivilegedJunkScope());
    113 
    114  RefPtr<Promise> promise = Promise::Create(global, IgnoreErrors());
    115  auto result = promise->ThenCatchWithCycleCollectedArgsJS(
    116      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&, Promise*) {
    117        return nullptr;
    118      },
    119      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&, Promise*) {
    120        return nullptr;
    121      },
    122      std::make_tuple(promise), std::make_tuple());
    123 }
    124 
    125 TEST(ThenCatchWithCycleCollectedArgsJS, RefPtrAndJSHandle)
    126 {
    127  nsCOMPtr<nsIGlobalObject> global =
    128      xpc::NativeGlobal(xpc::PrivilegedJunkScope());
    129 
    130  RefPtr<Promise> promise = Promise::Create(global, IgnoreErrors());
    131  auto result = promise->ThenCatchWithCycleCollectedArgsJS(
    132      [](JSContext*, JS::Handle<JS::Value> v, ErrorResult&, Promise*,
    133         JS::Handle<JS::Value>) { return nullptr; },
    134      [](JSContext*, JS::Handle<JS::Value> v, ErrorResult&, Promise*,
    135         JS::Handle<JS::Value>) { return nullptr; },
    136      std::make_tuple(promise), std::make_tuple(JS::UndefinedHandleValue));
    137 }
    138 
    139 TEST(ThenCatchWithCycleCollectedArgsJS, Mixed)
    140 {
    141  AutoJSAPI jsapi;
    142  MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope()));
    143  JSContext* cx = jsapi.cx();
    144  nsCOMPtr<nsIGlobalObject> global = xpc::CurrentNativeGlobal(cx);
    145  JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
    146 
    147  RefPtr<Promise> promise = Promise::Create(global, IgnoreErrors());
    148  auto result = promise->ThenCatchWithCycleCollectedArgsJS(
    149      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&, nsIGlobalObject*,
    150         Promise*, JS::Handle<JS::Value>,
    151         JS::Handle<JSObject*>) { return nullptr; },
    152      [](JSContext*, JS::Handle<JS::Value>, ErrorResult&, nsIGlobalObject*,
    153         Promise*, JS::Handle<JS::Value>,
    154         JS::Handle<JSObject*>) { return nullptr; },
    155      std::make_tuple(global, promise),
    156      std::make_tuple(JS::UndefinedHandleValue, JS::HandleObject(obj)));
    157 }