File: api_binding_test_util.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (191 lines) | stat: -rw-r--r-- 7,846 bytes parent folder | download | duplicates (5)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_RENDERER_BINDINGS_API_BINDING_TEST_UTIL_H_
#define EXTENSIONS_RENDERER_BINDINGS_API_BINDING_TEST_UTIL_H_

#include <memory>
#include <string>
#include <string_view>

#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "v8/include/v8.h"

namespace extensions {

// Returns a string with all single quotes replaced with double quotes. Useful
// to write JSON strings without needing to escape quotes.
std::string ReplaceSingleQuotes(std::string_view str);

// Returns a base::Value parsed from `str`. Will ADD_FAILURE on error.
base::Value ValueFromString(std::string_view str);

// As above, but returning a Value::List.
base::Value::List ListValueFromString(std::string_view str);

// As above, but returning a Value::Dict.
base::Value::Dict DictValueFromString(std::string_view str);

// Converts the given `value` to a JSON string. EXPECTs the conversion to
// succeed.
std::string ValueToString(const base::ValueView&);

// Converts the given `value` to a string. Returns "empty", "undefined", "null",
// or "function" for unserializable values. Note this differs from
// gin::V8ToString, which only accepts v8::String values.
std::string V8ToString(v8::Local<v8::Value> value,
                       v8::Local<v8::Context> context);

// Returns a v8::Value result from compiling and running `source`, or an empty
// local on failure.
v8::Local<v8::Value> V8ValueFromScriptSource(v8::Local<v8::Context> context,
                                             std::string_view source);

// Returns a v8::Function parsed from the given `source`. EXPECTs the conversion
// to succeed.
v8::Local<v8::Function> FunctionFromString(v8::Local<v8::Context> context,
                                           std::string_view source);

// Converts the given `value` to a base::Value and returns the result.
std::unique_ptr<base::Value> V8ToBaseValue(v8::Local<v8::Value> value,
                                           v8::Local<v8::Context> context);

// Calls the given `function` with the specified `receiver` and arguments, and
// returns the result. EXPECTs no errors to be thrown.
v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function,
                                 v8::Local<v8::Context> context,
                                 v8::Local<v8::Value> receiver,
                                 int argc,
                                 v8::Local<v8::Value> argv[]);

// Like RunFunction(), but uses v8::Undefined for the receiver.
v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function,
                                 v8::Local<v8::Context> context,
                                 int argc,
                                 v8::Local<v8::Value> argv[]);

// Like RunFunction(), but uses the `context`'s Global for the receiver.
v8::Local<v8::Value> RunFunctionOnGlobal(v8::Local<v8::Function> function,
                                         v8::Local<v8::Context> context,
                                         int argc,
                                         v8::Local<v8::Value> argv[]);

// Like RunFunctionOnGlobal(), but doesn't return the result. This is useful
// for binding in places a result isn't expected.
void RunFunctionOnGlobalAndIgnoreResult(v8::Local<v8::Function> function,
                                        v8::Local<v8::Context> context,
                                        int argc,
                                        v8::Local<v8::Value> argv[]);

// Like RunFunctionOnGlobal(), but returns a persistent handle for the result.
v8::Global<v8::Value> RunFunctionOnGlobalAndReturnHandle(
    v8::Local<v8::Function> function,
    v8::Local<v8::Context> context,
    int argc,
    v8::Local<v8::Value> argv[]);

// Calls the given `function` with the specified `receiver` and arguments, but
// EXPECTs the function to throw the `expected_error`.
void RunFunctionAndExpectError(v8::Local<v8::Function> function,
                               v8::Local<v8::Context> context,
                               v8::Local<v8::Value> receiver,
                               int argc,
                               v8::Local<v8::Value> argv[],
                               const std::string& expected_error);

// Like RunFunctionAndExpectError(), but uses v8::Undefined for the receiver.
void RunFunctionAndExpectError(v8::Local<v8::Function> function,
                               v8::Local<v8::Context> context,
                               int argc,
                               v8::Local<v8::Value> argv[],
                               const std::string& expected_error);

// Returns the property with the given `key` from the `object`. EXPECTs the
// operation not throw an error, but doesn't assume the key is present.
v8::Local<v8::Value> GetPropertyFromObject(v8::Local<v8::Object> object,
                                           v8::Local<v8::Context> context,
                                           std::string_view key);

// As above, but converts the result to a base::Value.
std::unique_ptr<base::Value> GetBaseValuePropertyFromObject(
    v8::Local<v8::Object> object,
    v8::Local<v8::Context> context,
    std::string_view key);

// As above, but returns a JSON-serialized version of the value, or
// "undefined", "null", "function", or "empty".
std::string GetStringPropertyFromObject(v8::Local<v8::Object> object,
                                        v8::Local<v8::Context> context,
                                        std::string_view key);

// A utility to determine if a V8 value is a certain type.
template <typename T>
struct ValueTypeChecker {};

template <>
struct ValueTypeChecker<v8::Function> {
  static bool IsType(v8::Local<v8::Value> value);
};

template <>
struct ValueTypeChecker<v8::Object> {
  static bool IsType(v8::Local<v8::Value> value);
};

template <>
struct ValueTypeChecker<v8::Promise> {
  static bool IsType(v8::Local<v8::Value> value);
};

template <>
struct ValueTypeChecker<v8::Array> {
  static bool IsType(v8::Local<v8::Value> value);
};

// Attempts to convert `value` to the expected type `T`, and populate `out`
// with the result. Returns true on success; adds test failures and returns
// false on error. (We do both so that it fits well into an EXPECT_TRUE() but
// also prints out more detailed failure information).
template <typename T>
bool GetValueAs(v8::Local<v8::Value> value, v8::Local<T>* out) {
  if (value.IsEmpty()) {
    ADD_FAILURE() << "Value is empty.";
    return false;
  }

  if (!ValueTypeChecker<T>::IsType(value)) {
    // TODO(devlin): Move the code to print out the type of a v8::Value from
    // argument_spec.cc into a common place, so we can print out
    // "Failed to convert value. Actual type <some type>".
    ADD_FAILURE() << "Value is incorrect type.";
    return false;
  }

  *out = value.As<T>();
  return true;
}

// Returns true if the given `value` is both non-empty and is the expected
// type `T`.
template <typename T>
bool V8ValueIs(v8::Local<v8::Value> value) {
  return !value.IsEmpty() && ValueTypeChecker<T>::IsType(value);
}

// Like GetPropertyFromObject(), but attempts to convert the value to the
// expected type `T`. Returns true and populates `out` on success; adds
// test failures and returns false on failure.
template <typename T>
bool GetPropertyFromObjectAs(v8::Local<v8::Object> object,
                             v8::Local<v8::Context> context,
                             std::string_view key,
                             v8::Local<T>* out) {
  v8::Local<v8::Value> value = GetPropertyFromObject(object, context, key);
  return GetValueAs(value, out);
}

}  // extensions

#endif  // EXTENSIONS_RENDERER_BINDINGS_API_BINDING_TEST_UTIL_H_