summaryrefslogtreecommitdiffstats
path: root/dom/fs/test/gtest/FileSystemMocks.cpp
blob: 1ad8e77c56a2560918285ecd2e16cfc0785d27ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FileSystemMocks.h"

#include <string>

#include "ErrorList.h"
#include "gtest/gtest-assertion-result.h"
#include "js/RootingAPI.h"
#include "jsapi.h"
#include "mozilla/dom/FileSystemManager.h"
#include "nsContentUtils.h"
#include "nsISupports.h"

namespace mozilla::dom::fs::test {

nsIGlobalObject* GetGlobal() {
  AutoJSAPI jsapi;
  DebugOnly<bool> ok = jsapi.Init(xpc::PrivilegedJunkScope());
  MOZ_ASSERT(ok);

  JSContext* cx = jsapi.cx();
  mozilla::dom::GlobalObject globalObject(cx, JS::CurrentGlobalOrNull(cx));
  nsCOMPtr<nsIGlobalObject> global =
      do_QueryInterface(globalObject.GetAsSupports());
  MOZ_ASSERT(global);

  return global.get();
}

nsresult GetAsString(const RefPtr<Promise>& aPromise, nsAString& aString) {
  AutoJSAPI jsapi;
  DebugOnly<bool> ok = jsapi.Init(xpc::PrivilegedJunkScope());
  MOZ_ASSERT(ok);

  JSContext* cx = jsapi.cx();

  JS::Rooted<JSObject*> promiseObj(cx, aPromise->PromiseObj());
  JS::Rooted<JS::Value> vp(cx, JS::GetPromiseResult(promiseObj));

  switch (aPromise->State()) {
    case Promise::PromiseState::Pending: {
      return NS_ERROR_DOM_INVALID_STATE_ERR;
    }

    case Promise::PromiseState::Resolved: {
      if (nsContentUtils::StringifyJSON(cx, vp, aString,
                                        UndefinedIsNullStringLiteral)) {
        return NS_OK;
      }

      return NS_ERROR_UNEXPECTED;
    }

    case Promise::PromiseState::Rejected: {
      if (vp.isInt32()) {
        int32_t errorCode = vp.toInt32();
        aString.AppendInt(errorCode);

        return NS_OK;
      }

      if (!vp.isObject()) {
        return NS_ERROR_UNEXPECTED;
      }

      RefPtr<Exception> exception;
      UNWRAP_OBJECT(Exception, &vp, exception);
      if (!exception) {
        return NS_ERROR_UNEXPECTED;
      }

      aString.Append(NS_ConvertUTF8toUTF16(
          GetStaticErrorName(static_cast<nsresult>(exception->Result()))));

      return NS_OK;
    }

    default:
      break;
  }

  return NS_ERROR_FAILURE;
}

mozilla::ipc::PrincipalInfo GetPrincipalInfo() {
  return mozilla::ipc::PrincipalInfo{mozilla::ipc::SystemPrincipalInfo{}};
}

}  // namespace mozilla::dom::fs::test