Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: LayoutTests/http/tests/push_messaging/resources/pushmessagedata-worker.js

Issue 1311853002: Update PushEvent and PushMessageData to match the spec. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 importScripts('../../serviceworker/resources/worker-testharness.js');
2 importScripts('/resources/testharness-helpers.js');
3
4 function createPushMessageData(data)
5 {
6 // The PushMessageData object does not expose a constructor, but we can get an object
7 // initialized with our data by constructing a PushEvent.
8 return new PushEvent('PushEvent', { data: data }).data;
9 }
10
11 test(function() {
12 const textContents = 'Hello, world!';
13
14 var runTest = pushEvent => {
15 assert_not_equals(pushEvent.data, null);
16 assert_equals(pushEvent.data.text(), textContents);
17 };
18
19 // JavaScript strings are UTF-16, whereas binary data for push message data will be
20 // encoded as UTF-8. Convert accordingly.
21 var bufferView = new TextEncoder('utf-8').encode(textContents);
22
23 runTest(new PushEvent('PushEvent', { data: bufferView }));
24 runTest(new PushEvent('PushEvent', { data: bufferView.buffer }));
25 runTest(new PushEvent('PushEvent', { data: textContents }));
26
27 }, 'PushEvent can be initialized from ArrayBuffer, ArrayBufferView and USVString s.');
28
29 test(function() {
30 var pushMessageData = createPushMessageData();
31
32 assert_equals(pushMessageData.arrayBuffer().byteLength, 0);
33
34 assert_equals(pushMessageData.blob().size, 0);
35 assert_equals(pushMessageData.blob().type, '');
36
37 // PushMessageData.json() is specified to be identical to JSON.parse() with
38 // the message's data as the argument. JSON.parse('') throws an exception,
39 // so verify that calling json() without a body throws the same exception.
40 try {
41 pushMessageData.json();
42 assert_unreached('Expected an exception to be thrown.');
43 } catch (eventDataException) {
44 try {
45 JSON.parse('');
46 assert_unreached('Expected an exception to be thrown.');
47 } catch (jsonParseException) {
48 assert_equals(eventDataException.name, jsonParseException.name);
49 assert_equals(eventDataException.message, jsonParseException.message );
50 }
51 }
52
53 assert_equals(pushMessageData.text().length, 0);
54
55 }, 'PushMessageData is empty by default.');
56
57 test(function() {
58 const binaryContents = [1, 2, 3];
59 const textContents = 'FOOBAR';
60
61 var pushMessageDataBinary = createPushMessageData(new Uint8Array(binaryConte nts)),
62 pushMessageDataString = createPushMessageData(textContents);
63
64 var binaryBuffer = pushMessageDataBinary.arrayBuffer(),
65 binaryBufferView = new Uint8Array(binaryBuffer);
66
67 assert_equals(binaryBuffer.byteLength, binaryContents.length);
68 assert_array_equals(binaryBufferView, binaryContents);
69
70 var stringBuffer = pushMessageDataString.arrayBuffer(),
71 stringBufferView = new Int8Array(stringBuffer);
72
73 assert_equals(stringBufferView.length, textContents.length);
74 assert_equals(stringBufferView[0], 70 /* F */);
75 assert_equals(stringBufferView[3], 66 /* B */);
76
77 }, 'PushMessageData handling of ArrayBuffer content.');
78
79 async_test(function(test) {
80 const textContents = 'Hello, world!';
81
82 var pushMessageData = createPushMessageData(textContents);
83
84 var blob = pushMessageData.blob(),
85 reader = new FileReader();
86
87 assert_equals(blob.size, textContents.length);
88 assert_equals(blob.type, '');
89
90 reader.addEventListener('load', () => {
91 assert_equals(reader.result, textContents);
92 test.done();
93 });
94
95 reader.readAsText(blob);
96
97 }, 'PushMessageData handling of Blob content.')
98
99 test(function() {
100 var pushMessageDataArray = createPushMessageData('[5, 6, 7]'),
101 pushMessageDataObject = createPushMessageData('{ "foo": { "bar": 42 } }' ),
102 pushMessageDataString = createPushMessageData('"foobar"');
103
104 var array = pushMessageDataArray.json();
105 assert_equals(array.length, 3);
106 assert_equals(array[1], 6);
107
108 var object = pushMessageDataObject.json();
109 assert_equals(object.foo.bar, 42);
110
111 var string = pushMessageDataString.json();
112 assert_equals(string, 'foobar');
113
114 }, 'PushMessageData handling of valid JSON content.');
115
116 test(function() {
117 // Note that we do not care about the exception code - it's pass through.
118 assert_throws(null, () => createPushMessageData('\\o/').json());
119
120 }, 'PushMessageData handling of invalid JSON content.');
121
122 test(function() {
123 assert_throws(null, () => new PushMessageData());
124 assert_throws(null, () => new PushMessageData('Hello, world!'));
125 assert_throws(null, () => new PushMessageData(new ArrayBuffer(8)));
126
127 }, 'PushMessageData should not be constructable.');
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698