summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/core/dom/TreeScopeStyleSheetCollectionTest.cpp
blob: c0cafd808471d64decacfe290a9b35f7efbcdc01 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2015 The Chromium 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/dom/TreeScopeStyleSheetCollection.h"

#include "core/css/CSSStyleSheet.h"
#include "core/css/StyleSheetContents.h"
#include "core/css/parser/CSSParserMode.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace blink {

class TreeScopeStyleSheetCollectionTest : public testing::Test {
protected:
    using SheetVector = WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet>>;
    using ContentsVector = WillBeHeapVector<RawPtrWillBeMember<StyleSheetContents>>;

    enum UpdateType {
        Reconstruct = TreeScopeStyleSheetCollection::Reconstruct,
        Reset = TreeScopeStyleSheetCollection::Reset,
        Additive = TreeScopeStyleSheetCollection::Additive
    };

    static PassRefPtrWillBeRawPtr<CSSStyleSheet> createSheet()
    {
        return CSSStyleSheet::create(StyleSheetContents::create(CSSParserContext(HTMLStandardMode, nullptr)));
    }

    static void compareStyleSheets(const SheetVector& oldStyleSheets, const SheetVector& newStyleSheets, const ContentsVector& expAddedSheets, UpdateType testUpdateType)
    {
        ContentsVector addedSheets;
        UpdateType updateType = static_cast<UpdateType>(TreeScopeStyleSheetCollection::compareStyleSheets(oldStyleSheets, newStyleSheets, addedSheets));
        EXPECT_EQ(testUpdateType, updateType);
        EXPECT_EQ(expAddedSheets.size(), addedSheets.size());
        if (expAddedSheets.size() == addedSheets.size()) {
            for (unsigned i = 0; i < addedSheets.size(); i++)
                EXPECT_EQ(expAddedSheets.at(i), addedSheets.at(i));
        }
    }
};

TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsAppend)
{
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet1 = createSheet();
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet2 = createSheet();

    ContentsVector added;
    SheetVector previous;
    SheetVector current;

    previous.append(sheet1);

    current.append(sheet1);
    current.append(sheet2);

    added.append(sheet2->contents());

    compareStyleSheets(previous, current, added, Additive);
}

TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsPrepend)
{
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet1 = createSheet();
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet2 = createSheet();

    ContentsVector added;
    SheetVector previous;
    SheetVector current;

    previous.append(sheet2);

    current.append(sheet1);
    current.append(sheet2);

    added.append(sheet1->contents());

    compareStyleSheets(previous, current, added, Reconstruct);
}

TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsInsert)
{
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet1 = createSheet();
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet2 = createSheet();
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet3 = createSheet();

    ContentsVector added;
    SheetVector previous;
    SheetVector current;

    previous.append(sheet1);
    previous.append(sheet3);

    current.append(sheet1);
    current.append(sheet2);
    current.append(sheet3);

    added.append(sheet2->contents());

    compareStyleSheets(previous, current, added, Reconstruct);
}

TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsRemove)
{
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet1 = createSheet();
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet2 = createSheet();
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet3 = createSheet();

    ContentsVector added;
    SheetVector previous;
    SheetVector current;

    previous.append(sheet1);
    previous.append(sheet2);
    previous.append(sheet3);

    current.append(sheet1);
    current.append(sheet3);

    added.append(sheet2->contents());

    // This is really the same as Insert. TreeScopeStyleSheetCollection::compareStyleSheets
    // will assert if you pass an array that is longer as the first parameter.
    compareStyleSheets(current, previous, added, Reconstruct);
}

TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsInsertRemove)
{
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet1 = createSheet();
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet2 = createSheet();
    RefPtrWillBeRawPtr<CSSStyleSheet> sheet3 = createSheet();

    ContentsVector added;
    SheetVector previous;
    SheetVector current;

    previous.append(sheet1);
    previous.append(sheet2);

    current.append(sheet2);
    current.append(sheet3);

    // TODO(rune@opera.com): This is clearly wrong. We add sheet3 and remove sheet1 and
    // compareStyleSheets returns sheet2 and sheet3 as added (crbug/475858).
    added.append(sheet2->contents());
    added.append(sheet3->contents());

    compareStyleSheets(previous, current, added, Reconstruct);
}

} // namespace blink