Bug 1689708 [wpt PR 27396] - Refactor native_file_system -> file_system_access in...
[gecko.git] / testing / web-platform / tests / file-system-access / resources / messaging-serialize-helpers.js
blobada68f43db50683ec3350e243ebe8da6966e8196
1 'use strict';
3 // This script depends on the following script:
4 //    /file-system-access/resources/test-helpers.js
6 // Serializes an array of FileSystemHandles where each element can be either a
7 // FileSystemFileHandle or FileSystemDirectoryHandle.
8 async function serialize_handles(handle_array) {
9   const serialized_handle_array = [];
10   for (let i = 0; i < handle_array.length; ++i) {
11     serialized_handle_array.push(await serialize_handle(handle_array[i]));
12   }
13   return serialized_handle_array;
16 // Serializes either a FileSystemFileHandle or FileSystemDirectoryHandle.
17 async function serialize_handle(handle) {
18   switch (handle.kind) {
19     case 'directory':
20       return await serialize_file_system_directory_handle(handle);
21     case 'file':
22       return await serialize_file_system_file_handle(handle);
23     default:
24       throw 'Object is not a FileSystemFileHandle or ' +
25           `FileSystemDirectoryHandle ${handle}`;
26   }
29 // Creates a dictionary for a FileSystemHandle base, which contains
30 // serialized properties shared by both FileSystemFileHandle and
31 // FileSystemDirectoryHandle.
32 async function serialize_file_system_handle(handle) {
33   const read_permission =
34     await handle.queryPermission({ mode: 'read' });
36   const write_permission =
37     await handle.queryPermission({ mode: 'readwrite' })
39   return {
40     kind: handle.kind,
41     name: handle.name,
42     read_permission,
43     write_permission
44   };
47 // Create a dictionary with each property value in FileSystemFileHandle.
48 // Also, reads the contents of the file to include with the returned
49 // dictionary.  Example output:
50 // {
51 //   kind: "file",
52 //   name: "example-file-name"
53 //   read_permission: "granted",
54 //   write_permission: "granted",
55 //   contents: "example-file-contents"
56 // }
57 async function serialize_file_system_file_handle(file_handle) {
58   const contents = await getFileContents(file_handle);
60   const serialized_file_system_handle =
61     await serialize_file_system_handle(file_handle);
63   return Object.assign(serialized_file_system_handle, { contents });
66 // Create a dictionary with each property value in FileSystemDirectoryHandle.
67 // Example output:
68 // {
69 //   kind: "directory",
70 //   name: "example-directory-name"
71 //   read_permission: "granted",
72 //   write_permission: "granted",
73 //   files: [<first serialized file>, ...]
74 //   directories: [<first serialized subdirectory>, ...]
75 // }
76 async function serialize_file_system_directory_handle(directory_handle) {
77   // Serialize the contents of the directory.
78   const serialized_files = [];
79   const serialized_directories = [];
80   for await (const child_handle of directory_handle.values()) {
81     const serialized_child_handle = await serialize_handle(child_handle);
82     if (child_handle.kind === "directory") {
83       serialized_directories.push(serialized_child_handle);
84     } else {
85       serialized_files.push(serialized_child_handle);
86     }
87   }
89   // Order the serialized contents of the directory by name.
90   serialized_files.sort((left, right) => {
91     return left.name.localeCompare(right.name);
92   });
93   serialized_directories.sort((left, right) => {
94     return left.name.localeCompare(right.name);
95   });
97   // Serialize the directory's common properties shared by all
98   // FileSystemHandles.
99   const serialized_file_system_handle =
100     await serialize_file_system_handle(directory_handle);
102   return Object.assign(
103     serialized_file_system_handle,
104     { files: serialized_files, directories: serialized_directories });
107 // Verifies |left_array| is a clone of |right_array| where each element
108 // is a cloned FileSystemHandle with the same properties and contents.
109 async function assert_equals_cloned_handles(left_array, right_array) {
110   assert_equals(left_array.length, right_array.length,
111     'Each array of FileSystemHandles must have the same length');
113   for (let i = 0; i < left_array.length; ++i) {
114     assert_not_equals(left_array[i], right_array[i],
115       'Clones must create new FileSystemHandle instances.');
117     const left_serialized = await serialize_handle(left_array[i]);
118     const right_serialized = await serialize_handle(right_array[i]);
119     assert_equals_serialized_handle(left_serialized, right_serialized);
120   }
123 // Verifies |left_array| is the same as |right_array| where each element
124 // is a serialized FileSystemHandle with the same properties.
125 function assert_equals_serialized_handles(left_array, right_array) {
126   assert_equals(left_array.length, right_array.length,
127     'Each array of serialized handles must have the same length');
129   for (let i = 0; i < left_array.length; ++i) {
130     assert_equals_serialized_handle(left_array[i], right_array[i]);
131   }
134 // Verifies each property of a serialized FileSystemFileHandle or
135 // FileSystemDirectoryHandle.
136 function assert_equals_serialized_handle(left, right) {
137   switch (left.kind) {
138     case 'directory':
139       assert_equals_serialized_file_system_directory_handle(left, right);
140       break;
141     case 'file':
142       assert_equals_serialized_file_system_file_handle(left, right);
143       break;
144     default:
145       throw 'Object is not a FileSystemFileHandle or ' +
146           `FileSystemDirectoryHandle ${left}`;
147   }
150 // Compares the output of serialize_file_system_handle() for
151 // two FileSystemHandles.
152 function assert_equals_serialized_file_system_handle(left, right) {
153   assert_equals(left.kind, right.kind,
154     'Each FileSystemHandle instance must use the expected "kind".');
156   assert_equals(left.name, right.name,
157     'Each FileSystemHandle instance must use the expected "name" ' +
158     ' property.');
160   assert_equals(left.read_permission, right.read_permission,
161     'Each FileSystemHandle instance must have the expected read ' +
162     ' permission.');
164   assert_equals(left.write_permission, right.write_permission,
165     'Each FileSystemHandle instance must have the expected write ' +
166     ' permission.');
169 // Compares the output of serialize_file_system_file_handle()
170 // for two FileSystemFileHandle.
171 function assert_equals_serialized_file_system_file_handle(left, right) {
172   assert_equals_serialized_file_system_handle(left, right);
173   assert_equals(left.contents, right.contents,
174     'Each FileSystemFileHandle instance must have the same contents.');
177 // Compares the output of serialize_file_system_directory_handle()
178 // for two FileSystemDirectoryHandles.
179 function assert_equals_serialized_file_system_directory_handle(left, right) {
180   assert_equals_serialized_file_system_handle(left, right);
182   assert_equals(left.files.length, right.files.length,
183     'Each FileSystemDirectoryHandle must contain the same number of ' +
184     'file children');
186   for (let i = 0; i < left.files.length; ++i) {
187     assert_equals_serialized_file_system_file_handle(
188       left.files[i], right.files[i]);
189   }
191   assert_equals(left.directories.length, right.directories.length,
192     'Each FileSystemDirectoryHandle must contain the same number of ' +
193     'directory children');
195   for (let i = 0; i < left.directories.length; ++i) {
196     assert_equals_serialized_file_system_directory_handle(
197       left.directories[i], right.directories[i]);
198   }
201 // Creates a dictionary with interesting property values from MessageEvent.
202 function serialize_message_error_event(message_error_event) {
203   return {
204     data: message_error_event.data,
205     origin: message_error_event.origin,
206     last_event_id: message_error_event.lastEventId,
207     has_source: (message_error_event.source !== null),
208     ports_length: message_error_event.ports.length
209   };
212 // Compares the output of serialize_message_error_event() with an
213 // expected result.
214 function assert_equals_serialized_message_error_event(
215   serialized_event, expected_origin, expected_has_source) {
216   assert_equals(serialized_event.data, null,
217     'The message error event must set the "data" property to null.');
219   assert_equals(serialized_event.origin, expected_origin,
220     'The message error event must have the expected "origin" property.');
222   assert_equals(serialized_event.last_event_id, "",
223     'The message error event must set the "lastEventId" property to the empty string.');
225   assert_equals(serialized_event.has_source, expected_has_source,
226     'The message error event must have the expected "source" property.');
228   assert_equals(serialized_event.ports_length, 0,
229     'The message error event must not contain any message ports.');