[flutter][test] Port to using libsys.

Bug: DX-1069 #comment
Bug: DX-1076 #comment

TEST=fx run-test flutter_runner_tests

Change-Id: I70713c062344aa01cd15089444a219f2fcc7cc27
diff --git a/runtime/flutter_runner/BUILD.gn b/runtime/flutter_runner/BUILD.gn
index a93575b..ab5a267 100644
--- a/runtime/flutter_runner/BUILD.gn
+++ b/runtime/flutter_runner/BUILD.gn
@@ -278,8 +278,8 @@
     deps = [
       ":jit${product_suffix}",
       "//topaz/runtime/flutter_runner/kernel:framework_shim_kernel",
-      snapshot_label,
       snapshot_framework_label,
+      snapshot_label,
     ]
 
     if (!product) {
@@ -386,13 +386,15 @@
   ]
 
   deps = [
-    "//garnet/public/lib/component/cpp",
-    "//garnet/public/lib/component/cpp/testing",
     "//garnet/public/lib/fsl",
+    "//garnet/public/lib/gtest",
     "//sdk/fidl/fuchsia.fonts",
+    "//sdk/fidl/fuchsia.sys",
+    "//sdk/lib/sys/cpp",
     "//third_party/googletest:gtest_main",
     "//third_party/icu",
     "//third_party/skia",
+    "//zircon/public/lib/trace",
   ]
 }
 
diff --git a/runtime/flutter_runner/fuchsia_font_manager_unittest.cc b/runtime/flutter_runner/fuchsia_font_manager_unittest.cc
index ac39c5d..286488a 100644
--- a/runtime/flutter_runner/fuchsia_font_manager_unittest.cc
+++ b/runtime/flutter_runner/fuchsia_font_manager_unittest.cc
@@ -6,9 +6,10 @@
 
 #include <fcntl.h>
 #include <fuchsia/fonts/cpp/fidl.h>
-#include <lib/async-loop/cpp/loop.h>
-#include <lib/component/cpp/startup_context.h>
-#include <lib/component/cpp/testing/test_with_environment.h>
+#include <fuchsia/sys/cpp/fidl.h>
+#include <lib/gtest/real_loop_fixture.h>
+#include <lib/sys/cpp/service_directory.h>
+#include <memory>
 
 #include "gtest/gtest.h"
 #include "lib/fsl/io/fd.h"
@@ -29,67 +30,48 @@
 constexpr char kFontsServiceUrl[] =
     "fuchsia-pkg://fuchsia.com/fonts#meta/fonts.cmx";
 
