[embedder] Support custom Dart SDK in run script.

This adds support for `--no-prebuilt-dart-sdk` to building
with a custom Flutter Engine in build_and_run_example.sh,
which lets you test changes to the Dart SDK.

This was already supported by build_and_copy_engine_artifacts.sh
but I forgot to add it to build_and_run_example.sh as well.

Change-Id: Id0511836c0f803715cf124ea4694d4e8bfc55036
Reviewed-on: https://fuchsia-review.googlesource.com/c/flutter-embedder/+/756863
Reviewed-by: Naud Ghebre <naudzghebre@google.com>
diff --git a/docs/making_engine_changes.md b/docs/making_engine_changes.md
index c52b4ae..4f1aab0 100644
--- a/docs/making_engine_changes.md
+++ b/docs/making_engine_changes.md
@@ -56,6 +56,10 @@
    $FUCHSIA_EMBEDDER_DIR/scripts/sync_engine_to_revision.sh $(cat $FUCHSIA_EMBEDDER_DIR/third_party/dart-pkg/internal/flutter/flutter/bin/internal/engine.version)
    ```
 
+   - By default this uses a prebuilt version of the Dart SDK instead of the Dart SDK source code
+     at `$FUCHSIA_EMBEDDER_DIR/third_party/engine/src/third_party/dart`. To build the Dart SDK from
+     source to test Dart SDK changes, pass `--no-prebuilt-dart-sdk`.
+
    - If you're a Googler, you can pass `--goma` to accelerate the build.
 
 You can then build Flutter Engine artifacts for this repository with your local changes:
diff --git a/scripts/build_and_run_example.sh b/scripts/build_and_run_example.sh
index 5858d2a..b01de6b 100755
--- a/scripts/build_and_run_example.sh
+++ b/scripts/build_and_run_example.sh
@@ -20,10 +20,13 @@
 #           TODO(akbiggs): Show FIDL calls from startup.
 #   --with-engine: Also builds the Flutter Engine alongside the example.
 #                  See https://fuchsia.googlesource.com/flutter-embedder/+/refs/heads/main/docs/making_engine_changes.md for setup.
-#   --with-fuchsia: Also builds a local Fuchsia SDK alongside the example.
-#                   See https://fuchsia.googlesource.com/flutter-embedder/+/refs/heads/main/docs/testing_fuchsia_sdk_changes.md for setup.
 #   --goma: Uses GOMA when building the Flutter Engine to accelerate the build.
 #           For Googlers only. Sorry. :(
+#   --no-prebuilt-dart-sdk: Builds the Dart SDK for the Flutter Engine from source
+#                           in third_party/engine/src/third_party/dart instead of
+#                           using a prebuilt Dart SDK.
+#   --with-fuchsia: Also builds a local Fuchsia SDK alongside the example.
+#                   See https://fuchsia.googlesource.com/flutter-embedder/+/refs/heads/main/docs/testing_fuchsia_sdk_changes.md for setup.
 #
 # TODO(akbiggs): Port this workflow to Bazel.
 
@@ -42,6 +45,7 @@
 with_engine=0
 with_fuchsia=0
 goma_flags=""
+no_prebuilt_dart_sdk_flags=""
 app_name="hello_flutter"
 while [[ $# -gt 0 ]]; do
   case $1 in
@@ -72,6 +76,10 @@
       goma_flags="--goma"
       shift # past argument
       ;;
+    --no-prebuilt-dart-sdk)
+      no_prebuilt_dart_sdk_flags="--no-prebuilt-dart-sdk"
+      shift # past argument
+      ;;
     *)
       app_name="$1"
       shift # past value
@@ -79,6 +87,18 @@
   esac
 done
 
+if [[ "${with_engine}" == 0 && "${no_prebuilt_dart_sdk_flags}" != "" ]]
+then
+  echo-error "--no-prebuilt-dart-sdk requires --with-engine."
+  exit 1
+fi
+
+if [[ "${with_engine}" == 0 && "${goma_flags}" != "" ]]
+then
+  echo-error "--goma requires --with-engine."
+  exit 1
+fi
+
 if [[ "${log}" != 0 && "${fidl}" != 0 ]]
 then
   echo-error "Both --log and --fidl require taking over stdout, so they can't both be specified."
@@ -95,7 +115,7 @@
 
 if [[ "${with_engine}" != 0 ]]
 then
-  "${FUCHSIA_EMBEDDER_DIR}"/scripts/build_and_copy_engine_artifacts.sh ${goma_flags}
+  "${FUCHSIA_EMBEDDER_DIR}"/scripts/build_and_copy_engine_artifacts.sh ${goma_flags} ${no_prebuilt_dart_sdk_flags}
 fi
 
 if [[ "${with_fuchsia}" != 0 ]]
diff --git a/scripts/tests/build_and_run_example_test.sh b/scripts/tests/build_and_run_example_test.sh
new file mode 100755
index 0000000..dea3bb6
--- /dev/null
+++ b/scripts/tests/build_and_run_example_test.sh
@@ -0,0 +1,51 @@
+#!/bin/bash
+#
+# Copyright 2022 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.
+#
+# Runs local tests for scripts/bootstrap.sh. Not runnable on CQ.
+#
+# Usage:
+#   $FUCHSIA_EMBEDDER_DIR/scripts/tests/bootstrap_test.sh
+
+set -e # Fail on any error.
+source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/../lib/helpers.sh || exit $?
+
+pushd $FUCHSIA_EMBEDDER_DIR
+
+echo-info 'Testing build_and_run_example.sh with no arguments ...'
+$FUCHSIA_EMBEDDER_DIR/scripts/build_and_run_example.sh
+
+if [[ -d "${embedder_engine_dir}" ]]
+then
+    echo-info 'Testing build_and_run_example.sh --with-engine ...'
+    $FUCHSIA_EMBEDDER_DIR/scripts/build_and_run_example.sh --with-engine
+
+    echo-info 'Testing build_and_run_example.sh --with-engine --goma ...'
+    $FUCHSIA_EMBEDDER_DIR/scripts/build_and_run_example.sh --with-engine --goma
+
+    echo-info 'Testing build_and_run_example.sh --with-engine --goma --no-prebuilt-dart-sdk ...'
+    $FUCHSIA_EMBEDDER_DIR/scripts/build_and_run_example.sh --with-engine --goma --no-prebuilt-dart-sdk
+else
+    echo-warning "Skipping --with-engine tests because ${embedder_engine_dir} doesn't exist."
+    echo-warning "To run these tests, follow the setup from https://fuchsia.googlesource.com/flutter-embedder/+/refs/heads/main/docs/making_engine_changes.md."
+fi
+
+if ! [ -z "${LOCAL_FUCHSIA_PLATFORM_BUILD}" ]
+then
+    echo-info 'Testing build_and_run_example.sh --with-fuchsia ...'
+    $FUCHSIA_EMBEDDER_DIR/scripts/build_and_run_example.sh --with-fuchsia
+else
+    echo-warning "Skipping --with-fuchsia tests because ${LOCAL_FUCHSIA_PLATFORM_BUILD} isn't set."
+    echo-warning "To run these tests, follow the setup from https://fuchsia.googlesource.com/flutter-embedder/+/refs/heads/main/docs/testing_fuchsia_sdk_changes.md."
+fi
+
+popd  # $FUCHSIA_EMBEDDER_DIR
+
+pushd $HOME
+
+echo-info 'Testing build_and_run_example.sh from $HOME ...'
+$FUCHSIA_EMBEDDER_DIR/scripts/build_and_run_example.sh
+
+popd  # $HOME