tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

change-observer-scope-test.js (2768B)


      1 // This script depends on the following scripts:
      2 //    resources/test-helpers.js
      3 
      4 // A helper class for WPTs testing FileSystemObserver scope behavior.
      5 //
      6 // Sets up a `watched_handle` for the test to watch. Provides the
      7 // `in_scope_paths()` and `out_of_scope_paths()` async iterators to get paths
      8 // that are in scope or out of scope of the `watched_path` respectively.
      9 class ScopeTest {
     10  #test_dir_handle;
     11 
     12  #watched_handle;
     13  #out_of_scope_directory;
     14 
     15  #child_dir_name;
     16  #child_dir_handle;
     17 
     18  #setup_promise_and_resolvers = Promise.withResolvers();
     19 
     20  constructor(test, test_dir_handle) {
     21    test.add_cleanup(async () => {
     22      await this.#setup_promise_and_resolvers.promise;
     23      this.#watched_handle.remove({recursive: true});
     24      this.#out_of_scope_directory.remove({recursive: true});
     25    });
     26 
     27    this.#test_dir_handle = test_dir_handle;
     28 
     29    this.#setup();
     30  }
     31 
     32  async watched_handle() {
     33    await this.#setup_promise_and_resolvers.promise;
     34    return this.#watched_handle;
     35  }
     36 
     37  async * in_scope_paths(recursive) {
     38    await this.#setup_promise_and_resolvers.promise;
     39 
     40    yield new ScopeTestPath(this.#watched_handle, [])
     41 
     42    if (recursive) {
     43      yield new ScopeTestPath(this.#child_dir_handle, [this.#child_dir_name]);
     44    }
     45  }
     46 
     47  async * out_of_scope_paths(recursive) {
     48    await this.#setup_promise_and_resolvers.promise;
     49 
     50    yield new ScopeTestPath(this.#out_of_scope_directory, [])
     51 
     52    if (!recursive) {
     53      yield new ScopeTestPath(this.#child_dir_handle, [this.#child_dir_name]);
     54    }
     55  }
     56 
     57  async #setup() {
     58    this.#watched_handle = await this.#test_dir_handle.getDirectoryHandle(
     59        getUniqueName(), {create: true});
     60 
     61    this.#child_dir_name = getUniqueName();
     62    this.#child_dir_handle = await this.#watched_handle.getDirectoryHandle(
     63        this.#child_dir_name, {create: true});
     64 
     65    this.#out_of_scope_directory =
     66        await this.#test_dir_handle.getDirectoryHandle(
     67            getUniqueName(), {create: true});
     68 
     69    this.#setup_promise_and_resolvers.resolve();
     70  }
     71 }
     72 
     73 // The class that ScopeTest delivers the in scope and out of scope paths in.
     74 class ScopeTestPath {
     75  #parentHandle;
     76  #fileName;
     77  #relativePathComponents;
     78 
     79  constructor(parentHandle, parentRelativePathComponents) {
     80    this.#parentHandle = parentHandle;
     81    this.#fileName = getUniqueName();
     82    this.#relativePathComponents =
     83        [...parentRelativePathComponents, this.#fileName];
     84  }
     85 
     86  parentHandle() {
     87    return this.#parentHandle;
     88  }
     89 
     90  fileName() {
     91    return this.#fileName;
     92  }
     93 
     94  // Returns the relative path components to the watched directory.
     95  relativePathComponents() {
     96    return this.#relativePathComponents;
     97  }
     98 
     99  createHandle() {
    100    return this.#parentHandle.getFileHandle(this.#fileName, {create: true});
    101  }
    102 }