Skip to content

Commit

Permalink
sensors: Add content_shell code path for virtual sensors manipulation
Browse files Browse the repository at this point in the history
content_shell does not use ChromeDriver, so we need a separate
implementation for the testdriver.js commands we are adding.

This implementation is anchored on Blink's Internals system, which uses
the newly-added WebSensorProviderAutomation Mojo interface implemented
by content/web_test/browser's WebTestSensorProviderManager to translate
the virtual sensor calls into calls to WebContentsSensorProviderProxy
that will ultimately reach //services.

Bug: 1278377
Change-Id: I13f49d529b36916d1f98b4792839e818fd7e8bdb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4770865
Commit-Queue: Raphael Kubo Da Costa <[email protected]>
Reviewed-by: Matt Reynolds <[email protected]>
Reviewed-by: Kentaro Hara <[email protected]>
Reviewed-by: danakj <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1212214}
  • Loading branch information
Raphael Kubo da Costa authored and chromium-wpt-export-bot committed Oct 19, 2023
1 parent cee0f0a commit 6328604
Showing 1 changed file with 159 additions and 0 deletions.
159 changes: 159 additions & 0 deletions resources/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,149 @@
*/
reset_fedcm_cooldown: function(context=null) {
return window.test_driver_internal.reset_fedcm_cooldown(context);
},

/**
* Creates a virtual sensor for use with the Generic Sensors APIs.
*
* Matches the `Create Virtual Sensor
* <https://w3c.github.io/sensors/#create-virtual-sensor-command>`_
* WebDriver command.
*
* Once created, a virtual sensor is available to all navigables under
* the same top-level traversable (i.e. all frames in the same page,
* regardless of origin).
*
* @param {String} sensor_type - A `virtual sensor type
* <https://w3c.github.io/sensors/#virtual-sensor-metadata-virtual-sensor-type>`_
* such as "accelerometer".
* @param {Object} [sensor_params={}] - Optional parameters described
* in `Create Virtual Sensor
* <https://w3c.github.io/sensors/#create-virtual-sensor-command>`_.
* @param {WindowProxy} [context=null] - Browsing context in which to
* run the call, or null for the
* current browsing context.
*
* @returns {Promise} Fulfilled when virtual sensor is created.
* Rejected in case the WebDriver command errors out
* (including if a virtual sensor of the same type
* already exists).
*/
create_virtual_sensor: function(sensor_type, sensor_params={}, context=null) {
return window.test_driver_internal.create_virtual_sensor(sensor_type, sensor_params, context);
},

/**
* Causes a virtual sensor to report a new reading to any connected
* platform sensor.
*
* Matches the `Update Virtual Sensor Reading
* <https://w3c.github.io/sensors/#update-virtual-sensor-reading-command>`_
* WebDriver command.
*
* Note: The ``Promise`` it returns may fulfill before or after a
* "reading" event is fired. When using
* :js:func:`EventWatcher.wait_for`, it is necessary to take this into
* account:
*
* Note: New values may also be discarded due to the checks in `update
* latest reading
* <https://w3c.github.io/sensors/#update-latest-reading>`_.
*
* @example
* // Avoid races between EventWatcher and update_virtual_sensor().
* // This assumes you are sure this reading will be processed (see
* // the example below otherwise).
* const reading = { x: 1, y: 2, z: 3 };
* await Promise.all([
* test_driver.update_virtual_sensor('gyroscope', reading),
* watcher.wait_for('reading')
* ]);
*
* @example
* // Do not wait forever if you are not sure the reading will be
* // processed.
* const readingPromise = watcher.wait_for('reading');
* const timeoutPromise = new Promise(resolve => {
* t.step_timeout(() => resolve('TIMEOUT', 3000))
* });
*
* const reading = { x: 1, y: 2, z: 3 };
* await test_driver.update_virtual_sensor('gyroscope', 'reading');
*
* const value =
* await Promise.race([timeoutPromise, readingPromise]);
* if (value !== 'TIMEOUT') {
* // Do something. The "reading" event was fired.
* }
*
* @param {String} sensor_type - A `virtual sensor type
* <https://w3c.github.io/sensors/#virtual-sensor-metadata-virtual-sensor-type>`_
* such as "accelerometer".
* @param {Object} reading - An Object describing a reading in a format
* dependent on ``sensor_type`` (e.g. ``{x:
* 1, y: 2, z: 3}`` or ``{ illuminance: 42
* }``).
* @param {WindowProxy} [context=null] - Browsing context in which to
* run the call, or null for the
* current browsing context.
*
* @returns {Promise} Fulfilled after the reading update reaches the
* virtual sensor. Rejected in case the WebDriver
* command errors out (including if a virtual sensor
* of the given type does not exist).
*/
update_virtual_sensor: function(sensor_type, reading, context=null) {
return window.test_driver_internal.update_virtual_sensor(sensor_type, reading, context);
},

/**
* Triggers the removal of a virtual sensor if it exists.
*
* Matches the `Delete Virtual Sensor
* <https://w3c.github.io/sensors/#delete-virtual-sensor-command>`_
* WebDriver command.
*
* @param {String} sensor_type - A `virtual sensor type
* <https://w3c.github.io/sensors/#virtual-sensor-metadata-virtual-sensor-type>`_
* such as "accelerometer".
* @param {WindowProxy} [context=null] - Browsing context in which to
* run the call, or null for the
* current browsing context.
*
* @returns {Promise} Fulfilled after the virtual sensor has been
* removed or if a sensor of the given type does not
* exist. Rejected in case the WebDriver command
* errors out.
*/
remove_virtual_sensor: function(sensor_type, context=null) {
return window.test_driver_internal.remove_virtual_sensor(sensor_type, context);
},

/**
* Returns information about a virtual sensor.
*
* Matches the `Get Virtual Sensor Information
* <https://w3c.github.io/sensors/#get-virtual-sensor-information-command>`_
* WebDriver command.
*
* @param {String} sensor_type - A `virtual sensor type
* <https://w3c.github.io/sensors/#virtual-sensor-metadata-virtual-sensor-type>`_
* such as "accelerometer".
* @param {WindowProxy} [context=null] - Browsing context in which to
* run the call, or null for the
* current browsing context.
*
* @returns {Promise} Fulfilled with an Object with the properties
* described in `Get Virtual Sensor Information
* <https://w3c.github.io/sensors/#get-virtual-sensor-information-command>`_.
* Rejected in case the WebDriver command errors out
* (including if a virtual sensor of the given type
* does not exist).
*/
get_virtual_sensor_information: function(sensor_type, context=null) {
return window.test_driver_internal.get_virtual_sensor_information(sensor_type, context);
}
};

Expand Down Expand Up @@ -980,6 +1123,22 @@

async reset_fedcm_cooldown(context=null) {
throw new Error("reset_fedcm_cooldown() is not implemented by testdriver-vendor.js");
},

async create_virtual_sensor(sensor_type, sensor_params, context=null) {
throw new Error("create_virtual_sensor() is not implemented by testdriver-vendor.js");
},

async update_virtual_sensor(sensor_type, reading, context=null) {
throw new Error("update_virtual_sensor() is not implemented by testdriver-vendor.js");
},

async remove_virtual_sensor(sensor_type, context=null) {
throw new Error("remove_virtual_sensor() is not implemented by testdriver-vendor.js");
},

async get_virtual_sensor_information(sensor_type, context=null) {
throw new Error("get_virtual_sensor_information() is not implemented by testdriver-vendor.js");
}
};
})();

0 comments on commit 6328604

Please sign in to comment.