summaryrefslogtreecommitdiffstats
path: root/dom/webshare/test/mochitest/test_navigator_share_consume_user_activation.html
blob: 9cb3f04d96a6a037ffab046af4c5c8ab07413bad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<title>Web Share: consume transient activation</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css" />
<body>
  <button id="share">Share</button>
</body>
<script>
  // TODO: add a task that tests share() consume the transient user activation.
  // Because OS-level prompt can't be cancelled, it's currently not possible to
  // test this. We need to add a Web share Mocking service:
  // https://bugzilla.mozilla.org/show_bug.cgi?id=1646229

  // test that share() would be blocked with an already-consumed-activation.
  add_task(async function blockedIfAlreadyConsumed() {
    const wrappedDoc = SpecialPowers.wrap(document);
    const button = document.getElementById("share");

    // Kick off transient activation
    synthesizeMouseAtCenter(button, {});

    ok(
      wrappedDoc.hasValidTransientUserGestureActivation,
      "Activated by a gesture"
    );

    wrappedDoc.consumeTransientUserGestureActivation();

    try {
      const sharePromise = navigator.share({ title: "test" });
      await sharePromise;
      ok(false, "must throw because activation was already consumed");
    } catch (err) {
      is(err.name, "NotAllowedError", "Expected NotAllowedError DOMException");
    } finally {
      ok(
        !wrappedDoc.hasValidTransientUserGestureActivation,
        "share() must consume the activation"
      );
    }
  });
</script>