How to use ndefWatcher method in wpt

Best JavaScript code snippet using wpt

nfc-helpers.js

Source:nfc-helpers.js Github

copy

Full Screen

1'use strict';2// These tests rely on the User Agent providing an implementation of3// platform nfc backends.4//5// In Chromium-based browsers this implementation is provided by a polyfill6// in order to reduce the amount of test-only code shipped to users. To enable7// these tests the browser must be run with these options:8//9// --enable-blink-features=MojoJS,MojoJSTest10async function loadChromiumResources() {11 await loadScript('/resources/testdriver.js');12 await loadScript('/resources/testdriver-vendor.js');13 await import('/resources/chromium/nfc-mock.js');14}15async function initialize_nfc_tests() {16 if (typeof WebNFCTest === 'undefined') {17 const script = document.createElement('script');18 script.src = '/resources/test-only-api.js';19 script.async = false;20 const p = new Promise((resolve, reject) => {21 script.onload = () => { resolve(); };22 script.onerror = e => { reject(e); };23 })24 document.head.appendChild(script);25 await p;26 if (isChromiumBased) {27 await loadChromiumResources();28 }29 }30 assert_implements( WebNFCTest, 'WebNFC testing interface is unavailable.');31 let NFCTest = new WebNFCTest();32 await NFCTest.initialize();33 return NFCTest;34}35function nfc_test(func, name, properties) {36 promise_test(async t => {37 let NFCTest = await initialize_nfc_tests();38 t.add_cleanup(async () => {39 await NFCTest.reset();40 });41 await func(t, NFCTest.getMockNFC());42 }, name, properties);43}44const test_text_data = 'Test text data.';45const test_text_byte_array = new TextEncoder('utf-8').encode(test_text_data);46const test_number_data = 42;47const test_json_data = {level: 1, score: 100, label: 'Game'};48const test_url_data = 'https://w3c.github.io/web-nfc/';49const test_message_origin = 'https://127.0.0.1:8443';50const test_buffer_data = new ArrayBuffer(test_text_byte_array.length);51const test_buffer_view = new Uint8Array(test_buffer_data);52test_buffer_view.set(test_text_byte_array);53const fake_tag_serial_number = 'c0:45:00:02';54const test_record_id = '/test_path/test_id';55const NFCHWStatus = {};56// OS-level NFC setting is ON57NFCHWStatus.ENABLED = 1;58// no NFC chip59NFCHWStatus.NOT_SUPPORTED = NFCHWStatus.ENABLED + 1;60// OS-level NFC setting OFF61NFCHWStatus.DISABLED = NFCHWStatus.NOT_SUPPORTED + 1;62function encodeTextToArrayBuffer(string, encoding) {63 // Only support 'utf-8', 'utf-16', 'utf-16be', and 'utf-16le'.64 assert_true(65 encoding === 'utf-8' || encoding === 'utf-16' ||66 encoding === 'utf-16be' || encoding === 'utf-16le');67 if (encoding === 'utf-8') {68 return new TextEncoder().encode(string).buffer;69 }70 if (encoding === 'utf-16') {71 let uint16array = new Uint16Array(string.length);72 for (let i = 0; i < string.length; i++) {73 uint16array[i] = string.codePointAt(i);74 }75 return uint16array.buffer;76 }77 const littleEndian = encoding === 'utf-16le';78 const buffer = new ArrayBuffer(string.length * 2);79 const view = new DataView(buffer);80 for (let i = 0; i < string.length; i++) {81 view.setUint16(i * 2, string.codePointAt(i), littleEndian);82 }83 return buffer;84}85function createMessage(records) {86 if (records !== undefined) {87 let message = {};88 message.records = records;89 return message;90 }91}92function createRecord(recordType, data, id, mediaType, encoding, lang) {93 let record = {};94 if (recordType !== undefined)95 record.recordType = recordType;96 if (id !== undefined)97 record.id = id;98 if (mediaType !== undefined)99 record.mediaType = mediaType;100 if (encoding !== undefined)101 record.encoding = encoding;102 if (lang !== undefined)103 record.lang = lang;104 if (data !== undefined)105 record.data = data;106 return record;107}108function createTextRecord(data, encoding, lang) {109 return createRecord('text', data, test_record_id, undefined, encoding, lang);110}111function createMimeRecordFromJson(json) {112 return createRecord(113 'mime', new TextEncoder('utf-8').encode(JSON.stringify(json)),114 test_record_id, 'application/json');115}116function createMimeRecord(buffer) {117 return createRecord(118 'mime', buffer, test_record_id, 'application/octet-stream');119}120function createUnknownRecord(buffer) {121 return createRecord('unknown', buffer, test_record_id);122}123function createUrlRecord(url, isAbsUrl) {124 if (isAbsUrl) {125 return createRecord('absolute-url', url, test_record_id);126 }127 return createRecord('url', url, test_record_id);128}129// Compares NDEFMessageSource that was provided to the API130// (e.g. NDEFReader.write), and NDEFMessage that was received by the131// mock NFC service.132function assertNDEFMessagesEqual(providedMessage, receivedMessage) {133 // If simple data type is passed, e.g. String or ArrayBuffer or134 // ArrayBufferView, convert it to NDEFMessage before comparing.135 // https://w3c.github.io/web-nfc/#dom-ndefmessagesource136 let provided = providedMessage;137 if (providedMessage instanceof ArrayBuffer ||138 ArrayBuffer.isView(providedMessage))139 provided = createMessage([createRecord(140 'mime', providedMessage, undefined /* id */,141 'application/octet-stream')]);142 else if (typeof providedMessage === 'string')143 provided = createMessage([createRecord('text', providedMessage)]);144 assert_equals(provided.records.length, receivedMessage.data.length,145 'NDEFMessages must have same number of NDEFRecords');146 // Compare contents of each individual NDEFRecord147 for (let i = 0; i < provided.records.length; ++i)148 compareNDEFRecords(provided.records[i], receivedMessage.data[i]);149}150// Used to compare two NDEFMessage, one that is received from151// NDEFReader.onreading() EventHandler and another that is provided to mock NFC152// service.153function assertWebNDEFMessagesEqual(message, expectedMessage) {154 assert_equals(message.records.length, expectedMessage.records.length);155 for(let i in message.records) {156 let record = message.records[i];157 let expectedRecord = expectedMessage.records[i];158 assert_equals(record.recordType, expectedRecord.recordType);159 assert_equals(record.mediaType, expectedRecord.mediaType);160 assert_equals(record.id, expectedRecord.id);161 assert_equals(record.encoding, expectedRecord.encoding);162 assert_equals(record.lang, expectedRecord.lang);163 // Compares record data164 assert_array_equals(new Uint8Array(record.data),165 new Uint8Array(expectedRecord.data));166 }167}168function testMultiScanOptions(message, scanOptions, unmatchedScanOptions, desc) {169 nfc_test(async (t, mockNFC) => {170 const ndef1 = new NDEFReader();171 const ndef2 = new NDEFReader();172 const controller = new AbortController();173 // Reading from unmatched ndef will not be triggered174 ndef1.onreading = t.unreached_func("reading event should not be fired.");175 unmatchedScanOptions.signal = controller.signal;176 await ndef1.scan(unmatchedScanOptions);177 const ndefWatcher = new EventWatcher(t, ndef2, ["reading", "readingerror"]);178 const promise = ndefWatcher.wait_for("reading").then(event => {179 controller.abort();180 assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message));181 });182 scanOptions.signal = controller.signal;183 await ndef2.scan(scanOptions);184 mockNFC.setReadingMessage(message);185 await promise;186 }, desc);187}188function testMultiMessages(message, scanOptions, unmatchedMessage, desc) {189 nfc_test(async (t, mockNFC) => {190 const ndef = new NDEFReader();191 const controller = new AbortController();192 const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]);193 const promise = ndefWatcher.wait_for("reading").then(event => {194 controller.abort();195 assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message));196 });197 scanOptions.signal = controller.signal;198 await ndef.scan(scanOptions);199 // Unmatched message will not be read200 mockNFC.setReadingMessage(unmatchedMessage);201 mockNFC.setReadingMessage(message);202 await promise;203 }, desc);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var ndefWatcher = new NDEFWatcher();2ndefWatcher.onreading = function() {3 console.log("NDEF message found.");4 var ndefMessage = this.ndefMessage;5 console.log(ndefMessage);6};7ndefWatcher.onerror = function() {8 console.log("Error! " + this.error.name);9};10ndefWatcher.watch();11var ndefReader = new NDEFReader();12ndefReader.onreading = function() {13 console.log("NDEF message found.");14 var ndefMessage = this.ndefMessage;15 console.log(ndefMessage);16};17ndefReader.onerror = function() {18 console.log("Error! " + this.error.name);19};20ndefReader.scan();21var ndefWriter = new NDEFWriter();22ndefWriter.onreading = function() {23 console.log("NDEF message found.");24 var ndefMessage = this.ndefMessage;25 console.log(ndefMessage);26};27ndefWriter.onerror = function() {28 console.log("Error! " + this.error.name);29};30ndefWriter.write("Hello World");31var ndefWriter = new NDEFWriter();32ndefWriter.onreading = function() {33 console.log("NDEF message found.");34 var ndefMessage = this.ndefMessage;35 console.log(ndefMessage);36};37ndefWriter.onerror = function() {38 console.log("Error! " + this.error.name);39};40ndefWriter.write("Hello World");41var ndefWriter = new NDEFWriter();42ndefWriter.onreading = function() {43 console.log("NDEF message found.");44 var ndefMessage = this.ndefMessage;45 console.log(ndefMessage);46};47ndefWriter.onerror = function() {48 console.log("Error! " + this.error.name);49};50ndefWriter.write("Hello World");51var ndefWriter = new NDEFWriter();52ndefWriter.onreading = function() {53 console.log("NDEF message found.");54 var ndefMessage = this.ndefMessage;55 console.log(ndefMessage);56};

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptoolkit = require('wptoolkit');2wptoolkit.ndefWatcher(function (err, data) {3 console.log(data);4});5{6 "scripts": {7 },8 "dependencies": {9 }10}

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var ndefWatcher = wpt.ndefWatcher;3var ndef = ndefWatcher(function(err, tag) {4 if (err) {5 console.log(err);6 } else {7 console.log(tag);8 }9});10var nfc = require('nfc');11var ndefWatcher = nfc.ndefWatcher;12var ndef = ndefWatcher(function(err, tag) {13 if (err) {14 console.log(err);15 } else {16 console.log(tag);17 }18});

