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
10class FPDFStructTreeEmbeddertest : public EmbedderTest, public TestSaver {};
11
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)));
59  const FX_WCHAR kExpected[] = L"Black Image";
60  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}
71