Index: LayoutTests/http/tests/push_messaging/resources/pushmessagedata-worker.js |
diff --git a/LayoutTests/http/tests/push_messaging/resources/pushmessagedata-worker.js b/LayoutTests/http/tests/push_messaging/resources/pushmessagedata-worker.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..de9be938189b98d655fbea026b0ccf8080e18abc |
--- /dev/null |
+++ b/LayoutTests/http/tests/push_messaging/resources/pushmessagedata-worker.js |
@@ -0,0 +1,127 @@ |
+importScripts('../../serviceworker/resources/worker-testharness.js'); |
+importScripts('/resources/testharness-helpers.js'); |
+ |
+function createPushMessageData(data) |
+{ |
+ // The PushMessageData object does not expose a constructor, but we can get an object |
+ // initialized with our data by constructing a PushEvent. |
+ return new PushEvent('PushEvent', { data: data }).data; |
+} |
+ |
+test(function() { |
+ const textContents = 'Hello, world!'; |
+ |
+ var runTest = pushEvent => { |
+ assert_not_equals(pushEvent.data, null); |
+ assert_equals(pushEvent.data.text(), textContents); |
+ }; |
+ |
+ // JavaScript strings are UTF-16, whereas binary data for push message data will be |
+ // encoded as UTF-8. Convert accordingly. |
+ var bufferView = new TextEncoder('utf-8').encode(textContents); |
+ |
+ runTest(new PushEvent('PushEvent', { data: bufferView })); |
+ runTest(new PushEvent('PushEvent', { data: bufferView.buffer })); |
+ runTest(new PushEvent('PushEvent', { data: textContents })); |
+ |
+}, 'PushEvent can be initialized from ArrayBuffer, ArrayBufferView and USVStrings.'); |
+ |
+test(function() { |
+ var pushMessageData = createPushMessageData(); |
+ |
+ assert_equals(pushMessageData.arrayBuffer().byteLength, 0); |
+ |
+ assert_equals(pushMessageData.blob().size, 0); |
+ assert_equals(pushMessageData.blob().type, ''); |
+ |
+ // PushMessageData.json() is specified to be identical to JSON.parse() with |
+ // the message's data as the argument. JSON.parse('') throws an exception, |
+ // so verify that calling json() without a body throws the same exception. |
+ try { |
+ pushMessageData.json(); |
+ assert_unreached('Expected an exception to be thrown.'); |
+ } catch (eventDataException) { |
+ try { |
+ JSON.parse(''); |
+ assert_unreached('Expected an exception to be thrown.'); |
+ } catch (jsonParseException) { |
+ assert_equals(eventDataException.name, jsonParseException.name); |
+ assert_equals(eventDataException.message, jsonParseException.message); |
+ } |
+ } |
+ |
+ assert_equals(pushMessageData.text().length, 0); |
+ |
+}, 'PushMessageData is empty by default.'); |
+ |
+test(function() { |
+ const binaryContents = [1, 2, 3]; |
+ const textContents = 'FOOBAR'; |
+ |
+ var pushMessageDataBinary = createPushMessageData(new Uint8Array(binaryContents)), |
+ pushMessageDataString = createPushMessageData(textContents); |
+ |
+ var binaryBuffer = pushMessageDataBinary.arrayBuffer(), |
+ binaryBufferView = new Uint8Array(binaryBuffer); |
+ |
+ assert_equals(binaryBuffer.byteLength, binaryContents.length); |
+ assert_array_equals(binaryBufferView, binaryContents); |
+ |
+ var stringBuffer = pushMessageDataString.arrayBuffer(), |
+ stringBufferView = new Int8Array(stringBuffer); |
+ |
+ assert_equals(stringBufferView.length, textContents.length); |
+ assert_equals(stringBufferView[0], 70 /* F */); |
+ assert_equals(stringBufferView[3], 66 /* B */); |
+ |
+}, 'PushMessageData handling of ArrayBuffer content.'); |
+ |
+async_test(function(test) { |
+ const textContents = 'Hello, world!'; |
+ |
+ var pushMessageData = createPushMessageData(textContents); |
+ |
+ var blob = pushMessageData.blob(), |
+ reader = new FileReader(); |
+ |
+ assert_equals(blob.size, textContents.length); |
+ assert_equals(blob.type, ''); |
+ |
+ reader.addEventListener('load', () => { |
+ assert_equals(reader.result, textContents); |
+ test.done(); |
+ }); |
+ |
+ reader.readAsText(blob); |
+ |
+}, 'PushMessageData handling of Blob content.') |
+ |
+test(function() { |
+ var pushMessageDataArray = createPushMessageData('[5, 6, 7]'), |
+ pushMessageDataObject = createPushMessageData('{ "foo": { "bar": 42 } }'), |
+ pushMessageDataString = createPushMessageData('"foobar"'); |
+ |
+ var array = pushMessageDataArray.json(); |
+ assert_equals(array.length, 3); |
+ assert_equals(array[1], 6); |
+ |
+ var object = pushMessageDataObject.json(); |
+ assert_equals(object.foo.bar, 42); |
+ |
+ var string = pushMessageDataString.json(); |
+ assert_equals(string, 'foobar'); |
+ |
+}, 'PushMessageData handling of valid JSON content.'); |
+ |
+test(function() { |
+ // Note that we do not care about the exception code - it's pass through. |
+ assert_throws(null, () => createPushMessageData('\\o/').json()); |
+ |
+}, 'PushMessageData handling of invalid JSON content.'); |
+ |
+test(function() { |
+ assert_throws(null, () => new PushMessageData()); |
+ assert_throws(null, () => new PushMessageData('Hello, world!')); |
+ assert_throws(null, () => new PushMessageData(new ArrayBuffer(8))); |
+ |
+}, 'PushMessageData should not be constructable.'); |