Best JavaScript code snippet using wpt
NDEFRecord_constructor.https.window.js
Source:NDEFRecord_constructor.https.window.js
1// META: script=/resources/testharness.js2// META: script=/resources/testharnessreport.js3// META: script=resources/nfc-helpers.js4// NDEFRecord constructor5// https://w3c.github.io/web-nfc/#dom-ndefrecord6test(() => {7 assert_equals(NDEFRecord.length, 1);8 assert_throws_js(TypeError, () => new NDEFRecord());9}, 'NDEFRecord constructor without init dict');10test(() => {11 assert_throws_js(12 TypeError, () => new NDEFRecord(null),13 'NDEFRecordInit#recordType is a required field.');14}, 'NDEFRecord constructor with null init dict');15test(() => {16 assert_throws_js(17 TypeError,18 () => new NDEFRecord({id: test_record_id, data: test_text_data}),19 'NDEFRecordInit#recordType is a required field.');20}, 'NDEFRecord constructor without NDEFRecordInit#recordType field');21test(() => {22 assert_throws_js(23 TypeError,24 () =>25 new NDEFRecord(createRecord('empty', test_text_data, test_record_id)),26 'id does not apply for empty record type.');27}, 'NDEFRecord constructor with empty record type and id');28test(() => {29 assert_throws_js(30 TypeError,31 () => new NDEFRecord(32 createRecord('empty', test_text_data, test_record_id, 'text/plain')),33 'mediaType does not apply for empty record type.');34 assert_throws_js(35 TypeError,36 () => new NDEFRecord(37 createRecord('text', test_text_data, test_record_id, 'text/plain')),38 'mediaType does not apply for text record type.');39 assert_throws_js(40 TypeError,41 () => new NDEFRecord(42 createRecord('url', test_url_data, test_record_id, 'text/plain')),43 'mediaType does not apply for url record type.');44 assert_throws_js(45 TypeError,46 () => new NDEFRecord(createRecord(47 'absolute-url', test_url_data, test_record_id, 'text/plain')),48 'mediaType does not apply for absolute-url record type.');49 assert_throws_js(50 TypeError,51 () => new NDEFRecord(createRecord(52 'unknown', test_buffer_data, test_record_id,53 'application/octet-stream')),54 'mediaType does not apply for unknown record type.');55 assert_throws_js(56 TypeError,57 () => new NDEFRecord(createRecord(58 'foo.example.com:bar', test_buffer_data, test_record_id,59 'application/octet-stream')),60 'mediaType does not apply for external record type.');61}, 'NDEFRecord constructor should only accept mediaType for mime record type');62test(63 () => {64 {65 const record = new NDEFRecord(createRecord('text', test_text_data));66 assert_equals(record.id, null, 'id');67 } {const record =68 new NDEFRecord(createRecord('text', test_text_data, ''));69 assert_equals(record.id, '', 'id');} {70 const dummy_id = 'https://dummy_host/mypath/myid';71 const record =72 new NDEFRecord(createRecord('text', test_text_data, dummy_id));73 assert_equals(record.id, dummy_id, 'id');74 } {const dummy_id = 'http://dummy_host/mypath/myid';75 const record =76 new NDEFRecord(createRecord('text', test_text_data, dummy_id));77 assert_equals(record.id, dummy_id, 'id');} {78 const dummy_id = 'mypath/myid';79 const record =80 new NDEFRecord(createRecord('text', test_text_data, dummy_id));81 assert_equals(record.id, dummy_id, 'id');82 }83 },84 'NDEFRecord constructor with custom record ids');85test(() => {86 const record = new NDEFRecord(createRecord('empty'));87 assert_equals(record.recordType, 'empty', 'recordType');88 assert_equals(record.mediaType, null, 'mediaType');89 assert_equals(record.id, null, 'id');90 assert_equals(record.encoding, null, 'encoding');91 assert_equals(record.lang, null, 'lang');92 assert_equals(record.data, null, 'data');93 assert_throws_dom(94 'NotSupportedError', () => record.toRecords(),95 'Only smart-poster, external type and local type records could have embedded records.');96}, 'NDEFRecord constructor with empty record type');97test(() => {98 const record = new NDEFRecord(createTextRecord(test_text_data));99 assert_equals(record.recordType, 'text', 'recordType');100 assert_equals(record.mediaType, null, 'mediaType');101 assert_equals(record.id, test_record_id, 'id');102 assert_equals(record.encoding, 'utf-8', 'encoding');103 assert_equals(record.lang, 'en', 'lang');104 const decoder = new TextDecoder();105 assert_equals(106 decoder.decode(record.data), test_text_data,107 'data has the same content with the original dictionary');108 assert_throws_dom(109 'NotSupportedError', () => record.toRecords(),110 'Only smart-poster, external type and local type records could have embedded records.');111}, 'NDEFRecord constructor with text record type and string data');112test(() => {113 const encoder = new TextEncoder();114 const uint8Array = encoder.encode(test_text_data);115 const record = new NDEFRecord(createTextRecord(uint8Array.buffer));116 assert_equals(record.recordType, 'text', 'recordType');117 assert_equals(record.mediaType, null, 'mediaType');118 assert_equals(record.id, test_record_id, 'id');119 // By default, 'utf-8'.120 assert_equals(record.encoding, 'utf-8', 'encoding');121 assert_equals(record.lang, 'en', 'lang');122 const decoder = new TextDecoder();123 assert_equals(124 decoder.decode(record.data), test_text_data,125 'data has the same content with the original dictionary');126 assert_throws_dom(127 'NotSupportedError', () => record.toRecords(),128 'Only smart-poster, external type and local type records could have embedded records.');129}, 'NDEFRecord constructor with text record type and arrayBuffer data');130test(() => {131 const encoder = new TextEncoder();132 const uint8Array = encoder.encode(test_text_data);133 const record = new NDEFRecord(createTextRecord(uint8Array));134 assert_equals(record.recordType, 'text', 'recordType');135 assert_equals(record.mediaType, null, 'mediaType');136 assert_equals(record.id, test_record_id, 'id');137 // By default, 'utf-8'.138 assert_equals(record.encoding, 'utf-8', 'encoding');139 assert_equals(record.lang, 'en', 'lang');140 const decoder = new TextDecoder();141 assert_equals(142 decoder.decode(record.data), test_text_data,143 'data has the same content with the original dictionary');144 assert_throws_dom(145 'NotSupportedError', () => record.toRecords(),146 'Only smart-poster, external type and local type records could have embedded records.');147}, 'NDEFRecord constructor with text record type and arrayBufferView data');148test(() => {149 assert_throws_js(150 TypeError,151 () =>152 new NDEFRecord(createTextRecord(test_text_data, 'random-encoding')));153 assert_throws_js(154 TypeError,155 () => new NDEFRecord(createTextRecord(test_text_data, 'utf-16')));156 // Only 'utf-8' is OK for a DOMString data source.157 const record =158 new NDEFRecord(createTextRecord(test_text_data, 'utf-8', 'fr'));159 assert_equals(record.recordType, 'text', 'recordType');160 assert_equals(record.encoding, 'utf-8', 'encoding');161 assert_equals(record.lang, 'fr', 'lang');162 const decoder = new TextDecoder();163 assert_equals(164 decoder.decode(record.data), test_text_data,165 'data has the same content with the original text');166 assert_throws_js(167 TypeError,168 () => new NDEFRecord(createTextRecord(169 encodeTextToArrayBuffer(test_text_data, 'utf-8'),170 'random-encoding')));171 // The encoding list valid for a BufferSource data source.172 const encodings = ['utf-8', 'utf-16', 'utf-16be', 'utf-16le'];173 for (const encoding of encodings) {174 const record = new NDEFRecord(createTextRecord(175 encodeTextToArrayBuffer(test_text_data, encoding), encoding, 'fr'));176 assert_equals(record.recordType, 'text', 'recordType');177 assert_equals(record.encoding, encoding, 'encoding');178 assert_equals(record.lang, 'fr', 'lang');179 const decoder = new TextDecoder(record.encoding);180 assert_equals(181 decoder.decode(record.data), test_text_data,182 'data has the same content with the original text. encoding: ' +183 encoding);184 }185}, 'NDEFRecord constructor with text record type, encoding, and lang');186test(t => {187 const previous_lang = document.querySelector('html').getAttribute('lang');188 const test_lang = 'fr';189 document.querySelector('html').setAttribute('lang', test_lang);190 t.add_cleanup(() => {191 document.querySelector('html').setAttribute('lang', previous_lang);192 });193 const record = new NDEFRecord(createTextRecord(test_text_data));194 assert_equals(record.recordType, 'text', 'recordType');195 assert_equals(record.mediaType, null, 'mediaType');196 assert_equals(record.id, test_record_id, 'id');197 assert_equals(record.encoding, 'utf-8', 'encoding');198 assert_equals(record.lang, test_lang, 'lang');199 const decoder = new TextDecoder();200 assert_equals(201 decoder.decode(record.data), test_text_data,202 'data has the same content with the original dictionary');203}, 'NDEFRecord constructor with text record type and custom document language');204test(() => {205 const record = new NDEFRecord(createUrlRecord(test_url_data));206 assert_equals(record.recordType, 'url', 'recordType');207 assert_equals(record.mediaType, null, 'mediaType');208 assert_equals(record.id, test_record_id, 'id');209 const decoder = new TextDecoder();210 assert_equals(211 decoder.decode(record.data), test_url_data,212 'data has the same content with the original dictionary');213 assert_throws_dom(214 'NotSupportedError', () => record.toRecords(),215 'Only smart-poster, external type and local type records could have embedded records.');216}, 'NDEFRecord constructor with url record type');217test(() => {218 const record = new NDEFRecord(createUrlRecord(test_url_data, true));219 assert_equals(record.recordType, 'absolute-url', 'recordType');220 assert_equals(record.mediaType, null, 'mediaType');221 assert_equals(record.id, test_record_id, 'id');222 const decoder = new TextDecoder();223 assert_equals(224 decoder.decode(record.data), test_url_data,225 'data has the same content with the original dictionary');226 assert_throws_dom(227 'NotSupportedError', () => record.toRecords(),228 'Only smart-poster, external type and local type records could have embedded records.');229}, 'NDEFRecord constructor with absolute-url record type');230test(() => {231 assert_throws_js(232 TypeError,233 () => new NDEFRecord(createMimeRecord('A string is not a BufferSource')),234 'Only BufferSource is allowed to be the record data.');235 let buffer = new ArrayBuffer(4);236 new Uint8Array(buffer).set([1, 2, 3, 4]);237 // Feed ArrayBuffer.238 {239 const record = new NDEFRecord(createMimeRecord(buffer));240 assert_equals(record.recordType, 'mime', 'recordType');241 assert_equals(record.mediaType, 'application/octet-stream', 'mediaType');242 assert_equals(record.id, test_record_id, 'id');243 assert_array_equals(244 new Uint8Array(record.data.buffer), [1, 2, 3, 4],245 'data has the same content with the original buffer');246 assert_throws_dom(247 'NotSupportedError', () => record.toRecords(),248 'Only smart-poster, external type and local type records could have embedded records.');249 }250 // Feed ArrayBufferView.251 {252 let buffer_view = new Uint8Array(buffer, 1);253 const record = new NDEFRecord(createMimeRecord(buffer_view));254 assert_equals(record.recordType, 'mime', 'recordType');255 assert_equals(record.id, test_record_id, 'id');256 assert_array_equals(257 new Uint8Array(record.data.buffer), [2, 3, 4],258 'data has the same content with the original buffer view');259 assert_throws_dom(260 'NotSupportedError', () => record.toRecords(),261 'Only smart-poster, external type and local type records could have embedded records.');262 }263}, 'NDEFRecord constructor with mime record type and stream data');264test(() => {265 const record = new NDEFRecord(createMimeRecordFromJson(test_json_data));266 assert_equals(record.recordType, 'mime', 'recordType');267 assert_equals(record.mediaType, 'application/json', 'mediaType');268 assert_equals(record.id, test_record_id, 'id');269 assert_object_equals(270 JSON.parse(new TextDecoder().decode(record.data)), test_json_data,271 'data has the same content with the original json');272 assert_throws_dom(273 'NotSupportedError', () => record.toRecords(),274 'Only smart-poster, external type and local type records could have embedded records.');275}, 'NDEFRecord constructor with mime record type and json data');276test(() => {277 assert_throws_js(278 TypeError,279 () =>280 new NDEFRecord(createUnknownRecord('A string is not a BufferSource')),281 'Only BufferSource is allowed to be the record data.');282 let buffer = new ArrayBuffer(4);283 new Uint8Array(buffer).set([1, 2, 3, 4]);284 // Feed ArrayBuffer.285 {286 const record = new NDEFRecord(createUnknownRecord(buffer));287 assert_equals(record.recordType, 'unknown', 'recordType');288 assert_equals(record.id, test_record_id, 'id');289 assert_array_equals(290 new Uint8Array(record.data.buffer), [1, 2, 3, 4],291 'data has the same content with the original buffer');292 assert_throws_dom(293 'NotSupportedError', () => record.toRecords(),294 'Only smart-poster, external type and local type records could have embedded records.');295 }296 // Feed ArrayBufferView.297 {298 let buffer_view = new Uint8Array(buffer, 1);299 const record = new NDEFRecord(createUnknownRecord(buffer_view));300 assert_equals(record.recordType, 'unknown', 'recordType');301 assert_equals(record.id, test_record_id, 'id');302 assert_array_equals(303 new Uint8Array(record.data.buffer), [2, 3, 4],304 'data has the same content with the original buffer view');305 assert_throws_dom(306 'NotSupportedError', () => record.toRecords(),307 'Only smart-poster, external type and local type records could have embedded records.');308 }309}, 'NDEFRecord constructor with unknown record type');310test(() => {311 assert_throws_js(312 TypeError,313 () => new NDEFRecord(createRecord(314 'foo.eXamPle.com:bAr*-',315 'A string is not a BufferSource or NDEFMessageInit')),316 'Only BufferSource and NDEFMessageInit are allowed to be the record data.');317 let buffer = new ArrayBuffer(4);318 new Uint8Array(buffer).set([1, 2, 3, 4]);319 // Feed ArrayBuffer.320 {321 const record = new NDEFRecord(322 createRecord('foo.eXamPle.com:bAr*-', buffer, test_record_id));323 assert_equals(record.recordType, 'foo.eXamPle.com:bAr*-', 'recordType');324 assert_equals(record.mediaType, null, 'mediaType');325 assert_equals(record.id, test_record_id, 'id');326 assert_array_equals(327 new Uint8Array(record.data.buffer), [1, 2, 3, 4],328 'data has the same content with the original buffer');329 assert_equals(330 record.toRecords(), null,331 'toRecords() returns null if the payload is not an NDEF message.');332 }333 // Feed ArrayBufferView.334 {335 let buffer_view = new Uint8Array(buffer, 1);336 const record = new NDEFRecord(337 createRecord('foo.eXamPle.com:bAr*-', buffer_view, test_record_id));338 assert_equals(record.recordType, 'foo.eXamPle.com:bAr*-', 'recordType');339 assert_equals(record.mediaType, null, 'mediaType');340 assert_equals(record.id, test_record_id, 'id');341 assert_array_equals(342 new Uint8Array(record.data.buffer), [2, 3, 4],343 'data has the same content with the original buffer view');344 assert_equals(345 record.toRecords(), null,346 'toRecords() returns null if the payload is not an NDEF message.');347 }348 // Feed NDEFMessageInit.349 {350 const payload_message = createMessage([createTextRecord(test_text_data)]);351 const record = new NDEFRecord(createRecord(352 'foo.eXamPle.com:bAr*-', payload_message, 'dummy_record_id'));353 assert_equals(record.recordType, 'foo.eXamPle.com:bAr*-', 'recordType');354 assert_equals(record.mediaType, null, 'mediaType');355 assert_equals(record.id, 'dummy_record_id', 'id');356 const embedded_records = record.toRecords();357 assert_equals(embedded_records.length, 1, 'Only one embedded record.');358 // The only one embedded record has correct content.359 assert_equals(embedded_records[0].recordType, 'text', 'recordType');360 assert_equals(embedded_records[0].mediaType, null, 'mediaType');361 assert_equals(embedded_records[0].id, test_record_id, 'id');362 const decoder = new TextDecoder();363 assert_equals(364 decoder.decode(embedded_records[0].data), test_text_data,365 'data has the same content with the original dictionary');366 }367}, 'NDEFRecord constructor with external record type');368test(() => {369 assert_throws_js(370 TypeError, () => new NDEFRecord(createRecord(':xyz', test_buffer_data)),371 'The local type record must be embedded in the payload of another record (smart-poster, external, or local)');372 // The following test cases use an external type record embedding our target373 // local type record.374 const local_record =375 createRecord(':xyz', undefined /* data */, 'dummy_id_for_local_type');376 const payload_message = createMessage([local_record]);377 const external_record_embedding_local_record =378 createRecord('example.com:foo', payload_message);379 local_record.data = 'A string is not a BufferSource or NDEFMessageInit';380 assert_throws_js(381 TypeError, () => new NDEFRecord(external_record_embedding_local_record),382 'Only BufferSource and NDEFMessageInit are allowed to be the record data.');383 let buffer = new ArrayBuffer(4);384 new Uint8Array(buffer).set([1, 2, 3, 4]);385 // Feed ArrayBuffer.386 {387 local_record.data = buffer;388 const record = new NDEFRecord(external_record_embedding_local_record);389 const embedded_records = record.toRecords();390 assert_equals(391 embedded_records.length, 1, 'Only the one embedded local record.');392 // The embedded local record is actually from |local_record|.393 assert_equals(embedded_records[0].recordType, ':xyz', 'recordType');394 assert_equals(embedded_records[0].mediaType, null, 'mediaType');395 assert_equals(embedded_records[0].id, 'dummy_id_for_local_type', 'id');396 assert_array_equals(397 new Uint8Array(embedded_records[0].data.buffer), [1, 2, 3, 4],398 'data has the same content with the original buffer');399 assert_equals(400 embedded_records[0].toRecords(), null,401 'toRecords() returns null if the payload is not an NDEF message.');402 }403 // Feed ArrayBufferView.404 {405 let buffer_view = new Uint8Array(buffer, 1);406 local_record.data = buffer_view;407 const record = new NDEFRecord(external_record_embedding_local_record);408 const embedded_records = record.toRecords();409 assert_equals(410 embedded_records.length, 1, 'Only the one embedded local record.');411 // The embedded local record is actually from |local_record|.412 assert_equals(embedded_records[0].recordType, ':xyz', 'recordType');413 assert_equals(embedded_records[0].mediaType, null, 'mediaType');414 assert_equals(embedded_records[0].id, 'dummy_id_for_local_type', 'id');415 assert_array_equals(416 new Uint8Array(embedded_records[0].data.buffer), [2, 3, 4],417 'data has the same content with the original buffer view');418 assert_equals(419 embedded_records[0].toRecords(), null,420 'toRecords() returns null if the payload is not an NDEF message.');421 }422 // Feed NDEFMessageInit.423 {424 const payload_message = createMessage([createTextRecord(test_text_data)]);425 local_record.data = payload_message;426 const record = new NDEFRecord(external_record_embedding_local_record);427 const embedded_records = record.toRecords();428 assert_equals(429 embedded_records.length, 1, 'Only the one embedded local record.');430 // The embedded local record is actually from |local_record|.431 assert_equals(embedded_records[0].recordType, ':xyz', 'recordType');432 assert_equals(embedded_records[0].mediaType, null, 'mediaType');433 assert_equals(embedded_records[0].id, 'dummy_id_for_local_type', 'id');434 // The embedded local record embeds another text record that's from435 // |payload_message|.436 const embedded_records_in_local_record = embedded_records[0].toRecords();437 assert_equals(438 embedded_records_in_local_record.length, 1,439 'Only one embedded record.');440 // The only one embedded record has correct content.441 assert_equals(442 embedded_records_in_local_record[0].recordType, 'text', 'recordType');443 assert_equals(444 embedded_records_in_local_record[0].mediaType, null, 'mediaType');445 assert_equals(embedded_records_in_local_record[0].id, test_record_id, 'id');446 const decoder = new TextDecoder();447 assert_equals(448 decoder.decode(embedded_records_in_local_record[0].data),449 test_text_data,450 'data has the same content with the original dictionary');451 }452}, 'NDEFRecord constructor with local record type');453test(() => {454 let buffer = new ArrayBuffer(4);455 new Uint8Array(buffer).set([1, 2, 3, 4]);456 const encoder = new TextEncoder();457 const uri_record = createUrlRecord(test_url_data);458 const title_record = createTextRecord(test_text_data, 'utf-8', 'en');459 const type_record = createRecord(':t', encoder.encode('image/gif'));460 const size_record = createRecord(':s', new Uint32Array([4096]));461 const action_record = createRecord(':act', new Uint8Array([3]));462 const icon_record = createRecord('mime', buffer, test_record_id, 'image/gif');463 const payload_message = createMessage([464 uri_record, title_record, type_record, size_record, action_record,465 icon_record466 ]);467 const smart_poster_record =468 createRecord('smart-poster', payload_message, 'dummy_record_id');469 const record = new NDEFRecord(smart_poster_record);470 assert_equals(record.recordType, 'smart-poster', 'recordType');471 assert_equals(record.mediaType, null, 'mediaType');472 assert_equals(record.id, 'dummy_record_id', 'id');473 const embedded_records = record.toRecords();474 assert_equals(embedded_records.length, 6, 'length');475 const decoder = new TextDecoder();476 let embedded_record_types = [];477 for (let record of embedded_records) {478 embedded_record_types.push(record.recordType);479 switch (record.recordType) {480 case 'url':481 assert_equals(record.mediaType, null, 'uri record\'s mediaType');482 assert_equals(record.id, test_record_id, 'uri record\'s id');483 assert_equals(484 decoder.decode(record.data), test_url_data, 'uri record\'s data');485 break;486 case 'text':487 assert_equals(record.mediaType, null, 'title record\'s mediaType');488 assert_equals(record.id, test_record_id, 'title record\'s id');489 assert_equals(record.encoding, 'utf-8', 'title record\'s encoding');490 assert_equals(record.lang, 'en', 'title record\'s lang');491 assert_equals(492 decoder.decode(record.data), test_text_data,493 'title record\'s data');494 break;495 case ':t':496 assert_equals(record.mediaType, null, 'type record\'s mediaType');497 assert_equals(record.id, null, 'type record\'s id');498 assert_equals(499 decoder.decode(record.data), 'image/gif', 'type record\'s data');500 break;501 case ':s':502 assert_equals(record.mediaType, null, 'size record\'s mediaType');503 assert_equals(record.id, null, 'size record\'s id');504 assert_equals(505 record.data.byteLength, 4, 'byteLength of size record\'s data');506 assert_equals(507 new Uint32Array(record.data.buffer)[0], 4096,508 'value of size record\'s data');509 break;510 case ':act':511 assert_equals(record.mediaType, null, 'action record\'s mediaType');512 assert_equals(record.id, null, 'action record\'s id');513 assert_equals(514 record.data.byteLength, 1, 'byteLength of action record\'s data');515 assert_equals(516 new Uint8Array(record.data.buffer)[0], 3,517 'value of action record\'s data');518 break;519 case 'mime':520 assert_equals(521 record.mediaType, 'image/gif', 'icon record\'s mediaType');522 assert_equals(record.id, test_record_id, 'icon record\'s id');523 assert_array_equals(524 new Uint8Array(record.data.buffer), [1, 2, 3, 4],525 'icon record\'s mediaType');526 break;527 default:528 assert_unreached('Unknown recordType');529 }530 }531 assert_array_equals(532 embedded_record_types.sort(), [':act', ':s', ':t', 'mime', 'text', 'url'],533 'smart-poster record\'s contained record types');534}, 'NDEFRecord constructor with smart-poster record type');535test(() => {536 const uri_record = createUrlRecord(test_url_data);537 const smart_poster_record = createRecord(538 'smart-poster', createMessage([uri_record]), 'dummy_record_id');539 const record = new NDEFRecord(smart_poster_record);540 assert_equals(record.recordType, 'smart-poster', 'recordType');541 assert_equals(record.mediaType, null, 'mediaType');542 assert_equals(record.id, 'dummy_record_id', 'id');543 const embedded_records = record.toRecords();544 // smart-poster record only contains a uri record.545 assert_equals(embedded_records.length, 1, 'length');546 const decoder = new TextDecoder();547 assert_equals(548 embedded_records[0].recordType, 'url', 'uri record\'s recordType');549 assert_equals(embedded_records[0].mediaType, null, 'uri record\'s mediaType');550 assert_equals(embedded_records[0].id, test_record_id, 'uri record\'s id');551 assert_equals(552 decoder.decode(embedded_records[0].data), test_url_data,553 'uri record\'s data');554}, 'NDEFRecord constructor with smart-poster record type that contains only a mandatory uri record');555test(() => {556 assert_throws_js(557 TypeError, () => new NDEFRecord(createRecord('EMptY')),558 'Unknown record type.');559 assert_throws_js(560 TypeError, () => new NDEFRecord(createRecord('TeXt', test_text_data)),561 'Unknown record type.');562 assert_throws_js(563 TypeError, () => new NDEFRecord(createRecord('uRL', test_url_data)),564 'Unknown record type.');565 assert_throws_js(566 TypeError, () => new NDEFRecord(createRecord('Mime', test_buffer_data)),567 'Unknown record type.');568 assert_throws_js(569 TypeError,570 () => new NDEFRecord(createRecord('sMart-PosTER', test_url_data)),571 'Unknown record type.');572}, 'NDEFRecord constructor with record type string being treated as case sensitive');573test(() => {574 assert_throws_js(575 TypeError,576 () => new NDEFRecord(createRecord('example.com:hellö', test_buffer_data)),577 'The external type must be an ASCII string.');578 // Length of the external type is 255, OK.579 const record = new NDEFRecord(createRecord(580 [...Array(251)].map(_ => 'a').join('') + ':xyz', test_buffer_data));581 // Exceeding 255, Throws.582 assert_throws_js(583 TypeError,584 () => new NDEFRecord(createRecord(585 [...Array(252)].map(_ => 'a').join('') + ':xyz', test_buffer_data)),586 'The external type should not be longer than 255.');587 assert_throws_js(588 TypeError, () => new NDEFRecord(createRecord('xyz', test_buffer_data)),589 'The external type must have a \':\'.');590 assert_throws_js(591 TypeError, () => new NDEFRecord(createRecord(':xyz', test_buffer_data)),592 'The domain should not be empty.');593 assert_throws_js(594 TypeError,595 () => new NDEFRecord(createRecord('example.com:', test_buffer_data)),596 'The type should not be empty.');597 assert_throws_js(598 TypeError,599 () => new NDEFRecord(createRecord('example.com:xyz[', test_buffer_data)),600 'The type should not contain \'[\'.');601 assert_throws_js(602 TypeError,603 () => new NDEFRecord(createRecord('example.com:xyz~', test_buffer_data)),604 'The type should not contain \'~\'.');605 assert_throws_js(606 TypeError,607 () => new NDEFRecord(createRecord('example.com:xyz/', test_buffer_data)),608 'The type should not contain \'/\'.');609}, 'NDEFRecord constructor with invalid external record type');610test(() => {611 const encoder = new TextEncoder();612 const uri_record = createUrlRecord(test_url_data);613 const title_record = createTextRecord(test_text_data, 'utf-8', 'en');614 const type_record = createRecord(':t', encoder.encode('image/gif'));615 const size_record = createRecord(':s', new Uint32Array([4096]));616 const action_record = createRecord(':act', new Uint8Array([0]));617 const icon_record =618 createRecord('mime', test_buffer_data, test_record_id, 'image/gif');619 const invalid_data_list = [620 {621 data: 'A string is not a NDEFMessageInit',622 message: 'A string is not allowed.'623 },624 {data: test_buffer_data, message: 'An ArrayBuffer is not allowed.'}, {625 data: createMessage(626 [title_record, type_record, size_record, action_record, icon_record]),627 message: 'Must contain a URI record.'628 },629 {630 data: createMessage([631 uri_record, title_record, type_record, size_record, action_record,632 icon_record, uri_record633 ]),634 message: 'Must not contain more than one uri record.'635 },636 {637 data: createMessage([638 uri_record, title_record, type_record, size_record, action_record,639 icon_record, type_record640 ]),641 message: 'Must not contain more than one type record.'642 },643 {644 data: createMessage([645 uri_record, title_record, type_record, size_record, action_record,646 icon_record, size_record647 ]),648 message: 'Must not contain more than one size record.'649 },650 {651 data: createMessage([652 uri_record, title_record, type_record, size_record, action_record,653 icon_record, action_record654 ]),655 message: 'Must not contain more than one action record.'656 },657 {658 data: createMessage([659 uri_record, title_record, type_record, action_record, icon_record,660 createRecord(':s', new Uint8Array([1]))661 ]),662 message:663 'Size record must have payload as 4-byte 32 bit unsigned integer.'664 },665 {666 data: createMessage([667 uri_record, title_record, type_record, size_record, icon_record,668 createRecord(':act', new Uint32Array([0]))669 ]),670 message:671 'Action record must have payload as 1-byte 8 bit unsigned integer.'672 }673 ];674 invalid_data_list.forEach(entry => {675 assert_throws_js(676 TypeError,677 () => new NDEFRecord(createRecord('smart-poster', entry.data)),678 entry.message);679 });680}, 'NDEFRecord constructor for smart-poster record with invalid embedded records.');681test(() => {682 assert_throws_js(683 TypeError, () => new NDEFRecord(createRecord(':xyz', test_buffer_data)),684 'The local type record must be embedded in the payload of another record (smart-poster, external, or local)');685 // The following test cases use an external type record embedding our target686 // local type record.687 const local_record = createRecord(':xyz', test_buffer_data);688 const payload_message = createMessage([local_record]);689 const external_record_embedding_local_record =690 createRecord('example.com:foo', payload_message);691 // OK.692 new NDEFRecord(external_record_embedding_local_record);693 local_record.recordType = ':xyZ123';694 new NDEFRecord(external_record_embedding_local_record);695 local_record.recordType = ':123XYz';696 new NDEFRecord(external_record_embedding_local_record);697 local_record.recordType = ':hellö';698 assert_throws_js(699 TypeError, () => new NDEFRecord(external_record_embedding_local_record),700 'The local type must be an ASCII string.');701 // Length of the local type excluding the prefix ':' is 255, OK.702 local_record.recordType = ':' + [...Array(255)].map(_ => 'a').join('');703 const record_255 = new NDEFRecord(external_record_embedding_local_record);704 // Exceeding 255, Throws.705 local_record.recordType = ':' + [...Array(256)].map(_ => 'a').join('');706 assert_throws_js(707 TypeError, () => new NDEFRecord(external_record_embedding_local_record),708 'The local type excluding the prefix \':\' should not be longer than 255.');709 local_record.recordType = 'xyz';710 assert_throws_js(711 TypeError, () => new NDEFRecord(external_record_embedding_local_record),712 'The local type must start with a \':\'.');713 local_record.recordType = ':Xyz';714 assert_throws_js(715 TypeError, () => new NDEFRecord(external_record_embedding_local_record),716 'The local type must have a lower case letter or digit following the prefix \':\'.');717 local_record.recordType = ':-xyz';718 assert_throws_js(719 TypeError, () => new NDEFRecord(external_record_embedding_local_record),720 'The local type must have a lower case letter or digit following the prefix \':\'.');...
Using AI Code Generation
1var wptools = require('wptools');2var wikidata = require('wikidata-sdk');3var request = require('request');4var fs = require('fs');5var async = require('async');6var _ = require('underscore');
Using AI Code Generation
1var wpt = require('./wpt');2var Wpt = new wpt();3var location = 'Dulles:Chrome';4var runs = 3;5var timeout = 30;6var wpt_options = {7};8Wpt.size_record(url, wpt_options, function(error, data) {9 if (error) {10 console.log(error);11 } else {12 console.log(data);13 }14});15var wpt = require('./wpt');16var Wpt = new wpt();17var test_id = '150412_4X_4e4a7a4d0a5b1e7f4e4a7a4d0a5b1e7f';18Wpt.status(test_id, function(error, data) {19 if (error) {20 console.log(error);21 } else {22 console.log(data);23 }24});25var wpt = require('./wpt');26var Wpt = new wpt();27var test_id = '150412_4X_4e4a7a4d0a5b1e7f4e4a7a4d0a5b1e7f';28Wpt.status(test_id, function(error, data) {29 if (error) {30 console.log(error);31 } else {32 console.log(data);33 }34});35var wpt = require('./wpt');36var Wpt = new wpt();37var test_id = '150412_4X_4e4a7a4d0a5b1e7f4e4a7a4d0a5b1e7f';38Wpt.status(test_id, function(error, data) {39 if (error) {40 console.log(error);41 } else {42 console.log(data);43 }44});
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.6c4a6f0c6c4b6a4a6c4a6f0c6c4b6a4a');3 if (err) throw err;4 console.log(data);5});6 [MIT](LICENSE)
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var client = wpt.createClient(options);5 if (err) return console.error(err);6 console.log(data);7 client.getTestResults(data.data.testId, function(err, data) {8 if (err) return console.error(err);9 console.log(data);10 });11});12client.getLocations(function(err, data) {13 if (err) return console.error(err);14 console.log(data);15});16client.getTesters(function(err, data) {17 if (err) return console.error(err);18 console.log(data);19});20client.getTestStatus('141109_3Y_3f8b0d1b1e3b3f6b5f6d5d6c5f6d5d6c', function(err, data) {21 if (err) return console.error(err);22 console.log(data);23});24client.getTestResults('141109_3Y_3f8b0d1b1e3b3f6b5f6d5d6c5f6d5d6c', function(err, data) {25 if (err) return console.error(err);26 console.log(data);27});28client.getHAR('141109_3Y_3f8b0d1b1e3b3f6b5f6d5d6c5f6d5d6c', function(err, data) {29 if (err) return console.error(err);30 console.log(data);31});32client.getPagespeed('141109_3Y_3f8b0d1b1e3b3f6b5f6d5d6c5f6d5d6c', function(err, data) {33 if (err) return console.error(err);34 console.log(data);35});36client.getVisualProgress('141109_3Y_3f8b0d1b1e3b3f6b5f6d5d6c5f6d5d6c', function(err, data) {37 if (err)
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('A.1e8b5eb7b0b5c9d7f5c5e5e7f5e5f5f5');3 if (err) return console.error(err);4 console.log('Poll the test results at: ' + data.data.userUrl);5 console.log('View test in the browser at: ' + data.data.summary);6 console.log('Test ID: ' + data.data.testId);7 api.getTestResults(data.data.testId, function(err, data) {8 if (err) return console.error(err);9 console.log('First View (fv): ' + data.data.average.firstView.loadTime);10 console.log('Repeat View (rv): ' + data.data.average.repeatView.loadTime);11 });12});13var wpt = require('webpagetest');14var api = new wpt('A.1e8b5eb7b0b5c9d7f5c5e5e7f5e5f5f5');15 if (err) return console.error(err);16 console.log('Poll the test results at: ' + data.data.userUrl);17 console.log('View test in the browser at: ' + data.data.summary);18 console.log('Test ID: ' + data.data.testId);19 api.getTestResults(data.data.testId, function(err, data) {20 if (err) return console.error(err);21 console.log('First View (fv): ' + data.data.average.firstView.loadTime);22 console.log('Repeat View (rv): ' + data.data.average.repeatView.loadTime);23 });24});25var wpt = require('webpagetest');26var api = new wpt('A.1e8b5eb7b0b5c9d7f5c5e5e7f5e5f5f5');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!