Add unittest that runs Dart code synchronously. (flutter/engine#7495)
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "flutter/runtime/dart_vm.h"
|
||||
#include "flutter/testing/testing.h"
|
||||
#include "flutter/testing/thread_test.h"
|
||||
#include "third_party/tonic/scopes/dart_isolate_scope.h"
|
||||
|
||||
#define CURRENT_TEST_NAME \
|
||||
std::string { \
|
||||
@@ -99,6 +100,19 @@ class AutoIsolateShutdown {
|
||||
|
||||
bool IsValid() const { return isolate_ != nullptr; }
|
||||
|
||||
FML_WARN_UNUSED_RESULT
|
||||
bool RunInIsolateScope(std::function<bool(void)> closure) {
|
||||
if (!isolate_) {
|
||||
return false;
|
||||
}
|
||||
tonic::DartIsolateScope scope(isolate_->isolate());
|
||||
tonic::DartApiScope api_scope;
|
||||
if (closure) {
|
||||
return closure();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
blink::DartIsolate* get() {
|
||||
FML_CHECK(isolate_);
|
||||
return isolate_.get();
|
||||
@@ -210,4 +224,18 @@ TEST_F(DartIsolateTest, IsolateCannotLoadAndRunUnknownDartEntrypoint) {
|
||||
ASSERT_FALSE(isolate);
|
||||
}
|
||||
|
||||
TEST_F(DartIsolateTest, CanRunDartCodeCodeSynchronously) {
|
||||
auto isolate = RunDartCodeInIsolate(GetCurrentTaskRunner(), "main");
|
||||
|
||||
ASSERT_TRUE(isolate);
|
||||
|
||||
ASSERT_TRUE(isolate->RunInIsolateScope([]() -> bool {
|
||||
if (tonic::LogIfError(::Dart_Invoke(Dart_RootLibrary(),
|
||||
tonic::ToDart("sayHi"), 0, nullptr))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
|
||||
@@ -3,5 +3,12 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
void main() {
|
||||
print('Hello');
|
||||
}
|
||||
|
||||
void sayHi() {
|
||||
print("Hi");
|
||||
}
|
||||
|
||||
void throwExceptionNow() {
|
||||
throw("Hello");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user