Change in pdfium[master]: Start CSS parser unit tests

4 views
Skip to first unread message

dsinclair (Gerrit)

unread,
Jan 12, 2017, 3:22:25 PM1/12/17
to pdfium-...@googlegroups.com, dsinclair

dsinclair uploaded this change for review.

View Change

Start CSS parser unit tests

Start adding unit tests for the css parser.

Change-Id: Id863d9cd5f13ab82626bc7b945de925253c88d43
---
M BUILD.gn
A xfa/fde/css/cfde_cssstylesheet_unittest.cpp
M xfa/fde/css/fde_cssdeclaration.cpp
M xfa/fde/css/fde_cssdeclaration.h
4 files changed, 122 insertions(+), 0 deletions(-)

diff --git a/BUILD.gn b/BUILD.gn
index 5e0a630..c69e6e4 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1746,6 +1746,7 @@
   if (pdf_enable_xfa) {
     sources += [
       "xfa/fde/cfde_txtedtbuf_unittest.cpp",
+      "xfa/fde/css/cfde_cssstylesheet_unittest.cpp",
       "xfa/fde/css/fde_cssdatatable_unittest.cpp",
       "xfa/fde/xml/fde_xml_imp_unittest.cpp",
       "xfa/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp",
diff --git a/xfa/fde/css/cfde_cssstylesheet_unittest.cpp b/xfa/fde/css/cfde_cssstylesheet_unittest.cpp
new file mode 100644
index 0000000..1edc4b5
--- /dev/null
+++ b/xfa/fde/css/cfde_cssstylesheet_unittest.cpp
@@ -0,0 +1,112 @@
+// Copyright 2017 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.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "xfa/fde/css/fde_cssstylesheet.h"
+
+#include <memory>
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/base/ptr_util.h"
+
+class CFDE_CSSStyleSheetTest : public testing::Test {
+ public:
+  void SetUp() override {
+    sheet_ = pdfium::MakeUnique<CFDE_CSSStyleSheet>();
+    decl_ = nullptr;
+  }
+
+  void TearDown() override { decl_ = nullptr; }
+
+  void LoadAndVerifyDecl(const FX_WCHAR* buf, size_t decl_count) {
+    ASSERT(sheet_);
+
+    EXPECT_TRUE(sheet_->LoadFromBuffer(buf, FXSYS_wcslen(buf)));
+    EXPECT_EQ(1, sheet_->CountRules());
+
+    CFDE_CSSRule* rule = sheet_->GetRule(0);
+    EXPECT_EQ(FDE_CSSRuleType::Style, rule->GetType());
+
+    CFDE_CSSStyleRule* style = static_cast<CFDE_CSSStyleRule*>(rule);
+    decl_ = style->GetDeclaration();
+    EXPECT_EQ(decl_count, decl_->PropertyCountForTesting());
+  }
+
+  void VerifyFloat(FDE_CSSProperty prop, float val, FDE_CSSPrimitiveType type) {
+    ASSERT(decl_);
+
+    bool important;
+    CFDE_CSSValue* v = decl_->GetProperty(prop, important);
+    CFDE_CSSPrimitiveValue* pval = static_cast<CFDE_CSSPrimitiveValue*>(v);
+    EXPECT_EQ(type, pval->GetPrimitiveType());
+    EXPECT_EQ(val, pval->GetFloat());
+  }
+
+  void VerifyEnum(FDE_CSSProperty prop, FDE_CSSPropertyValue val) {
+    ASSERT(decl_);
+
+    bool important;
+    CFDE_CSSValue* v = decl_->GetProperty(prop, important);
+    CFDE_CSSPrimitiveValue* pval = static_cast<CFDE_CSSPrimitiveValue*>(v);
+    EXPECT_EQ(FDE_CSSPrimitiveType::Enum, pval->GetPrimitiveType());
+    EXPECT_EQ(val, pval->GetEnum());
+  }
+
+  std::unique_ptr<CFDE_CSSStyleSheet> sheet_;
+  CFDE_CSSDeclaration* decl_;
+};
+
+TEST_F(CFDE_CSSStyleSheetTest, ParseBorder) {
+  LoadAndVerifyDecl(L"a { border: 5px; }", 4);
+  VerifyFloat(FDE_CSSProperty::BorderLeftWidth, 5.0,
+              FDE_CSSPrimitiveType::Pixels);
+  VerifyFloat(FDE_CSSProperty::BorderRightWidth, 5.0,
+              FDE_CSSPrimitiveType::Pixels);
+  VerifyFloat(FDE_CSSProperty::BorderTopWidth, 5.0,
+              FDE_CSSPrimitiveType::Pixels);
+  VerifyFloat(FDE_CSSProperty::BorderBottomWidth, 5.0,
+              FDE_CSSPrimitiveType::Pixels);
+}
+
+TEST_F(CFDE_CSSStyleSheetTest, ParseBorderFull) {
+  LoadAndVerifyDecl(L"a { border: 5px solid red; }", 4);
+  VerifyFloat(FDE_CSSProperty::BorderLeftWidth, 5.0,
+              FDE_CSSPrimitiveType::Pixels);
+  VerifyFloat(FDE_CSSProperty::BorderRightWidth, 5.0,
+              FDE_CSSPrimitiveType::Pixels);
+  VerifyFloat(FDE_CSSProperty::BorderTopWidth, 5.0,
+              FDE_CSSPrimitiveType::Pixels);
+  VerifyFloat(FDE_CSSProperty::BorderBottomWidth, 5.0,
+              FDE_CSSPrimitiveType::Pixels);
+}
+
+TEST_F(CFDE_CSSStyleSheetTest, ParseBorderLeft) {
+  LoadAndVerifyDecl(L"a { border-left: 2.5pc; }", 1);
+  VerifyFloat(FDE_CSSProperty::BorderLeftWidth, 2.5,
+              FDE_CSSPrimitiveType::Picas);
+}
+
+TEST_F(CFDE_CSSStyleSheetTest, ParseBorderLeftThick) {
+  LoadAndVerifyDecl(L"a { border-left: thick; }", 1);
+  VerifyEnum(FDE_CSSProperty::BorderLeftWidth, FDE_CSSPropertyValue::Thick);
+}
+
+TEST_F(CFDE_CSSStyleSheetTest, ParseBorderRight) {
+  LoadAndVerifyDecl(L"a { border-right: 2.5pc; }", 1);
+  VerifyFloat(FDE_CSSProperty::BorderRightWidth, 2.5,
+              FDE_CSSPrimitiveType::Picas);
+}
+
+TEST_F(CFDE_CSSStyleSheetTest, ParseBorderTop) {
+  LoadAndVerifyDecl(L"a { border-top: 2.5pc; }", 1);
+  VerifyFloat(FDE_CSSProperty::BorderTopWidth, 2.5,
+              FDE_CSSPrimitiveType::Picas);
+}
+
+TEST_F(CFDE_CSSStyleSheetTest, ParseBorderBottom) {
+  LoadAndVerifyDecl(L"a { border-bottom: 2.5pc; }", 1);
+  VerifyFloat(FDE_CSSProperty::BorderBottomWidth, 2.5,
+              FDE_CSSPrimitiveType::Picas);
+}
diff --git a/xfa/fde/css/fde_cssdeclaration.cpp b/xfa/fde/css/fde_cssdeclaration.cpp
index cf5e73e..1d9e855 100644
--- a/xfa/fde/css/fde_cssdeclaration.cpp
+++ b/xfa/fde/css/fde_cssdeclaration.cpp
@@ -642,3 +642,10 @@
   }
   return true;
 }
