Pump V8 foreground tasks queued to platform message loop.

Make pdfium_test behave more similarly to what the chromium
embedder is actually doing. Note if any work happens on stderr
(so as not to cause diffs in text-based tests writing to stdout).

Change-Id: I6381401f73b24bcf82a0e25606807254428d9b02
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/70392
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc
index d983526..45500dc 100644
--- a/samples/pdfium_test.cc
+++ b/samples/pdfium_test.cc
@@ -1214,6 +1214,17 @@
     }
     RenderPdf(filename, file_contents.get(), file_length, options, events);
 
+#ifdef PDF_ENABLE_V8
+    if (!options.disable_javascript) {
+      int task_count = 0;
+      while (v8::platform::PumpMessageLoop(platform.get(), isolate.get()))
+        ++task_count;
+
+      if (task_count)
+        fprintf(stderr, "Pumped %d tasks\n", task_count);
+    }
+#endif  // PDF_ENABLE_V8
+
 #ifdef ENABLE_CALLGRIND
     if (options.callgrind_delimiters)
       CALLGRIND_STOP_INSTRUMENTATION;
diff --git a/testing/resources/javascript/foreground_task.in b/testing/resources/javascript/foreground_task.in
new file mode 100644
index 0000000..b96917c
--- /dev/null
+++ b/testing/resources/javascript/foreground_task.in
@@ -0,0 +1,44 @@
+{{header}}
+{{object 1 0}} <<
+  /Type /Catalog
+  /Pages 2 0 R
+  /OpenAction 10 0 R
+>>
+endobj
+{{object 2 0}} <<
+  /Type /Pages
+  /Count 1
+  /Kids [
+    3 0 R
+  ]
+>>
+endobj
+{{object 3 0}} <<
+  /Type /Page
+  /Parent 2 0 R
+  /MediaBox [0 0 612 792]
+>>
+endobj
+{{object 10 0}} <<
+  /Type /Action
+  /S /JavaScript
+  /JS 11 0 R
+>>
+endobj
+{{object 11 0}} <<
+  {{streamlen}}
+>>
+stream
+try {
+  // TODO(tsepez): figure out why .then() doesn't fire.
+  gc({type: 'major', execution: 'async'}).then(() => { app.alert('resolved') });
+  app.alert('Posted');
+} catch (e) {
+  app.alert('Error: ' + e);
+}
+endstream
+endobj
+{{xref}}
+{{trailer}}
+{{startxref}}
+%%EOF
diff --git a/testing/resources/javascript/foreground_task_expected.txt b/testing/resources/javascript/foreground_task_expected.txt
new file mode 100644
index 0000000..a561e88
--- /dev/null
+++ b/testing/resources/javascript/foreground_task_expected.txt
@@ -0,0 +1 @@
+Alert: Posted