summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/target/browser_createTarget.js
blob: 80e45f1c7af0ded2af82f7553e05e53161a75d56 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const PAGE_TEST =
  "https://example.com/browser/remote/cdp/test/browser/target/doc_test.html";

add_task(
  async function raisesWithoutArguments({ client }) {
    const { Target } = client;

    let exceptionThrown = false;
    try {
      await Target.createTarget();
    } catch (e) {
      exceptionThrown = true;
    }
    ok(exceptionThrown, "createTarget raised error without a URL");
  },
  { createTab: false }
);

add_task(
  async function raisesWithInvalidUrlType({ client }) {
    const { Target } = client;

    for (const url of [null, true, 1, [], {}]) {
      info(`Checking url with invalid value: ${url}`);

      let errorThrown = "";
      try {
        await Target.createTarget({
          url,
        });
      } catch (e) {
        errorThrown = e.message;
      }

      ok(
        errorThrown.match(/url: string value expected/),
        `URL fails for invalid type: ${url}`
      );
    }
  },
  { createTab: false }
);

add_task(
  async function invalidUrlDefaults({ client }) {
    const { Target } = client;
    const expectedUrl = "about:blank";

    for (const url of ["", "example.com", "https://example[.com", "https:"]) {
      // Here we cannot wait for browserLoaded, because the tab might already
      // be on about:blank when `createTarget` resolves.
      const onNewTabLoaded = BrowserTestUtils.waitForNewTab(
        gBrowser,
        "about:blank",
        true
      );
      const { targetId } = await Target.createTarget({ url });
      is(typeof targetId, "string", "Got expected type for target id");

      // Wait for the load to be done before checking the URL.
      const tab = await onNewTabLoaded;
      const browser = tab.linkedBrowser;
      is(browser.currentURI.spec, expectedUrl, "Expected URL loaded");
    }
  },
  { createTab: false }
);

add_task(
  async function opensTabWithCorrectInfo({ client }) {
    const { Target } = client;

    const url = PAGE_TEST;
    const onNewTabLoaded = BrowserTestUtils.waitForNewTab(gBrowser, url, true);
    const { targetId } = await Target.createTarget({ url });

    is(typeof targetId, "string", "Got expected type for target id");

    const tab = await onNewTabLoaded;
    const browser = tab.linkedBrowser;
    is(browser.currentURI.spec, url, "Expected URL loaded");

    const { targetInfos } = await Target.getTargets();
    const targetInfo = targetInfos.find(info => info.targetId === targetId);
    ok(!!targetInfo, "Found target info with the same target id");
    is(targetInfo.url, url, "Target info refers to the same target URL");
    is(
      targetInfo.type,
      "page",
      "Target info refers to the same target as page type"
    );
    ok(
      !targetInfo.attached,
      "Target info refers to the same target as not attached"
    );
  },
  { createTab: false }
);