summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/inspector-protocol/layers/get-layers.html
blob: 37928cb50f889be807f93041c237f8e65ad0841d (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
152
153
154
155
156
<html>
<head>
<script type="text/javascript" src="../../http/tests/inspector-protocol/inspector-protocol-test.js"></script>
<script type="text/javascript" src="layer-protocol-test.js"></script>
<script type="text/javascript">

function addCompositedLayer()
{
    var element = document.createElement("div");
    element.className = "composited";
    element.id = "last-element";
    document.body.appendChild(element);
};

function test()
{
    var documentNode;
    var initialLayers;
    var modifiedLayers;

    InspectorTest.enableLayerTreeAgent(gotInitialLayerTree);

    function gotInitialLayerTree(layers)
    {
        initialLayers = layers;
        InspectorTest.setLayerTreeChangeCallback(gotModifiedLayerTree);

        InspectorTest.sendCommand("Runtime.evaluate", {"expression": "addCompositedLayer()"});
    };

    function gotModifiedLayerTree(layers)
    {
        modifiedLayers = layers;

        var mutations = layerMutations(initialLayers, modifiedLayers);
        var newLayer = mutations.additions[0];

        InspectorTest.sendCommand("DOM.pushNodesByBackendIdsToFrontend", {"backendNodeIds": [newLayer.backendNodeId]}, InspectorTest.wrapCallback(gotNodeId));
    };

    function gotNodeId(result)
    {
        InspectorTest.sendCommand("DOM.getAttributes", {"nodeId": result.nodeIds[0]}, InspectorTest.wrapCallback(gotNodeAttributes));
    }

    function gotNodeAttributes(result)
    {
        var attributes = attributesDictionaryFromArray(result.attributes);
        if (attributes.id !== "last-element")
            InspectorTest.log("FAIL: Did not obtain the expected element for the last inserted layer.");

        dumpLayers(initialLayers);
        dumpLayers(modifiedLayers);
        InspectorTest.log("DONE!");
        InspectorTest.completeTest();
    };

    function layerMutations(oldLayers, newLayers)
    {
        function layerIdMap(layer) {
            return layer.layerId;
        }

        var oldLayerIds = oldLayers.map(layerIdMap);
        var newLayerIds = newLayers.map(layerIdMap);

        return {
            additions: newLayers.filter(function (layer) {
                return (oldLayerIds.indexOf(layer.layerId) === -1);
            }),
            removals: oldLayers.filter(function (layer) {
                return (newLayerIds.indexOf(layer.layerId) === -1);
            })
        };
    };

    function attributesDictionaryFromArray(attributes)
    {
        var dictionary = {}
        for (var i = 0, count = attributes.length; i < count; i += 2) {
            dictionary[attributes[i]] = attributes[i + 1];
        }
        return dictionary;
    };

    function dumpLayers(layers)
    {
        // Keep "internal" layers out for better stability.
        layers = layers.filter(function(layer) { return !!layer.backendNodeId; });
        function replacer(key, value)
        {

            if (["layerId", "parentLayerId", "backendNodeId", "paintCount"].indexOf(key) >= 0)
                return typeof(value);

            // some values differ based on port, but the ones we most
            // care about will always be less or equal 100.
            if ((key === "width" || key === "height") && value > 100) 
                return typeof(value);

            return value;
        };

        InspectorTest.log("\n" + JSON.stringify(layers, replacer, "    "));
    };
};

window.addEventListener("DOMContentLoaded", function () {
    runTest();
}, false);

</script>
<style type="text/css">
      
    div {
        position: absolute;
        top: 0;
        left: 0;
    }
      
    .regular {
        width: 100px;
        height: 100px;
        background-color: black;
    }

    .composited {
        top: 25px;
        left: 25px;
        width: 50px;
        height: 50px;
        background-color: blue;
        transform: translateZ(0);
    }
      
    .offset {
        left: 200px;
        transform: translateZ(0);
    }

</style>
</head>
<body>

    <div class="regular"></div>

    <div class="composited">
        <div class="composited"></div>
    </div>

    <div class="regular offset">
        <div class="composited"></div>
    </div>

</body>
</html>