-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create
and navigate.browsingContext
#55
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b32c8f4
`browsingContext.navigate`
sadym-chromium 0d040a0
Pass `EVALUATOR_SCRIPT` as a parameter for testibility.
sadym-chromium b8adffc
Unit tests + avoid race condition while waiting `Context` creation.
sadym-chromium 5a92cd2
minor change
sadym-chromium 74244e3
`browsingContext.navigate` -> `PROTO.browsingContext.navigate`
sadym-chromium 953a9ab
Update src/bidiMapper/domains/context/browsingContextProcessor.ts
sadym-chromium 8f83b58
`PROTO.browsingContext.navigate` -> `browsingContext.navigate`
sadym-chromium 3fd095b
`browsingContext.create` -> `PROTO.browsingContext.create`
sadym-chromium e515527
PR fixes:
sadym-chromium File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
src/bidiMapper/domains/context/browsingContextProcessor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/** | ||
* Copyright 2021 Google LLC. | ||
* Copyright (c) Microsoft Corporation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { StubTransport } from '../../../tests/stubTransport.spec'; | ||
|
||
import * as chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
chai.use(chaiAsPromised); | ||
|
||
import * as sinon from 'sinon'; | ||
|
||
import { BrowsingContextProcessor } from './browsingContextProcessor'; | ||
import { CdpConnection, CdpClient } from '../../../cdp'; | ||
import { Context } from './context'; | ||
import { BrowsingContext } from '../../bidiProtocolTypes'; | ||
|
||
describe('BrowsingContextProcessor', function () { | ||
let mockCdpServer: StubTransport; | ||
let browsingContextProcessor: BrowsingContextProcessor; | ||
let cdpConnection: CdpConnection; | ||
|
||
const EVALUATOR_SCRIPT = 'EVALUATOR_SCRIPT'; | ||
const NEW_CONTEXT_ID = 'NEW_CONTEXT_ID'; | ||
const TARGET_ATTACHED_TO_TARGET_EVENT = { | ||
method: 'Target.attachedToTarget', | ||
params: { | ||
sessionId: '_ANY_VALUE_', | ||
targetInfo: { | ||
targetId: NEW_CONTEXT_ID, | ||
type: 'page', | ||
title: '', | ||
url: '', | ||
attached: true, | ||
canAccessOpener: false, | ||
browserContextId: '_ANY_ANOTHER_VALUE_', | ||
}, | ||
waitingForDebugger: false, | ||
}, | ||
}; | ||
|
||
beforeEach(async function () { | ||
mockCdpServer = new StubTransport(); | ||
cdpConnection = new CdpConnection(mockCdpServer); | ||
browsingContextProcessor = new BrowsingContextProcessor( | ||
cdpConnection, | ||
'SELF_TARGET_ID', | ||
sinon.fake as unknown as (t: Context) => Promise<void>, | ||
sinon.fake as unknown as (t: Context) => Promise<void>, | ||
EVALUATOR_SCRIPT | ||
); | ||
|
||
// Actual `Context.create` logic involves several CDP calls, so mock it to avoid all the simulations. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a helpful comment, thanks! |
||
Context.create = sinon.fake( | ||
async (_1: string, _2: CdpClient, _3: string) => { | ||
return sinon.createStubInstance(Context) as unknown as Context; | ||
} | ||
); | ||
}); | ||
|
||
describe('handle events', async function () { | ||
it('`Target.attachedToTarget` creates Context', async function () { | ||
sinon.assert.notCalled(Context.create as sinon.SinonSpy); | ||
await mockCdpServer.emulateIncomingMessage( | ||
TARGET_ATTACHED_TO_TARGET_EVENT | ||
); | ||
sinon.assert.calledOnceWithExactly( | ||
Context.create as sinon.SinonSpy, | ||
NEW_CONTEXT_ID, | ||
sinon.match.any, | ||
EVALUATOR_SCRIPT | ||
); | ||
}); | ||
}); | ||
|
||
describe('handle `process_PROTO_browsingContext_create`', async function () { | ||
const BROWSING_CONTEXT_CREATE_COMMAND: BrowsingContext.PROTO.BrowsingContextCreateCommand = | ||
{ | ||
method: 'PROTO.browsingContext.create', | ||
params: {}, | ||
}; | ||
|
||
const EXPECTED_TARGET_CREATE_TARGET_CALL = { | ||
id: 0, | ||
method: 'Target.createTarget', | ||
params: { | ||
url: 'about:blank', | ||
newWindow: false, | ||
}, | ||
}; | ||
|
||
const TARGET_CREATE_TARGET_RESPONSE = { | ||
id: 0, | ||
result: { | ||
targetId: NEW_CONTEXT_ID, | ||
}, | ||
}; | ||
|
||
it('Target.attachedToTarget before command finished', async function () { | ||
const createResultPromise = | ||
browsingContextProcessor.process_PROTO_browsingContext_create( | ||
BROWSING_CONTEXT_CREATE_COMMAND | ||
); | ||
|
||
sinon.assert.calledOnceWithExactly( | ||
mockCdpServer.sendMessage, | ||
JSON.stringify(EXPECTED_TARGET_CREATE_TARGET_CALL) | ||
); | ||
|
||
await mockCdpServer.emulateIncomingMessage( | ||
TARGET_ATTACHED_TO_TARGET_EVENT | ||
); | ||
|
||
await mockCdpServer.emulateIncomingMessage(TARGET_CREATE_TARGET_RESPONSE); | ||
|
||
await createResultPromise; | ||
}); | ||
|
||
it('Target.attachedToTarget after command finished', async function () { | ||
const createResultPromise = | ||
browsingContextProcessor.process_PROTO_browsingContext_create( | ||
BROWSING_CONTEXT_CREATE_COMMAND | ||
); | ||
|
||
sinon.assert.calledOnceWithExactly( | ||
mockCdpServer.sendMessage, | ||
JSON.stringify(EXPECTED_TARGET_CREATE_TARGET_CALL) | ||
); | ||
|
||
await mockCdpServer.emulateIncomingMessage(TARGET_CREATE_TARGET_RESPONSE); | ||
|
||
await mockCdpServer.emulateIncomingMessage( | ||
TARGET_ATTACHED_TO_TARGET_EVENT | ||
); | ||
|
||
await createResultPromise; | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you wrap the
sinon.fake as unknown as (t: Context) => Promise<void>
code into atypedFake
function similar totypedSpy
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, shouldn't this call
fake
(e.g.fake()
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed