|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include <memory> |
| 6 | + |
| 7 | +#include "flutter/common/settings.h" |
| 8 | +#include "flutter/common/task_runners.h" |
| 9 | +#include "flutter/lib/ui/ui_dart_state.h" |
| 10 | +#include "flutter/runtime/dart_isolate.h" |
| 11 | +#include "flutter/runtime/dart_vm_lifecycle.h" |
| 12 | +#include "flutter/runtime/isolate_configuration.h" |
| 13 | +#include "flutter/testing/dart_fixture.h" |
| 14 | +#include "flutter/testing/dart_isolate_runner.h" |
| 15 | +#include "flutter/testing/fixture_test.h" |
| 16 | +#include "flutter/testing/testing.h" |
| 17 | +#include "impeller/fixtures/box_fade.frag.h" |
| 18 | +#include "impeller/fixtures/box_fade.vert.h" |
| 19 | +#include "impeller/playground/playground_test.h" |
| 20 | +#include "impeller/renderer/pipeline_library.h" |
| 21 | +#include "impeller/renderer/render_pass.h" |
| 22 | +#include "impeller/renderer/sampler_library.h" |
| 23 | + |
| 24 | +#include "third_party/imgui/imgui.h" |
| 25 | + |
| 26 | +namespace impeller { |
| 27 | +namespace testing { |
| 28 | + |
| 29 | +class RendererDartTest : public PlaygroundTest, |
| 30 | + public flutter::testing::DartFixture { |
| 31 | + public: |
| 32 | + RendererDartTest() |
| 33 | + : settings_(CreateSettingsForFixture()), |
| 34 | + vm_ref_(flutter::DartVMRef::Create(settings_)) { |
| 35 | + fml::MessageLoop::EnsureInitializedForCurrentThread(); |
| 36 | + current_task_runner_ = fml::MessageLoop::GetCurrent().GetTaskRunner(); |
| 37 | + isolate_ = CreateDartIsolate(); |
| 38 | + assert(isolate_); |
| 39 | + assert(isolate_->get()->GetPhase() == flutter::DartIsolate::Phase::Running); |
| 40 | + } |
| 41 | + |
| 42 | + flutter::testing::AutoIsolateShutdown* GetIsolate() { return isolate_.get(); } |
| 43 | + |
| 44 | + private: |
| 45 | + std::unique_ptr<flutter::testing::AutoIsolateShutdown> CreateDartIsolate() { |
| 46 | + const auto settings = CreateSettingsForFixture(); |
| 47 | + flutter::TaskRunners task_runners(flutter::testing::GetCurrentTestName(), |
| 48 | + current_task_runner_, // |
| 49 | + current_task_runner_, // |
| 50 | + current_task_runner_, // |
| 51 | + current_task_runner_ // |
| 52 | + ); |
| 53 | + return flutter::testing::RunDartCodeInIsolate( |
| 54 | + vm_ref_, settings, task_runners, "main", {}, |
| 55 | + flutter::testing::GetDefaultKernelFilePath()); |
| 56 | + } |
| 57 | + |
| 58 | + const flutter::Settings settings_; |
| 59 | + flutter::DartVMRef vm_ref_; |
| 60 | + fml::RefPtr<fml::TaskRunner> current_task_runner_; |
| 61 | + std::unique_ptr<flutter::testing::AutoIsolateShutdown> isolate_; |
| 62 | +}; |
| 63 | + |
| 64 | +INSTANTIATE_PLAYGROUND_SUITE(RendererDartTest); |
| 65 | + |
| 66 | +TEST_P(RendererDartTest, CanRunDartInPlaygroundFrame) { |
| 67 | + auto isolate = GetIsolate(); |
| 68 | + |
| 69 | + SinglePassCallback callback = [&](RenderPass& pass) { |
| 70 | + ImGui::Begin("Dart test", nullptr); |
| 71 | + ImGui::Text( |
| 72 | + "This test executes Dart code during the playground frame callback."); |
| 73 | + ImGui::End(); |
| 74 | + |
| 75 | + return isolate->RunInIsolateScope([]() -> bool { |
| 76 | + if (tonic::CheckAndHandleError(::Dart_Invoke( |
| 77 | + Dart_RootLibrary(), tonic::ToDart("sayHi"), 0, nullptr))) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + return true; |
| 81 | + }); |
| 82 | + }; |
| 83 | + OpenPlaygroundHere(callback); |
| 84 | +} |
| 85 | + |
| 86 | +} // namespace testing |
| 87 | +} // namespace impeller |
0 commit comments