summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/xpcshell/test_StreamRegistry.js
blob: 329f33d27522307e181f41ce53044d40f047d884 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { Stream, StreamRegistry } = ChromeUtils.importESModule(
  "chrome://remote/content/cdp/StreamRegistry.sys.mjs"
);

add_task(function test_constructor() {
  const registry = new StreamRegistry();
  equal(registry.streams.size, 0);
});

add_task(async function test_destructor() {
  const registry = new StreamRegistry();

  const stream1 = await createFileStream("foo bar");
  const stream2 = await createFileStream("foo bar");

  registry.add(stream1);
  registry.add(stream2);

  equal(registry.streams.size, 2);

  await registry.destructor();
  equal(registry.streams.size, 0);

  ok(!(await IOUtils.exists(stream1.path)), "temporary file has been removed");
  ok(!(await IOUtils.exists(stream2.path)), "temporary file has been removed");
});

add_task(async function test_addValidStreamType() {
  const registry = new StreamRegistry();

  const stream = await createFileStream("foo bar");
  const handle = registry.add(stream);

  equal(registry.streams.size, 1, "A single stream has been added");
  equal(typeof handle, "string", "Handle is of type string");
  ok(registry.streams.has(handle), "Handle has been found");

  const rv = registry.streams.get(handle);
  equal(rv, stream, "Expected stream found");
});

add_task(async function test_addCreatesDifferentHandles() {
  const registry = new StreamRegistry();
  const stream = await createFileStream("foo bar");

  const handle1 = registry.add(stream);
  equal(registry.streams.size, 1, "A single stream has been added");
  equal(typeof handle1, "string", "Handle is of type string");
  ok(registry.streams.has(handle1), "Handle has been found");
  equal(registry.streams.get(handle1), stream, "Expected stream found");

  const handle2 = registry.add(stream);
  equal(registry.streams.size, 2, "A single stream has been added");
  equal(typeof handle2, "string", "Handle is of type string");
  ok(registry.streams.has(handle2), "Handle has been found");
  equal(registry.streams.get(handle2), stream, "Expected stream found");

  notEqual(handle1, handle2, "Different handles have been generated");
});

add_task(async function test_addInvalidStreamType() {
  const registry = new StreamRegistry();
  Assert.throws(() => registry.add(new Blob([])), /UnsupportedError/);
});

add_task(async function test_getForValidHandle() {
  const registry = new StreamRegistry();
  const stream = await createFileStream("foo bar");
  const handle = registry.add(stream);

  equal(registry.streams.size, 1, "A single stream has been added");
  equal(registry.get(handle), stream, "Expected stream found");
});

add_task(async function test_getForInvalidHandle() {
  const registry = new StreamRegistry();
  const stream = await createFileStream("foo bar");
  registry.add(stream);

  equal(registry.streams.size, 1, "A single stream has been added");
  Assert.throws(() => registry.get("foo"), /TypeError/);
});

add_task(async function test_removeForValidHandle() {
  const registry = new StreamRegistry();
  const stream1 = await createFileStream("foo bar");
  const stream2 = await createFileStream("foo bar");

  const handle1 = registry.add(stream1);
  const handle2 = registry.add(stream2);

  equal(registry.streams.size, 2);

  await registry.remove(handle1);
  equal(registry.streams.size, 1);
  equal(registry.get(handle2), stream2, "Second stream has not been closed");

  ok(
    !(await IOUtils.exists(stream1.path)),
    "temporary file for first stream is removed"
  );
  ok(
    await IOUtils.exists(stream2.path),
    "temporary file for second stream is not removed"
  );
});

add_task(async function test_removeForInvalidHandle() {
  const registry = new StreamRegistry();
  const stream = await createFileStream("foo bar");
  registry.add(stream);

  equal(registry.streams.size, 1, "A single stream has been added");
  await Assert.rejects(registry.remove("foo"), /TypeError/);
});

/**
 * Create a stream with the specified contents.
 *
 * @param {string} contents
 *     Contents of the file.
 * @param {object} options
 * @param {string=} options.path
 *     Path of the file. Defaults to the temporary directory.
 * @param {boolean=} options.remove
 *     If true, automatically remove the file after the test. Defaults to true.
 *
 * @returns {Promise<Stream>}
 */
async function createFileStream(contents, options = {}) {
  let { path = null, remove = true } = options;

  if (!path) {
    path = await IOUtils.createUniqueFile(
      PathUtils.tempDir,
      "remote-agent.txt"
    );
  }

  await IOUtils.writeUTF8(path, contents);

  const stream = new Stream(path);
  if (remove) {
    registerCleanupFunction(() => stream.destroy());
  }

  return stream;
}