|
| 1 | +// Copyright 2020 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 Protocol from '../../generated/protocol.js'; |
| 6 | +import {createTarget} from '../../testing/EnvironmentHelpers.js'; |
| 7 | +import { |
| 8 | + clearAllMockConnectionResponseHandlers, |
| 9 | + clearMockConnectionResponseHandler, |
| 10 | + describeWithMockConnection, |
| 11 | + dispatchEvent, |
| 12 | + setMockConnectionResponseHandler, |
| 13 | +} from '../../testing/MockConnection.js'; |
| 14 | + |
| 15 | +import * as SDK from './sdk.js'; |
| 16 | + |
| 17 | +const noop = () => {}; |
| 18 | + |
| 19 | +const QUALITY = 80; |
| 20 | +const MAX_WIDTH = 10; |
| 21 | +const MAX_HEIGHT = 20; |
| 22 | +const EVERY_NTH_FRAME = 2; |
| 23 | + |
| 24 | +async function expectStartScreencastCalled<T>(action: () => Promise<T>| T): Promise<{ |
| 25 | + cdpRequest: Protocol.Page.StartScreencastRequest, |
| 26 | + actionResult: T, |
| 27 | +}> { |
| 28 | + clearMockConnectionResponseHandler('Page.startScreencast'); |
| 29 | + |
| 30 | + const startScreencastCalledPromise = Promise.withResolvers<Protocol.Page.StartScreencastRequest>(); |
| 31 | + setMockConnectionResponseHandler('Page.startScreencast', (request: Protocol.Page.StartScreencastRequest) => { |
| 32 | + startScreencastCalledPromise.resolve(request); |
| 33 | + return {}; |
| 34 | + }); |
| 35 | + |
| 36 | + const response = await action(); |
| 37 | + |
| 38 | + return { |
| 39 | + cdpRequest: await startScreencastCalledPromise.promise, |
| 40 | + actionResult: response, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +async function expectStopScreencastCaled<T>(action: () => Promise<T>| T): Promise<{ |
| 45 | + actionResult: T, |
| 46 | +}> { |
| 47 | + clearMockConnectionResponseHandler('Page.stopScreencast'); |
| 48 | + |
| 49 | + const stopScreencastCalledPromise = Promise.withResolvers<void>(); |
| 50 | + setMockConnectionResponseHandler('Page.stopScreencast', () => { |
| 51 | + stopScreencastCalledPromise.resolve(); |
| 52 | + return {}; |
| 53 | + }); |
| 54 | + |
| 55 | + const response = await action(); |
| 56 | + |
| 57 | + return {actionResult: response}; |
| 58 | +} |
| 59 | + |
| 60 | +async function startMockScreencast(screenCaptureModel: SDK.ScreenCaptureModel.ScreenCaptureModel, { |
| 61 | + format = Protocol.Page.StartScreencastRequestFormat.Jpeg, |
| 62 | + quality = QUALITY, |
| 63 | + maxWidth = MAX_WIDTH, |
| 64 | + maxHeight = MAX_HEIGHT, |
| 65 | + everyNthFrame = EVERY_NTH_FRAME, |
| 66 | + onFrame = noop, |
| 67 | + onVisibilityChanged = noop, |
| 68 | +}: { |
| 69 | + format?: Protocol.Page.StartScreencastRequestFormat, |
| 70 | + quality?: number, |
| 71 | + maxWidth?: number, |
| 72 | + maxHeight?: number, |
| 73 | + everyNthFrame?: number, |
| 74 | + onFrame?: () => void, |
| 75 | + onVisibilityChanged?: () => void, |
| 76 | +} = {}) { |
| 77 | + const { |
| 78 | + cdpRequest, |
| 79 | + actionResult: id, |
| 80 | + } = await expectStartScreencastCalled(() => { |
| 81 | + return screenCaptureModel.startScreencast( |
| 82 | + format, quality, maxWidth, maxHeight, everyNthFrame, onFrame, onVisibilityChanged); |
| 83 | + }); |
| 84 | + |
| 85 | + return { |
| 86 | + id, |
| 87 | + cdpRequest, |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +async function stopMockScreencast(screenCaptureModel: SDK.ScreenCaptureModel.ScreenCaptureModel, {id}: { |
| 92 | + id: number, |
| 93 | +}) { |
| 94 | + await expectStopScreencastCaled(() => { |
| 95 | + screenCaptureModel.stopScreencast(id); |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +describeWithMockConnection('ScreenCaptureModel', () => { |
| 100 | + let target: SDK.Target.Target; |
| 101 | + let screenCaptureModel: SDK.ScreenCaptureModel.ScreenCaptureModel; |
| 102 | + beforeEach(() => { |
| 103 | + target = createTarget(); |
| 104 | + const model = target.model(SDK.ScreenCaptureModel.ScreenCaptureModel); |
| 105 | + assert.exists(model); |
| 106 | + screenCaptureModel = model; |
| 107 | + }); |
| 108 | + |
| 109 | + afterEach(() => { |
| 110 | + clearAllMockConnectionResponseHandlers(); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('Screencasting', () => { |
| 114 | + describe('only one screencast operation', () => { |
| 115 | + it('startScreencast should start screen casting', async () => { |
| 116 | + const {cdpRequest} = await startMockScreencast(screenCaptureModel, { |
| 117 | + format: Protocol.Page.StartScreencastRequestFormat.Jpeg, |
| 118 | + quality: 1, |
| 119 | + maxWidth: 2, |
| 120 | + maxHeight: 3, |
| 121 | + everyNthFrame: 4, |
| 122 | + }); |
| 123 | + |
| 124 | + assert.deepEqual(cdpRequest, { |
| 125 | + format: Protocol.Page.StartScreencastRequestFormat.Jpeg, |
| 126 | + quality: 1, |
| 127 | + maxWidth: 2, |
| 128 | + maxHeight: 3, |
| 129 | + everyNthFrame: 4 |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it('stopScreencast should stop screen casting', async () => { |
| 134 | + const {id} = await startMockScreencast(screenCaptureModel); |
| 135 | + |
| 136 | + await stopMockScreencast(screenCaptureModel, {id}); |
| 137 | + }); |
| 138 | + |
| 139 | + it('stopScreencast throws an error for trying to stop screencast when there are no screencast operations in progress', |
| 140 | + async () => { |
| 141 | + try { |
| 142 | + await stopMockScreencast(screenCaptureModel, {id: 42}); |
| 143 | + assert.fail('Expected `stopScreencast` to throw'); |
| 144 | + } catch (err) { |
| 145 | + assert.strictEqual(err.message, 'There is no screencast operation to stop.'); |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + it('stopScreencast throws an error for trying to stop a different screencast than what is being in progress right now', |
| 150 | + async () => { |
| 151 | + await startMockScreencast(screenCaptureModel); |
| 152 | + try { |
| 153 | + await stopMockScreencast(screenCaptureModel, {id: 42}); |
| 154 | + assert.fail('Expected `stopScreencast` to throw'); |
| 155 | + } catch (err) { |
| 156 | + assert.strictEqual( |
| 157 | + err.message, 'Trying to stop a screencast operation that is not being served right now.'); |
| 158 | + } |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + describe('multiple screencast operations', () => { |
| 163 | + beforeEach(() => { |
| 164 | + setMockConnectionResponseHandler('Page.stopScreencast', () => ({})); |
| 165 | + }); |
| 166 | + |
| 167 | + it('second call to startScreencast stops the ongoing screencasting', async () => { |
| 168 | + await startMockScreencast(screenCaptureModel); |
| 169 | + |
| 170 | + // Stop screencast is called for the initial call before starting a new screencast. |
| 171 | + await expectStopScreencastCaled(async () => { |
| 172 | + await startMockScreencast(screenCaptureModel); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + it('only the last operation receives the callbacks', async () => { |
| 177 | + const initialFrameCallback = sinon.stub(); |
| 178 | + const initialVisibilityChangeCallback = sinon.stub(); |
| 179 | + const lastFrameCallback = sinon.stub(); |
| 180 | + const lastVisibilityChangeCallback = sinon.stub(); |
| 181 | + |
| 182 | + await startMockScreencast( |
| 183 | + screenCaptureModel, {onFrame: initialFrameCallback, onVisibilityChanged: initialVisibilityChangeCallback}); |
| 184 | + await startMockScreencast( |
| 185 | + screenCaptureModel, {onFrame: lastFrameCallback, onVisibilityChanged: lastVisibilityChangeCallback}); |
| 186 | + dispatchEvent(target, 'Page.screencastFrame', {}); |
| 187 | + dispatchEvent(target, 'Page.screencastVisibilityChanged', {}); |
| 188 | + |
| 189 | + sinon.assert.notCalled(initialFrameCallback); |
| 190 | + sinon.assert.notCalled(initialVisibilityChangeCallback); |
| 191 | + sinon.assert.calledOnce(lastFrameCallback); |
| 192 | + sinon.assert.calledOnce(lastVisibilityChangeCallback); |
| 193 | + }); |
| 194 | + |
| 195 | + it('after the last operation is stopped, the previous one continues to receive callbacks', async () => { |
| 196 | + const initialFrameCallback = sinon.stub(); |
| 197 | + const initialVisibilityChangeCallback = sinon.stub(); |
| 198 | + const lastFrameCallback = sinon.stub(); |
| 199 | + const lastVisibilityChangeCallback = sinon.stub(); |
| 200 | + |
| 201 | + await startMockScreencast( |
| 202 | + screenCaptureModel, {onFrame: initialFrameCallback, onVisibilityChanged: initialVisibilityChangeCallback}); |
| 203 | + const {id} = await startMockScreencast( |
| 204 | + screenCaptureModel, {onFrame: lastFrameCallback, onVisibilityChanged: lastVisibilityChangeCallback}); |
| 205 | + await stopMockScreencast(screenCaptureModel, {id}); |
| 206 | + dispatchEvent(target, 'Page.screencastFrame', {}); |
| 207 | + dispatchEvent(target, 'Page.screencastVisibilityChanged', {}); |
| 208 | + |
| 209 | + sinon.assert.calledOnce(initialFrameCallback); |
| 210 | + sinon.assert.calledOnce(initialVisibilityChangeCallback); |
| 211 | + sinon.assert.notCalled(lastFrameCallback); |
| 212 | + sinon.assert.notCalled(lastVisibilityChangeCallback); |
| 213 | + }); |
| 214 | + }); |
| 215 | + }); |
| 216 | +}); |
0 commit comments