[Aw WebExposed] Formalize rebaseline mode extra

This makes WebViewLayoutTest use a new rebaseline mode extra and adds
test_runner.py integration to allow setting this extra via a flag.

run_tests_rebaseline.sh is updated to perform rebaselines using the new
test_runner.py supported flag.

Bug: 381090604
Change-Id: I35a0b40c47413a0f4badd3281def6178bca3b87b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6054083
Reviewed-by: Nate Fischer <ntfschr@chromium.org>
Commit-Queue: Ashley Newson <ashleynewson@chromium.org>
Reviewed-by: Sam Maier <smaier@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1395883}
diff --git a/android_webview/tools/system_webview_shell/layout_tests/src/org/chromium/webview_shell/test/WebViewLayoutTest.java b/android_webview/tools/system_webview_shell/layout_tests/src/org/chromium/webview_shell/test/WebViewLayoutTest.java
index 5e9ad72..5dbfeaf 100644
--- a/android_webview/tools/system_webview_shell/layout_tests/src/org/chromium/webview_shell/test/WebViewLayoutTest.java
+++ b/android_webview/tools/system_webview_shell/layout_tests/src/org/chromium/webview_shell/test/WebViewLayoutTest.java
@@ -53,7 +53,11 @@
 
     private static final long TIMEOUT_SECONDS = 20;
 
+    // LINT.IfChange
+    private static final String EXTRA_REBASELINE =
+            "org.chromium.android_webview.test.RebaselineMode";
     private static final String MODE_REBASELINE = "rebaseline";
+    // LINT.ThenChange(//build/android/pylib/local/device/local_device_instrumentation_test_run.py)
 
     private WebViewLayoutTestActivity mTestActivity;
     private boolean mRebaseLine;
@@ -68,7 +72,7 @@
         mTestActivity = mActivityTestRule.getActivity();
         try {
             Bundle arguments = InstrumentationRegistry.getArguments();
-            String modeArgument = arguments.getString("mode");
+            String modeArgument = arguments.getString(EXTRA_REBASELINE);
             mRebaseLine = MODE_REBASELINE.equals(modeArgument);
         } catch (IllegalStateException exception) {
             Log.w(TAG, "Got no instrumentation arguments", exception);
diff --git a/android_webview/tools/system_webview_shell/test/data/run_tests_rebaseline.sh b/android_webview/tools/system_webview_shell/test/data/run_tests_rebaseline.sh
index ba2d663..bfe1957 100755
--- a/android_webview/tools/system_webview_shell/test/data/run_tests_rebaseline.sh
+++ b/android_webview/tools/system_webview_shell/test/data/run_tests_rebaseline.sh
@@ -43,14 +43,8 @@
 echo $SCRIPT_BUILD_DIR
 
 echo ""
-echo "Running the layout test using test runner to install it..."
-$RUNNER
-
-echo ""
-echo "Running layout test again in rebaseline mode..."
-adb shell am instrument -w -e mode rebaseline -e class \
-    $PACKAGE_NAME.WebViewLayoutTest \
-    $PACKAGE_NAME/org.chromium.base.test.BaseChromiumAndroidJUnitRunner
+echo "Running layout test in rebaseline mode..."
+"$RUNNER" --webview-rebaseline-mode
 
 echo ""
 echo "Pulling new expected files..."
diff --git a/build/android/pylib/instrumentation/instrumentation_test_instance.py b/build/android/pylib/instrumentation/instrumentation_test_instance.py
index b3deb88..f1ce14d 100644
--- a/build/android/pylib/instrumentation/instrumentation_test_instance.py
+++ b/build/android/pylib/instrumentation/instrumentation_test_instance.py
@@ -618,6 +618,8 @@
 
     self._webview_process_mode = args.webview_process_mode
 
+    self._webview_rebaseline_mode = args.webview_rebaseline_mode
+
     self._wpr_enable_record = args.wpr_enable_record
 
     self._external_shard_index = args.test_launcher_shard_index
@@ -1054,6 +1056,10 @@
     return self._webview_process_mode
 
   @property
+  def webview_rebaseline_mode(self):
+    return self._webview_rebaseline_mode
+
+  @property
   def wpr_replay_mode(self):
     return not self._wpr_enable_record
 
diff --git a/build/android/pylib/local/device/local_device_instrumentation_test_run.py b/build/android/pylib/local/device/local_device_instrumentation_test_run.py
index 975d7cd..02879372 100644
--- a/build/android/pylib/local/device/local_device_instrumentation_test_run.py
+++ b/build/android/pylib/local/device/local_device_instrumentation_test_run.py
@@ -114,6 +114,14 @@
 
 _EXTRA_WEBVIEW_PROCESS_MODE = 'AwJUnit4ClassRunner.ProcessMode'
 
+# LINT.IfChange
+_EXTRA_WEBVIEW_REBASELINE_MODE = (
+    'org.chromium.android_webview.test.RebaselineMode')
+_VALUE_WEBVIEW_REBASELINE_MODE = 'rebaseline'
+# pylint: disable=line-too-long
+# LINT.ThenChange(//android_webview/tools/system_webview_shell/layout_tests/src/org/chromium/webview_shell/test/WebViewLayoutTest.java)
+# pylint: enable=line-too-long
+
 FEATURE_ANNOTATION = 'Feature'
 RENDER_TEST_FEATURE_ANNOTATION = 'RenderTest'
 WPR_ARCHIVE_FILE_PATH_ANNOTATION = 'WPRArchiveDirectory'
@@ -1094,6 +1102,9 @@
       flags_to_add.append('--render-test-output-dir=%s' %
                           self._render_tests_device_output_dir.name)
 
+    if self._test_instance.webview_rebaseline_mode:
+      extras[_EXTRA_WEBVIEW_REBASELINE_MODE] = _VALUE_WEBVIEW_REBASELINE_MODE
+
     if _IsWPRRecordReplayTest(test):
       wpr_archive_relative_path = _GetWPRArchivePath(test)
       if not wpr_archive_relative_path:
diff --git a/build/android/test_runner.py b/build/android/test_runner.py
index 9f47e2a..a9394dd 100755
--- a/build/android/test_runner.py
+++ b/build/android/test_runner.py
@@ -665,6 +665,11 @@
       '-w', '--wait-for-java-debugger', action='store_true',
       help='Wait for java debugger to attach before running any application '
            'code. Also disables test timeouts and sets retries=0.')
+  parser.add_argument(
+      '--webview-rebaseline-mode',
+      action='store_true',
+      help=('Run WebView tests in rebaselining mode, updating on-device '
+            'expectation files.'))
 
   # WPR record mode.
   parser.add_argument('--wpr-enable-record',