| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "src/virtualization/tests/lib/gpu_enclosed_guest.h" | |
| #include <fuchsia/component/cpp/fidl.h> | |
| #include <fuchsia/element/cpp/fidl.h> | |
| #include <fuchsia/kernel/cpp/fidl.h> | |
| #include <fuchsia/logger/cpp/fidl.h> | |
| #include <fuchsia/scheduler/cpp/fidl.h> | |
| #include <fuchsia/sysinfo/cpp/fidl.h> | |
| #include <fuchsia/sysmem/cpp/fidl.h> | |
| #include <fuchsia/sysmem2/cpp/fidl.h> | |
| #include <fuchsia/tracing/provider/cpp/fidl.h> | |
| #include <fuchsia/ui/app/cpp/fidl.h> | |
| #include <fuchsia/ui/composition/cpp/fidl.h> | |
| #include <fuchsia/ui/input3/cpp/fidl.h> | |
| #include <fuchsia/ui/observation/geometry/cpp/fidl.h> | |
| #include <fuchsia/virtualization/cpp/fidl.h> | |
| #include <fuchsia/vulkan/loader/cpp/fidl.h> | |
| #include <lib/fit/result.h> | |
| #include <lib/sys/component/cpp/testing/realm_builder.h> | |
| #include <lib/sys/cpp/service_directory.h> | |
| #include <lib/syslog/cpp/macros.h> | |
| #include <lib/zx/time.h> | |
| #include <cmath> | |
| #include <cstdint> | |
| #include <memory> | |
| #include <optional> | |
| #include <utility> | |
| #include "src/ui/testing/ui_test_realm/ui_test_realm.h" | |
| namespace gpu_enclosed_guest::internal { | |
| namespace { | |
| using fuchsia::ui::observation::geometry::ViewDescriptor; | |
| void InstallTestGraphicalPresenter(component_testing::Realm& realm) { | |
| using component_testing::ChildRef; | |
| using component_testing::ParentRef; | |
| using component_testing::Protocol; | |
| using component_testing::Route; | |
| // UITestRealm does not currently provide a fuchsia.element.GraphicalPresenter, but the | |
| // test_graphical_presenter exposes a ViewProvider and a GraphicalPresenter. We will connect this | |
| // to the UITestRealm such that our view under test will become a child of the | |
| // test_graphical_presenter. | |
| constexpr auto kGraphicalPresenterName = "test_graphical_presenter"; | |
| constexpr auto kGraphicalPresenterUrl = "#meta/test_graphical_presenter.cm"; | |
| realm.AddChild(kGraphicalPresenterName, kGraphicalPresenterUrl); | |
| realm | |
| .AddRoute(Route{ | |
| .capabilities = | |
| { | |
| Protocol{.name = fuchsia::logger::LogSink::Name_}, | |
| Protocol{.name = fuchsia::scheduler::RoleManager::Name_}, | |
| Protocol{.name = fuchsia::sysmem::Allocator::Name_}, | |
| Protocol{.name = fuchsia::sysmem2::Allocator::Name_}, | |
| Protocol{.name = fuchsia::tracing::provider::Registry::Name_}, | |
| Protocol{.name = fuchsia::vulkan::loader::Loader::Name_}, | |
| }, | |
| .source = {ParentRef()}, | |
| .targets = {ChildRef{kGraphicalPresenterName}}, | |
| }) | |
| .AddRoute(Route{ | |
| .capabilities = | |
| { | |
| Protocol{.name = fuchsia::ui::composition::Flatland::Name_}, | |
| Protocol{.name = fuchsia::ui::composition::Allocator::Name_}, | |
| Protocol{.name = fuchsia::ui::input3::Keyboard::Name_}, | |
| }, | |
| .source = {ParentRef()}, | |
| .targets = | |
| { | |
| ChildRef{kGraphicalPresenterName}, | |
| ChildRef{EnclosedGuest::kGuestManagerName}, | |
| }, | |
| }) | |
| .AddRoute(Route{ | |
| .capabilities = | |
| { | |
| Protocol{.name = fuchsia::element::GraphicalPresenter::Name_}, | |
| }, | |
| .source = {ChildRef{kGraphicalPresenterName}}, | |
| .targets = {ChildRef{EnclosedGuest::kGuestManagerName}}, | |
| }) | |
| .AddRoute(Route{ | |
| .capabilities = | |
| { | |
| Protocol{.name = fuchsia::ui::app::ViewProvider::Name_}, | |
| }, | |
| .source = {ChildRef{kGraphicalPresenterName}}, | |
| .targets = {ParentRef()}, | |
| }); | |
| } | |
| std::optional<ViewDescriptor> FindDisplayView(ui_testing::UITestManager& ui_test_manager) { | |
| if (!ui_test_manager.ClientViewIsRendering()) { | |
| return {}; | |
| } | |
| auto presenter_koid = ui_test_manager.ClientViewRefKoid(); | |
| if (!presenter_koid) { | |
| return {}; | |
| } | |
| auto presenter = ui_test_manager.FindViewFromSnapshotByKoid(*presenter_koid); | |
| if (!presenter || !presenter->has_children() || presenter->children().empty()) { | |
| return {}; | |
| } | |
| return ui_test_manager.FindViewFromSnapshotByKoid(presenter->children()[0]); | |
| } | |
| } // namespace | |
| std::unique_ptr<sys::ServiceDirectory> GpuUiSession::Start(EnclosedGuest& guest, | |
| GuestLaunchInfo& guest_launch_info) { | |
| using component_testing::Protocol; | |
| using component_testing::Storage; | |
| // UITestManager allows us to run these tests against a hermetic UI stack (ex: to test | |
| // interactions with Flatland, GraphicalPresenter, and Input). | |
| // | |
| // As structured, the virtualization components will be run in a sub-realm created by the | |
| // UITestRealm. Some of the below config fields will allow us to route capabilities through that | |
| // realm. | |
| ui_testing::UITestRealm::Config ui_config; | |
| ui_config.use_scene_owner = true; | |
| ui_config.accessibility_owner = ui_testing::UITestRealm::AccessibilityOwnerType::FAKE; | |
| // These are services that we need to expose from the UITestRealm. | |
| ui_config.exposed_client_services = { | |
| guest_launch_info.interface_name, | |
| fuchsia::virtualization::LinuxManager::Name_, | |
| fuchsia::ui::app::ViewProvider::Name_, | |
| }; | |
| // These are the services we need to consume from the UITestRealm. | |
| ui_config.ui_to_client_services = { | |
| fuchsia::ui::composition::Flatland::Name_, | |
| fuchsia::ui::composition::Allocator::Name_, | |
| fuchsia::ui::input3::Keyboard::Name_, | |
| }; | |
| // These are the parent services (from our cml) that we need the UITestRealm to forward to us so | |
| // that they can be routed to the guest manager. | |
| ui_config.passthrough_capabilities = { | |
| Protocol{.name = fuchsia::kernel::HypervisorResource::Name_}, | |
| Protocol{.name = fuchsia::kernel::VmexResource::Name_}, | |
| Protocol{.name = fuchsia::sysinfo::SysInfo::Name_}, | |
| Storage{.name = "data", .path = "/data"}, | |
| }; | |
| // Now create and install the virtualization components into a new sub-realm. | |
| ui_test_manager_.emplace(std::move(ui_config)); | |
| auto guest_realm = ui_test_manager_->AddSubrealm(); | |
| guest.InstallInRealm(guest_realm, guest_launch_info); | |
| InstallTestGraphicalPresenter(guest_realm); | |
| ui_test_manager_->BuildRealm(); | |
| ui_test_manager_->InitializeScene(); | |
| return ui_test_manager_->CloneExposedServicesDirectory(); | |
| } | |
| void GpuUiSession::Teardown(EnclosedGuest& guest) { | |
| if (!ui_test_manager_.has_value()) { | |
| return; | |
| } | |
| bool ui_test_manager_teardown_complete = false; | |
| ui_test_manager_->TeardownRealm( | |
| [&](fit::result<fuchsia::component::Error>) { ui_test_manager_teardown_complete = true; }); | |
| guest.RunLoopUntil([&] { return ui_test_manager_teardown_complete; }, zx::time::infinite()); | |
| } | |
| DisplayInfo GpuUiSession::WaitForDisplay(EnclosedGuest& guest) { | |
| // Wait for the display view to render. | |
| std::optional<ViewDescriptor> view_descriptor; | |
| guest.RunLoopUntil( | |
| [this, &view_descriptor] { | |
| view_descriptor = FindDisplayView(*ui_test_manager_); | |
| return view_descriptor.has_value(); | |
| }, | |
| zx::time::infinite()); | |
| FX_CHECK(view_descriptor.has_value()) << "Display view not found after wait"; | |
| // Now wait for the view to get focus. | |
| auto koid = view_descriptor->view_ref_koid(); | |
| guest.RunLoopUntil([this, koid] { return ui_test_manager_->ViewIsFocused(koid); }, | |
| zx::time::infinite()); | |
| const auto& extent = view_descriptor->layout().extent; | |
| return DisplayInfo{ | |
| .width = static_cast<uint32_t>(std::round(extent.max.x - extent.min.x)), | |
| .height = static_cast<uint32_t>(std::round(extent.max.y - extent.min.y)), | |
| }; | |
| } | |
| } // namespace gpu_enclosed_guest::internal |