• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/fxcrt/fx_string.h"
6 #include "public/fpdf_structtree.h"
7 #include "testing/embedder_test.h"
8 #include "testing/test_support.h"
9 #include "third_party/base/optional.h"
10 
11 class FPDFStructTreeEmbeddertest : public EmbedderTest {};
12 
TEST_F(FPDFStructTreeEmbeddertest,GetAltText)13 TEST_F(FPDFStructTreeEmbeddertest, GetAltText) {
14   ASSERT_TRUE(OpenDocument("tagged_alt_text.pdf"));
15   FPDF_PAGE page = LoadPage(0);
16   ASSERT_TRUE(page);
17 
18   FPDF_STRUCTTREE struct_tree = FPDF_StructTree_GetForPage(page);
19   ASSERT_TRUE(struct_tree);
20   ASSERT_EQ(1, FPDF_StructTree_CountChildren(struct_tree));
21 
22   FPDF_STRUCTELEMENT element = FPDF_StructTree_GetChildAtIndex(struct_tree, -1);
23   EXPECT_EQ(nullptr, element);
24   element = FPDF_StructTree_GetChildAtIndex(struct_tree, 1);
25   EXPECT_EQ(nullptr, element);
26   element = FPDF_StructTree_GetChildAtIndex(struct_tree, 0);
27   ASSERT_NE(nullptr, element);
28   EXPECT_EQ(-1, FPDF_StructElement_GetMarkedContentID(element));
29   EXPECT_EQ(0U, FPDF_StructElement_GetAltText(element, nullptr, 0));
30 
31   ASSERT_EQ(1, FPDF_StructElement_CountChildren(element));
32   FPDF_STRUCTELEMENT child_element =
33       FPDF_StructElement_GetChildAtIndex(element, -1);
34   EXPECT_EQ(nullptr, child_element);
35   child_element = FPDF_StructElement_GetChildAtIndex(element, 1);
36   EXPECT_EQ(nullptr, child_element);
37   child_element = FPDF_StructElement_GetChildAtIndex(element, 0);
38   ASSERT_NE(nullptr, child_element);
39   EXPECT_EQ(-1, FPDF_StructElement_GetMarkedContentID(child_element));
40   EXPECT_EQ(0U, FPDF_StructElement_GetAltText(child_element, nullptr, 0));
41 
42   ASSERT_EQ(1, FPDF_StructElement_CountChildren(child_element));
43   FPDF_STRUCTELEMENT gchild_element =
44       FPDF_StructElement_GetChildAtIndex(child_element, -1);
45   EXPECT_EQ(nullptr, gchild_element);
46   gchild_element = FPDF_StructElement_GetChildAtIndex(child_element, 1);
47   EXPECT_EQ(nullptr, gchild_element);
48   gchild_element = FPDF_StructElement_GetChildAtIndex(child_element, 0);
49   ASSERT_NE(nullptr, gchild_element);
50   EXPECT_EQ(-1, FPDF_StructElement_GetMarkedContentID(gchild_element));
51   ASSERT_EQ(24U, FPDF_StructElement_GetAltText(gchild_element, nullptr, 0));
52 
53   unsigned short buffer[12];
54   memset(buffer, 0, sizeof(buffer));
55   // Deliberately pass in a small buffer size to make sure |buffer| remains
56   // untouched.
57   ASSERT_EQ(24U, FPDF_StructElement_GetAltText(gchild_element, buffer, 1));
58   for (size_t i = 0; i < FX_ArraySize(buffer); ++i)
59     EXPECT_EQ(0U, buffer[i]);
60 
61   EXPECT_EQ(-1, FPDF_StructElement_GetMarkedContentID(gchild_element));
62   ASSERT_EQ(24U, FPDF_StructElement_GetAltText(gchild_element, buffer,
63                                                sizeof(buffer)));
64   const wchar_t kExpected[] = L"Black Image";
65   EXPECT_EQ(WideString(kExpected),
66             WideString::FromUTF16LE(buffer, FXSYS_len(kExpected)));
67 
68   ASSERT_EQ(1, FPDF_StructElement_CountChildren(gchild_element));
69   FPDF_STRUCTELEMENT ggchild_element =
70       FPDF_StructElement_GetChildAtIndex(gchild_element, 0);
71   EXPECT_EQ(nullptr, ggchild_element);
72 
73   FPDF_StructTree_Close(struct_tree);
74   FPDF_ClosePage(page);
75 }
76 
TEST_F(FPDFStructTreeEmbeddertest,GetMarkedContentID)77 TEST_F(FPDFStructTreeEmbeddertest, GetMarkedContentID) {
78   ASSERT_TRUE(OpenDocument("marked_content_id.pdf"));
79   FPDF_PAGE page = LoadPage(0);
80   ASSERT_TRUE(page);
81 
82   FPDF_STRUCTTREE struct_tree = FPDF_StructTree_GetForPage(page);
83   ASSERT_TRUE(struct_tree);
84   ASSERT_EQ(1, FPDF_StructTree_CountChildren(struct_tree));
85 
86   FPDF_STRUCTELEMENT element = FPDF_StructTree_GetChildAtIndex(struct_tree, 0);
87   EXPECT_EQ(0, FPDF_StructElement_GetMarkedContentID(element));
88 
89   FPDF_StructTree_Close(struct_tree);
90   FPDF_ClosePage(page);
91 }
92 
TEST_F(FPDFStructTreeEmbeddertest,GetType)93 TEST_F(FPDFStructTreeEmbeddertest, GetType) {
94   ASSERT_TRUE(OpenDocument("tagged_alt_text.pdf"));
95   FPDF_PAGE page = LoadPage(0);
96   ASSERT_TRUE(page);
97 
98   FPDF_STRUCTTREE struct_tree = FPDF_StructTree_GetForPage(page);
99   ASSERT_TRUE(struct_tree);
100   ASSERT_EQ(1, FPDF_StructTree_CountChildren(struct_tree));
101 
102   FPDF_STRUCTELEMENT element = FPDF_StructTree_GetChildAtIndex(struct_tree, 0);
103   ASSERT_NE(nullptr, element);
104 
105   unsigned short buffer[12];
106   memset(buffer, 0, sizeof(buffer));
107   // Deliberately pass in a small buffer size to make sure |buffer| remains
108   // untouched.
109   ASSERT_EQ(18U, FPDF_StructElement_GetType(element, buffer, 1));
110   for (size_t i = 0; i < FX_ArraySize(buffer); ++i)
111     EXPECT_EQ(0U, buffer[i]);
112 
113   ASSERT_EQ(18U, FPDF_StructElement_GetType(element, buffer, sizeof(buffer)));
114   const wchar_t kExpected[] = L"Document";
115   EXPECT_EQ(WideString(kExpected),
116             WideString::FromUTF16LE(buffer, FXSYS_len(kExpected)));
117 
118   FPDF_StructTree_Close(struct_tree);
119   FPDF_ClosePage(page);
120 }
121