Skip to content
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
37 changes: 34 additions & 3 deletions injected/integration-test/web-compat-android.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,34 @@ test.describe('Web Share API', () => {
});

test.describe('navigator.canShare()', () => {
test('should not let you share files', async ({ page }) => {
test('should allow empty files arrays', async ({ page }) => {
await navigate(page);
const refuseFileShare = await page.evaluate(() => {
const allowEmptyFiles = await page.evaluate(() => {
return navigator.canShare({ text: 'xxx', files: [] });
});
expect(allowEmptyFiles).toEqual(true);
});

test('should not let you share non-empty files arrays', async ({ page }) => {
await navigate(page);
const refuseFileShare = await page.evaluate(() => {
// Create a mock File object
const mockFile = new File([''], 'test.txt', { type: 'text/plain' });
return navigator.canShare({ text: 'xxx', files: [mockFile] });
});
expect(refuseFileShare).toEqual(false);
});

test('should reject non-array files values', async ({ page }) => {
await navigate(page);
const rejectNonArrayFiles = await page.evaluate(() => {
// eslint-disable-next-line
// @ts-ignore intentionally testing invalid files type
return navigator.canShare({ text: 'xxx', files: 'not-an-array' });
});
expect(rejectNonArrayFiles).toEqual(false);
});

test('should not let you share non-http urls', async ({ page }) => {
await navigate(page);
const refuseShare = await page.evaluate(() => {
Expand Down Expand Up @@ -218,10 +238,21 @@ test.describe('Web Share API', () => {
expect(result).toBeUndefined();
});

test('should throw when sharing files', async ({ page }) => {
test('should allow sharing with empty files array', async ({ page }) => {
await navigate(page);
await beforeEach(page);
const { result, message } = await checkShare(page, { title: 'title', files: [] });
expect(message).toMatchObject({ featureName: 'webCompat', method: 'webShare', params: { title: 'title', text: '' } });
expect(result).toBeUndefined();
});

test('should throw when sharing non-empty files arrays', async ({ page }) => {
await navigate(page);
await beforeEach(page);
const { result, message } = await checkShare(page, {
title: 'title',
files: [new File([''], 'test.txt', { type: 'text/plain' })],
});
expect(message).toBeNull();
expect(result.threw.message).toContain('TypeError: Invalid share data');
});
Expand Down
5 changes: 4 additions & 1 deletion injected/src/features/web-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const MSG_DEVICE_ENUMERATION = 'deviceEnumeration';
function canShare(data) {
if (typeof data !== 'object') return false;
if (!('url' in data) && !('title' in data) && !('text' in data)) return false; // At least one of these is required
if ('files' in data) return false; // File sharing is not supported at the moment
if ('files' in data) {
if (!Array.isArray(data.files)) return false;
if (data.files.length > 0) return false; // File sharing is not supported at the moment
}
if ('title' in data && typeof data.title !== 'string') return false;
if ('text' in data && typeof data.text !== 'string') return false;
if ('url' in data) {
Expand Down
Loading