|
| 1 | +// Copyright 2023 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as SDK from '../../core/sdk/sdk.js'; |
| 6 | +import type * as Protocol from '../../generated/protocol.js'; |
| 7 | +import {createTarget, getGetHostConfigStub, stubNoopSettings} from '../../testing/EnvironmentHelpers.js'; |
| 8 | +import {describeWithMockConnection} from '../../testing/MockConnection.js'; |
| 9 | +import * as UI from '../../ui/legacy/legacy.js'; |
| 10 | + |
| 11 | +import * as Elements from './elements.js'; |
| 12 | + |
| 13 | +function createNode(target: SDK.Target.Target, {nodeId}: {nodeId: Protocol.DOM.NodeId}): SDK.DOMModel.DOMNode { |
| 14 | + const domModel = target.model(SDK.DOMModel.DOMModel); |
| 15 | + assert.exists(domModel); |
| 16 | + |
| 17 | + return SDK.DOMModel.DOMNode.create(domModel, null, false, { |
| 18 | + nodeId, |
| 19 | + backendNodeId: 2 as Protocol.DOM.BackendNodeId, |
| 20 | + nodeType: Node.ELEMENT_NODE, |
| 21 | + nodeName: 'div', |
| 22 | + localName: 'div', |
| 23 | + nodeValue: '', |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +describeWithMockConnection('ComputedStyleModel', () => { |
| 28 | + let target: SDK.Target.Target; |
| 29 | + let computedStyleModel: Elements.ComputedStyleModel.ComputedStyleModel; |
| 30 | + let trackComputedStyleUpdatesForNodeSpy: sinon.SinonSpy; |
| 31 | + |
| 32 | + async function waitForTrackComputedStyleUpdatesForNodeCall(): Promise<void> { |
| 33 | + if (trackComputedStyleUpdatesForNodeSpy.getCalls().length > 0) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + await new Promise<void>(resolve => { |
| 38 | + requestAnimationFrame(async () => { |
| 39 | + await waitForTrackComputedStyleUpdatesForNodeCall(); |
| 40 | + resolve(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + stubNoopSettings(); |
| 47 | + target = createTarget(); |
| 48 | + const cssModel = target.model(SDK.CSSModel.CSSModel); |
| 49 | + |
| 50 | + UI.Context.Context.instance().setFlavor(Elements.StylesSidebarPane.StylesSidebarPane, null); |
| 51 | + UI.Context.Context.instance().setFlavor(Elements.ComputedStyleWidget.ComputedStyleWidget, null); |
| 52 | + UI.Context.Context.instance().setFlavor(SDK.DOMModel.DOMNode, null); |
| 53 | + |
| 54 | + sinon.stub(Elements.ComputedStyleModel.ComputedStyleModel.prototype, 'cssModel').returns(cssModel); |
| 55 | + trackComputedStyleUpdatesForNodeSpy = |
| 56 | + sinon.spy(SDK.CSSModel.CSSModel.prototype, 'trackComputedStyleUpdatesForNode'); |
| 57 | + computedStyleModel = new Elements.ComputedStyleModel.ComputedStyleModel(); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + computedStyleModel.dispose(); |
| 62 | + trackComputedStyleUpdatesForNodeSpy.restore(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should track computed style updates when computed widget is shown', async () => { |
| 66 | + UI.Context.Context.instance().setFlavor( |
| 67 | + SDK.DOMModel.DOMNode, createNode(target, {nodeId: 1 as Protocol.DOM.NodeId})); |
| 68 | + UI.Context.Context.instance().setFlavor( |
| 69 | + Elements.ComputedStyleWidget.ComputedStyleWidget, |
| 70 | + sinon.createStubInstance(Elements.ComputedStyleWidget.ComputedStyleWidget)); |
| 71 | + |
| 72 | + await waitForTrackComputedStyleUpdatesForNodeCall(); |
| 73 | + |
| 74 | + sinon.assert.calledWith(trackComputedStyleUpdatesForNodeSpy, 1); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should track computed style updates when styles tab is shown and DevToolsAnimationStylesInStylesTab is enabled', |
| 78 | + async () => { |
| 79 | + const hostConfigStub = getGetHostConfigStub({ |
| 80 | + devToolsAnimationStylesInStylesTab: { |
| 81 | + enabled: true, |
| 82 | + }, |
| 83 | + }); |
| 84 | + UI.Context.Context.instance().setFlavor( |
| 85 | + SDK.DOMModel.DOMNode, createNode(target, {nodeId: 1 as Protocol.DOM.NodeId})); |
| 86 | + UI.Context.Context.instance().setFlavor( |
| 87 | + Elements.StylesSidebarPane.StylesSidebarPane, |
| 88 | + sinon.createStubInstance(Elements.StylesSidebarPane.StylesSidebarPane)); |
| 89 | + |
| 90 | + await waitForTrackComputedStyleUpdatesForNodeCall(); |
| 91 | + |
| 92 | + sinon.assert.calledWith(trackComputedStyleUpdatesForNodeSpy, 1); |
| 93 | + hostConfigStub.restore(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should track computed style updates when the node is changed', async () => { |
| 97 | + UI.Context.Context.instance().setFlavor( |
| 98 | + SDK.DOMModel.DOMNode, createNode(target, {nodeId: 1 as Protocol.DOM.NodeId})); |
| 99 | + UI.Context.Context.instance().setFlavor( |
| 100 | + Elements.ComputedStyleWidget.ComputedStyleWidget, |
| 101 | + sinon.createStubInstance(Elements.ComputedStyleWidget.ComputedStyleWidget)); |
| 102 | + |
| 103 | + await waitForTrackComputedStyleUpdatesForNodeCall(); |
| 104 | + sinon.assert.calledWith(trackComputedStyleUpdatesForNodeSpy, 1); |
| 105 | + trackComputedStyleUpdatesForNodeSpy.resetHistory(); |
| 106 | + |
| 107 | + UI.Context.Context.instance().setFlavor( |
| 108 | + SDK.DOMModel.DOMNode, createNode(target, {nodeId: 2 as Protocol.DOM.NodeId})); |
| 109 | + await waitForTrackComputedStyleUpdatesForNodeCall(); |
| 110 | + |
| 111 | + sinon.assert.calledWith(trackComputedStyleUpdatesForNodeSpy, 2); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should stop tracking when computed widget is hidden', async () => { |
| 115 | + UI.Context.Context.instance().setFlavor( |
| 116 | + SDK.DOMModel.DOMNode, createNode(target, {nodeId: 1 as Protocol.DOM.NodeId})); |
| 117 | + UI.Context.Context.instance().setFlavor( |
| 118 | + Elements.ComputedStyleWidget.ComputedStyleWidget, |
| 119 | + sinon.createStubInstance(Elements.ComputedStyleWidget.ComputedStyleWidget)); |
| 120 | + |
| 121 | + await waitForTrackComputedStyleUpdatesForNodeCall(); |
| 122 | + sinon.assert.calledWith(trackComputedStyleUpdatesForNodeSpy, 1); |
| 123 | + trackComputedStyleUpdatesForNodeSpy.resetHistory(); |
| 124 | + |
| 125 | + UI.Context.Context.instance().setFlavor(Elements.ComputedStyleWidget.ComputedStyleWidget, null); |
| 126 | + await waitForTrackComputedStyleUpdatesForNodeCall(); |
| 127 | + |
| 128 | + sinon.assert.calledWith(trackComputedStyleUpdatesForNodeSpy, undefined); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should stop tracking when computed widget is hidden and styles tab is shown but the flag is not enabled', |
| 132 | + async () => { |
| 133 | + const hostConfigStub = getGetHostConfigStub({ |
| 134 | + devToolsAnimationStylesInStylesTab: { |
| 135 | + enabled: false, |
| 136 | + }, |
| 137 | + }); |
| 138 | + UI.Context.Context.instance().setFlavor( |
| 139 | + SDK.DOMModel.DOMNode, createNode(target, {nodeId: 1 as Protocol.DOM.NodeId})); |
| 140 | + UI.Context.Context.instance().setFlavor( |
| 141 | + Elements.ComputedStyleWidget.ComputedStyleWidget, |
| 142 | + sinon.createStubInstance(Elements.ComputedStyleWidget.ComputedStyleWidget)); |
| 143 | + |
| 144 | + await waitForTrackComputedStyleUpdatesForNodeCall(); |
| 145 | + sinon.assert.calledWith(trackComputedStyleUpdatesForNodeSpy, 1); |
| 146 | + trackComputedStyleUpdatesForNodeSpy.resetHistory(); |
| 147 | + |
| 148 | + UI.Context.Context.instance().setFlavor(Elements.ComputedStyleWidget.ComputedStyleWidget, null); |
| 149 | + UI.Context.Context.instance().setFlavor( |
| 150 | + Elements.StylesSidebarPane.StylesSidebarPane, |
| 151 | + sinon.createStubInstance(Elements.StylesSidebarPane.StylesSidebarPane)); |
| 152 | + await waitForTrackComputedStyleUpdatesForNodeCall(); |
| 153 | + |
| 154 | + sinon.assert.calledWith(trackComputedStyleUpdatesForNodeSpy, undefined); |
| 155 | + hostConfigStub.restore(); |
| 156 | + }); |
| 157 | +}); |
0 commit comments