Skip to content

Commit

Permalink
fix: waitForNavigation in OOPIFs (#8117)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Mar 9, 2022
1 parent d4b5a79 commit 34775e5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/common/FrameManager.ts
Expand Up @@ -53,6 +53,7 @@ export const FrameManagerEmittedEvents = {
FrameAttached: Symbol('FrameManager.FrameAttached'),
FrameNavigated: Symbol('FrameManager.FrameNavigated'),
FrameDetached: Symbol('FrameManager.FrameDetached'),
FrameSwapped: Symbol('FrameManager.FrameSwapped'),
LifecycleEvent: Symbol('FrameManager.LifecycleEvent'),
FrameNavigatedWithinDocument: Symbol(
'FrameManager.FrameNavigatedWithinDocument'
Expand Down Expand Up @@ -422,6 +423,8 @@ export class FrameManager extends EventEmitter {
// an actual removement of the frame.
// For frames that become OOP iframes, the reason would be 'swap'.
if (frame) this._removeFramesRecursively(frame);
} else if (reason === 'swap') {
this.emit(FrameManagerEmittedEvents.FrameSwapped, frame);
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/common/LifecycleWatcher.ts
Expand Up @@ -82,6 +82,7 @@ export class LifecycleWatcher {

_maximumTimer?: NodeJS.Timeout;
_hasSameDocumentNavigation?: boolean;
_swapped?: boolean;

constructor(
frameManager: FrameManager,
Expand Down Expand Up @@ -121,6 +122,11 @@ export class LifecycleWatcher {
FrameManagerEmittedEvents.FrameNavigatedWithinDocument,
this._navigatedWithinDocument.bind(this)
),
helper.addEventListener(
this._frameManager,
FrameManagerEmittedEvents.FrameSwapped,
this._frameSwapped.bind(this)
),
helper.addEventListener(
this._frameManager,
FrameManagerEmittedEvents.FrameDetached,
Expand Down Expand Up @@ -211,15 +217,26 @@ export class LifecycleWatcher {
this._checkLifecycleComplete();
}

_frameSwapped(frame: Frame): void {
if (frame !== this._frame) return;
this._swapped = true;
this._checkLifecycleComplete();
}

_checkLifecycleComplete(): void {
// We expect navigation to commit.
if (!checkLifecycle(this._frame, this._expectedLifecycle)) return;
this._lifecycleCallback();
if (
this._frame._loaderId === this._initialLoaderId &&
!this._hasSameDocumentNavigation
)
) {
if (this._swapped) {
this._swapped = false;
this._newDocumentNavigationCompleteCallback();
}
return;
}
if (this._hasSameDocumentNavigation)
this._sameDocumentNavigationCompleteCallback();
if (this._frame._loaderId !== this._initialLoaderId)
Expand Down
24 changes: 24 additions & 0 deletions test/oopif.spec.ts
Expand Up @@ -154,6 +154,30 @@ describeChromeOnly('OOPIF', function () {
await utils.detachFrame(page, 'frame1');
expect(page.frames()).toHaveLength(1);
});

it('should support wait for navigation for transitions from local to OOPIF', async () => {
const { server } = getTestState();

await page.goto(server.EMPTY_PAGE);
const framePromise = page.waitForFrame((frame) => {
return page.frames().indexOf(frame) === 1;
});
await utils.attachFrame(page, 'frame1', server.EMPTY_PAGE);

const frame = await framePromise;
expect(frame.isOOPFrame()).toBe(false);
const nav = frame.waitForNavigation();
await utils.navigateFrame(
page,
'frame1',
server.CROSS_PROCESS_PREFIX + '/empty.html'
);
await nav;
expect(frame.isOOPFrame()).toBe(true);
await utils.detachFrame(page, 'frame1');
expect(page.frames()).toHaveLength(1);
});

it('should keep track of a frames OOP state', async () => {
const { server } = getTestState();

Expand Down

0 comments on commit 34775e5

Please sign in to comment.