How to use shmemView method in wpt

Best JavaScript code snippet using wpt

generic_sensor_mocks.js

Source:generic_sensor_mocks.js Github

copy

Full Screen

1import {ReportingMode, Sensor, SensorClientRemote, SensorReceiver, SensorRemote, SensorType} from '/gen/services/device/public/mojom/sensor.mojom.m.js';2import {SensorCreationResult, SensorInitParams_READ_BUFFER_SIZE_FOR_TESTS, SensorProvider, SensorProviderReceiver} from '/gen/services/device/public/mojom/sensor_provider.mojom.m.js';3// A "sliding window" that iterates over |data| and returns one item at a4// time, advancing and wrapping around as needed. |data| must be an array of5// arrays.6self.RingBuffer = class {7 constructor(data) {8 this.bufferPosition_ = 0;9 // Validate |data|'s format and deep-copy every element.10 this.data_ = Array.from(data, element => {11 if (!Array.isArray(element)) {12 throw new TypeError('Every |data| element must be an array.');13 }14 return Array.from(element);15 })16 }17 next() {18 const value = this.data_[this.bufferPosition_];19 this.bufferPosition_ = (this.bufferPosition_ + 1) % this.data_.length;20 return { done: false, value: value };21 }22 value() {23 return this.data_[this.bufferPosition_];24 }25 [Symbol.iterator]() {26 return this;27 }28};29self.GenericSensorTest = (() => {30 // Default sensor frequency in default configurations.31 const DEFAULT_FREQUENCY = 5;32 // Class that mocks Sensor interface defined in33 // https://cs.chromium.org/chromium/src/services/device/public/mojom/sensor.mojom34 class MockSensor {35 constructor(sensorRequest, buffer, reportingMode) {36 this.client_ = null;37 this.startShouldFail_ = false;38 this.notifyOnReadingChange_ = true;39 this.reportingMode_ = reportingMode;40 this.sensorReadingTimerId_ = null;41 this.readingData_ = null;42 this.requestedFrequencies_ = [];43 // The Blink implementation (third_party/blink/renderer/modules/sensor/sensor.cc)44 // sets a timestamp by creating a DOMHighResTimeStamp from a given platform timestamp.45 // In this mock implementation we use a starting value46 // and an increment step value that resemble a platform timestamp reasonably enough.47 this.timestamp_ = window.performance.timeOrigin;48 this.buffer_ = buffer;49 this.buffer_.fill(0);50 this.receiver_ = new SensorReceiver(this);51 this.receiver_.$.bindHandle(sensorRequest.handle);52 }53 // Returns default configuration.54 async getDefaultConfiguration() {55 return { frequency: DEFAULT_FREQUENCY };56 }57 // Adds configuration for the sensor and starts reporting fake data58 // through setSensorReading function.59 async addConfiguration(configuration) {60 this.requestedFrequencies_.push(configuration.frequency);61 // Sort using descending order.62 this.requestedFrequencies_.sort(63 (first, second) => { return second - first });64 if (!this.startShouldFail_ )65 this.startReading();66 return { success: !this.startShouldFail_ };67 }68 // Removes sensor configuration from the list of active configurations and69 // stops notification about sensor reading changes if70 // requestedFrequencies_ is empty.71 removeConfiguration(configuration) {72 const index = this.requestedFrequencies_.indexOf(configuration.frequency);73 if (index == -1)74 return;75 this.requestedFrequencies_.splice(index, 1);76 if (this.requestedFrequencies_.length === 0)77 this.stopReading();78 }79 // ConfigureReadingChangeNotifications(bool enabled)80 // Configures whether to report a reading change when in ON_CHANGE81 // reporting mode.82 configureReadingChangeNotifications(notifyOnReadingChange) {83 this.notifyOnReadingChange_ = notifyOnReadingChange;84 }85 resume() {86 this.startReading();87 }88 suspend() {89 this.stopReading();90 }91 // Mock functions92 // Resets mock Sensor state.93 reset() {94 this.stopReading();95 this.startShouldFail_ = false;96 this.requestedFrequencies_ = [];97 this.notifyOnReadingChange_ = true;98 this.readingData_ = null;99 this.buffer_.fill(0);100 this.receiver_.$.close();101 }102 // Sets fake data that is used to deliver sensor reading updates.103 async setSensorReading(readingData) {104 this.readingData_ = new RingBuffer(readingData);105 return this;106 }107 // This is a workaround to accommodate Blink's Device Orientation108 // implementation. In general, all tests should use setSensorReading()109 // instead.110 setSensorReadingImmediately(readingData) {111 this.setSensorReading(readingData);112 const reading = this.readingData_.value();113 this.buffer_.set(reading, 2);114 this.buffer_[1] = this.timestamp_++;115 }116 // Sets flag that forces sensor to fail when addConfiguration is invoked.117 setStartShouldFail(shouldFail) {118 this.startShouldFail_ = shouldFail;119 }120 startReading() {121 if (this.readingData_ != null) {122 this.stopReading();123 }124 let maxFrequencyUsed = this.requestedFrequencies_[0];125 let timeout = (1 / maxFrequencyUsed) * 1000;126 this.sensorReadingTimerId_ = window.setInterval(() => {127 if (this.readingData_) {128 // |buffer_| is a TypedArray, so we need to make sure pass an129 // array to set().130 const reading = this.readingData_.next().value;131 if (!Array.isArray(reading)) {132 throw new TypeError("startReading(): The readings passed to " +133 "setSensorReading() must be arrays");134 }135 this.buffer_.set(reading, 2);136 }137 // For all tests sensor reading should have monotonically138 // increasing timestamp.139 this.buffer_[1] = this.timestamp_++;140 if (this.reportingMode_ === ReportingMode.ON_CHANGE &&141 this.notifyOnReadingChange_) {142 this.client_.sensorReadingChanged();143 }144 }, timeout);145 }146 stopReading() {147 if (this.sensorReadingTimerId_ != null) {148 window.clearInterval(this.sensorReadingTimerId_);149 this.sensorReadingTimerId_ = null;150 }151 }152 getSamplingFrequency() {153 if (this.requestedFrequencies_.length == 0) {154 throw new Error("getSamplingFrequency(): No configured frequency");155 }156 return this.requestedFrequencies_[0];157 }158 isReadingData() {159 return this.sensorReadingTimerId_ != null;160 }161 }162 // Class that mocks SensorProvider interface defined in163 // https://cs.chromium.org/chromium/src/services/device/public/mojom/sensor_provider.mojom164 class MockSensorProvider {165 constructor() {166 this.readingSizeInBytes_ =167 Number(SensorInitParams_READ_BUFFER_SIZE_FOR_TESTS);168 this.sharedBufferSizeInBytes_ =169 this.readingSizeInBytes_ * (SensorType.MAX_VALUE + 1);170 let rv = Mojo.createSharedBuffer(this.sharedBufferSizeInBytes_);171 if (rv.result != Mojo.RESULT_OK) {172 throw new Error('MockSensorProvider: Failed to create shared buffer');173 }174 const handle = rv.handle;175 rv = handle.mapBuffer(0, this.sharedBufferSizeInBytes_);176 if (rv.result != Mojo.RESULT_OK) {177 throw new Error("MockSensorProvider: Failed to map shared buffer");178 }179 this.shmemArrayBuffer_ = rv.buffer;180 rv = handle.duplicateBufferHandle({readOnly: true});181 if (rv.result != Mojo.RESULT_OK) {182 throw new Error(183 'MockSensorProvider: failed to duplicate shared buffer');184 }185 this.readOnlySharedBufferHandle_ = rv.handle;186 this.activeSensors_ = new Map();187 this.resolveFuncs_ = new Map();188 this.getSensorShouldFail_ = new Map();189 this.permissionsDenied_ = new Map();190 this.maxFrequency_ = 60;191 this.minFrequency_ = 1;192 this.mojomSensorType_ = new Map([193 ['Accelerometer', SensorType.ACCELEROMETER],194 ['LinearAccelerationSensor', SensorType.LINEAR_ACCELERATION],195 ['GravitySensor', SensorType.GRAVITY],196 ['AmbientLightSensor', SensorType.AMBIENT_LIGHT],197 ['Gyroscope', SensorType.GYROSCOPE],198 ['Magnetometer', SensorType.MAGNETOMETER],199 ['AbsoluteOrientationSensor',200 SensorType.ABSOLUTE_ORIENTATION_QUATERNION],201 ['AbsoluteOrientationEulerAngles',202 SensorType.ABSOLUTE_ORIENTATION_EULER_ANGLES],203 ['RelativeOrientationSensor',204 SensorType.RELATIVE_ORIENTATION_QUATERNION],205 ['RelativeOrientationEulerAngles',206 SensorType.RELATIVE_ORIENTATION_EULER_ANGLES],207 ['ProximitySensor', SensorType.PROXIMITY]208 ]);209 this.receiver_ = new SensorProviderReceiver(this);210 this.interceptor_ =211 new MojoInterfaceInterceptor(SensorProvider.$interfaceName);212 this.interceptor_.oninterfacerequest = e => {213 this.bindToPipe(e.handle);214 };215 this.interceptor_.start();216 }217 // Returns initialized Sensor proxy to the client.218 async getSensor(type) {219 if (this.getSensorShouldFail_.get(type)) {220 return {result: SensorCreationResult.ERROR_NOT_AVAILABLE,221 initParams: null};222 }223 if (this.permissionsDenied_.get(type)) {224 return {result: SensorCreationResult.ERROR_NOT_ALLOWED,225 initParams: null};226 }227 const offset = type * this.readingSizeInBytes_;228 const reportingMode = ReportingMode.ON_CHANGE;229 const sensor = new SensorRemote();230 if (!this.activeSensors_.has(type)) {231 const shmemView = new Float64Array(232 this.shmemArrayBuffer_, offset,233 this.readingSizeInBytes_ / Float64Array.BYTES_PER_ELEMENT);234 const mockSensor = new MockSensor(235 sensor.$.bindNewPipeAndPassReceiver(), shmemView, reportingMode);236 this.activeSensors_.set(type, mockSensor);237 this.activeSensors_.get(type).client_ = new SensorClientRemote();238 }239 const rv = this.readOnlySharedBufferHandle_.duplicateBufferHandle(240 {readOnly: true});241 if (rv.result != Mojo.RESULT_OK) {242 throw new Error('getSensor(): failed to duplicate shared buffer');243 }244 const defaultConfig = { frequency: DEFAULT_FREQUENCY };245 // Consider sensor traits to meet assertions in C++ code (see246 // services/device/public/cpp/generic_sensor/sensor_traits.h)247 if (type == SensorType.AMBIENT_LIGHT || type == SensorType.MAGNETOMETER) {248 this.maxFrequency_ = Math.min(10, this.maxFrequency_);249 }250 // Chromium applies some rounding and other privacy-related measures that251 // can cause ALS not to report a reading when it has not changed beyond a252 // certain threshold compared to the previous illuminance value. Make253 // each reading return a different value that is significantly different254 // from the previous one when setSensorReading() is not called by client255 // code (e.g. run_generic_sensor_iframe_tests()).256 if (type == SensorType.AMBIENT_LIGHT) {257 this.activeSensors_.get(type).setSensorReading([258 [window.performance.now() * 100],259 [(window.performance.now() + 50) * 100]260 ]);261 }262 const client = this.activeSensors_.get(type).client_;263 const initParams = {264 sensor,265 clientReceiver: client.$.bindNewPipeAndPassReceiver(),266 memory: {buffer: rv.handle},267 bufferOffset: BigInt(offset),268 mode: reportingMode,269 defaultConfiguration: defaultConfig,270 minimumFrequency: this.minFrequency_,271 maximumFrequency: this.maxFrequency_272 };273 if (this.resolveFuncs_.has(type)) {274 for (let resolveFunc of this.resolveFuncs_.get(type)) {275 resolveFunc(this.activeSensors_.get(type));276 }277 this.resolveFuncs_.delete(type);278 }279 return {result: SensorCreationResult.SUCCESS, initParams};280 }281 // Binds object to mojo message pipe282 bindToPipe(pipe) {283 this.receiver_.$.bindHandle(pipe);284 }285 // Mock functions286 // Resets state of mock SensorProvider between test runs.287 reset() {288 for (const sensor of this.activeSensors_.values()) {289 sensor.reset();290 }291 this.activeSensors_.clear();292 this.resolveFuncs_.clear();293 this.getSensorShouldFail_.clear();294 this.permissionsDenied_.clear();295 this.maxFrequency_ = 60;296 this.minFrequency_ = 1;297 this.receiver_.$.close();298 this.interceptor_.stop();299 }300 // Sets flag that forces mock SensorProvider to fail when getSensor() is301 // invoked.302 setGetSensorShouldFail(sensorType, shouldFail) {303 this.getSensorShouldFail_.set(this.mojomSensorType_.get(sensorType),304 shouldFail);305 }306 setPermissionsDenied(sensorType, permissionsDenied) {307 this.permissionsDenied_.set(this.mojomSensorType_.get(sensorType),308 permissionsDenied);309 }310 // Returns mock sensor that was created in getSensor to the layout test.311 getCreatedSensor(sensorType) {312 const type = this.mojomSensorType_.get(sensorType);313 if (typeof type != "number") {314 throw new TypeError(`getCreatedSensor(): Invalid sensor type ${sensorType}`);315 }316 if (this.activeSensors_.has(type)) {317 return Promise.resolve(this.activeSensors_.get(type));318 }319 return new Promise(resolve => {320 if (!this.resolveFuncs_.has(type)) {321 this.resolveFuncs_.set(type, []);322 }323 this.resolveFuncs_.get(type).push(resolve);324 });325 }326 // Sets the maximum frequency for a concrete sensor.327 setMaximumSupportedFrequency(frequency) {328 this.maxFrequency_ = frequency;329 }330 // Sets the minimum frequency for a concrete sensor.331 setMinimumSupportedFrequency(frequency) {332 this.minFrequency_ = frequency;333 }334 }335 let testInternal = {336 initialized: false,337 sensorProvider: null338 }339 class GenericSensorTestChromium {340 constructor() {341 Object.freeze(this); // Make it immutable.342 }343 async initialize() {344 if (testInternal.initialized)345 throw new Error('Call reset() before initialize().');346 // Grant sensor permissions for Chromium testdriver.347 // testdriver.js only works in the top-level browsing context, so do348 // nothing if we're in e.g. an iframe.349 if (window.parent === window) {350 for (const entry351 of ['accelerometer', 'gyroscope', 'magnetometer',352 'ambient-light-sensor']) {353 await test_driver.set_permission({name: entry}, 'granted', false);354 }355 }356 testInternal.sensorProvider = new MockSensorProvider;357 testInternal.initialized = true;358 }359 // Resets state of sensor mocks between test runs.360 async reset() {361 if (!testInternal.initialized)362 throw new Error('Call initialize() before reset().');363 testInternal.sensorProvider.reset();364 testInternal.sensorProvider = null;365 testInternal.initialized = false;366 // Wait for an event loop iteration to let any pending mojo commands in367 // the sensor provider finish.368 await new Promise(resolve => setTimeout(resolve, 0));369 }370 getSensorProvider() {371 return testInternal.sensorProvider;372 }373 }374 return GenericSensorTestChromium;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4};5 if (err) return console.error(err);6 console.log(data);7});8var WebPageTest = require('webpagetest');9var wpt = new WebPageTest('www.webpagetest.org');10var options = {11};12 if (err) return console.error(err);13 console.log(data);14});15var WebPageTest = require('webpagetest');16var wpt = new WebPageTest('www.webpagetest.org');17var options = {18};19 if (err) return console.error(err);20 console.log(data);21});22var WebPageTest = require('webpagetest');

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var shmemView = wptools.shmemView;3var wptools = require('wptools');4var shmemView = wptools.shmemView;5var wptools = require('wptools');6var shmemView = wptools.shmemView;7var wptools = require('wptools');8var shmemView = wptools.shmemView;9var wptools = require('wptools');10var shmemView = wptools.shmemView;11var wptools = require('wptools');12var shmemView = wptools.shmemView;13var wptools = require('wptools');14var shmemView = wptools.shmemView;15var wptools = require('wptools');16var shmemView = wptools.shmemView;17var wptools = require('wptools');18var shmemView = wptools.shmemView;19var wptools = require('wptools');20var shmemView = wptools.shmemView;21var wptools = require('wptools');22var shmemView = wptools.shmemView;

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var shmemView = wptools.shmemView;3console.log(shmem);4shmem.on('ready', function () {5 console.log('ready');6 console.log(shmem);7 console.log(shmem.get('title'));8 console.log(shmem.get('links'));9 console.log(shmem.get('links').length);10 shmem.close();11});12var wptools = require('wptools');13var shmemView = wptools.shmemView;14shmem.on('ready', function () {15 console.log('ready');16 shmem.close();17});18 at errnoException (child_process.js:988:11)19 at Process.ChildProcess._handle.onexit (child_process.js:779:34)

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var shmemView = wptools.shmemView;3view.on('ready', function() {4 console.log(view.plainText());5 view.close();6});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptDriver = require('wptdriver');2var shmemView = wptDriver.shmemView;3var shmem = shmemView(0, 100);4var wptDriver = require('wptdriver');5var shmemView = wptDriver.shmemView;6var shmem = shmemView(0, 100);7var wptDriver = require('wptdriver');8var shmemView = wptDriver.shmemView;9var shmem = shmemView(0, 100);10var wptDriver = require('wptdriver');11var shmemView = wptDriver.shmemView;12var shmem = shmemView(0, 100);13var wptDriver = require('wptdriver');14var shmemView = wptDriver.shmemView;15var shmem = shmemView(0, 100);16var wptDriver = require('wptdriver');17var shmemView = wptDriver.shmemView;18var shmem = shmemView(0, 100);19var wptDriver = require('wptdriver');20var shmemView = wptDriver.shmemView;21var shmem = shmemView(0, 100);22var wptDriver = require('wptdriver');23var shmemView = wptDriver.shmemView;24var shmem = shmemView(0, 100);25var wptDriver = require('wptdriver');26var shmemView = wptDriver.shmemView;27var shmem = shmemView(0, 100);28var wptDriver = require('wptdriver');29var shmemView = wptDriver.shmemView;30var shmem = shmemView(0, 100);31var wptDriver = require('

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require("wptools");2var shmemView = wptools.shmemView;3var view = shmemView(3, 2, "int32");4view.set(0, 0, 1);5view.set(0, 1, 2);6view.set(1, 0, 3);7view.set(1, 1, 4);8view.set(2, 0, 5);9view.set(2, 1, 6);10console.log(view.get(0, 1));11console.log(view.get(1, 0));12console.log(view.get(2, 1));13console.log(view.get(2, 2));14console.log(view.get(3, 0));15console.log(view.get(3, 3));16console.log(view.get(0, 0));17console.log(view.get(0, 2));18console.log(view.get(2, 0));19console.log(view.get(2, 2));20view.set(2, 2, 7);21console.log(view.get(2,

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptext = require('wptext');2var view = wptext.shmemView(0, 10);3view.write("Hello World");4view.write("Hello World", 1);5view.write("Hello World", 2);6view.write("Hello World", 3);7view.write("Hello World", 4);8view.write("Hello World", 5);9view.write("Hello World", 6);10view.write("Hello World", 7);11view.write("Hello World", 8);12view.write("Hello World", 9);13var wptext = require('wptext');14var view = wptext.shmemView(0, 10);15var data = view.read();16console.log(data);

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptb = require('wptb');2var shmem = wptb.shmemView(10, 'int32');3shmem[0] = 1;4shmem[1] = 2;5shmem[2] = 3;6shmem[3] = 4;7shmem[4] = 5;8shmem[5] = 6;9shmem[6] = 7;10shmem[7] = 8;11shmem[8] = 9;12shmem[9] = 10;13console.log(shmem);14shmem.close();15var shmem2 = wptb.WPTBShmemView.from(shmem);16console.log(shmem2);17shmem2.close();18var shmem3 = wptb.WPTBShmemView.get(shmem.name);19console.log(shmem3);20shmem3.close();21console.log(wptb.WPTBShmemView.exists(shmem.name));22console.log(wptb.WPTBShmemView.exists('doesnotexist'));23wptb.WPTBShmemView.remove(shmem.name);24console.log(wptb.WPTBShmemView.exists(shmem.name));

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