Skip to content
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

Add integration test to check inter-frame communication #4090

Merged
merged 2 commits into from
Jan 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions src/shared/test/integration/inter-frame-communication-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { delay } from '../../../test-util/wait';
import { PortRPC as PortRPC_ } from '../../port-rpc';
import { ListenerCollection as ListenerCollection_ } from '../../listener-collection';
import { PortFinder as PortFinder_ } from '../../port-finder';
import { PortProvider as PortProvider_ } from '../../port-provider';
import { isMessageEqual } from '../../port-util';

describe('PortProvider-PortFinder-PortRPC integration', () => {
let destroyables;

// These proxy classes are to keep track of instances that need to be destroyed.
class PortRPC extends PortRPC_ {
constructor() {
super();
destroyables.push(this);
}
}

class ListenerCollection extends ListenerCollection_ {
constructor() {
super();
destroyables.push(this);
}
destroy() {
this.removeAll();
}
}

class PortFinder extends PortFinder_ {
constructor(...args) {
super(...args);
destroyables.push(this);
}
}

class PortProvider extends PortProvider_ {
constructor(...args) {
super(...args);
destroyables.push(this);
}
}

beforeEach(() => {
destroyables = [];
});

afterEach(() => {
destroyables.forEach(instance => instance.destroy());
});

it('enables the communication between guest-host', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To summarize what these tests are doing, each runs a bunch of concurrent activities that simulate the guest, host and sidebar roles in the inter-frame communication process. All the only communication between frames has to go via message channels (no shared variables). One way to make this structure more obvious might be to wrap each "actor" in a function and then run all the actors concurrently at the end.

it('enables ... communication', async () => {
  const simulateGuest = async () => {
    // Guest steps here
  };

  const simulateHost = async () => {
    // Host steps here
  };

  return Promise.all([simulateGuest(), simulateHost()]);
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good idea! Thanks!

const simulateGuest = async () => {
const portFinder = new PortFinder({
hostFrame: window,
source: 'guest',
});
const port = await portFinder.discover('host');

const hostRPC = new PortRPC();
hostRPC.connect(port);
await new Promise(resolve =>
hostRPC.call('ping', response => {
assert.equal(response, 'pong');
resolve();
})
);
};

const simulateHost = async () => {
await delay(10); // simulate scenario when host frame is ready before the guest frame

const portProvider = new PortProvider(window.location.origin);
portProvider.listen();

const guestRPC = new PortRPC();
guestRPC.on('ping', cb => cb('pong'));

portProvider.on('frameConnected', (source, port) => {
if (source === 'guest') {
guestRPC.connect(port);
}
});
};

return Promise.all([simulateGuest(), simulateHost()]);
});

it('enables the communication between guest-sidebar', async () => {
const simulateGuest = async () => {
const portFinder = new PortFinder({
hostFrame: window,
source: 'guest',
});
const hostRPC = new PortRPC();

const port = await portFinder.discover('sidebar');
hostRPC.connect(port);

await new Promise(resolve =>
hostRPC.call('ping', response => {
assert.equal(response, 'pong');
resolve();
})
);
};

const simulateSidebar = async () => {
const portFinder = new PortFinder({
hostFrame: window,
source: 'sidebar',
});
const port = await portFinder.discover('host');

const guestRPC = new PortRPC();
guestRPC.on('ping', cb => cb('pong'));

const listenerCollection = new ListenerCollection();
listenerCollection.add(port, 'message', ({ data, ports }) => {
if (
isMessageEqual(data, {
frame1: 'guest',
frame2: 'sidebar',
type: 'offer',
})
) {
guestRPC.connect(ports[0]);
}
});

port.start(); // `start` is normally invoked by `hostRPC.connect(port)`
};

const simulateHost = async () => {
await delay(10); // simulate scenario when host frame is ready before the guest frame

const portProvider = new PortProvider(window.location.origin);
portProvider.listen();
};

return Promise.all([simulateGuest(), simulateSidebar(), simulateHost()]);
});

it('enables the communication between sidebar-host', async () => {
const simulateSidebar = async () => {
const portFinder = new PortFinder({
hostFrame: window,
source: 'sidebar',
});
const port = await portFinder.discover('host');

const hostRPC = new PortRPC();
hostRPC.connect(port);

await new Promise(resolve => {
hostRPC.call('ping', response => {
assert.equal(response, 'pong');
resolve();
});
});
};

const simulateHost = () => {
const portProvider = new PortProvider(window.location.origin);
portProvider.listen();

const sidebarRPC = new PortRPC();
sidebarRPC.on('ping', cb => cb('pong'));

portProvider.on('frameConnected', (source, port) => {
if (source === 'sidebar') {
sidebarRPC.connect(port);
}
});
};

return Promise.all([simulateSidebar(), simulateHost()]);
});
});