From f5b237293b6e79299ec62a6ff2dabc762e963385 Mon Sep 17 00:00:00 2001 From: "Hawdziejuk, Kamil" Date: Mon, 19 Sep 2022 23:28:15 -0700 Subject: [PATCH] * implementing turning off camera (setting deviceId to null; showWebcam to false; and most importantly releasing resources by calling nextWebcam.complete() on a subject) * adding unit test --- src/app/app.component.spec.ts | 17 +++++++++++++++++ src/app/app.component.ts | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index 6879b0e..5695e62 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -15,15 +15,32 @@ describe('AppComponent', () => { ] }).compileComponents(); })); + it('should create the app', waitForAsync(() => { const fixture = TestBed.createComponent(AppComponent); const app = fixture.debugElement.componentInstance; expect(app).toBeTruthy(); })); + it('should render title in a h1 tag', waitForAsync(() => { const fixture = TestBed.createComponent(AppComponent); fixture.detectChanges(); const compiled = fixture.debugElement.nativeElement; expect(compiled.querySelector('h1').textContent).toContain('ngx-webcam Demo'); })); + + it('closing camera connection when turning off camera', waitForAsync(() => { + const fixture = TestBed.createComponent(AppComponent); + const app = fixture.debugElement.componentInstance; + app.deviceId = '111'; + app.showWebcam = true; + const compiled = fixture.debugElement.nativeElement; + + app.turnOffCamera(); + + expect(app.deviceId).toBeNull(); + expect(app.showWebcam).toBeFalse(); + expect(compiled.querySelector('webcam')).toBeNull(); + })); + }); diff --git a/src/app/app.component.ts b/src/app/app.component.ts index f500bb0..95950b2 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -36,6 +36,15 @@ export class AppComponent implements OnInit { public toggleWebcam(): void { this.showWebcam = !this.showWebcam; + if (!this.showWebcam) { + this.turnOffCamera(); + } + } + + public turnOffCamera(): void { + this.deviceId = null; + this.showWebcam = false; + this.nextWebcam.complete(); } public handleInitError(error: WebcamInitError): void {