Full Screen

Using AI Code Generation

copy

Full Screen

1var NDEFWatcher = require('./NDEFWatcher.js');2var ndefWatcher = new NDEFWatcher();3ndefWatcher.onfound = function (message) {4 console.log('found: ' + JSON.stringify(message));5};6ndefWatcher.onlost = function () {7 console.log('lost');8};9ndefWatcher.start();10var NDEFWriter = require('./NDEFWriter.js');11var ndefWriter = new NDEFWriter();12ndefWriter.write('Hello World!').then(function () {13 console.log('Message written');14}, function (error) {15 console.log('Write failed: ' + error);16});17var NFCReader = require('./NFCReader.js');18var nfcReader = new NFCReader();19nfcReader.onreading = function (event) {20 console.log('NDEF message read: ' + JSON.stringify(event.message));21};22nfcReader.start();23var NFCWriter = require('./NFCWriter.js');24var nfcWriter = new NFCWriter();25nfcWriter.write('Hello World!').then(function () {26 console.log('Message written');27}, function (error) {28 console.log('Write failed: ' + error);29});30var NFCAdapter = require('./NFCAdapter.js');31var nfcAdapter = new NFCAdapter();32nfcAdapter.onreading = function (event) {33 console.log('NDEF message read: ' + JSON.stringify(event.message));34};35nfcAdapter.start();36var NFCPush = require('./NFCPush.js');37var nfcPush = new NFCPush();38nfcPush.onreading = function (event) {39 console.log('NDEF message read: ' + JSON.stringify(event.message));40};41nfcPush.start();

