Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 <ReactCommon/CallInvoker.h>
#include <jsi/jsi.h>
#include <list>
#include <memory>

namespace facebook::react {

class TestCallInvoker : public CallInvoker {
public:
explicit TestCallInvoker(std::shared_ptr<facebook::jsi::Runtime> runtime)
: runtime_(runtime) {}

void invokeAsync(CallFunc&& func) noexcept override {
queue_.push_back(std::move(func));
}

void invokeSync(CallFunc&& func) override {
if (auto runtime = runtime_.lock()) {
func(*runtime);
}
}

void flushQueue() {
if (auto runtime = runtime_.lock()) {
while (!queue_.empty()) {
queue_.front()(*runtime);
queue_.pop_front();
runtime->drainMicrotasks(); // Run microtasks every cycle.
}
}
}

private:
std::list<CallFunc> queue_{};
std::weak_ptr<facebook::jsi::Runtime> runtime_{};
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <ReactCommon/TestCallInvoker.h>
#include <gtest/gtest.h>
#include <hermes/hermes.h>
#include <react/bridging/Bridging.h>
Expand All @@ -15,34 +16,24 @@

namespace facebook::react {

class TestCallInvoker : public CallInvoker {
class BridgingTest : public ::testing::Test {
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;
BridgingTest(BridgingTest& other) = delete;
BridgingTest& operator=(BridgingTest& other) = delete;
BridgingTest(BridgingTest&& other) = delete;
BridgingTest& operator=(BridgingTest&& other) = delete;

std::list<CallFunc> queue_;
};

class BridgingTest : public ::testing::Test {
protected:
BridgingTest()
: invoker(std::make_shared<TestCallInvoker>()),
runtime(hermes::makeHermesRuntime(
: runtime(hermes::makeHermesRuntime(
::hermes::vm::RuntimeConfig::Builder()
// Make promises work with Hermes microtasks.
.withMicrotaskQueue(true)
.build())),
rt(*runtime) {}
rt(*runtime),
invoker(std::make_shared<TestCallInvoker>(runtime)) {}

~BridgingTest() {
~BridgingTest() override {
LongLivedObjectCollection::get(rt).clear();
}

Expand All @@ -62,16 +53,12 @@ class BridgingTest : public ::testing::Test {
}

void flushQueue() {
while (!invoker->queue_.empty()) {
invoker->queue_.front()(*runtime);
invoker->queue_.pop_front();
rt.drainMicrotasks(); // Run microtasks every cycle.
}
invoker->flushQueue();
}

std::shared_ptr<TestCallInvoker> invoker;
std::unique_ptr<jsi::Runtime> runtime;
std::shared_ptr<jsi::Runtime> runtime;
jsi::Runtime& rt;
std::shared_ptr<TestCallInvoker> invoker;
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <folly/portability/Windows.h>
#endif

#include <ReactCommon/CallInvoker.h>
#include <ReactCommon/TestCallInvoker.h>
#include <folly/json.h>
#include <gtest/gtest.h>
#include <hermes/hermes.h>
Expand All @@ -21,25 +21,11 @@

namespace facebook::react {

class TestCallInvoker : public CallInvoker {
public:
void invokeAsync(CallFunc&& fn) noexcept override {
queue_.push_back(std::move(fn));
}

void invokeSync(CallFunc&& /*func*/) override {
FAIL() << "JSCallInvoker does not support invokeSync()";
}

private:
std::list<CallFunc> queue_;
};

class NetworkingModuleTests : public testing::Test {
protected:
void SetUp() override {
rt_ = facebook::hermes::makeHermesRuntime();
jsInvoker_ = std::make_shared<TestCallInvoker>();
jsInvoker_ = std::make_shared<TestCallInvoker>(rt_);
}

static void verifyFormData(
Expand All @@ -55,7 +41,7 @@ class NetworkingModuleTests : public testing::Test {
}
}

std::unique_ptr<facebook::hermes::HermesRuntime> rt_;
std::shared_ptr<facebook::hermes::HermesRuntime> rt_;
std::shared_ptr<CallInvoker> jsInvoker_;
};

Expand Down
Loading