From a89ec2928ec43d4140644c4635a717fd8fa1cbc5 Mon Sep 17 00:00:00 2001 From: John Beech Date: Wed, 31 May 2017 14:37:04 +0100 Subject: [PATCH] PS3Base changes (#450) * Add stub tests for all ps3base methods * Add ps3base system event test using example accessfunction * Convert any system events into message events using window.postEvent --- .../script-tests/tests/devices/ps3basetest.js | 87 +++++++++++++++---- static/script/devices/ps3base.js | 4 + 2 files changed, 73 insertions(+), 18 deletions(-) diff --git a/static/script-tests/tests/devices/ps3basetest.js b/static/script-tests/tests/devices/ps3basetest.js index 60fcddbb..6e73be95 100644 --- a/static/script-tests/tests/devices/ps3basetest.js +++ b/static/script-tests/tests/devices/ps3basetest.js @@ -4,21 +4,72 @@ */ require( - [ - 'antie/devices/ps3base', - ], - function (PS3Base) { - 'use strict'; - - describe('antie.devices.PS3Base', function () { - var device; - - beforeEach(function () { - device = new PS3Base(antie.framework.deviceConfiguration); - }); - - it('should expose nativeCallback as a method', function () { - expect(typeof device.nativeCallback).toEqual('function'); - }); - }) - }); + [ + 'antie/devices/ps3base', + 'antie/devices/data/nativejson' + ], + function (PS3Base) { + 'use strict'; + + describe('antie.devices.PS3Base', function () { + var device; + + // accessfunction is the way playstation sends system events from the WebMAF to the DOM + function example_accessfunction(json) { + // See: https://github.com/bbc/tal-page-strategies/blob/master/playstation3/body + device.nativeCallback(JSON.stringify(json)); + } + + beforeEach(function () { + device = new PS3Base(antie.framework.deviceConfiguration); + }); + + it('should respond to a registered system event', function (done) { + device.addNativeEventListener('externalParameter', function (data) { + expect(data.command).toEqual('externalParameter'); + expect(data.value).toEqual('some data'); + done(); + }); + example_accessfunction({ + command: 'externalParameter', + value: 'some data' + }); + }); + + it('should expose nativeCallback as a method', function () { + expect(typeof device.nativeCallback).toEqual('function'); + }); + + it('should expose addNativeEventListener as a method', function () { + expect(typeof device.addNativeEventListener).toEqual('function'); + }); + + it('should expose removeNativeEventListener as a method', function () { + expect(typeof device.removeNativeEventListener).toEqual('function'); + }); + + it('should expose nativeCommand as a method', function () { + expect(typeof device.nativeCommand).toEqual('function'); + }); + + it('should expose loadAuthenticatedURL as a method', function () { + expect(typeof device.loadAuthenticatedURL).toEqual('function'); + }); + + it('should expose isHDEnabled as a method', function () { + expect(typeof device.isHDEnabled).toEqual('function'); + }); + + it('should convert any system events into message events using window.postEvent', function (done) { + window.addEventListener('message', function(event) { + expect(event.data.command).toEqual('externalParameter'); + expect(event.data.value).toEqual('some data'); + done(); + }); + example_accessfunction({ + command: 'externalParameter', + value: 'some data' + }); + }); + }); + }); diff --git a/static/script/devices/ps3base.js b/static/script/devices/ps3base.js index 67e6718f..72f900c2 100644 --- a/static/script/devices/ps3base.js +++ b/static/script/devices/ps3base.js @@ -109,6 +109,10 @@ define( break; } } + + if (typeof window.postMessage === 'function') { + window.postMessage(data, '*'); + } }, addNativeEventListener: function(name, callback) { var callbacks;