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

@W-16404595: Add Identity Discontinuity test cases for MaxPerfMode to near-membrane's binary data tests #466

Merged
Merged
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions test/membrane/binary-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,50 @@ describe('FileSaver library', () => {
`);
});
});

describe('Identity Discontinuity', () => {
beforeEach(createEnvironmentThatAlwaysRemapsTypedArray);

function getBlueBlob() {
return new Blob([new Uint8Array([1, 2, 3])], { type: 'text/plain;charset=utf-8' });
}

it('can save blue blob', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect, getBlueBlob }),
});

env.evaluate(`
${untrusted('FileSaver.js')}
const transferBlueBlob = false;
const expectedToThrow = false;
${untrusted('FileSaver-blue-blob.js')}
`);
});
it('fails to save blue blob, when maxPerfMode is true and blob is not transferred to red', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect, getBlueBlob }),
maxPerfMode: true,
});

env.evaluate(`
${untrusted('FileSaver.js')}
const transferBlueBlob = false;
const expectedToThrow = true;
${untrusted('FileSaver-blue-blob.js')}
`);
});
it('can save blue blob, when maxPerfMode is true and blob is transferred to red', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect, getBlueBlob }),
maxPerfMode: true,
});

env.evaluate(`
${untrusted('FileSaver.js')}
const transferBlueBlob = true;
const expectedToThrow = false;
${untrusted('FileSaver-blue-blob.js')}
`);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

This is great!

});
46 changes: 46 additions & 0 deletions test/membrane/untrusted/binary-data/FileSaver-blue-blob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function blueBlobToRedBlob(blueBlob) {
const reader = blueBlob.stream().getReader();
let result = new Uint8Array(0);
let resolve, reject;
const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
const handler = ({ done, value }) => {
try {
if (done) {
const redBlob = new Blob([result.buffer], { type: blueBlob.type });
resolve(redBlob);
return;
}
const merge = new Uint8Array(result.length + value.length);
merge.set(result);
merge.set(value, result.length);
result = merge;
return reader.read().then(handler, reject);
} catch (e) {
reject(e);
}
};
reader.read().then(handler, reject);
return promise;
}

const blueBlob = getBlueBlob();

const blobToSavePromise = transferBlueBlob
? blueBlobToRedBlob(blueBlob)
: Promise.resolve(blueBlob);

blobToSavePromise.then((blobToSave) => {
if (expectedToThrow) {
expect(() => {
saveAs(blobToSave, 'blue-binary.txt');
}).toThrow();
} else {
expect(() => {
saveAs(blobToSave, 'blue-binary.txt');
Copy link
Contributor

Choose a reason for hiding this comment

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

I love that we're again using the library that customers will use. Excellent!

}).not.toThrow();
}
done();
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!!

Loading