summaryrefslogtreecommitdiffstats
path: root/remote/shared/webdriver/test/xpcshell/test_Assert.js
blob: cf474868b6a86d8b6992f4141a2bd11a6b44296e (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* 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/. */

"use strict";
/* eslint-disable no-array-constructor, no-object-constructor */

const { assert } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/webdriver/Assert.sys.mjs"
);
const { error } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/webdriver/Errors.sys.mjs"
);

add_task(function test_session() {
  assert.session({ id: "foo" });

  const invalidTypes = [
    null,
    undefined,
    [],
    {},
    { id: undefined },
    { id: null },
    { id: true },
    { id: 1 },
    { id: [] },
    { id: {} },
  ];

  for (const invalidType of invalidTypes) {
    Assert.throws(() => assert.session(invalidType), /InvalidSessionIDError/);
  }

  Assert.throws(() => assert.session({ id: null }, "custom"), /custom/);
});

add_task(function test_platforms() {
  // at least one will fail
  let raised;
  for (let fn of [assert.desktop, assert.mobile]) {
    try {
      fn();
    } catch (e) {
      raised = e;
    }
  }
  ok(raised instanceof error.UnsupportedOperationError);
});

add_task(function test_noUserPrompt() {
  assert.noUserPrompt(null);
  assert.noUserPrompt(undefined);
  Assert.throws(() => assert.noUserPrompt({}), /UnexpectedAlertOpenError/);
  Assert.throws(() => assert.noUserPrompt({}, "custom"), /custom/);
});

add_task(function test_defined() {
  assert.defined({});
  Assert.throws(() => assert.defined(undefined), /InvalidArgumentError/);
  Assert.throws(() => assert.noUserPrompt({}, "custom"), /custom/);
});

add_task(function test_number() {
  assert.number(1);
  assert.number(0);
  assert.number(-1);
  assert.number(1.2);
  for (let i of ["foo", "1", {}, [], NaN, Infinity, undefined]) {
    Assert.throws(() => assert.number(i), /InvalidArgumentError/);
  }

  Assert.throws(() => assert.number("foo", "custom"), /custom/);
});

add_task(function test_callable() {
  assert.callable(function () {});
  assert.callable(() => {});

  for (let typ of [undefined, "", true, {}, []]) {
    Assert.throws(() => assert.callable(typ), /InvalidArgumentError/);
  }

  Assert.throws(() => assert.callable("foo", "custom"), /custom/);
});

add_task(function test_integer() {
  assert.integer(1);
  assert.integer(0);
  assert.integer(-1);
  Assert.throws(() => assert.integer("foo"), /InvalidArgumentError/);
  Assert.throws(() => assert.integer(1.2), /InvalidArgumentError/);

  Assert.throws(() => assert.integer("foo", "custom"), /custom/);
});

add_task(function test_positiveInteger() {
  assert.positiveInteger(1);
  assert.positiveInteger(0);
  Assert.throws(() => assert.positiveInteger(-1), /InvalidArgumentError/);
  Assert.throws(() => assert.positiveInteger("foo"), /InvalidArgumentError/);
  Assert.throws(() => assert.positiveInteger("foo", "custom"), /custom/);
});

add_task(function test_positiveNumber() {
  assert.positiveNumber(1);
  assert.positiveNumber(0);
  assert.positiveNumber(1.1);
  assert.positiveNumber(Number.MAX_VALUE);
  // eslint-disable-next-line no-loss-of-precision
  Assert.throws(() => assert.positiveNumber(1.8e308), /InvalidArgumentError/);
  Assert.throws(() => assert.positiveNumber(-1), /InvalidArgumentError/);
  Assert.throws(() => assert.positiveNumber(Infinity), /InvalidArgumentError/);
  Assert.throws(() => assert.positiveNumber("foo"), /InvalidArgumentError/);
  Assert.throws(() => assert.positiveNumber("foo", "custom"), /custom/);
});

add_task(function test_boolean() {
  assert.boolean(true);
  assert.boolean(false);
  Assert.throws(() => assert.boolean("false"), /InvalidArgumentError/);
  Assert.throws(() => assert.boolean(undefined), /InvalidArgumentError/);
  Assert.throws(() => assert.boolean(undefined, "custom"), /custom/);
});

add_task(function test_string() {
  assert.string("foo");
  assert.string(`bar`);
  Assert.throws(() => assert.string(42), /InvalidArgumentError/);
  Assert.throws(() => assert.string(42, "custom"), /custom/);
});

add_task(function test_open() {
  assert.open({ currentWindowGlobal: {} });

  for (let typ of [null, undefined, { currentWindowGlobal: null }]) {
    Assert.throws(() => assert.open(typ), /NoSuchWindowError/);
  }

  Assert.throws(() => assert.open(null, "custom"), /custom/);
});

add_task(function test_object() {
  assert.object({});
  assert.object(new Object());
  for (let typ of [42, "foo", true, null, undefined]) {
    Assert.throws(() => assert.object(typ), /InvalidArgumentError/);
  }

  Assert.throws(() => assert.object(null, "custom"), /custom/);
});

add_task(function test_in() {
  assert.in("foo", { foo: 42 });
  for (let typ of [{}, 42, true, null, undefined]) {
    Assert.throws(() => assert.in("foo", typ), /InvalidArgumentError/);
  }

  Assert.throws(() => assert.in("foo", { bar: 42 }, "custom"), /custom/);
});

add_task(function test_array() {
  assert.array([]);
  assert.array(new Array());
  Assert.throws(() => assert.array(42), /InvalidArgumentError/);
  Assert.throws(() => assert.array({}), /InvalidArgumentError/);

  Assert.throws(() => assert.array(42, "custom"), /custom/);
});

add_task(function test_that() {
  equal(1, assert.that(n => n + 1)(1));
  Assert.throws(() => assert.that(() => false)(), /InvalidArgumentError/);
  Assert.throws(() => assert.that(val => val)(false), /InvalidArgumentError/);
  Assert.throws(
    () => assert.that(val => val, "foo", error.SessionNotCreatedError)(false),
    /SessionNotCreatedError/
  );

  Assert.throws(() => assert.that(() => false, "custom")(), /custom/);
});

/* eslint-enable no-array-constructor, no-new-object */