1 // Copyright 2016 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 "public/fpdf_edit.h"
6 
7 #include "core/fpdfapi/cpdf_modulemgr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 
10 class PDFEditTest : public testing::Test {
SetUp()11   void SetUp() override {
12     CPDF_ModuleMgr* module_mgr = CPDF_ModuleMgr::Get();
13     module_mgr->InitPageModule();
14   }
15 
TearDown()16   void TearDown() override { CPDF_ModuleMgr::Destroy(); }
17 };
18 
TEST_F(PDFEditTest,InsertObjectWithInvalidPage)19 TEST_F(PDFEditTest, InsertObjectWithInvalidPage) {
20   FPDF_DOCUMENT doc = FPDF_CreateNewDocument();
21   FPDF_PAGE page = FPDFPage_New(doc, 0, 100, 100);
22   EXPECT_EQ(0, FPDFPage_CountObject(page));
23 
24   FPDFPage_InsertObject(nullptr, nullptr);
25   EXPECT_EQ(0, FPDFPage_CountObject(page));
26 
27   FPDFPage_InsertObject(page, nullptr);
28   EXPECT_EQ(0, FPDFPage_CountObject(page));
29 
30   FPDF_PAGEOBJECT page_image = FPDFPageObj_NewImgeObj(doc);
31   FPDFPage_InsertObject(nullptr, page_image);
32   EXPECT_EQ(0, FPDFPage_CountObject(page));
33 
34   FPDF_ClosePage(page);
35   FPDF_CloseDocument(doc);
36 }
37 
TEST_F(PDFEditTest,NewImgeObj)38 TEST_F(PDFEditTest, NewImgeObj) {
39   FPDF_DOCUMENT doc = FPDF_CreateNewDocument();
40   FPDF_PAGE page = FPDFPage_New(doc, 0, 100, 100);
41   EXPECT_EQ(0, FPDFPage_CountObject(page));
42 
43   FPDF_PAGEOBJECT page_image = FPDFPageObj_NewImgeObj(doc);
44   FPDFPage_InsertObject(page, page_image);
45   EXPECT_EQ(1, FPDFPage_CountObject(page));
46   EXPECT_TRUE(FPDFPage_GenerateContent(page));
47 
48   FPDF_ClosePage(page);
49   FPDF_CloseDocument(doc);
50 }
51 
TEST_F(PDFEditTest,NewImgeObjGenerateContent)52 TEST_F(PDFEditTest, NewImgeObjGenerateContent) {
53   FPDF_DOCUMENT doc = FPDF_CreateNewDocument();
54   FPDF_PAGE page = FPDFPage_New(doc, 0, 100, 100);
55   EXPECT_EQ(0, FPDFPage_CountObject(page));
56 
57   constexpr int kBitmapSize = 50;
58   FPDF_BITMAP bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 0);
59   FPDFBitmap_FillRect(bitmap, 0, 0, kBitmapSize, kBitmapSize, 0x00000000);
60   EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(bitmap));
61   EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(bitmap));
62 
63   FPDF_PAGEOBJECT page_image = FPDFPageObj_NewImgeObj(doc);
64   ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, page_image, bitmap));
65   ASSERT_TRUE(
66       FPDFImageObj_SetMatrix(page_image, kBitmapSize, 0, 0, kBitmapSize, 0, 0));
67   FPDFPage_InsertObject(page, page_image);
68   EXPECT_EQ(1, FPDFPage_CountObject(page));
69   EXPECT_TRUE(FPDFPage_GenerateContent(page));
70 
71   FPDFBitmap_Destroy(bitmap);
72   FPDF_ClosePage(page);
73   FPDF_CloseDocument(doc);
74 }
75