blob: 43a4d0159f2c30f1b3697e2e194c5a5fa55b49e3 [file] [log] [blame]
thestig9067fd62016-11-23 14:10:06 -08001// 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
Nicolas Pena3ff54002017-07-05 11:55:35 -040010class FPDFStructTreeEmbeddertest : public EmbedderTest {};
thestig9067fd62016-11-23 14:10:06 -080011
12TEST_F(FPDFStructTreeEmbeddertest, GetAltText) {
13 ASSERT_TRUE(OpenDocument("tagged_alt_text.pdf"));
14 FPDF_PAGE page = LoadPage(0);
15 ASSERT_TRUE(page);
16
17 FPDF_STRUCTTREE struct_tree = FPDF_StructTree_GetForPage(page);
18 ASSERT_TRUE(struct_tree);
19 ASSERT_EQ(1, FPDF_StructTree_CountChildren(struct_tree));
20
21 FPDF_STRUCTELEMENT element = FPDF_StructTree_GetChildAtIndex(struct_tree, -1);
22 EXPECT_EQ(nullptr, element);
23 element = FPDF_StructTree_GetChildAtIndex(struct_tree, 1);
24 EXPECT_EQ(nullptr, element);
25 element = FPDF_StructTree_GetChildAtIndex(struct_tree, 0);
26 ASSERT_NE(nullptr, element);
27 EXPECT_EQ(0U, FPDF_StructElement_GetAltText(element, nullptr, 0));
28
29 ASSERT_EQ(1, FPDF_StructElement_CountChildren(element));
30 FPDF_STRUCTELEMENT child_element =
31 FPDF_StructElement_GetChildAtIndex(element, -1);
32 EXPECT_EQ(nullptr, child_element);
33 child_element = FPDF_StructElement_GetChildAtIndex(element, 1);
34 EXPECT_EQ(nullptr, child_element);
35 child_element = FPDF_StructElement_GetChildAtIndex(element, 0);
36 ASSERT_NE(nullptr, child_element);
37 EXPECT_EQ(0U, FPDF_StructElement_GetAltText(child_element, nullptr, 0));
38
39 ASSERT_EQ(1, FPDF_StructElement_CountChildren(child_element));
40 FPDF_STRUCTELEMENT gchild_element =
41 FPDF_StructElement_GetChildAtIndex(child_element, -1);
42 EXPECT_EQ(nullptr, gchild_element);
43 gchild_element = FPDF_StructElement_GetChildAtIndex(child_element, 1);
44 EXPECT_EQ(nullptr, gchild_element);
45 gchild_element = FPDF_StructElement_GetChildAtIndex(child_element, 0);
46 ASSERT_NE(nullptr, gchild_element);
47 ASSERT_EQ(24U, FPDF_StructElement_GetAltText(gchild_element, nullptr, 0));
48
49 unsigned short buffer[12];
50 memset(buffer, 0, sizeof(buffer));
51 // Deliberately pass in a small buffer size to make sure |buffer| remains
52 // untouched.
53 ASSERT_EQ(24U, FPDF_StructElement_GetAltText(gchild_element, buffer, 1));
54 for (size_t i = 0; i < FX_ArraySize(buffer); ++i)
55 EXPECT_EQ(0U, buffer[i]);
56
57 ASSERT_EQ(24U, FPDF_StructElement_GetAltText(gchild_element, buffer,
58 sizeof(buffer)));
Dan Sinclair812e96c2017-03-13 16:43:37 -040059 const wchar_t kExpected[] = L"Black Image";
thestig9067fd62016-11-23 14:10:06 -080060 EXPECT_EQ(CFX_WideString(kExpected),
61 CFX_WideString::FromUTF16LE(buffer, FXSYS_len(kExpected)));
62
63 ASSERT_EQ(1, FPDF_StructElement_CountChildren(gchild_element));
64 FPDF_STRUCTELEMENT ggchild_element =
65 FPDF_StructElement_GetChildAtIndex(gchild_element, 0);
66 EXPECT_EQ(nullptr, ggchild_element);
67
68 FPDF_StructTree_Close(struct_tree);
69 FPDF_ClosePage(page);
70}
Dan Sinclair29479f62017-04-04 10:48:19 -040071
72TEST_F(FPDFStructTreeEmbeddertest, GetType) {
73 ASSERT_TRUE(OpenDocument("tagged_alt_text.pdf"));
74 FPDF_PAGE page = LoadPage(0);
75 ASSERT_TRUE(page);
76
77 FPDF_STRUCTTREE struct_tree = FPDF_StructTree_GetForPage(page);
78 ASSERT_TRUE(struct_tree);
79 ASSERT_EQ(1, FPDF_StructTree_CountChildren(struct_tree));
80
81 FPDF_STRUCTELEMENT element = FPDF_StructTree_GetChildAtIndex(struct_tree, 0);
82 ASSERT_NE(nullptr, element);
83
84 unsigned short buffer[12];
85 memset(buffer, 0, sizeof(buffer));
86 // Deliberately pass in a small buffer size to make sure |buffer| remains
87 // untouched.
88 ASSERT_EQ(18U, FPDF_StructElement_GetType(element, buffer, 1));
89 for (size_t i = 0; i < FX_ArraySize(buffer); ++i)
90 EXPECT_EQ(0U, buffer[i]);
91
92 ASSERT_EQ(18U, FPDF_StructElement_GetType(element, buffer, sizeof(buffer)));
93 const wchar_t kExpected[] = L"Document";
94 EXPECT_EQ(CFX_WideString(kExpected),
95 CFX_WideString::FromUTF16LE(buffer, FXSYS_len(kExpected)));
96
97 FPDF_StructTree_Close(struct_tree);
98 FPDF_ClosePage(page);
99}