FileSystemObserver-sync-access-handle.https.tentative.worker.js (3162B)
1 importScripts('/resources/testharness.js'); 2 importScripts('resources/sandboxed-fs-test-helpers.js'); 3 importScripts('resources/test-helpers.js'); 4 importScripts('resources/collecting-file-system-observer.js'); 5 6 'use strict'; 7 8 directory_test(async (t, root_dir) => { 9 const file = await root_dir.getFileHandle(getUniqueName(), {create: true}); 10 11 const observer = new CollectingFileSystemObserver(t, root_dir); 12 await observer.observe([file]); 13 14 // Write to `file` through a `FileSystemSyncAccessHandle`. 15 const syncHandle = await createSAHWithCleanup(t, file); 16 const writeBuffer = new TextEncoder().encode('contents'); 17 syncHandle.write(writeBuffer); 18 syncHandle.close(); 19 20 // Expect one "modified" event to happen on `file`. 21 const records = await observer.getRecords(); 22 await assert_records_equal(file, records, [modifiedEvent(file, [])]); 23 }, 'FileSystemSyncAccessHandle.write produces a "modified" event'); 24 25 directory_test(async (t, root_dir) => { 26 const file = await root_dir.getFileHandle(getUniqueName(), {create: true}); 27 28 const observer = new CollectingFileSystemObserver(t, root_dir); 29 await observer.observe([file]); 30 31 // Write to `file` through a `FileSystemSyncAccessHandle` multiple times. 32 const syncHandle = await createSAHWithCleanup(t, file); 33 const writeBuffer = new TextEncoder().encode('contents'); 34 35 const numberOfWrites = 3; 36 const expectedEvents = []; 37 for (let i = 0; i < numberOfWrites; i++) { 38 expectedEvents.push(modifiedEvent(file, [])); 39 syncHandle.write(writeBuffer); 40 } 41 syncHandle.close(); 42 43 // Expect the same number of "modified" event to happen on `file` as there 44 // were writes to it. 45 const records = await observer.getRecords(); 46 await assert_records_equal(file, records, expectedEvents); 47 }, 'Multiple FileSystemSyncAccessHandle.writes produces the same amount of "modified" event'); 48 49 directory_test(async (t, root_dir) => { 50 const file = await root_dir.getFileHandle(getUniqueName(), {create: true}); 51 52 const observer = new CollectingFileSystemObserver(t, root_dir); 53 await observer.observe([file]); 54 55 // Truncate to `file` through a `FileSystemSyncAccessHandle`. 56 const syncHandle = await createSAHWithCleanup(t, file); 57 syncHandle.truncate(1); 58 syncHandle.close(); 59 60 // Expect one "modified" event to happen on `file`. 61 const records = await observer.getRecords(); 62 await assert_records_equal(file, records, [modifiedEvent(file, [])]); 63 }, 'FileSystemSyncAccessHandle.truncate produces a "modified" event'); 64 65 directory_test(async (t, root_dir) => { 66 const file = await root_dir.getFileHandle(getUniqueName(), {create: true}); 67 68 const observer = new CollectingFileSystemObserver(t, root_dir); 69 await observer.observe([file]); 70 71 // Write to `file`. 72 const syncHandle = await createSAHWithCleanup(t, file); 73 const readBuffer = new Uint8Array(24); 74 syncHandle.read(readBuffer); 75 syncHandle.flush(); 76 syncHandle.getSize(); 77 syncHandle.close(); 78 79 // Expect no events to happen. 80 const records = await observer.getRecords(); 81 await assert_records_equal(file, records, []); 82 }, 'FileSystemSyncAccessHandle methods that don\'t modify the file don\'t produce events'); 83 84 done();