blob: 13c063453be6b5f750f15ce65e9c4cbbd88b1c41 [file] [log] [blame]
Henrique Nakashima077f6432017-10-16 13:32:01 -04001// Copyright 2017 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_catalog.h"
6
7#include "core/fpdfapi/cpdf_modulemgr.h"
8#include "core/fpdfapi/parser/cpdf_document.h"
9#include "core/fpdfapi/parser/cpdf_number.h"
10#include "core/fpdfapi/parser/cpdf_parser.h"
11#include "core/fpdfapi/parser/cpdf_string.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "testing/test_support.h"
14
15#ifdef PDF_ENABLE_XFA
16#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
17#endif // PDF_ENABLE_XFA
18
19class CPDF_TestDocument : public CPDF_Document {
20 public:
21 CPDF_TestDocument() : CPDF_Document(nullptr) {}
22
23 void SetRoot(CPDF_Dictionary* root) {
24 m_pRootDict = root;
25 GetRoot();
26 }
27};
28
29#ifdef PDF_ENABLE_XFA
30class CPDF_TestXFAContext : public CPDFXFA_Context {
31 public:
32 CPDF_TestXFAContext()
33 : CPDFXFA_Context(pdfium::MakeUnique<CPDF_TestDocument>()) {}
34
35 void SetRoot(CPDF_Dictionary* root) {
36 reinterpret_cast<CPDF_TestDocument*>(GetPDFDoc())->SetRoot(root);
37 }
38
39 CPDF_IndirectObjectHolder* GetHolder() { return GetPDFDoc(); }
40};
41using CPDF_TestPdfDocument = CPDF_TestXFAContext;
42#else // PDF_ENABLE_XFA
43using CPDF_TestPdfDocument = CPDF_TestDocument;
44#endif // PDF_ENABLE_XFA
45
46class PDFCatalogTest : public testing::Test {
47 public:
48 void SetUp() override {
49 CPDF_ModuleMgr::Get()->Init();
50
51 m_pDoc = pdfium::MakeUnique<CPDF_TestPdfDocument>();
52
53 // Setup the root directory.
54 m_pRootObj = pdfium::MakeUnique<CPDF_Dictionary>();
55 }
56
57 void TearDown() override {
58 m_pDoc.reset();
59 CPDF_ModuleMgr::Destroy();
60 }
61
62 protected:
63 std::unique_ptr<CPDF_TestPdfDocument> m_pDoc;
64 std::unique_ptr<CPDF_Dictionary> m_pRootObj;
65};
66
67TEST_F(PDFCatalogTest, IsTagged) {
68 // Null doc
69 EXPECT_FALSE(FPDFCatalog_IsTagged(nullptr));
70
71 // No root
72 m_pDoc->SetRoot(nullptr);
73 EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
74
75 // Empty root
76 m_pDoc->SetRoot(m_pRootObj.get());
77 EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
78
79 // Root with other key
80 m_pRootObj->SetNewFor<CPDF_String>("OTHER_KEY", "other value", false);
81 EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
82
83 // Root with empty MarkInfo
84 CPDF_Dictionary* markInfoDict =
85 m_pRootObj->SetNewFor<CPDF_Dictionary>("MarkInfo");
86 EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
87
88 // MarkInfo present but Marked is 0
89 markInfoDict->SetNewFor<CPDF_Number>("Marked", 0);
90 EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
91
92 // MarkInfo present and Marked is 1, PDF is considered tagged.
93 markInfoDict->SetNewFor<CPDF_Number>("Marked", 1);
94 EXPECT_TRUE(FPDFCatalog_IsTagged(m_pDoc.get()));
95}