From 048afc6aba4848d5296affb4335500f960262580 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 1 May 2018 17:01:54 +0000 Subject: Fix CFX_XML and add unit tests This CL fixes several issues in the CFX_XML class and adds unit tests. Change-Id: I05270690de8f3c45dceb866e17ef899ae6d23389 Reviewed-on: https://pdfium-review.googlesource.com/31753 Commit-Queue: dsinclair Reviewed-by: Ryan Harrison Reviewed-by: Henrique Nakashima --- core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp | 111 +++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp (limited to 'core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp') diff --git a/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp b/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp new file mode 100644 index 0000000000..c60d401303 --- /dev/null +++ b/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp @@ -0,0 +1,111 @@ +// Copyright 2018 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "core/fxcrt/xml/cfx_xmlinstruction.h" +#include "core/fxcrt/cfx_memorystream.h" +#include "core/fxcrt/xml/cfx_xmlelement.h" +#include "core/fxcrt/xml/cfx_xmlparser.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/string_write_stream.h" +#include "testing/test_support.h" + +TEST(CFX_XMLInstructionTest, GetType) { + CFX_XMLInstruction node(L"acrobat"); + EXPECT_EQ(FX_XMLNODE_Instruction, node.GetType()); +} + +TEST(CFX_XMLInstructionTest, AcrobatInstruction) { + CFX_XMLInstruction node(L"acrobat"); + EXPECT_TRUE(node.IsAcrobat()); + EXPECT_FALSE(node.IsOriginalXFAVersion()); +} + +TEST(CFX_XMLInstructionTest, OriginalXFAInstruction) { + CFX_XMLInstruction node(L"originalXFAVersion"); + EXPECT_TRUE(node.IsOriginalXFAVersion()); + EXPECT_FALSE(node.IsAcrobat()); +} + +TEST(CFX_XMLInstructionTest, TargetData) { + CFX_XMLInstruction node(L"acrobat"); + EXPECT_EQ(0U, node.GetTargetData().size()); + + node.AppendData(L"firstString"); + node.AppendData(L"secondString"); + + auto& data = node.GetTargetData(); + ASSERT_EQ(2U, data.size()); + EXPECT_EQ(L"firstString", data[0]); + EXPECT_EQ(L"secondString", data[1]); +} + +TEST(CFX_XMLInstructionTest, Clone) { + CFX_XMLInstruction node(L"acrobat"); + node.AppendData(L"firstString"); + node.AppendData(L"secondString"); + + auto clone = node.Clone(); + EXPECT_TRUE(clone != nullptr); + + ASSERT_EQ(FX_XMLNODE_Instruction, clone->GetType()); + CFX_XMLInstruction* inst = static_cast(clone.get()); + + EXPECT_TRUE(inst->IsAcrobat()); + + auto& data = inst->GetTargetData(); + ASSERT_EQ(2U, data.size()); + EXPECT_EQ(L"firstString", data[0]); + EXPECT_EQ(L"secondString", data[1]); +} + +TEST(CFX_XMLInstructionTest, SaveXML) { + auto stream = pdfium::MakeRetain(); + CFX_XMLInstruction node(L"xml"); + node.Save(stream); + EXPECT_EQ("\n", stream->ToString()); +} + +TEST(CFX_XMLInstructionTest, SaveAcrobat) { + auto stream = pdfium::MakeRetain(); + CFX_XMLInstruction node(L"acrobat"); + node.AppendData(L"http://www.xfa.org/schema/xfa-template/3.3/"); + node.AppendData(L"Display:1"); + + node.Save(stream); + EXPECT_EQ( + "\n", + stream->ToString()); +} + +TEST(CFX_XMLInstructionTest, ParseAndReSave) { + const char* input = + "\n" + ""; + + auto in_stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input), + false); + + CFX_XMLElement root(L"root"); + CFX_XMLParser parser(&root, in_stream); + ASSERT_TRUE(parser.Parse()); + ASSERT_TRUE(root.GetFirstChild() != nullptr); + ASSERT_EQ(FX_XMLNODE_Instruction, root.GetFirstChild()->GetType()); + + CFX_XMLInstruction* node = + static_cast(root.GetFirstChild()); + ASSERT_TRUE(node != nullptr); + EXPECT_TRUE(node->IsAcrobat()); + + auto& data = node->GetTargetData(); + ASSERT_EQ(2U, data.size()); + EXPECT_EQ(L"http://www.xfa.org/schema/xfa-template/3.3/", data[0]); + EXPECT_EQ(L"Display:1", data[1]); + + auto out_stream = pdfium::MakeRetain(); + node->Save(out_stream); + EXPECT_EQ( + "\n", + out_stream->ToString()); +} -- cgit v1.2.3