1 // Copyright 2019 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/fxge/win32/win32_int.h"
6
7 #include <windows.h>
8
9 #include <memory>
10
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/base/ptr_util.h"
13
14 namespace {
15
16 constexpr CFX_Matrix kIdentityMatrix;
17
18 } // namespace
19
20 class CFX_WindowsRenderDeviceTest : public testing::Test {
21 public:
SetUp()22 void SetUp() override {
23 // Get a device context with Windows GDI.
24 m_hDC = CreateCompatibleDC(nullptr);
25 ASSERT_TRUE(m_hDC);
26 CFX_GEModule::Create(nullptr);
27 m_driver = pdfium::MakeUnique<CFX_WindowsRenderDevice>(m_hDC, nullptr);
28 m_driver->SaveState();
29 }
30
TearDown()31 void TearDown() override {
32 m_driver->RestoreState(false);
33 m_driver.reset();
34 CFX_GEModule::Destroy();
35 DeleteDC(m_hDC);
36 }
37
38 protected:
39 HDC m_hDC;
40 std::unique_ptr<CFX_WindowsRenderDevice> m_driver;
41 };
42
TEST_F(CFX_WindowsRenderDeviceTest,SimpleClipTriangle)43 TEST_F(CFX_WindowsRenderDeviceTest, SimpleClipTriangle) {
44 CFX_PathData path_data;
45 CFX_PointF p1(0.0f, 0.0f);
46 CFX_PointF p2(0.0f, 100.0f);
47 CFX_PointF p3(100.0f, 100.0f);
48
49 path_data.AppendLine(p1, p2);
50 path_data.AppendLine(p2, p3);
51 path_data.AppendLine(p3, p1);
52 path_data.ClosePath();
53 EXPECT_TRUE(
54 m_driver->SetClip_PathFill(&path_data, &kIdentityMatrix, FXFILL_WINDING));
55 }
56
TEST_F(CFX_WindowsRenderDeviceTest,SimpleClipRect)57 TEST_F(CFX_WindowsRenderDeviceTest, SimpleClipRect) {
58 CFX_PathData path_data;
59
60 path_data.AppendRect(0.0f, 100.0f, 200.0f, 0.0f);
61 path_data.ClosePath();
62 EXPECT_TRUE(
63 m_driver->SetClip_PathFill(&path_data, &kIdentityMatrix, FXFILL_WINDING));
64 }
65
TEST_F(CFX_WindowsRenderDeviceTest,GargantuanClipRect)66 TEST_F(CFX_WindowsRenderDeviceTest, GargantuanClipRect) {
67 CFX_PathData path_data;
68
69 path_data.AppendRect(-257698020.0f, -257697252.0f, 257698044.0f,
70 257698812.0f);
71 path_data.ClosePath();
72 // These coordinates for a clip path are valid, just very large. Using these
73 // for a clip path should allow IntersectClipRect() to return success;
74 // however they do not because the GDI API IntersectClipRect() errors out and
75 // affect subsequent imaging. crbug.com/1019026
76 EXPECT_FALSE(
77 m_driver->SetClip_PathFill(&path_data, &kIdentityMatrix, FXFILL_WINDING));
78 }
79
TEST_F(CFX_WindowsRenderDeviceTest,GargantuanClipRectWithBaseClip)80 TEST_F(CFX_WindowsRenderDeviceTest, GargantuanClipRectWithBaseClip) {
81 CFX_PathData path_data;
82 const FX_RECT kBaseClip(0, 0, 5100, 6600);
83
84 m_driver->SetBaseClip(kBaseClip);
85 path_data.AppendRect(-257698020.0f, -257697252.0f, 257698044.0f,
86 257698812.0f);
87 path_data.ClosePath();
88 // Use of a reasonable base clip ensures that we avoid getting an error back
89 // from GDI API IntersectClipRect().
90 EXPECT_TRUE(
91 m_driver->SetClip_PathFill(&path_data, &kIdentityMatrix, FXFILL_WINDING));
92 }
93