UNPKG

4.23 kBPlain TextView Raw
1import { BasicTests } from "../../test/helper/Basic.js";
2import { UserMedia } from "./UserMedia.js";
3import { expect } from "chai";
4import { OfflineContext } from "../core/index.js";
5
6describe("UserMedia", () => {
7 // run the common tests
8 BasicTests(UserMedia);
9
10 context("Source Tests", () => {
11 it("can be constructed with the input number", () => {
12 const extIn = new UserMedia();
13 extIn.dispose();
14 });
15
16 it("can be constructed with an options object", () => {
17 const extIn = new UserMedia({
18 volume: -10,
19 mute: false,
20 });
21 expect(extIn.volume.value).to.be.closeTo(-10, 0.1);
22 expect(extIn.mute).to.be.false;
23 extIn.dispose();
24 });
25
26 it("properties return undefined before open", () => {
27 const extIn = new UserMedia();
28 expect(extIn.deviceId).to.be.undefined;
29 expect(extIn.groupId).to.be.undefined;
30 expect(extIn.label).to.be.undefined;
31 extIn.dispose();
32 });
33
34 it("indicates if the browser has UserMedia support", () => {
35 expect(UserMedia.supported).to.be.a("boolean");
36 });
37 });
38
39 context("Opening and closing", function () {
40 // long timeout to give testers time to allow the microphone
41 this.timeout(100000);
42
43 let HAS_USER_MEDIA_INPUTS = false;
44
45 before(() => {
46 return UserMedia.enumerateDevices().then((devices) => {
47 HAS_USER_MEDIA_INPUTS = devices.length > 0;
48 });
49 });
50
51 it("open returns a promise", () => {
52 if (HAS_USER_MEDIA_INPUTS) {
53 const extIn = new UserMedia();
54 const promise = extIn.open();
55 expect(promise).to.have.property("then");
56 return promise.then(() => {
57 extIn.dispose();
58 });
59 }
60 });
61
62 it("can open an input", () => {
63 if (HAS_USER_MEDIA_INPUTS) {
64 const extIn = new UserMedia();
65 return extIn.open().then(() => {
66 extIn.dispose();
67 });
68 }
69 });
70
71 it("can open an input by name", () => {
72 if (HAS_USER_MEDIA_INPUTS) {
73 const extIn = new UserMedia();
74 let name: string;
75 return UserMedia.enumerateDevices()
76 .then((devices) => {
77 name = devices[0].deviceId;
78 return extIn.open(name);
79 })
80 .then(() => {
81 expect(extIn.deviceId).to.equal(name);
82 extIn.dispose();
83 });
84 }
85 });
86
87 it("can open an input by index", () => {
88 if (HAS_USER_MEDIA_INPUTS) {
89 const extIn = new UserMedia();
90 return extIn.open(0).then(() => {
91 extIn.dispose();
92 });
93 }
94 });
95
96 it("throws an error if it cant find the device name", () => {
97 if (HAS_USER_MEDIA_INPUTS) {
98 const extIn = new UserMedia();
99 return extIn
100 .open("doesn't exist")
101 .then(() => {
102 // shouldn't call 'then'
103 throw new Error("shouldnt call 'then'");
104 })
105 .catch(() => {
106 extIn.dispose();
107 });
108 }
109 });
110
111 it("is 'started' after media is open and 'stopped' otherwise", () => {
112 if (HAS_USER_MEDIA_INPUTS) {
113 const extIn = new UserMedia();
114 expect(extIn.state).to.equal("stopped");
115 return extIn.open().then(() => {
116 expect(extIn.state).to.equal("started");
117 extIn.dispose();
118 });
119 }
120 });
121
122 it("has a label, group and device id when open", () => {
123 if (HAS_USER_MEDIA_INPUTS) {
124 const extIn = new UserMedia();
125 return extIn.open().then(() => {
126 expect(extIn.deviceId).to.be.a("string");
127 expect(extIn.groupId).to.be.a("string");
128 expect(extIn.label).to.be.a("string");
129 extIn.dispose();
130 });
131 }
132 });
133
134 it("can reopen an input", () => {
135 if (HAS_USER_MEDIA_INPUTS) {
136 const extIn = new UserMedia();
137 return extIn
138 .open()
139 .then(() => {
140 return extIn.open();
141 })
142 .then(() => {
143 extIn.dispose();
144 });
145 }
146 });
147
148 it("can close an input", () => {
149 if (HAS_USER_MEDIA_INPUTS) {
150 const extIn = new UserMedia();
151 return extIn.open().then(() => {
152 extIn.close();
153 extIn.dispose();
154 });
155 }
156 });
157
158 it("can enumerate devices", () => {
159 return UserMedia.enumerateDevices().then((devices) => {
160 expect(devices).to.be.instanceOf(Array);
161 });
162 });
163
164 it("doesn't work in OfflineContext", (done) => {
165 if (HAS_USER_MEDIA_INPUTS) {
166 const context = new OfflineContext(2, 2, 44100);
167 const extIn = new UserMedia({ context });
168 extIn.open().catch(() => {
169 done();
170 });
171 } else {
172 done();
173 }
174 });
175 });
176});