1717 */
1818
1919import { memory , tensor3d , test_util } from '@tensorflow/tfjs-core' ;
20- import { describeBrowserEnvs , setupFakeVideoStream } from '../util/test_utils' ;
20+ import { describeBrowserEnvs , replaceHTMLVideoElementSource , setupFakeVideoStream } from '../util/test_utils' ;
2121import { WebcamIterator } from './webcam_iterator' ;
2222
2323describeBrowserEnvs ( 'WebcamIterator' , ( ) => {
@@ -27,38 +27,44 @@ describeBrowserEnvs('WebcamIterator', () => {
2727
2828 it ( 'create webcamIterator with html element' , async ( ) => {
2929 const videoElement = document . createElement ( 'video' ) ;
30- videoElement . width = 100 ;
31- videoElement . height = 200 ;
30+ videoElement . width = 160 ;
31+ videoElement . height = 90 ;
3232
3333 const webcamIterator = await WebcamIterator . create ( videoElement ) ;
34+
35+ await replaceHTMLVideoElementSource ( videoElement ) ;
36+
3437 const result = await webcamIterator . next ( ) ;
3538 expect ( result . done ) . toBeFalsy ( ) ;
36- expect ( result . value . shape ) . toEqual ( [ 200 , 100 , 3 ] ) ;
39+ expect ( result . value . shape ) . toEqual ( [ 90 , 160 , 3 ] ) ;
3740 } ) ;
3841
3942 it ( 'create webcamIterator with html element and capture' , async ( ) => {
4043 const videoElement = document . createElement ( 'video' ) ;
41- videoElement . width = 100 ;
42- videoElement . height = 200 ;
44+ videoElement . width = 160 ;
45+ videoElement . height = 90 ;
4346
4447 const webcamIterator = await WebcamIterator . create ( videoElement ) ;
48+
49+ await replaceHTMLVideoElementSource ( videoElement ) ;
50+
4551 const result = await webcamIterator . capture ( ) ;
46- expect ( result . shape ) . toEqual ( [ 200 , 100 , 3 ] ) ;
52+ expect ( result . shape ) . toEqual ( [ 90 , 160 , 3 ] ) ;
4753 } ) ;
4854
4955 it ( 'create webcamIterator with no html element' , async ( ) => {
5056 const webcamIterator = await WebcamIterator . create (
51- null , { resizeWidth : 100 , resizeHeight : 200 } ) ;
57+ null , { resizeWidth : 300 , resizeHeight : 150 } ) ;
5258 const result = await webcamIterator . next ( ) ;
5359 expect ( result . done ) . toBeFalsy ( ) ;
54- expect ( result . value . shape ) . toEqual ( [ 200 , 100 , 3 ] ) ;
60+ expect ( result . value . shape ) . toEqual ( [ 150 , 300 , 3 ] ) ;
5561 } ) ;
5662
5763 it ( 'create webcamIterator with no html element and capture' , async ( ) => {
5864 const webcamIterator = await WebcamIterator . create (
59- null , { resizeWidth : 100 , resizeHeight : 200 } ) ;
65+ null , { resizeWidth : 300 , resizeHeight : 150 } ) ;
6066 const result = await webcamIterator . capture ( ) ;
61- expect ( result . shape ) . toEqual ( [ 200 , 100 , 3 ] ) ;
67+ expect ( result . shape ) . toEqual ( [ 150 , 300 , 3 ] ) ;
6268 } ) ;
6369
6470 it ( 'create webcamIterator with no html element and no size' , async done => {
@@ -164,13 +170,14 @@ describeBrowserEnvs('WebcamIterator', () => {
164170
165171 it ( 'webcamIterator could stop' , async ( ) => {
166172 const videoElement = document . createElement ( 'video' ) ;
167- videoElement . width = 100 ;
168- videoElement . height = 100 ;
173+ videoElement . width = 160 ;
174+ videoElement . height = 90 ;
169175
170176 const webcamIterator = await WebcamIterator . create ( videoElement ) ;
177+ await replaceHTMLVideoElementSource ( videoElement ) ;
171178 const result1 = await webcamIterator . next ( ) ;
172179 expect ( result1 . done ) . toBeFalsy ( ) ;
173- expect ( result1 . value . shape ) . toEqual ( [ 100 , 100 , 3 ] ) ;
180+ expect ( result1 . value . shape ) . toEqual ( [ 90 , 160 , 3 ] ) ;
174181
175182 await webcamIterator . stop ( ) ;
176183 const result2 = await webcamIterator . next ( ) ;
@@ -180,31 +187,29 @@ describeBrowserEnvs('WebcamIterator', () => {
180187
181188 it ( 'webcamIterator could restart' , async ( ) => {
182189 const videoElement = document . createElement ( 'video' ) ;
183- videoElement . width = 100 ;
184- videoElement . height = 100 ;
190+ videoElement . width = 160 ;
191+ videoElement . height = 90 ;
185192
186193 const webcamIterator = await WebcamIterator . create ( videoElement ) ;
194+ await replaceHTMLVideoElementSource ( videoElement ) ;
187195 const result1 = await webcamIterator . next ( ) ;
188196 expect ( result1 . done ) . toBeFalsy ( ) ;
189- expect ( result1 . value . shape ) . toEqual ( [ 100 , 100 , 3 ] ) ;
197+ expect ( result1 . value . shape ) . toEqual ( [ 90 , 160 , 3 ] ) ;
190198
191199 webcamIterator . stop ( ) ;
192200 const result2 = await webcamIterator . next ( ) ;
193201 expect ( result2 . done ) . toBeTruthy ( ) ;
194202 expect ( result2 . value ) . toBeNull ( ) ;
195203
196- // Reset fake media stream after stopped the stream.
197- setupFakeVideoStream ( ) ;
198-
199- await webcamIterator . start ( ) ;
200204 // Skip validation when it's in Firefox and Mac OS, because BrowserStack for
201- // Firefox on travis does not support restarting experimental function
202- // HTMLCanvasElement.captureStream().
205+ // Firefox does not trigger the readyState event when restarting.
203206 if ( navigator . userAgent . search ( 'Firefox' ) < 0 &&
204207 navigator . userAgent . search ( 'OS X' ) < 0 ) {
208+ await webcamIterator . start ( ) ;
209+ await replaceHTMLVideoElementSource ( videoElement ) ;
205210 const result3 = await webcamIterator . next ( ) ;
206211 expect ( result3 . done ) . toBeFalsy ( ) ;
207- expect ( result3 . value . shape ) . toEqual ( [ 100 , 100 , 3 ] ) ;
212+ expect ( result3 . value . shape ) . toEqual ( [ 90 , 160 , 3 ] ) ;
208213 }
209214 } ) ;
210215
0 commit comments