tor-browser

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

subscription.html (5403B)


      1 <!DOCTYPE html>
      2 <meta charset="utf-8">
      3 <title>Can subscribe and receive WebDriver BiDi events</title>
      4 <script src="/resources/testharness.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 <script src="/resources/testdriver.js?feature=bidi"></script>
      7 <script src="/resources/testdriver-vendor.js"></script>
      8 <script>
      9    const MAIN_WINDOW_MESSAGE = "1. MAIN WINDOW MESSAGE";
     10    const POPUP_MESSAGE = "2. POPUP MESSAGE";
     11 
     12    /** Assert the event is `console.log` with the given message. */
     13    function assertLogEvent(event, expected_message) {
     14        assert_equals(event.type, "console");
     15        assert_equals(event.method, "log");
     16        assert_equals(event.args.length, 1);
     17        const event_message = event.args[0];
     18        assert_equals(event_message.type, "string");
     19        assert_equals(event_message.value, expected_message);
     20    }
     21 
     22    /** Wait for a given number of log messages. */
     23    function waitForLogMessages(count) {
     24        return new Promise(resolve => {
     25            const events = []
     26            test_driver.bidi.log.entry_added.on(event => {
     27                events.push(event);
     28                if (events.length === count) {
     29                    // The order of the events from different browsing contexts
     30                    // are not guaranteed, so sort them by the message for
     31                    // test consistency.
     32                    events.sort((a, b) => a?.args[0]?.value?.localeCompare(
     33                        b?.args[0]?.value));
     34                    resolve(events);
     35                }
     36            });
     37        });
     38    }
     39 
     40    promise_test(async (t) => {
     41        // Open a new window.
     42        const popup = window.open();
     43        t.add_cleanup(() => popup.close());
     44 
     45        const unsubscribe_handle = await test_driver.bidi.log.entry_added.subscribe();
     46        t.add_cleanup(async () => await unsubscribe_handle());
     47 
     48        // Add a listener for the log.entryAdded event.
     49        const messages_promise = waitForLogMessages(1)
     50 
     51        // The order of the calls matter, as if the subscription does not
     52        // isolate events properly, the first invoked event is likely (but not
     53        // guaranteed to) be emitted and received first.
     54 
     55        popup.console.log(POPUP_MESSAGE);
     56        console.log(MAIN_WINDOW_MESSAGE);
     57 
     58        const events = await messages_promise;
     59        assert_equals(events.length, 1);
     60        assertLogEvent(events[0], MAIN_WINDOW_MESSAGE)
     61    }, "Assert testdriver can subscribe without arguments");
     62 
     63    promise_test(async (t) => {
     64        // Open a new window.
     65        const popup = window.open();
     66        t.add_cleanup(() => popup.close());
     67 
     68        const unsubscribe_handle = await test_driver.bidi.log.entry_added.subscribe(
     69            {contexts: [window]});
     70        t.add_cleanup(async () => await unsubscribe_handle());
     71 
     72        // Add a listener for the log.entryAdded event.
     73        const messages_promise = waitForLogMessages(1)
     74 
     75        // The order of the calls matter, as if the subscription does not
     76        // isolate events properly, the first invoked event is likely (but not
     77        // guaranteed to) be emitted and received first.
     78 
     79        popup.console.log(POPUP_MESSAGE);
     80        console.log(MAIN_WINDOW_MESSAGE);
     81 
     82        const events = await messages_promise;
     83        assert_equals(events.length, 1);
     84        assertLogEvent(events[0], MAIN_WINDOW_MESSAGE)
     85    }, "Assert testdriver can subscribe for current window");
     86 
     87    promise_test(async (t) => {
     88        // Open a new window.
     89        const popup = window.open();
     90        t.add_cleanup(() => popup.close());
     91 
     92        const unsubscribe_handle = await test_driver.bidi.log.entry_added.subscribe(
     93            {contexts: [popup]});
     94        t.add_cleanup(async () => await unsubscribe_handle());
     95 
     96        // Add a listener for the log.entryAdded event.
     97        const messages_promise = waitForLogMessages(1)
     98 
     99        // The order of the calls matter, as if the subscription does not
    100        // isolate events properly, the first invoked event is likely (but not
    101        // guaranteed to) be emitted and received first.
    102 
    103        console.log(MAIN_WINDOW_MESSAGE);
    104        popup.console.log(POPUP_MESSAGE);
    105 
    106        const events = await messages_promise;
    107        assert_equals(events.length, 1);
    108        // Expect the popup log message.
    109        assertLogEvent(events[0], POPUP_MESSAGE)
    110    }, "Assert testdriver can subscribe for another window");
    111 
    112    promise_test(async (t) => {
    113        // Open a new window.
    114        const popup = window.open();
    115        t.add_cleanup(() => popup.close());
    116 
    117        const unsubscribe_handle = await test_driver.bidi.log.entry_added.subscribe(
    118            {contexts: null});
    119        t.add_cleanup(async () => await unsubscribe_handle());
    120 
    121        // Add a listener for the log.entryAdded event.
    122        const messages_promise = waitForLogMessages(2)
    123 
    124        // The order of the calls does not matter, as the events will be
    125        // sorted by the `waitForLogMessages` helper.
    126 
    127        console.log(MAIN_WINDOW_MESSAGE);
    128        popup.console.log(POPUP_MESSAGE);
    129 
    130        // The order of the events is guaranteed by the `waitForLogMessages`
    131        // helper.
    132        const events = await messages_promise;
    133        assert_equals(events.length, 2);
    134        assertLogEvent(events[0], MAIN_WINDOW_MESSAGE)
    135        assertLogEvent(events[1], POPUP_MESSAGE)
    136    }, "Assert testdriver can subscribe globally");
    137 </script>