-class FuchsiaFontManagerTest : public component::testing::TestWithEnvironment {
+class FuchsiaFontManagerTest : public gtest::RealLoopFixture {
  public:
-  FuchsiaFontManagerTest() : loop_(&kAsyncLoopConfigNoAttachToThread) {
-    // Grab the current Environment. We'll need it to create a new set of
-    // services on a background thread.
-    auto context = component::StartupContext::CreateFromStartupInfo();
-    fuchsia::sys::EnvironmentPtr parent_env;
-    context->ConnectToEnvironmentService(parent_env.NewRequest());
+  FuchsiaFontManagerTest() {
+    auto services = sys::ServiceDirectory::CreateFromNamespace();
+    // Grab launcher and launch font provider.
+    fuchsia::sys::LauncherPtr launcher;
+    services->Connect(launcher.NewRequest());
 
-    // Create a new set of services running on a newly started (background)
-    // thread.
-    loop_.StartThread();
-    auto services = component::testing::EnvironmentServices::Create(
-        parent_env, loop_.dispatcher());
+    zx::channel out_services_request;
+    auto font_services =
+        sys::ServiceDirectory::CreateWithRequest(&out_services_request);
+    auto launch_info_font_service = GetLaunchInfoForFontService();
+    launch_info_font_service.directory_request =
+        std::move(out_services_request);
 
-    // Add the fonts provider service to services. We need to use the version of
-    // AddServiceWithLaunchInfo that takes a callback to produce LaunchInfo
-    // because the version that takes LaunchInfo directly only uses 'url' and
-    // 'aguments', but we need 'flat_namespace' as well in order to have the
-    // font provider use the test fonts in our package.
-    zx_status_t status = services->AddServiceWithLaunchInfo(
-        kFontsServiceUrl,
-        []() {
-          fuchsia::sys::LaunchInfo launch_info;
-          launch_info.url = kFontsServiceUrl;
-          launch_info.arguments.reset(
-              {"--no-default-fonts",
-               "--font-manifest=/test_fonts/manifest.json"});
-          fxl::UniqueFD tmp_dir_fd(
-              open("/pkg/data/testdata/test_fonts", O_DIRECTORY | O_RDONLY));
-          launch_info.flat_namespace = fuchsia::sys::FlatNamespace::New();
-          launch_info.flat_namespace->paths.push_back("/test_fonts");
-          launch_info.flat_namespace->directories.push_back(
-              fsl::CloneChannelFromFileDescriptor(tmp_dir_fd.get()));
-          return launch_info;
-        },
-        fuchsia::fonts::Provider::Name_);
-    EXPECT_EQ(ZX_OK, status);
+    launcher->CreateComponent(std::move(launch_info_font_service),
+                              font_service_controller_.NewRequest());
 
-    // Create an enclosing environment wrapping the new set of services.
-    environment_ = CreateNewEnclosingEnvironment("font_manager_tests",
-                                                 std::move(services));
-    EXPECT_TRUE(WaitForEnclosingEnvToStart(environment_.get()));
-
-    // Connect to the font provider service through the enclosing environment
-    // (so that it runs on the background thread), and then wrap it inside the
-    // font manager we will be testing.
+    // Connect to the font provider service and then wrap it inside the font
+    // manager we will be testing.
     fuchsia::fonts::ProviderSyncPtr provider_ptr;
-    environment_->ConnectToService(provider_ptr.NewRequest());
+    font_services.Connect(provider_ptr.NewRequest());
+
     font_manager_ = sk_make_sp<FuchsiaFontManager>(std::move(provider_ptr));
   }
 
-  void TearDown() override {
-    // Make sure the background thread terminates before tearing down the
-    // enclosing environment (otherwise we get a crash).
-    loop_.Quit();
+  fuchsia::sys::LaunchInfo GetLaunchInfoForFontService() {
+    fuchsia::sys::LaunchInfo launch_info;
+    launch_info.url = kFontsServiceUrl;
+    launch_info.arguments.reset(
+        {"--no-default-fonts", "--font-manifest=/test_fonts/manifest.json"});
+    fxl::UniqueFD tmp_dir_fd(
+        open("/pkg/data/testdata/test_fonts", O_DIRECTORY | O_RDONLY));
+    launch_info.flat_namespace = fuchsia::sys::FlatNamespace::New();
+    launch_info.flat_namespace->paths.push_back("/test_fonts");
+    launch_info.flat_namespace->directories.push_back(
+        fsl::CloneChannelFromFileDescriptor(tmp_dir_fd.get()));
+    return launch_info;
   }
 
  protected:
-  async::Loop loop_;
-  std::unique_ptr<component::testing::EnclosingEnvironment> environment_;
+  fuchsia::sys::ComponentControllerPtr font_service_controller_;
   sk_sp<SkFontMgr> font_manager_;
 };
 
diff --git a/runtime/flutter_runner/meta/flutter_runner_tests.cmx b/runtime/flutter_runner/meta/flutter_runner_tests.cmx
index 0c2050d..c66080b 100644
--- a/runtime/flutter_runner/meta/flutter_runner_tests.cmx
+++ b/runtime/flutter_runner/meta/flutter_runner_tests.cmx
@@ -4,9 +4,7 @@
     },
     "sandbox": {
         "services": [
-            "fuchsia.fonts.Provider",
-            "fuchsia.sys.Environment",
-            "fuchsia.sys.Loader"
+            "fuchsia.sys.Launcher"
         ]
     }
 }