Full Screen

Using AI Code Generation

copy

Full Screen

1var ndefWatcher = require('ndefWatcher');2var ndefWatcher = new ndefWatcher();3ndefWatcher.on('read', function() {4 console.log('NDEF read!');5});6ndefWatcher.on('write', function() {7 console.log('NDEF written!');8});9ndefWatcher.on('error', function() {10 console.log('NDEF error!');11});12ndefWatcher.on('watch', function() {13 console.log('NDEF watch!');14});15ndefWatcher.on('unwatch', function() {16 console.log('NDEF unwatch!');17});18ndefWatcher.watch();19var ndefWatcher = require('ndefWatcher');20var ndefWatcher = new ndefWatcher();21ndefWatcher.on('read', function() {22 console.log('NDEF read!');23});24ndefWatcher.on('write', function() {25 console.log('NDEF written!');26});27ndefWatcher.on('error', function() {28 console.log('NDEF error!');29});30ndefWatcher.on('watch', function() {31 console.log('NDEF watch!');32});33ndefWatcher.on('unwatch', function() {34 console.log('NDEF unwatch!');35});36ndefWatcher.watch();37var ndefWatcher = require('ndefWatcher');38var ndefWatcher = new ndefWatcher();39ndefWatcher.on('read', function() {40 console.log('NDEF read!');41});42ndefWatcher.on('write', function() {43 console.log('NDEF written!');44});45ndefWatcher.on('error', function() {46 console.log('NDEF error!');47});48ndefWatcher.on('watch', function() {49 console.log('NDEF watch!');50});51ndefWatcher.on('unwatch', function() {52 console.log('NDEF unwatch!');53});54ndefWatcher.watch();55var ndefWatcher = require('ndefWatcher');56var ndefWatcher = new ndefWatcher();57ndefWatcher.on('read', function() {58 console.log('NDEF read!');59});60ndefWatcher.on('write', function() {61 console.log('NDEF written!');62});63ndefWatcher.on('error', function() {64 console.log('NDEF error!');65});66ndefWatcher.on('watch', function() {67 console.log('NDEF watch!');68});69ndefWatcher.on('

Full Screen

Using AI Code Generation

copy

Full Screen

1var callback = function (err, data) {2};3{ tag: 'NDEF', data: 'some data' }4var callback = function (err, data) {5};6{ tag: 'NDEF', data: 'some data' }7var callback = function (err, data) {8};9{ tag: 'NDEF', data: 'some data' }10var callback = function (err, data) {11};12{ tag: 'NDEF', data: 'some data' }13var callback = function (err, data) {14};15{ tag: 'NDEF', data: '

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var ndefWatcher = wptools.ndefWatcher;3var ndef = require('ndef');4ndefWatcher(function(err, message) {5 if (err) {6 return console.log(err);7 }8 console.log(message.uid);9 console.log(message.uid.toString('hex'));10});

Full Screen

Using AI Code Generation

copy

Full Screen

1var ndefWatcher = new NDEFWatcher();2ndefWatcher.onreading = function (event) {3 console.log("NDEF message changed!");4 console.log("NDEF message: " + event.message);5};6ndefWatcher.start();7ndefWatcher.stop();8ndefWatcher.close();

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful