From 99d758d27c7a651bf3ebe4b43bb9df80df3d2b1f Mon Sep 17 00:00:00 2001 From: Conrad Chan Date: Wed, 23 Jun 2021 15:48:07 -0700 Subject: [PATCH] fix(controls): Default to show CC --- src/lib/viewers/media/DashViewer.js | 6 +++++ .../media/__tests__/DashViewer-test.js | 8 ++++++ test/integration/media/DashViewer.e2e.test.js | 2 ++ test/support/mediaSettingsTests.js | 25 ++++++++++++++++++- 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/lib/viewers/media/DashViewer.js b/src/lib/viewers/media/DashViewer.js index d86fa1c5d..83a07e429 100644 --- a/src/lib/viewers/media/DashViewer.js +++ b/src/lib/viewers/media/DashViewer.js @@ -634,6 +634,12 @@ class DashViewer extends VideoBaseViewer { */ initSubtitles() { const clientLanguage = getLanguageName(this.options.location.locale.substring(0, 2)); + const showSubtitles = this.cache.get('media-subtitles-toggle'); + + // If no entry in the cache (nor localstorage) default to showing CC + if (showSubtitles === undefined) { + this.cache.set('media-subtitles-toggle', true, true); + } this.textTracks = this.textTracks.map(track => ({ ...track, diff --git a/src/lib/viewers/media/__tests__/DashViewer-test.js b/src/lib/viewers/media/__tests__/DashViewer-test.js index ab47a48ce..118f6a7a2 100644 --- a/src/lib/viewers/media/__tests__/DashViewer-test.js +++ b/src/lib/viewers/media/__tests__/DashViewer-test.js @@ -1644,6 +1644,8 @@ describe('lib/viewers/media/DashViewer', () => { beforeEach(() => { jest.spyOn(dash, 'getSubtitleId').mockImplementation(); jest.spyOn(dash, 'setSubtitle').mockImplementation(); + jest.spyOn(dash.cache, 'get').mockImplementation(); + jest.spyOn(dash.cache, 'set').mockImplementation(); const english = { language: 'eng', id: 5 }; const russian = { language: 'rus', id: 4 }; @@ -1691,6 +1693,12 @@ describe('lib/viewers/media/DashViewer', () => { expect(dash.selectedSubtitle).toBe(4); }); + + test('should set media-subtitles-toggle to true by default', () => { + dash.initSubtitles(); + + expect(dash.cache.set).toBeCalledWith('media-subtitles-toggle', true, true); + }); }); describe('setSubtitle()', () => { diff --git a/test/integration/media/DashViewer.e2e.test.js b/test/integration/media/DashViewer.e2e.test.js index b2f003582..696dc484b 100644 --- a/test/integration/media/DashViewer.e2e.test.js +++ b/test/integration/media/DashViewer.e2e.test.js @@ -50,6 +50,8 @@ describe('Dash Viewer', () => { runQualityMenuTests(); runAudioTracksTests(); + + runSubtitlesTests(); }); }); }); diff --git a/test/support/mediaSettingsTests.js b/test/support/mediaSettingsTests.js index 754dcbbcf..f3cc2d426 100644 --- a/test/support/mediaSettingsTests.js +++ b/test/support/mediaSettingsTests.js @@ -110,7 +110,7 @@ export function runSubtitlesTests() { cy.getByTestId('bp-media-settings-subtitles').contains('Spanish'); }); - it('Should be able to turn off subtitles via the button', () => { + it('Toggle CC button should turn off and on subtitles', () => { cy.getByTitle('Subtitles/Closed Captions') .as('subtitlesBtn') .should('be.visible') @@ -122,7 +122,18 @@ export function runSubtitlesTests() { .click() .should('have.attr', 'aria-pressed', 'false'); + cy.getByTitle('Settings').click(); + cy.getByTestId('bp-media-settings-subtitles').contains('Off'); + + // Toggling CC back on should restore the previously used text track + cy.get('@subtitlesBtn') + .click() + .should('have.attr', 'aria-pressed', 'true'); + + cy.getByTitle('Settings').click(); + + cy.getByTestId('bp-media-settings-subtitles').contains('English'); }); it('Should be able to turn off subtitles via the menu', () => { @@ -142,6 +153,18 @@ export function runSubtitlesTests() { cy.get('@subtitlesBtn').should('have.attr', 'aria-pressed', 'false'); cy.getByTestId('bp-media-settings-subtitles').contains('Off'); + + // Toggling CC back on should restore the previously used text track + cy.getByTitle('Subtitles/Closed Captions') + .as('subtitlesBtn') + .should('be.visible') + .should('have.attr', 'aria-pressed', 'false') + .click() + .should('have.attr', 'aria-pressed', 'true'); + + cy.getByTitle('Settings').click(); + + cy.getByTestId('bp-media-settings-subtitles').contains('English'); }); }); }