forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridgingTest.h
More file actions
77 lines (60 loc) · 1.92 KB
/
BridgingTest.h
File metadata and controls
77 lines (60 loc) · 1.92 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <gtest/gtest.h>
#include <hermes/hermes.h>
#include <react/bridging/Bridging.h>
#define EXPECT_JSI_THROW(expr) EXPECT_THROW((expr), facebook::jsi::JSIException)
namespace facebook::react {
class TestCallInvoker : public CallInvoker {
public:
void invokeAsync(CallFunc&& fn) noexcept override {
queue_.push_back(std::move(fn));
}
void invokeSync(CallFunc&&) override {
FAIL() << "JSCallInvoker does not support invokeSync()";
}
private:
friend class BridgingTest;
std::list<CallFunc> queue_;
};
class BridgingTest : public ::testing::Test {
protected:
BridgingTest()
: invoker(std::make_shared<TestCallInvoker>()),
runtime(hermes::makeHermesRuntime(
::hermes::vm::RuntimeConfig::Builder()
// Make promises work with Hermes microtasks.
.withMicrotaskQueue(true)
.build())),
rt(*runtime) {}
~BridgingTest() {
LongLivedObjectCollection::get(rt).clear();
}
void TearDown() override {
flushQueue();
// After flushing the invoker queue, we shouldn't leak memory.
EXPECT_EQ(0, LongLivedObjectCollection::get(rt).size());
}
jsi::Value eval(const std::string& js) {
return rt.global().getPropertyAsFunction(rt, "eval").call(rt, js);
}
jsi::Function function(const std::string& js) {
return eval(("(" + js + ")").c_str()).getObject(rt).getFunction(rt);
}
void flushQueue() {
while (!invoker->queue_.empty()) {
invoker->queue_.front()(*runtime);
invoker->queue_.pop_front();
rt.drainMicrotasks(); // Run microtasks every cycle.
}
}
std::shared_ptr<TestCallInvoker> invoker;
std::unique_ptr<jsi::Runtime> runtime;
jsi::Runtime& rt;
};
} // namespace facebook::react