|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
| 5 | +import type * as Common from '../../core/common/common.js'; |
5 | 6 | import * as SDK from '../../core/sdk/sdk.js'; |
6 | 7 | import {renderElementIntoDOM} from '../../testing/DOMHelpers.js'; |
7 | | -import {createTarget} from '../../testing/EnvironmentHelpers.js'; |
| 8 | +import {createTarget, stubNoopSettings} from '../../testing/EnvironmentHelpers.js'; |
8 | 9 | import {expectCall} from '../../testing/ExpectStubCall.js'; |
9 | 10 | import {describeWithMockConnection} from '../../testing/MockConnection.js'; |
10 | 11 | import {createViewFunctionStub} from '../../testing/ViewFunctionHelpers.js'; |
@@ -35,4 +36,117 @@ describeWithMockConnection('PerformanceMonitor', () => { |
35 | 36 | await expectCall(getMetrics, {fakeFn: () => Promise.resolve({metrics: [{name: 'LayoutCount', value: 84}]})}); |
36 | 37 | assert.isNotEmpty((await view.nextInput).metrics); |
37 | 38 | }); |
| 39 | + |
| 40 | + it('starts polling when shown and stops when hidden', async () => { |
| 41 | + const getMetrics = sinon.stub(target.performanceAgent(), 'invoke_getMetrics'); |
| 42 | + const view = createViewFunctionStub(PerformanceMonitor.PerformanceMonitor.PerformanceMonitorImpl); |
| 43 | + performanceMonitor = new PerformanceMonitor.PerformanceMonitor.PerformanceMonitorImpl(10, view); |
| 44 | + const model = target.model(SDK.PerformanceMetricsModel.PerformanceMetricsModel); |
| 45 | + assert.exists(model); |
| 46 | + const modelEnableSpy = sinon.spy(model, 'enable'); |
| 47 | + const modelDisableSpy = sinon.spy(model, 'disable'); |
| 48 | + |
| 49 | + performanceMonitor.markAsRoot(); |
| 50 | + renderElementIntoDOM(performanceMonitor); |
| 51 | + |
| 52 | + // Starts polling when shown. |
| 53 | + await expectCall(getMetrics, {fakeFn: () => Promise.resolve({metrics: [], getError: () => undefined})}); |
| 54 | + sinon.assert.calledOnce(modelEnableSpy); |
| 55 | + |
| 56 | + performanceMonitor.detach(); |
| 57 | + |
| 58 | + // Stops polling when hidden. |
| 59 | + getMetrics.resetHistory(); |
| 60 | + await new Promise(resolve => setTimeout(resolve, 20)); |
| 61 | + sinon.assert.notCalled(getMetrics); |
| 62 | + sinon.assert.calledOnce(modelDisableSpy); |
| 63 | + |
| 64 | + renderElementIntoDOM(performanceMonitor); |
| 65 | + |
| 66 | + // Resumes polling when shown again. |
| 67 | + await expectCall(getMetrics, {fakeFn: () => Promise.resolve({metrics: [], getError: () => undefined})}); |
| 68 | + sinon.assert.calledTwice(modelEnableSpy); |
| 69 | + }); |
| 70 | + |
| 71 | + it('updates chart visibility and height', async () => { |
| 72 | + const view = createViewFunctionStub(PerformanceMonitor.PerformanceMonitor.PerformanceMonitorImpl); |
| 73 | + performanceMonitor = new PerformanceMonitor.PerformanceMonitor.PerformanceMonitorImpl(0, view); |
| 74 | + renderElementIntoDOM(performanceMonitor); |
| 75 | + |
| 76 | + const {onMetricChanged, chartsInfo, height: initialHeight} = await view.nextInput; |
| 77 | + // From recalcChartHeight(): height = this.scaleHeight when no active charts. |
| 78 | + // scaleHeight is 16. |
| 79 | + assert.strictEqual(initialHeight, Math.ceil(16 * window.devicePixelRatio)); |
| 80 | + |
| 81 | + const chartToActivate = chartsInfo[0].metrics[0].name; |
| 82 | + onMetricChanged(chartToActivate, true); |
| 83 | + |
| 84 | + const {height: heightAfterActivation} = await view.nextInput; |
| 85 | + // graphHeight is 90. |
| 86 | + assert.strictEqual(heightAfterActivation, Math.ceil((16 + 90) * window.devicePixelRatio)); |
| 87 | + |
| 88 | + onMetricChanged(chartToActivate, false); |
| 89 | + const {height: heightAfterDeactivation} = await view.nextInput; |
| 90 | + assert.strictEqual(heightAfterDeactivation, initialHeight); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe('ControlPane', () => { |
| 95 | + const chartsInfo: PerformanceMonitor.PerformanceMonitor.ChartInfo[] = [ |
| 96 | + { |
| 97 | + title: 'Chart1' as unknown as Common.UIString.LocalizedString, |
| 98 | + metrics: [{name: 'Metric1', color: 'red'}], |
| 99 | + }, |
| 100 | + { |
| 101 | + title: 'Chart2' as unknown as Common.UIString.LocalizedString, |
| 102 | + metrics: [{name: 'Metric2', color: 'blue'}], |
| 103 | + }, |
| 104 | + ]; |
| 105 | + |
| 106 | + beforeEach(() => { |
| 107 | + stubNoopSettings(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('renders indicators', async () => { |
| 111 | + const view = createViewFunctionStub(PerformanceMonitor.PerformanceMonitor.ControlPane); |
| 112 | + const controlPane = new PerformanceMonitor.PerformanceMonitor.ControlPane(document.createElement('div'), view); |
| 113 | + renderElementIntoDOM(controlPane); |
| 114 | + controlPane.chartsInfo = chartsInfo; |
| 115 | + const {chartsInfo: renderedChartsInfo} = await view.nextInput; |
| 116 | + assert.deepEqual(renderedChartsInfo.map(c => c.title), ['Chart1', 'Chart2']); |
| 117 | + }); |
| 118 | + |
| 119 | + it('toggles charts', async () => { |
| 120 | + const view = createViewFunctionStub(PerformanceMonitor.PerformanceMonitor.ControlPane); |
| 121 | + const controlPane = new PerformanceMonitor.PerformanceMonitor.ControlPane(document.createElement('div'), view); |
| 122 | + renderElementIntoDOM(controlPane); |
| 123 | + |
| 124 | + const onMetricChanged = sinon.spy(); |
| 125 | + controlPane.onMetricChanged = onMetricChanged; |
| 126 | + |
| 127 | + controlPane.chartsInfo = chartsInfo; |
| 128 | + const {onCheckboxChange} = await view.nextInput; |
| 129 | + |
| 130 | + const event = {target: {checked: true}} as unknown as Event; |
| 131 | + onCheckboxChange('Metric1', event); |
| 132 | + |
| 133 | + assert.isTrue(onMetricChanged.calledOnceWith('Metric1', true)); |
| 134 | + |
| 135 | + const event2 = {target: {checked: false}} as unknown as Event; |
| 136 | + onCheckboxChange('Metric1', event2); |
| 137 | + sinon.assert.calledTwice(onMetricChanged); |
| 138 | + sinon.assert.calledWith(onMetricChanged.secondCall, 'Metric1', false); |
| 139 | + }); |
| 140 | + |
| 141 | + it('updates metric values', async () => { |
| 142 | + const view = createViewFunctionStub(PerformanceMonitor.PerformanceMonitor.ControlPane); |
| 143 | + const controlPane = new PerformanceMonitor.PerformanceMonitor.ControlPane(document.createElement('div'), view); |
| 144 | + renderElementIntoDOM(controlPane); |
| 145 | + controlPane.chartsInfo = chartsInfo; |
| 146 | + await view.nextInput; |
| 147 | + |
| 148 | + controlPane.metrics = new Map([['Metric1', 42]]); |
| 149 | + const {metricValues} = await view.nextInput; |
| 150 | + assert.strictEqual(metricValues.get('Metric1'), 42); |
| 151 | + }); |
38 | 152 | }); |
0 commit comments