Add some unit tests for FX_ParseDateUsingFormat().

Move the existing FakeTimeTest class to its own file, and use it in
fx_date_helpers_unittest.cpp to write tests that work consistently,
regardless of the actual time.

Change-Id: I17981c08723887c2f539ec6f8ffe5960b708fa8c
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/101412
Reviewed-by: K. Moon <kmoon@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/BUILD.gn b/core/fxcrt/BUILD.gn
index 9fab012..09400e8 100644
--- a/core/fxcrt/BUILD.gn
+++ b/core/fxcrt/BUILD.gn
@@ -162,6 +162,19 @@
   }
 }
 
+source_set("unit_test_support") {
+  testonly = true
+  sources = [
+    "fake_time_test.cpp",
+    "fake_time_test.h",
+  ]
+  configs += [ "../../:pdfium_strict_config" ]
+  deps = [
+    ":fxcrt",
+    "//testing/gtest",
+  ]
+}
+
 pdfium_unittest_source_set("unittests") {
   sources = [
     "autonuller_unittest.cpp",
@@ -210,7 +223,7 @@
     "xml/cfx_xmlparser_unittest.cpp",
     "xml/cfx_xmltext_unittest.cpp",
   ]
-  deps = []
+  deps = [ ":unit_test_support" ]
   pdfium_root_dir = "../../"
 
   if (pdf_enable_xfa) {
diff --git a/core/fxcrt/cfx_datetime_unittest.cpp b/core/fxcrt/cfx_datetime_unittest.cpp
index 92d7635..5349e2c 100644
--- a/core/fxcrt/cfx_datetime_unittest.cpp
+++ b/core/fxcrt/cfx_datetime_unittest.cpp
@@ -4,22 +4,7 @@
 
 #include "core/fxcrt/cfx_datetime.h"
 
-#include "core/fxcrt/fx_extension.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-class FakeTimeTest : public ::testing::Test {
- public:
-  void SetUp() override {
-    // Arbitrary, picked descending digits, 2020-04-23 15:05:21.
-    FXSYS_SetTimeFunction([]() -> time_t { return 1587654321; });
-    FXSYS_SetLocaltimeFunction([](const time_t* t) { return gmtime(t); });
-  }
-
-  void TearDown() override {
-    FXSYS_SetTimeFunction(nullptr);
-    FXSYS_SetLocaltimeFunction(nullptr);
-  }
-};
+#include "core/fxcrt/fake_time_test.h"
 
 TEST_F(FakeTimeTest, Now) {
   CFX_DateTime dt = CFX_DateTime::Now();
diff --git a/core/fxcrt/fake_time_test.cpp b/core/fxcrt/fake_time_test.cpp
new file mode 100644
index 0000000..a3968ff
--- /dev/null
+++ b/core/fxcrt/fake_time_test.cpp
@@ -0,0 +1,18 @@
+// Copyright 2022 The PDFium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/fxcrt/fake_time_test.h"
+
+#include "core/fxcrt/fx_extension.h"
+
+void FakeTimeTest::SetUp() {
+  // Arbitrary, picked descending digits, 2020-04-23 15:05:21.
+  FXSYS_SetTimeFunction([]() -> time_t { return 1587654321; });
+  FXSYS_SetLocaltimeFunction([](const time_t* t) { return gmtime(t); });
+}
+
+void FakeTimeTest::TearDown() {
+  FXSYS_SetTimeFunction(nullptr);
+  FXSYS_SetLocaltimeFunction(nullptr);
+}
diff --git a/core/fxcrt/fake_time_test.h b/core/fxcrt/fake_time_test.h
new file mode 100644
index 0000000..9e514d1
--- /dev/null
+++ b/core/fxcrt/fake_time_test.h
@@ -0,0 +1,16 @@
+// Copyright 2022 The PDFium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CORE_FXCRT_FAKE_TIME_TEST_H_
+#define CORE_FXCRT_FAKE_TIME_TEST_H_
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+class FakeTimeTest : public ::testing::Test {
+ public:
+  void SetUp() override;
+  void TearDown() override;
+};
+
+#endif  // CORE_FXCRT_FAKE_TIME_TEST_H_
diff --git a/fxjs/BUILD.gn b/fxjs/BUILD.gn
index eb42424..e3f62ff 100644
--- a/fxjs/BUILD.gn
+++ b/fxjs/BUILD.gn
@@ -250,7 +250,10 @@
       "fx_date_helpers_unittest.cpp",
     ]
     configs = [ "//v8:external_startup_data" ]
-    deps = [ ":fxjs" ]
+    deps = [
+      ":fxjs",
+      "../core/fxcrt:unit_test_support",
+    ]
     pdfium_root_dir = "../"
     if (pdf_enable_xfa) {
       sources += [
diff --git a/fxjs/fx_date_helpers_unittest.cpp b/fxjs/fx_date_helpers_unittest.cpp
index 72117cc..cc95719 100644
--- a/fxjs/fx_date_helpers_unittest.cpp
+++ b/fxjs/fx_date_helpers_unittest.cpp
@@ -4,6 +4,7 @@
 
 #include "fxjs/fx_date_helpers.h"
 
+#include "core/fxcrt/fake_time_test.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 namespace {
@@ -12,6 +13,8 @@
 
 }  // namespace
 
+using fxjs::ConversionStatus;
+
 TEST(FX_DateHelper, GetYearFromTime) {
   static constexpr struct {
     double time_ms;
@@ -106,3 +109,47 @@
         << test.time_ms;
   }
 }
+
+using FXDateHelperFakeTimeTest = FakeTimeTest;
+
+TEST_F(FXDateHelperFakeTimeTest, ParseDateUsingFormatWithEmptyParams) {
+  double result = 0.0;
+  EXPECT_EQ(ConversionStatus::kSuccess,
+            FX_ParseDateUsingFormat(L"", L"", &result));
+  EXPECT_DOUBLE_EQ(1'587'654'321'000, result);
+  EXPECT_EQ(ConversionStatus::kSuccess,
+            FX_ParseDateUsingFormat(L"value", L"", &result));
+  EXPECT_DOUBLE_EQ(1'587'654'321'000, result);
+  EXPECT_EQ(ConversionStatus::kSuccess,
+            FX_ParseDateUsingFormat(L"", L"format", &result));
+  EXPECT_DOUBLE_EQ(1'587'654'321'000, result);
+}
+
+TEST_F(FXDateHelperFakeTimeTest, ParseDateUsingFormatForValidMonthDay) {
+  double result = 0.0;
+  EXPECT_EQ(ConversionStatus::kSuccess,
+            FX_ParseDateUsingFormat(L"01/02/2000", L"mm/dd/yyyy", &result));
+  EXPECT_DOUBLE_EQ(946'825'521'000, result);
+  EXPECT_EQ(ConversionStatus::kSuccess,
+            FX_ParseDateUsingFormat(L"1/2/2000", L"m/d/yyyy", &result));
+  EXPECT_DOUBLE_EQ(946'825'521'000, result);
+  EXPECT_EQ(ConversionStatus::kSuccess,
+            FX_ParseDateUsingFormat(L"1-2-2000", L"m-d-yyyy", &result));
+  EXPECT_DOUBLE_EQ(946'825'521'000, result);
+  EXPECT_EQ(ConversionStatus::kSuccess,
+            FX_ParseDateUsingFormat(L"2-1-2000", L"d-m-yyyy", &result));
+  EXPECT_DOUBLE_EQ(946'825'521'000, result);
+
+  EXPECT_EQ(ConversionStatus::kSuccess,
+            FX_ParseDateUsingFormat(L"11/12/2000", L"mm/dd/yyyy", &result));
+  EXPECT_DOUBLE_EQ(973'955'121'000, result);
+  EXPECT_EQ(ConversionStatus::kSuccess,
+            FX_ParseDateUsingFormat(L"11/12/2000", L"m/d/yyyy", &result));
+  EXPECT_DOUBLE_EQ(973'955'121'000, result);
+  EXPECT_EQ(ConversionStatus::kSuccess,
+            FX_ParseDateUsingFormat(L"11-12-2000", L"m-d-yyyy", &result));
+  EXPECT_DOUBLE_EQ(973'955'121'000, result);
+  EXPECT_EQ(ConversionStatus::kSuccess,
+            FX_ParseDateUsingFormat(L"12-11-2000", L"d-m-yyyy", &result));
+  EXPECT_DOUBLE_EQ(973'955'121'000, result);
+}