Skip to content

Commit

Permalink
fix: use Screen Orientation API where supported (#8031)
Browse files Browse the repository at this point in the history
* feat:  introduces Screen Orientation API for detecting orientation change

* feat:  add missing unit tests
  • Loading branch information
gjanblaszczyk authored Feb 1, 2023
1 parent 267b5c6 commit 33b476d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/js/tracks/text-track-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ class TextTrackDisplay extends Component {
player.on('fullscreenchange', updateDisplayHandler);
player.on('playerresize', updateDisplayHandler);

window.addEventListener('orientationchange', updateDisplayHandler);
player.on('dispose', () => window.removeEventListener('orientationchange', updateDisplayHandler));
const screenOrientation = window.screen.orientation || window;
const changeOrientationEvent = window.screen.orientation ? 'change' : 'orientationchange';

screenOrientation.addEventListener(changeOrientationEvent, updateDisplayHandler);
player.on('dispose', () => screenOrientation.removeEventListener(changeOrientationEvent, updateDisplayHandler));

const tracks = this.options_.playerOptions.tracks || [];

Expand Down
39 changes: 39 additions & 0 deletions test/unit/tracks/text-track-display.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env qunit */
import window from 'global/window';
import Html5 from '../../../src/js/tech/html5.js';
import { constructColor } from '../../../src/js/tracks/text-track-display.js';
import Component from '../../../src/js/component.js';
Expand Down Expand Up @@ -105,6 +106,44 @@ QUnit.test('shows the default caption track first', function(assert) {
});

if (!Html5.supportsNativeTextTracks()) {
QUnit.test('text track display should attach screen orientation change event handler', function(assert) {
const oldScreen = window.screen;
const removeHandlerSpy = sinon.spy();
let changeHandlerSpy;
let changeHandlerAttached;

window.screen = {
orientation: {
addEventListener: (type, func) => {
changeHandlerAttached = true;
changeHandlerSpy = sinon.spy();
},
dispatchEvent: (type) => changeHandlerSpy(),
removeEventListener: removeHandlerSpy
}
};

const player = TestHelpers.makePlayer();

this.clock.tick(1);

assert.true(changeHandlerAttached, 'screen orientation change event handler was not attached');
assert.strictEqual(changeHandlerSpy.callCount, 0, 'screen orientation change event handler should not be called');

window.screen.orientation.dispatchEvent('change');

assert.strictEqual(changeHandlerSpy.callCount, 1, 'screen orientation change event handler was not called');

player.dispose();

assert.strictEqual(
removeHandlerSpy.callCount,
1,
'screen orientation change event handler was not removed during player dispose'
);
window.screen = oldScreen;
});

QUnit.test('selectedlanguagechange is triggered by a track mode change', function(assert) {
const player = TestHelpers.makePlayer();
const track1 = {
Expand Down

0 comments on commit 33b476d

Please sign in to comment.