Shaka Player Embedded
test_type.cc
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Allow struct fields so we can test the GC. This should only be used in this
16 // file since struct fields should not be used normally. This must appear
17 // before the headers so this is defined when register_member.h is included.
18 #define ALLOW_STRUCT_FIELDS
19 
20 #include "src/js/test_type.h"
21 
22 #include <utility>
23 
25 #include "src/js/console.h"
26 #include "src/mapping/js_utils.h"
27 #include "src/memory/heap_tracer.h"
28 
29 namespace shaka {
30 namespace js {
31 
32 namespace {
33 
34 std::string GetExpectedString() {
35  // Since this contains embedded nulls, we can't use the normal char*
36  // constructor. Instead use the size of the array to get the length of the
37  // string literal and subtract one for the ending '\0'.
38  return std::string(EXPECTED_STRING,
39  EXPECTED_STRING + sizeof(EXPECTED_STRING) - 1);
40 }
41 
42 } // namespace
43 
45 
46 TestType::TestType() : int_or_object(0) {}
47 // \cond Doxygen_Skip
48 TestType::~TestType() {}
49 // \endcond Doxygen_Skip
50 
51 void TestType::Trace(memory::HeapTracer* tracer) const {
52  BackingObject::Trace(tracer);
53  tracer->Trace(&optional_object);
54  tracer->Trace(&int_or_object);
55  tracer->Trace(&struct_);
56  tracer->Trace(&array);
57  tracer->Trace(&callback);
58  tracer->Trace(&any);
59  tracer->Trace(&buffer);
60 }
61 
62 bool TestType::IsExpectedString(const std::string& arg) const {
63  return arg == GetExpectedString();
64 }
65 
67  return arg.has_value();
68 }
69 
71  return holds_alternative<int>(arg) && get<int>(arg) == EXPECTED_INT;
72 }
73 
75  return holds_alternative<TestTypeOptions>(arg) &&
76  IsExpectedConvertedStruct(get<TestTypeOptions>(arg));
77 }
78 
80  return opts.string == GetExpectedString() && opts.boolean;
81 }
82 
84  return opts.string.empty() && !opts.boolean;
85 }
86 
88  return e == EXPECTED_NUMBER_ENUM;
89 }
90 
92  return e == EXPECTED_STRING_ENUM;
93 }
94 
96  const std::vector<std::string>& data) const {
97  return data == GetArrayOfStrings();
98 }
99 
101  std::string str;
102  return anything.TryConvertTo(&str) && str == GetExpectedString();
103 }
104 
105 bool TestType::IsTruthy(Any anything) const {
106  return anything.IsTruthy();
107 }
108 
110  Callback call_copy = // NOLINT(performance-unnecessary-copy-initialization)
111  callback;
112  call_copy(GetExpectedString());
113 }
114 
116  this->buffer = std::move(buffer);
117 }
118 
120  opts.string = "abc";
121  return opts;
122 }
123 
125  return JsError::Error(message);
126 }
127 
128 Promise TestType::PromiseAcceptString(const std::string& /* value */) const {
129  LocalVar<JsValue> value(JsUndefined());
130  return Promise::Resolved(value);
131 }
132 
134  LocalVar<JsValue> rooted(value.ToJsValue());
135  return Promise::Resolved(rooted);
136 }
137 
138 Promise TestType::PromiseResolveAfter(uint64_t delay) const {
139  RefPtr<Promise> ret = MakeJsRef<Promise>(Promise::PendingPromise());
140  JsManagerImpl::Instance()->MainThread()->AddTimer(delay, [=]() {
141  LocalVar<JsValue> value(JsUndefined());
142  ret->ResolveWith(value);
143  });
144  return *ret;
145 }
146 
147 std::string TestType::GetString() const {
148  return GetExpectedString();
149 }
150 
152  if (!has_value)
153  return nullopt;
154  return GetExpectedString();
155 }
156 
158  if (get_int)
159  return EXPECTED_INT;
160  return GetExpectedString();
161 }
162 
164  TestTypeOptions ret;
165  ret.string = GetExpectedString();
166  ret.boolean = true;
167  return ret;
168 }
169 
171  return EXPECTED_NUMBER_ENUM;
172 }
173 
175  return EXPECTED_STRING_ENUM;
176 }
177 
178 std::vector<std::string> TestType::GetArrayOfStrings() const {
179  return {"abc", "123", GetExpectedString()};
180 }
181 
182 std::unordered_map<std::string, std::string> TestType::GetMapOfStrings() const {
183  std::unordered_map<std::string, std::string> ret;
184  ret["a"] = "1";
185  ret["b"] = "2";
186  return ret;
187 }
188 
190  return buffer;
191 }
192 
193 std::string TestType::ToPrettyString(Any anything) const {
194  LocalVar<JsValue> value = anything.ToJsValue();
195  return Console::ConvertToPrettyString(value);
196 }
197 
199  AddMemberFunction("acceptNumber", &TestType::AcceptNumber);
200  AddMemberFunction("acceptBoolean", &TestType::AcceptBoolean);
201  AddMemberFunction("acceptString", &TestType::AcceptString);
202  AddMemberFunction("acceptOptionalString", &TestType::AcceptOptionalString);
203  AddMemberFunction("acceptOptionalStruct", &TestType::AcceptOptionalStruct);
204  AddMemberFunction("acceptIntOrStruct", &TestType::AcceptIntOrStruct);
205  AddMemberFunction("acceptStringEnumOrAnyNumber",
207  AddMemberFunction("acceptStruct", &TestType::AcceptStruct);
208  AddMemberFunction("acceptNumberEnum", &TestType::AcceptNumberEnum);
209  AddMemberFunction("acceptStringEnum", &TestType::AcceptStringEnum);
210  AddMemberFunction("acceptArrayOfStrings", &TestType::AcceptArrayOfStrings);
211  AddMemberFunction("acceptCallback", &TestType::AcceptCallback);
212  AddMemberFunction("acceptAnything", &TestType::AcceptAnything);
213  AddMemberFunction("acceptByteBuffer", &TestType::AcceptByteBuffer);
214 
215  AddMemberFunction("isExpectedString", &TestType::IsExpectedString);
216  AddMemberFunction("isOptionalPresent", &TestType::IsOptionalPresent);
217  AddMemberFunction("isExpectedIntWithOr", &TestType::IsExpectedIntWithOr);
218  AddMemberFunction("isExpectedStructWithOr",
220  AddMemberFunction("isExpectedConvertedStruct",
222  AddMemberFunction("isConvertedStructEmpty",
224  AddMemberFunction("isExpectedNumberEnum", &TestType::IsExpectedNumberEnum);
225  AddMemberFunction("isExpectedStringEnum", &TestType::IsExpectedStringEnum);
226  AddMemberFunction("isExpectedArrayOfStrings",
228  AddMemberFunction("isExpectedStringWithAny",
230  AddMemberFunction("isTruthy", &TestType::IsTruthy);
231 
232  AddMemberFunction("invokeCallbackWithString",
234  AddMemberFunction("storeByteBuffer", &TestType::StoreByteBuffer);
235  AddMemberFunction("changeStringField", &TestType::ChangeStringField);
236 
237  AddMemberFunction("throwException", &TestType::ThrowException);
238 
239  AddMemberFunction("promiseAcceptString", &TestType::PromiseAcceptString);
240  AddMemberFunction("promiseResolveWith", &TestType::PromiseResolveWith);
241  AddMemberFunction("promiseResolveAfter", &TestType::PromiseResolveAfter);
242 
243  AddMemberFunction("getString", &TestType::GetString);
244  AddMemberFunction("getOptionalString", &TestType::GetOptionalString);
245  AddMemberFunction("getIntOrString", &TestType::GetIntOrString);
246  AddMemberFunction("getStruct", &TestType::GetStruct);
247  AddMemberFunction("getNumberEnum", &TestType::GetNumberEnum);
248  AddMemberFunction("getStringEnum", &TestType::GetStringEnum);
249  AddMemberFunction("getArrayOfStrings", &TestType::GetArrayOfStrings);
250  AddMemberFunction("getMapOfStrings", &TestType::GetMapOfStrings);
251  AddMemberFunction("getByteBuffer", &TestType::GetByteBuffer);
252 
253  AddMemberFunction("toPrettyString", &TestType::ToPrettyString);
254 
255  AddReadWriteProperty("optionalObject", &TestType::optional_object);
256  AddReadWriteProperty("intOrObject", &TestType::int_or_object);
257  AddReadWriteProperty("struct", &TestType::struct_);
258  AddReadWriteProperty("array", &TestType::array);
259  AddReadWriteProperty("callback", &TestType::callback);
260  AddReadWriteProperty("any", &TestType::any);
261  AddReadWriteProperty("buffer", &TestType::buffer);
262 }
263 
264 } // namespace js
265 } // namespace shaka
void AcceptIntOrStruct(variant< int, TestTypeOptions >) const
Definition: test_type.h:86
void AcceptStringEnumOrAnyNumber(variant< TestStringEnum, double >) const
Definition: test_type.h:87
static JsError Error(const std::string &message)
Definition: js_error.cc:97
TestTypeOptions struct_
Definition: test_type.h:134
Definition: any.h:31
TestNumberEnum
Definition: test_type.h:46
void AcceptByteBuffer(ByteBuffer) const
Definition: test_type.h:94
bool IsExpectedNumberEnum(TestNumberEnum e) const
Definition: test_type.cc:87
bool IsOptionalPresent(optional< std::string > arg) const
Definition: test_type.cc:66
bool IsExpectedStringWithAny(Any anything) const
Definition: test_type.cc:100
constexpr const TestNumberEnum EXPECTED_NUMBER_ENUM
Definition: test_type.h:58
ReturnVal< JsValue > JsUndefined()
Definition: js_wrappers.cc:284
TestStringEnum
Definition: test_type.h:51
void Trace(memory::HeapTracer *tracer) const override
ExceptionOr< void > ThrowException(const std::string &message) const
Definition: test_type.cc:124
std::vector< std::string > GetArrayOfStrings() const
Definition: test_type.cc:178
TestTypeOptions ChangeStringField(TestTypeOptions opts)
Definition: test_type.cc:119
const ByteBuffer & GetByteBuffer() const
Definition: test_type.cc:189
optional< Any > optional_object
Definition: test_type.h:132
Promise PromiseResolveAfter(uint64_t delay) const
Definition: test_type.cc:138
std::string ToPrettyString(Any anything) const
Definition: test_type.cc:193
void AcceptOptionalString(optional< std::string >) const
Definition: test_type.h:84
bool TryConvertTo(T *result) const
Definition: any.h:72
std::vector< Any > array
Definition: test_type.h:135
bool IsExpectedArrayOfStrings(const std::vector< std::string > &data) const
Definition: test_type.cc:95
std::string GetString() const
Definition: test_type.cc:147
TestTypeOptions GetStruct() const
Definition: test_type.cc:163
const nullopt_t nullopt
Definition: optional.cc:22
bool IsTruthy() const
Definition: any.cc:32
constexpr const TestStringEnum EXPECTED_STRING_ENUM
Definition: test_type.h:59
Promise PromiseAcceptString(const std::string &value) const
Definition: test_type.cc:128
void AcceptOptionalStruct(optional< TestTypeOptions >) const
Definition: test_type.h:85
bool IsExpectedConvertedStruct(TestTypeOptions opts) const
Definition: test_type.cc:79
optional< std::string > GetOptionalString(bool has_value) const
Definition: test_type.cc:151
void AcceptArrayOfStrings(std::vector< std::string >) const
Definition: test_type.h:91
void AcceptCallback(Callback) const
Definition: test_type.h:92
bool IsExpectedString(const std::string &arg) const
Definition: test_type.cc:62
void AcceptAnything(Any) const
Definition: test_type.h:93
void Trace(const Traceable *ptr)
Definition: heap_tracer.cc:43
std::unordered_map< std::string, std::string > GetMapOfStrings() const
Definition: test_type.cc:182
ByteBuffer buffer
Definition: test_type.h:138
constexpr const int EXPECTED_INT
Definition: test_type.h:57
int AddTimer(uint64_t delay_ms, Func &&callback)
Definition: task_runner.h:243
void StoreByteBuffer(ByteBuffer buffer)
Definition: test_type.cc:115
void AcceptStringEnum(TestStringEnum) const
Definition: test_type.h:90
void AcceptString(const std::string &) const
Definition: test_type.h:83
variant< int, std::string > GetIntOrString(bool get_int) const
Definition: test_type.cc:157
void Trace(memory::HeapTracer *tracer) const override
Definition: test_type.cc:51
DEFINE_STRUCT_SPECIAL_METHODS_COPYABLE(TestTypeOptions)
bool IsConvertedStructEmpty(TestTypeOptions opts) const
Definition: test_type.cc:83
void AcceptBoolean(bool) const
Definition: test_type.h:82
bool IsExpectedStringEnum(TestStringEnum e) const
Definition: test_type.cc:91
static Promise PendingPromise()
Definition: promise.h:54
bool IsExpectedStructWithOr(variant< int, TestTypeOptions > arg) const
Definition: test_type.cc:74
variant< int, Any > int_or_object
Definition: test_type.h:133
void AcceptStruct(TestTypeOptions) const
Definition: test_type.h:88
ReturnVal< JsValue > ToJsValue() const override
Definition: any.cc:56
const char * message
static Promise Resolved()
Definition: promise.h:64
TestStringEnum GetStringEnum() const
Definition: test_type.cc:174
Promise PromiseResolveWith(Any value) const
Definition: test_type.cc:133
void AcceptNumber(double) const
Definition: test_type.h:81
static std::string ConvertToPrettyString(Handle< JsValue > value)
Definition: console.cc:235
constexpr const char EXPECTED_STRING[]
Definition: test_type.h:62
bool IsTruthy(Any anything) const
Definition: test_type.cc:105
void AcceptNumberEnum(TestNumberEnum) const
Definition: test_type.h:89
bool has_value() const
Definition: optional.h:143
void InvokeCallbackWithString(Callback callback) const
Definition: test_type.cc:109
bool IsExpectedIntWithOr(variant< int, Any > arg) const
Definition: test_type.cc:70
TestNumberEnum GetNumberEnum() const
Definition: test_type.cc:170
TaskRunner * MainThread()