Add --write-pngs flag to pdfium_embeddertests.

During test development, there will be places where we want to dump
image files in addition to checking their hashes. This option will
dump a file whenever we would just do a hash comparison. Use the
--gtest_filter= flag to avoid creating a large number of images.

Images will be named by their hash, which should match up with
any failed comparisons reported by gtest.

Change-Id: I6547979fee906a0a820dbacc97057be457708d7e
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/93833
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp
index 16a0a50..e398ab6 100644
--- a/testing/embedder_test.cpp
+++ b/testing/embedder_test.cpp
@@ -16,6 +16,7 @@
 #include "public/fpdf_edit.h"
 #include "public/fpdf_text.h"
 #include "public/fpdfview.h"
+#include "testing/embedder_test_environment.h"
 #include "testing/gmock/include/gmock/gmock.h"
 #include "testing/test_loader.h"
 #include "testing/utils/bitmap_saver.h"
@@ -831,7 +832,11 @@
   if (!expected_md5sum)
     return;
 
-  EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
+  std::string actual_md5sum = HashBitmap(bitmap);
+  EXPECT_EQ(expected_md5sum, actual_md5sum);
+  if (EmbedderTestEnvironment::GetInstance()->write_pngs()) {
+    WriteBitmapToPng(bitmap, actual_md5sum + ".png");
+  }
 }
 
 // static
diff --git a/testing/embedder_test_environment.cpp b/testing/embedder_test_environment.cpp
index 168ad69..dec8de0 100644
--- a/testing/embedder_test_environment.cpp
+++ b/testing/embedder_test_environment.cpp
@@ -4,6 +4,8 @@
 
 #include "testing/embedder_test_environment.h"
 
+#include <ostream>
+
 #include "core/fxcrt/fx_system.h"
 #include "public/fpdfview.h"
 #include "third_party/base/check.h"
@@ -58,3 +60,15 @@
 void EmbedderTestEnvironment::TearDown() {
   FPDF_DestroyLibrary();
 }
+
+void EmbedderTestEnvironment::AddFlags(int argc, char** argv) {
+  for (int i = 1; i < argc; ++i)
+    AddFlag(argv[i]);
+}
+
+void EmbedderTestEnvironment::AddFlag(const std::string& flag) {
+  if (flag == "--write-pngs")
+    write_pngs_ = true;
+  else
+    std::cerr << "Unknown flag: " << flag << "\n";
+}
diff --git a/testing/embedder_test_environment.h b/testing/embedder_test_environment.h
index 703c003..cf7c8c1 100644
--- a/testing/embedder_test_environment.h
+++ b/testing/embedder_test_environment.h
@@ -5,6 +5,8 @@
 #ifndef TESTING_EMBEDDER_TEST_ENVIRONMENT_H_
 #define TESTING_EMBEDDER_TEST_ENVIRONMENT_H_
 
+#include <string>
+
 #include "public/fpdfview.h"
 #include "testing/gtest/include/gtest/gtest.h"
 #include "testing/test_fonts.h"
@@ -22,7 +24,14 @@
   void SetUp() override;
   void TearDown() override;
 
+  void AddFlags(int argc, char** argv);
+
+  bool write_pngs() const { return write_pngs_; }
+
  private:
+  void AddFlag(const std::string& flag);
+
+  bool write_pngs_ = false;
   TestFonts test_fonts_;
 };
 
diff --git a/testing/embedder_test_main.cpp b/testing/embedder_test_main.cpp
index 193ee6e..0698205 100644
--- a/testing/embedder_test_main.cpp
+++ b/testing/embedder_test_main.cpp
@@ -29,5 +29,8 @@
   testing::InitGoogleTest(&argc, argv);
   testing::InitGoogleMock(&argc, argv);
 
+  // Anything remaining in argc/argv is an embedder_tests flag.
+  EmbedderTestEnvironment::GetInstance()->AddFlags(argc, argv);
+
   return RUN_ALL_TESTS();
 }