summaryrefslogtreecommitdiff
path: root/xfa/fde/css/cfde_cssstylesheet_unittest.cpp
blob: 1edc4b5f510aeacda089759bbd357a701168639b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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);
}