summaryrefslogtreecommitdiffstats
path: root/testing/marionette/test/unit/test_navigate.js
blob: 1298d9e14b247297439568123d6f76332ba9f702 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]);

const { navigate } = ChromeUtils.import(
  "chrome://marionette/content/navigate.js"
);

const topContext = {
  id: 7,
  get top() {
    return this;
  },
};

const nestedContext = {
  id: 8,
  parent: topContext,
  top: topContext,
};

add_test(function test_isLoadEventExpectedForCurrent() {
  Assert.throws(
    () => navigate.isLoadEventExpected(undefined),
    /Expected at least one URL/
  );

  ok(navigate.isLoadEventExpected(new URL("http://a/")));

  run_next_test();
});

add_test(function test_isLoadEventExpectedForFuture() {
  const data = [
    { current: "http://a/", future: undefined, expected: true },
    { current: "http://a/", future: "http://a/", expected: true },
    { current: "http://a/", future: "http://a/#", expected: true },
    { current: "http://a/#", future: "http://a/", expected: true },
    { current: "http://a/#a", future: "http://a/#A", expected: true },
    { current: "http://a/#a", future: "http://a/#a", expected: false },
    { current: "http://a/", future: "javascript:whatever", expected: false },
  ];

  for (const entry of data) {
    const current = new URL(entry.current);
    const future = entry.future ? new URL(entry.future) : undefined;
    equal(navigate.isLoadEventExpected(current, { future }), entry.expected);
  }

  run_next_test();
});

add_test(function test_isLoadEventExpectedForTarget() {
  for (const target of ["_parent", "_top"]) {
    Assert.throws(
      () => navigate.isLoadEventExpected(new URL("http://a"), { target }),
      /Expected browsingContext when target is _parent or _top/
    );
  }

  const data = [
    { cur: "http://a/", target: "", expected: true },
    { cur: "http://a/", target: "_blank", expected: false },
    { cur: "http://a/", target: "_parent", bc: topContext, expected: true },
    { cur: "http://a/", target: "_parent", bc: nestedContext, expected: false },
    { cur: "http://a/", target: "_self", expected: true },
    { cur: "http://a/", target: "_top", bc: topContext, expected: true },
    { cur: "http://a/", target: "_top", bc: nestedContext, expected: false },
  ];

  for (const entry of data) {
    const current = entry.cur ? new URL(entry.cur) : undefined;
    equal(
      navigate.isLoadEventExpected(current, {
        target: entry.target,
        browsingContext: entry.bc,
      }),
      entry.expected
    );
  }

  run_next_test();
});