summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/target/tests/browser_target_command_invalid_api_usage.js
blob: dbdaae7f053aa61d3bdf181491acd0d96b75de53 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test watch/unwatchTargets throw when provided with invalid types.

const TEST_URL = "data:text/html;charset=utf-8,invalid api usage test";

add_task(async function () {
  info("Setup the test page with workers of all types");
  const tab = await addTab(TEST_URL);

  info("Create a target list for a tab target");
  const commands = await CommandsFactory.forTab(tab);
  const targetCommand = commands.targetCommand;

  const onAvailable = function () {};

  await Assert.rejects(
    targetCommand.watchTargets({ types: [null], onAvailable }),
    /TargetCommand.watchTargets invoked with an unknown type/,
    "watchTargets should throw for null type"
  );

  await Assert.rejects(
    targetCommand.watchTargets({ types: [undefined], onAvailable }),
    /TargetCommand.watchTargets invoked with an unknown type/,
    "watchTargets should throw for undefined type"
  );

  await Assert.rejects(
    targetCommand.watchTargets({ types: ["NOT_A_TARGET"], onAvailable }),
    /TargetCommand.watchTargets invoked with an unknown type/,
    "watchTargets should throw for unknown type"
  );

  await Assert.rejects(
    targetCommand.watchTargets({
      types: [targetCommand.TYPES.FRAME, "NOT_A_TARGET"],
      onAvailable,
    }),
    /TargetCommand.watchTargets invoked with an unknown type/,
    "watchTargets should throw for unknown type mixed with a correct type"
  );

  Assert.throws(
    () => targetCommand.unwatchTargets({ types: [null], onAvailable }),
    /TargetCommand.unwatchTargets invoked with an unknown type/,
    "unwatchTargets should throw for null type"
  );

  Assert.throws(
    () => targetCommand.unwatchTargets({ types: [undefined], onAvailable }),
    /TargetCommand.unwatchTargets invoked with an unknown type/,
    "unwatchTargets should throw for undefined type"
  );

  Assert.throws(
    () =>
      targetCommand.unwatchTargets({ types: ["NOT_A_TARGET"], onAvailable }),
    /TargetCommand.unwatchTargets invoked with an unknown type/,
    "unwatchTargets should throw for unknown type"
  );

  Assert.throws(
    () =>
      targetCommand.unwatchTargets({
        types: [targetCommand.TYPES.CONSOLE_MESSAGE, "NOT_A_TARGET"],
        onAvailable,
      }),
    /TargetCommand.unwatchTargets invoked with an unknown type/,
    "unwatchTargets should throw for unknown type mixed with a correct type"
  );

  BrowserTestUtils.removeTab(tab);
  await commands.destroy();
});