+
+size_t CFDE_CSSDeclaration::PropertyCountForTesting() const {
+  size_t ret = 0;
+  for (const FDE_CSSPropertyHolder* p = m_pFirstProperty; p; p = p->pNext)
+    ret++;
+  return ret;
+}
diff --git a/xfa/fde/css/fde_cssdeclaration.h b/xfa/fde/css/fde_cssdeclaration.h
index 2dbd7a0..cef98c7 100644
--- a/xfa/fde/css/fde_cssdeclaration.h
+++ b/xfa/fde/css/fde_cssdeclaration.h
@@ -58,6 +58,8 @@
                    const FX_WCHAR* pszValue,
                    int32_t iValueLen);
 
+  size_t PropertyCountForTesting() const;
+
  protected:
   bool ParseFontProperty(const FDE_CSSPropertyArgs* pArgs,
                          const FX_WCHAR* pszValue,

To view, visit this change. To unsubscribe, visit settings.

Gerrit-Project: pdfium
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id863d9cd5f13ab82626bc7b945de925253c88d43
Gerrit-Change-Number: 2180
Gerrit-PatchSet: 1
Gerrit-Owner: dsinclair <dsin...@chromium.org>

dsinclair (Gerrit)

unread,
Jan 16, 2017, 12:51:03 PM1/16/17
to dsinclair, pdfium-...@googlegroups.com, Chromium commit bot

dsinclair uploaded patch set #3 to this change.

View Change

Start CSS parser unit tests

Start adding unit tests for the css parser. Fixup memory
leaks that are exposed by the tests.

Change-Id: Id863d9cd5f13ab82626bc7b945de925253c88d43
---
M BUILD.gn
A xfa/fde/css/cfde_cssstylesheet_unittest.cpp
M xfa/fde/css/cfde_cssvalue.cpp
M xfa/fde/css/cfde_cssvalue.h
M xfa/fde/css/fde_cssdatatable.cpp
M xfa/fde/css/fde_cssdatatable.h
M xfa/fde/css/fde_cssdeclaration.cpp
M xfa/fde/css/fde_cssdeclaration.h
M xfa/fde/css/fde_cssstyleselector.cpp
M xfa/fde/css/fde_cssstylesheet.cpp
M xfa/fde/css/fde_cssstylesheet.h
11 files changed, 505 insertions(+), 377 deletions(-)

To view, visit this change. To unsubscribe, visit settings.

Gerrit-Project: pdfium
Gerrit-Branch: master
Gerrit-MessageType: newpatchset
Gerrit-Change-Id: Id863d9cd5f13ab82626bc7b945de925253c88d43
Gerrit-Change-Number: 2180
Gerrit-PatchSet: 3
Gerrit-Owner: dsinclair <dsin...@chromium.org>
Gerrit-Reviewer: dsinclair <dsin...@chromium.org>
Gerrit-CC: Chromium commit bot <commi...@chromium.org>

dsinclair (Gerrit)

unread,
Jan 16, 2017, 1:00:09 PM1/16/17
to dsinclair, Tom Sepez, Nicolás Peña, Chromium commit bot, pdfium-...@googlegroups.com

dsinclair posted comments on this change.

View Change

Patch set 3:Commit-Queue +1

PTAL.

    To view, visit this change. To unsubscribe, visit settings.

    Gerrit-Project: pdfium
    Gerrit-Branch: master
    Gerrit-MessageType: comment
    Gerrit-Change-Id: Id863d9cd5f13ab82626bc7b945de925253c88d43
    Gerrit-Change-Number: 2180
    Gerrit-PatchSet: 3
    Gerrit-Owner: dsinclair <dsin...@chromium.org>
    Gerrit-Reviewer: Nicolás Peña <n...@chromium.org>
    Gerrit-Reviewer: Tom Sepez <tse...@chromium.org>
    Gerrit-Reviewer: dsinclair <dsin...@chromium.org>
    Gerrit-CC: Chromium commit bot <commi...@chromium.org>
    Gerrit-Comment-Date: Mon, 16 Jan 2017 18:00:07 +0000
    Gerrit-HasComments: No

    Nicolás Peña (Gerrit)

    unread,
    Jan 16, 2017, 3:09:27 PM1/16/17
    to dsinclair, Tom Sepez, Chromium commit bot, pdfium-...@googlegroups.com

    Nicolás Peña posted comments on this change.

    View Change

    Patch set 3:

    (14 comments)

    To view, visit this change. To unsubscribe, visit settings.

    Gerrit-Project: pdfium
    Gerrit-Branch: master
    Gerrit-MessageType: comment
    Gerrit-Change-Id: Id863d9cd5f13ab82626bc7b945de925253c88d43
    Gerrit-Change-Number: 2180
    Gerrit-PatchSet: 3
    Gerrit-Owner: dsinclair <dsin...@chromium.org>
    Gerrit-Reviewer: Nicolás Peña <n...@chromium.org>
    Gerrit-Reviewer: Tom Sepez <tse...@chromium.org>
    Gerrit-Reviewer: dsinclair <dsin...@chromium.org>
    Gerrit-CC: Chromium commit bot <commi...@chromium.org>
    Gerrit-Comment-Date: Mon, 16 Jan 2017 20:09:25 +0000
    Gerrit-HasComments: Yes

    dsinclair (Gerrit)

    unread,
    Jan 16, 2017, 4:28:15 PM1/16/17
    to dsinclair, Nicolás Peña, Tom Sepez, Chromium commit bot, pdfium-...@googlegroups.com

    dsinclair posted comments on this change.

    View Change

    Patch set 3:

    (14 comments)

      • I imagine this 1 is because all your tests so far have only 1 rule

      • See line 25.

      • Patch Set #3, Line 18: void Retain() { ++m_nCount; }

        Is it possible to use CFX_RetainPtr or something like this, to avoid adding

      • Maybe use |const_prop_iterator| instead? Or capitalize the c.

      • Followup: this always returns true

      • If you are several doing several of CFX_RetainPtr<Class>(new Class()), mayb

      • Possibly, will leave for a potential followup.

      • Patch Set #3, Line 639: size_t CFDE_CSSDeclaration::PropertyCountForTesting() const {

      • This, or make the test class a friend. Up to you

      • Followup: Pass bImportant by pointer instead of non-const ref

      • Yup, I messed it up, it's actually fixed in the next CL where I add a test for this. The var gets removed and the size is used directly.

      • Patch Set #3, Line 28: if (IsCSSChar(wch) || wch == ':') {

        Nit: no {}'s

        Done

      • Patch Set #3, Line 235: pdfium::WrapUnique<CFDE_CSSRule>(rule.release()));

      • push_back(std::move(rule)) does not work? Otherwise, |rule| is unneeded var

      • rule is needed so we can assign rule.get() to pStyleRule. Not sure why I didn't use std::move ....

      • Patch Set #3, Line 287: pdfium::WrapUnique<CFDE_CSSRule>(rule.release()));

        ditto

        Done

      • Patch Set #3, Line 408: return pdfium::WrapUnique<CFDE_CSSSelector>(m_pNext.release());

        return std::move(m_pNext);

        Done

      • Patch Set #3, Line 477: if (iNameLen == 0) {

        Nit: not {}'s

        Done

    To view, visit this change. To unsubscribe, visit settings.

    Gerrit-Project: pdfium
    Gerrit-Branch: master
    Gerrit-MessageType: comment
    Gerrit-Change-Id: Id863d9cd5f13ab82626bc7b945de925253c88d43
    Gerrit-Change-Number: 2180
    Gerrit-PatchSet: 3
    Gerrit-Owner: dsinclair <dsin...@chromium.org>
    Gerrit-Reviewer: Nicolás Peña <n...@chromium.org>
    Gerrit-Reviewer: Tom Sepez <tse...@chromium.org>
    Gerrit-Reviewer: dsinclair <dsin...@chromium.org>
    Gerrit-CC: Chromium commit bot <commi...@chromium.org>
    Gerrit-Comment-Date: Mon, 16 Jan 2017 21:28:13 +0000
    Gerrit-HasComments: Yes

    dsinclair (Gerrit)

    unread,
    Jan 17, 2017, 4:02:04 PM1/17/17
    to dsinclair, pdfium-...@googlegroups.com, Tom Sepez, Chromium commit bot, Nicolás Peña

    dsinclair posted comments on this change.

    View Change

    Patch set 5:Commit-Queue +1

    I'd prefer to keep the tests and RetainPtr changes together as the tests are what tell me the RetainPtr code is covering all the leaks for those given tests.

    (1 comment)

    To view, visit change 2180. To unsubscribe, visit settings.

    Gerrit-Project: pdfium
    Gerrit-Branch: master
    Gerrit-MessageType: comment
    Gerrit-Change-Id: Id863d9cd5f13ab82626bc7b945de925253c88d43
    Gerrit-Change-Number: 2180
    Gerrit-PatchSet: 5
    Gerrit-Owner: dsinclair <dsin...@chromium.org>
    Gerrit-Reviewer: Nicolás Peña <n...@chromium.org>
    Gerrit-Reviewer: Tom Sepez <tse...@chromium.org>
    Gerrit-Reviewer: dsinclair <dsin...@chromium.org>
    Gerrit-CC: Chromium commit bot <commi...@chromium.org>
    Gerrit-Comment-Date: Tue, 17 Jan 2017 21:02:02 +0000
    Gerrit-HasComments: Yes

    dsinclair (Gerrit)

    unread,
    Jan 17, 2017, 4:22:34 PM1/17/17
    to dsinclair, pdfium-...@googlegroups.com, Tom Sepez, Nicolás Peña

    dsinclair posted comments on this change.

    View Change

    Patch set 6:

    (3 comments)

    To view, visit change 2180. To unsubscribe, visit settings.

    Gerrit-Project: pdfium
    Gerrit-Branch: master
    Gerrit-MessageType: comment
    Gerrit-Change-Id: Id863d9cd5f13ab82626bc7b945de925253c88d43
    Gerrit-Change-Number: 2180
    Gerrit-PatchSet: 6
    Gerrit-Owner: dsinclair <dsin...@chromium.org>
    Gerrit-Reviewer: Nicolás Peña <n...@chromium.org>
    Gerrit-Reviewer: Tom Sepez <tse...@chromium.org>
    Gerrit-Reviewer: dsinclair <dsin...@chromium.org>
    Gerrit-Comment-Date: Tue, 17 Jan 2017 21:22:33 +0000
    Gerrit-HasComments: Yes
    Reply all
    Reply to author
    Forward
    0 new messages