[dart] Migrate tests to the new dart_test

dart_test, defined and reviewed in an earlier patch, was committed in
https://fuchsia-review.googlesource.com/c/fuchsia/+/262750

Change-Id: Idc82dcbc547fd073f5d7d76cc6a6b7cee6b4e9f7
diff --git a/app/dashboard/BUILD.gn b/app/dashboard/BUILD.gn
index de69bbe..633aed4 100644
--- a/app/dashboard/BUILD.gn
+++ b/app/dashboard/BUILD.gn
@@ -2,7 +2,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import("//topaz/runtime/dart/flutter_test.gni")
+import("//build/dart/test.gni")
 import("//topaz/runtime/flutter_runner/flutter_app.gni")
 
 flutter_app("dashboard") {
@@ -46,7 +46,7 @@
   ]
 }
 
-flutter_test("dashboard_tests") {
+dart_test("dashboard_tests") {
   sources = [
     "../test/build_bucket_service_test.dart",
     "../test/enums_test.dart",
diff --git a/bin/dart_fidl_json/test/BUILD.gn b/bin/dart_fidl_json/test/BUILD.gn
index 3f2d743..fa7be7c 100644
--- a/bin/dart_fidl_json/test/BUILD.gn
+++ b/bin/dart_fidl_json/test/BUILD.gn
@@ -3,7 +3,7 @@
 # found in the LICENSE file.
 
 import("//topaz/bin/dart_fidl_json/dart_fidl_json.gni")
-import("//topaz/runtime/dart/flutter_test.gni")
+import("//build/dart/test.gni")
 
 json_fidl("dart_fidl_json_test_fidl_json") {
   fidl_target = "//topaz//bin/dart_fidl_json/test/fidl/dart.fidl.json.test"
@@ -13,7 +13,7 @@
   ]
 }
 
-flutter_test("dart_fidl_json_test") {
+dart_test("dart_fidl_json_test") {
   sources = [
     "json_test.dart",
   ]
diff --git a/bin/session_shell/capybara_session_shell/BUILD.gn b/bin/session_shell/capybara_session_shell/BUILD.gn
index 6dc5c41..68c94d5 100644
--- a/bin/session_shell/capybara_session_shell/BUILD.gn
+++ b/bin/session_shell/capybara_session_shell/BUILD.gn
@@ -2,7 +2,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import("//topaz/runtime/dart/flutter_test.gni")
+import("//build/dart/test.gni")
 import("//topaz/runtime/flutter_runner/flutter_app.gni")
 
 flutter_app("capybara_session_shell") {
@@ -40,7 +40,7 @@
   ]
 }
 
-# flutter_test("capybara_session_shell_tests") {
+# dart_test("capybara_session_shell_tests") {
 #   sources = [
 #     "dummy_test.dart",
 #   ]
diff --git a/bin/userpicker_base_shell/BUILD.gn b/bin/userpicker_base_shell/BUILD.gn
index 8cfc464..61f0e1d 100644
--- a/bin/userpicker_base_shell/BUILD.gn
+++ b/bin/userpicker_base_shell/BUILD.gn
@@ -3,7 +3,6 @@
 # found in the LICENSE file.
 
 import("//third_party/cobalt_config/metrics_registry.gni")
-import("//topaz/runtime/dart/flutter_test.gni")
 import("//topaz/runtime/flutter_runner/flutter_app.gni")
 
 metrics_registry("sysui_metrics_config") {
diff --git a/bin/xi/xi_mod/BUILD.gn b/bin/xi/xi_mod/BUILD.gn
index 101f026..3d16192 100644
--- a/bin/xi/xi_mod/BUILD.gn
+++ b/bin/xi/xi_mod/BUILD.gn
@@ -2,7 +2,6 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import("//topaz/runtime/dart/flutter_test.gni")
 import("//topaz/runtime/flutter_runner/flutter_app.gni")
 
 flutter_app("xi_mod") {
diff --git a/examples/tictactoe/lib/tictactoe/common/BUILD.gn b/examples/tictactoe/lib/tictactoe/common/BUILD.gn
index 6dd8e04..59d556d 100644
--- a/examples/tictactoe/lib/tictactoe/common/BUILD.gn
+++ b/examples/tictactoe/lib/tictactoe/common/BUILD.gn
@@ -3,7 +3,7 @@
 # found in the LICENSE file.
 
 import("//build/dart/dart_library.gni")
-import("//topaz/runtime/dart/flutter_test.gni")
+import("//build/dart/test.gni")
 
 dart_library("common") {
   package_name = "tictactoe_common"
@@ -23,7 +23,7 @@
   ]
 }
 
-flutter_test("tictactoe_game_test") {
+dart_test("tictactoe_game_test") {
   sources = [
     "game_test.dart",
     "win_detection_test.dart",
diff --git a/lib/xi/BUILD.gn b/lib/xi/BUILD.gn
index 7e18201..462b3e8 100644
--- a/lib/xi/BUILD.gn
+++ b/lib/xi/BUILD.gn
@@ -5,6 +5,6 @@
 group("dart_unittests") {
   testonly = true
   deps = [
-    "//topaz/lib/xi/fuchsia_client:xi_fuchsia_client_tests",
+    "//topaz/lib/xi/fuchsia_client:xi_fuchsia_client_tests($host_toolchain)",
   ]
 }
diff --git a/lib/xi/fuchsia_client/BUILD.gn b/lib/xi/fuchsia_client/BUILD.gn
index 16b0693..d9ff21a 100644
--- a/lib/xi/fuchsia_client/BUILD.gn
+++ b/lib/xi/fuchsia_client/BUILD.gn
@@ -3,7 +3,7 @@
 # found in the LICENSE file.
 
 import("//build/dart/dart_library.gni")
-import("//topaz/runtime/dart/flutter_test.gni")
+import("//build/dart/test.gni")
 
 dart_library("xi_fuchsia_client") {
   package_name = "xi_fuchsia_client"
@@ -22,7 +22,7 @@
   ]
 }
 
-flutter_test("xi_fuchsia_client_tests") {
+dart_test("xi_fuchsia_client_tests") {
   sources = [
     "xi_fuchsia_client_test.dart",
   ]
diff --git a/packages/tests/dart_unittests b/packages/tests/dart_unittests
index 4a99013..295fdfa 100644
--- a/packages/tests/dart_unittests
+++ b/packages/tests/dart_unittests
@@ -25,8 +25,5 @@
       "//topaz/public/lib/schemas/dart:dart_schema_tests",
       "//topaz/public/lib/testing/app_driver/dart:fake_module_driver_test",
       "//topaz/shell/mondrian_story_shell:mondrian_story_shell_tests"
-    ],
-    "labels": [
-      "//topaz:dart_unittests"
     ]
 }
diff --git a/public/dart/fuchsia_inspect/BUILD.gn b/public/dart/fuchsia_inspect/BUILD.gn
index d9ba3d0..e40bb19 100644
--- a/public/dart/fuchsia_inspect/BUILD.gn
+++ b/public/dart/fuchsia_inspect/BUILD.gn
@@ -3,7 +3,7 @@
 # found in the LICENSE file.
 
 import("//build/dart/dart_library.gni")
-import("//topaz/runtime/dart/dart_test.gni")
+import("//build/dart/test.gni")
 
 dart_library("fuchsia_inspect") {
   package_name = "fuchsia_inspect"
@@ -43,4 +43,3 @@
     "//third_party/dart-pkg/pub/test",
   ]
 }
-
diff --git a/public/dart/fuchsia_logger/BUILD.gn b/public/dart/fuchsia_logger/BUILD.gn
index c5c14f3..673832a 100644
--- a/public/dart/fuchsia_logger/BUILD.gn
+++ b/public/dart/fuchsia_logger/BUILD.gn
@@ -3,7 +3,7 @@
 # found in the LICENSE file.
 
 import("//build/dart/dart_library.gni")
-import("//topaz/runtime/dart/flutter_test.gni")
+import("//build/dart/test.gni")
 
 dart_library("fuchsia_logger") {
   package_name = "fuchsia_logger"
@@ -32,7 +32,7 @@
 
 # Runs these tests using:
 #   fx run-host-tests fuchsia_logger_package_unittests
-flutter_test("fuchsia_logger_package_unittests") {
+dart_test("fuchsia_logger_package_unittests") {
   sources = [
     "internal/log_message_test.dart",
     "internal/log_writer_test.dart",
diff --git a/public/dart/fuchsia_modular/BUILD.gn b/public/dart/fuchsia_modular/BUILD.gn
index f705b94..a2e17f4 100644
--- a/public/dart/fuchsia_modular/BUILD.gn
+++ b/public/dart/fuchsia_modular/BUILD.gn
@@ -65,8 +65,9 @@
     "//topaz/public/dart/fuchsia_logger",
     "//topaz/public/dart/fuchsia_services",
     "//topaz/public/dart/zircon",
-    "//zircon/public/fidl/fuchsia-mem",
   ]
+
+  non_dart_deps = [ "//zircon/public/fidl/fuchsia-mem" ]
 }
 
 # Runs these tests using:
@@ -78,11 +79,11 @@
     "lifecycle/lifecycle_test.dart",
     "matchers.dart",
     "module/intent_test.dart",
+    "module/internal/_streaming_intent_handler_impl_test.dart",
     "module/internal/fidl_transformers_test.dart",
     "module/internal/intent_handler_impl_test.dart",
     "module/internal/module_impl_test.dart",
     "module/internal/ongoing_activity_impl_test.dart",
-    "module/internal/_streaming_intent_handler_impl_test.dart",
     "module/module_test.dart",
     "module/noop_intent_handler_test.dart",
     "proposal/internal/proposal_listener_impl_test.dart",
diff --git a/public/dart/fuchsia_services/BUILD.gn b/public/dart/fuchsia_services/BUILD.gn
index 12c3c02..be2c636 100644
--- a/public/dart/fuchsia_services/BUILD.gn
+++ b/public/dart/fuchsia_services/BUILD.gn
@@ -29,8 +29,9 @@
     "//topaz/public/dart/fuchsia",
     "//topaz/public/dart/fuchsia_vfs",
     "//topaz/public/dart/zircon",
-    "//zircon/public/fidl/fuchsia-io",
   ]
+
+  non_dart_deps = [ "//zircon/public/fidl/fuchsia-io" ]
 }
 
 flutter_test("fuchsia_services_package_unittests") {
@@ -43,7 +44,6 @@
   deps = [
     ":fuchsia_services",
     "//third_party/dart-pkg/pub/test",
-    "//zircon/public/fidl/fuchsia-io",
   ]
 }
 
diff --git a/public/lib/schemas/dart/BUILD.gn b/public/lib/schemas/dart/BUILD.gn
index 446cb79..3007bf6 100644
--- a/public/lib/schemas/dart/BUILD.gn
+++ b/public/lib/schemas/dart/BUILD.gn
@@ -3,7 +3,7 @@
 # found in the LICENSE file.
 
 import("//build/dart/dart_library.gni")
-import("//topaz/runtime/dart/flutter_test.gni")
+import("//build/dart/test.gni")
 
 dart_library("dart") {
   package_name = "lib.schemas.dart"
@@ -55,7 +55,7 @@
   ]
 }
 
-flutter_test("dart_schema_tests") {
+dart_test("dart_schema_tests") {
   sources = [
     "../test/entity_codec_test.dart",
   ]
diff --git a/runtime/dart/dart_test.gni b/runtime/dart/dart_test.gni
deleted file mode 100644
index ae50746..0000000
--- a/runtime/dart/dart_test.gni
+++ /dev/null
@@ -1,13 +0,0 @@
-# Copyright 2016 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.
-
-import("//topaz/runtime/dart/flutter_test.gni")
-
-# TODO(joshuaseaton): Replace this with a template that defines a dart test
-# without flutter dependencies.
-template("dart_test") {
-  flutter_test(target_name) {
-    forward_variables_from(invoker, "*")
-  }
-}
diff --git a/runtime/dart/gen_dart_test_invocation.py b/runtime/dart/gen_dart_test_invocation.py
new file mode 100755
index 0000000..f84b101
--- /dev/null
+++ b/runtime/dart/gen_dart_test_invocation.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+# Copyright 2019 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.
+
+import argparse
+import os
+import stat
+import string
+import sys
+
+def main():
+  parser = argparse.ArgumentParser(
+      description='Generate a script that invokes a Dart application')
+  parser.add_argument('--wd',
+                      help='Path to the working directory, relative to that of the script',
+                      required=True)
+  parser.add_argument('--out',
+                      help='Path to the invocation file to generate',
+                      required=True)
+  parser.add_argument('--dart',
+                      help='Path to the Dart binary, relative to the working directory',
+                      required=True)
+  parser.add_argument('--snapshot',
+                      help='path to the binary snapshot, relative to the working directory',
+                      required=True)
+  args = parser.parse_args()
+
+  app_file = args.out
+  app_path = os.path.dirname(app_file)
+  if not os.path.exists(app_path):
+    os.makedirs(app_path)
+
+  script_template = string.Template('''#!/bin/sh
+# DO NOT EDIT
+# This script is generated by:
+#   //topaz/runtime/dart/gen_dart_test_invocation.py
+
+cd "$$(dirname $$0)/$wd"
+
+$dart \\
+  $snapshot \\
+  "$$@"
+''')
+  with open(app_file, 'w') as file:
+    file.write(script_template.substitute(args.__dict__))
+  permissions = (stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
+                 stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP |
+                 stat.S_IROTH)
+  os.chmod(app_file, permissions)
+
+
+if __name__ == '__main__':
+  sys.exit(main())
diff --git a/runtime/dart/gen_flutter_test_invocation.py b/runtime/dart/gen_flutter_test_invocation.py
index 259d24e..e7fbb85 100755
--- a/runtime/dart/gen_flutter_test_invocation.py
+++ b/runtime/dart/gen_flutter_test_invocation.py
@@ -45,6 +45,10 @@
     os.makedirs(test_path)
 
   script_template = string.Template('''#!/bin/bash
+# DO NOT EDIT
+# This script is generated by:
+#   //topaz/runtime/dart/gen_flutter_test_invocation.py
+# See: //topaz/runtime/dart/flutter_test.gni
 
 $test_runner \\
   --packages=$dot_packages \\
diff --git a/runtime/dart/group_tests.py b/runtime/dart/group_tests.py
new file mode 100755
index 0000000..2dbef52
--- /dev/null
+++ b/runtime/dart/group_tests.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+# Copyright 2019 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.
+
+import argparse
+import os
+import stat
+import string
+import sys
+
+def main():
+  parser = argparse.ArgumentParser(
+      description='Generates a grouped dart test file from individual tests')
+  parser.add_argument('--out',
+                      help='Path to the invocation file to generate',
+                      required=True)
+  parser.add_argument('--source',
+                      help='Path to a dart source file. Will be ignored if the file does not end in _test.dart',
+                      action='append',
+                      required=True)
+  args = parser.parse_args()
+
+  grouped_test = args.out
+  grouped_test_dir = os.path.dirname(grouped_test)
+  if not os.path.exists(grouped_test_dir):
+    os.makedirs(grouped_test_dir)
+
+  tests = filter(lambda src: src.endswith('_test.dart'), args.source)
+  imports = ["import 'package:test/test.dart';"]
+  invocations = []
+  for test in tests:
+    filename = os.path.splitext(os.path.basename(test))[0]
+    imports.append("import '%s' as %s;" % (test, filename))
+    invocations.append("group('%s', %s.main);" % (filename, filename))
+
+  contents = '''// DO NOT EDIT
+// This script is generated by:
+//   //topaz/runtime/dart/group_tests.py
+
+%s
+void main() {
+  %s
+}''' % ('\n'.join(imports), '\n  '.join(invocations))
+
+  with open(grouped_test, 'w') as file:
+    file.write(contents)
+  permissions = (stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
+                 stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP |
+                 stat.S_IROTH)
+  os.chmod(grouped_test, permissions)
+
+
+if __name__ == '__main__':
+  sys.exit(main())
diff --git a/shell/keyboard/BUILD.gn b/shell/keyboard/BUILD.gn
index 1d02b15..3d3118b 100644
--- a/shell/keyboard/BUILD.gn
+++ b/shell/keyboard/BUILD.gn
@@ -3,7 +3,7 @@
 # found in the LICENSE file.
 
 import("//build/dart/dart_library.gni")
-import("//topaz/runtime/dart/flutter_test.gni")
+import("//build/dart/test.gni")
 
 dart_library("keyboard") {
   package_name = "keyboard"
@@ -20,7 +20,7 @@
   ]
 }
 
-flutter_test("keyboard_tests") {
+dart_test("keyboard_tests") {
   sources = [
     "keyboard_test.dart",
     "keys_test.dart",