Add unit test for CStrecthEngine::PixelWeights.

Demonstrate the underlying issue before applying fix.

Bug: pdfium:1688
Change-Id: Ia6f2d2cc803f2a10fe048c4c5cda5698c9a4ed69
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/82153
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxge/dib/cstretchengine_unittest.cpp b/core/fxge/dib/cstretchengine_unittest.cpp
index c3e3b8d..fec9eb0 100644
--- a/core/fxge/dib/cstretchengine_unittest.cpp
+++ b/core/fxge/dib/cstretchengine_unittest.cpp
@@ -14,6 +14,42 @@
 #include "core/fxge/dib/fx_dib.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+namespace {
+
+uint32_t PixelWeightSum(const CStretchEngine::PixelWeight* weights) {
+  uint32_t sum = 0;
+  for (int j = weights->m_SrcStart; j <= weights->m_SrcEnd; ++j) {
+    sum += weights->GetWeightForPosition(j);
+  }
+  return sum;
+}
+
+void ExecuteOneStretchTest(uint32_t dest_width,
+                           uint32_t src_width,
+                           const FXDIB_ResampleOptions& options) {
+  constexpr uint32_t kExpectedSum = 65536;  // kFixedPointOne not exposed yet.
+  CStretchEngine::WeightTable table;
+  table.CalculateWeights(dest_width, 0, dest_width, src_width, 0, src_width,
+                         options);
+  for (uint32_t i = 0; i < dest_width; ++i) {
+    EXPECT_EQ(kExpectedSum, PixelWeightSum(table.GetPixelWeight(i)))
+        << "for { " << src_width << ", " << dest_width << " } at " << i;
+  }
+}
+
+void ExecuteStretchTests(const FXDIB_ResampleOptions& options) {
+  // Can't test everything, few random values chosen.
+  constexpr uint32_t kDestWidths[] = {1, 2, 337, 512, 808, 2550};
+  constexpr uint32_t kSrcWidths[] = {1, 2, 187, 256, 809, 1110};
+  for (uint32_t src_width : kSrcWidths) {
+    for (uint32_t dest_width : kDestWidths) {
+      ExecuteOneStretchTest(dest_width, src_width, options);
+    }
+  }
+}
+
+}  // namespace
+
 TEST(CStretchEngine, OverflowInCtor) {
   FX_RECT clip_rect;
   RetainPtr<CPDF_Dictionary> dict_obj = pdfium::MakeRetain<CPDF_Dictionary>();
@@ -30,3 +66,29 @@
   EXPECT_FALSE(engine.GetResampleOptionsForTest().bNoSmoothing);
   EXPECT_FALSE(engine.GetResampleOptionsForTest().bLossy);
 }
+
+// See https://crbug.com/pdfium/1688
+TEST(CStretchEngine, DISABLED_WeightRounding) {
+  FXDIB_ResampleOptions options;
+  ExecuteStretchTests(options);
+}
+
+TEST(CStretchEngine, WeightRoundingNoSmoothing) {
+  FXDIB_ResampleOptions options;
+  options.bNoSmoothing = true;
+  ExecuteStretchTests(options);
+}
+
+// See https://crbug.com/pdfium/1688
+TEST(CStretchEngine, DISABLED_WeightRoundingBilinear) {
+  FXDIB_ResampleOptions options;
+  options.bInterpolateBilinear = true;
+  ExecuteStretchTests(options);
+}
+
+TEST(CStretchEngine, WeightRoundingNoSmoothingBilinear) {
+  FXDIB_ResampleOptions options;
+  options.bNoSmoothing = true;
+  options.bInterpolateBilinear = true;
+  ExecuteStretchTests(options);
+}