Move embedder_test_main.cc's testing::Environment to embedder_test.cc

Rename it to EmbedderTestEnvironment. Allows other embedder tests to
access Environment resources (such as the v8::Platform) in future CLs.

Change-Id: Ia31d1e3edecc9a375aed6c31f248b2a490b1e340
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/70512
Commit-Queue: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 8d7098c..5bcb9cb 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -294,6 +294,7 @@
   testonly = true
   sources = [ "testing/embedder_test_main.cpp" ]
   deps = [
+    ":pdfium_embeddertest_deps",
     "core/fpdfapi/edit:embeddertests",
     "core/fpdfapi/parser:embeddertests",
     "core/fpdfapi/render:embeddertests",
diff --git a/testing/BUILD.gn b/testing/BUILD.gn
index 19679c0..35e9d31 100644
--- a/testing/BUILD.gn
+++ b/testing/BUILD.gn
@@ -87,7 +87,6 @@
     "range_set.cpp",
     "range_set.h",
   ]
-
   deps = [
     ":test_support",
     "../:pdfium_public_headers",
@@ -106,6 +105,7 @@
       "js_embedder_test.h",
     ]
     deps += [ "../fxjs" ]
+    configs += [ "//v8:external_startup_data" ]
 
     if (pdf_enable_xfa) {
       sources += [
diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp
index 650805e..9dd001f 100644
--- a/testing/embedder_test.cpp
+++ b/testing/embedder_test.cpp
@@ -29,12 +29,15 @@
 #include "third_party/base/stl_util.h"
 
 #ifdef PDF_ENABLE_V8
+#include "testing/v8_initializer.h"
 #include "v8/include/v8-platform.h"
 #include "v8/include/v8.h"
 #endif  // PDF_ENABLE_V8
 
 namespace {
 
+EmbedderTestEnvironment* g_environment = nullptr;
+
 int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
   return EmbedderTest::BytesPerPixelForFormat(FPDFBitmap_GetFormat(bitmap));
 }
@@ -53,6 +56,55 @@
 
 }  // namespace
 
+EmbedderTestEnvironment::EmbedderTestEnvironment(const char* exe_name)
+#ifdef PDF_ENABLE_V8
+    : exe_path_(exe_name)
+#endif
+{
+  ASSERT(!g_environment);
+  g_environment = this;
+}
+
+EmbedderTestEnvironment::~EmbedderTestEnvironment() {
+  ASSERT(g_environment);
+  g_environment = nullptr;
+
+#ifdef PDF_ENABLE_V8
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA
+  if (v8_snapshot_)
+    free(const_cast<char*>(v8_snapshot_->data));
+#endif  // V8_USE_EXTERNAL_STARTUP_DATA
+#endif  // PDF_ENABLE_V8
+}
+
+// static
+EmbedderTestEnvironment* EmbedderTestEnvironment::GetInstance() {
+  return g_environment;
+}
+
+void EmbedderTestEnvironment::SetUp() {
+#ifdef PDF_ENABLE_V8
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA
+  if (v8_snapshot_) {
+    platform_ =
+        InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(), nullptr);
+  } else {
+    v8_snapshot_ = std::make_unique<v8::StartupData>();
+    platform_ = InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(),
+                                                     v8_snapshot_.get());
+  }
+#else
+  platform_ = InitializeV8ForPDFium(exe_path_);
+#endif  // V8_USE_EXTERNAL_STARTUP_DATA
+#endif  // FPDF_ENABLE_V8
+}
+
+void EmbedderTestEnvironment::TearDown() {
+#ifdef PDF_ENABLE_V8
+  v8::V8::ShutdownPlatform();
+#endif  // PDF_ENABLE_V8
+}
+
 EmbedderTest::EmbedderTest()
     : default_delegate_(std::make_unique<EmbedderTest::Delegate>()),
       delegate_(default_delegate_.get()) {
diff --git a/testing/embedder_test.h b/testing/embedder_test.h
index cdb4268..45e2a5d 100644
--- a/testing/embedder_test.h
+++ b/testing/embedder_test.h
@@ -23,8 +23,45 @@
 #include "testing/gtest/include/gtest/gtest.h"
 #include "third_party/base/span.h"
 
+#ifdef PDF_ENABLE_V8
+namespace v8 {
+class Platform;
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA
+class StartupData;
+#endif  // V8_USE_EXTERNAL_STARTUP_DATA
+}  // namespace v8
+#endif  // PDF_ENABLE_V8
+
 class TestLoader;
 
+// The loading time of the CFGAS_FontMgr is linear in the number of times it is
+// loaded. So, if a test suite has a lot of tests that need a font manager they
+// can end up executing very, very slowly.
+class EmbedderTestEnvironment final : public testing::Environment {
+ public:
+  explicit EmbedderTestEnvironment(const char* exe_path);
+  ~EmbedderTestEnvironment() override;
+
+  // Note: does not create one if it does not exist.
+  static EmbedderTestEnvironment* GetInstance();
+
+  void SetUp() override;
+  void TearDown() override;
+
+#ifdef PDF_ENABLE_V8
+  v8::Platform* platform() const { return platform_.get(); }
+#endif  // PDF_ENABLE_V8
+
+ private:
+#ifdef PDF_ENABLE_V8
+  const char* const exe_path_;
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA
+  std::unique_ptr<v8::StartupData> v8_snapshot_;
+#endif  // V8_USE_EXTERNAL_STARTUP_DATA
+  std::unique_ptr<v8::Platform> platform_;
+#endif  // PDF_ENABLE_V8
+};
+
 // This class is used to load a PDF document, and then run programatic
 // API tests against it.
 class EmbedderTest : public ::testing::Test,
diff --git a/testing/embedder_test_main.cpp b/testing/embedder_test_main.cpp
index 34dfb06..d4318d1 100644
--- a/testing/embedder_test_main.cpp
+++ b/testing/embedder_test_main.cpp
@@ -2,89 +2,22 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include <memory>
-#include <string>
-
 #include "core/fxcrt/fx_memory.h"
+#include "testing/embedder_test.h"
 #include "testing/gmock/include/gmock/gmock.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
-#ifdef PDF_ENABLE_V8
-#include "testing/v8_initializer.h"
-#include "v8/include/v8-platform.h"
-#include "v8/include/v8.h"
-#endif  // PDF_ENABLE_v8
-
-namespace {
-
-const char* g_exe_path = nullptr;
-
-#ifdef PDF_ENABLE_V8
-#ifdef V8_USE_EXTERNAL_STARTUP_DATA
-v8::StartupData* g_v8_snapshot = nullptr;
-#endif  // V8_USE_EXTERNAL_STARTUP_DATA
-#endif  // PDF_ENABLE_V8
-
-// The loading time of the CFGAS_FontMgr is linear in the number of times it is
-// loaded. So, if a test suite has a lot of tests that need a font manager they
-// can end up executing very, very slowly.
-class Environment final : public testing::Environment {
- public:
-  void SetUp() override {
-#ifdef PDF_ENABLE_V8
-#ifdef V8_USE_EXTERNAL_STARTUP_DATA
-    if (g_v8_snapshot) {
-      platform_ = InitializeV8ForPDFiumWithStartupData(g_exe_path,
-                                                       std::string(), nullptr);
-    } else {
-      g_v8_snapshot = new v8::StartupData;
-      platform_ = InitializeV8ForPDFiumWithStartupData(
-          g_exe_path, std::string(), g_v8_snapshot);
-    }
-#else
-    platform_ = InitializeV8ForPDFium(g_exe_path);
-#endif  // V8_USE_EXTERNAL_STARTUP_DATA
-#endif  // FPDF_ENABLE_V8
-  }
-
-  void TearDown() override {
-#ifdef PDF_ENABLE_V8
-    v8::V8::ShutdownPlatform();
-#endif  // PDF_ENABLE_V8
-  }
-
- private:
-#ifdef PDF_ENABLE_V8
-  std::unique_ptr<v8::Platform> platform_;
-#endif  // PDF_ENABLE_V8
-};
-
-Environment* env_ = nullptr;
-
-}  // namespace
-
-// Can't use gtest-provided main since we need to stash the path to the
-// executable in order to find the external V8 binary data files.
+// Can't use gtest-provided main since we need to create our own
+// testing environment which needs the executable path in order to
+// find the external V8 binary data files.
 int main(int argc, char** argv) {
-  g_exe_path = argv[0];
-
   FXMEM_InitializePartitionAlloc();
 
-  env_ = new Environment();
   // The env will be deleted by gtest.
-  AddGlobalTestEnvironment(env_);
+  AddGlobalTestEnvironment(new EmbedderTestEnvironment(argv[0]));
 
   testing::InitGoogleTest(&argc, argv);
   testing::InitGoogleMock(&argc, argv);
 
-  int ret_val = RUN_ALL_TESTS();
-
-#ifdef PDF_ENABLE_V8
-#ifdef V8_USE_EXTERNAL_STARTUP_DATA
-  if (g_v8_snapshot)
-    free(const_cast<char*>(g_v8_snapshot->data));
-#endif  // V8_USE_EXTERNAL_STARTUP_DATA
-#endif  // PDF_ENABLE_V8
-
-  return ret_val;
+  return RUN_ALL_TESTS();
 }