Commit 52cc0da6 authored by Raphael Kubo da Costa's avatar Raphael Kubo da Costa Committed by Commit Bot

sensors: Rewrite AmbientLightSensorTest.IlluminanceRounding as a web test.

This does not directly fix the timeouts on the ARM64 Mac bots reported in
the bug below, but it simplifies the test and hopefully eliminates some of
the sources of flakiness.

Bug: 1128439
Change-Id: I4b1840a8db6e8d2541003b5b1ecb860f4bc04c57
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2418378
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Auto-Submit: Raphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Cr-Commit-Position: refs/heads/master@{#809633}
parent c62238cd
...@@ -81,85 +81,6 @@ TEST(AmbientLightSensorTest, IlluminanceInSensorWithoutReading) { ...@@ -81,85 +81,6 @@ TEST(AmbientLightSensorTest, IlluminanceInSensorWithoutReading) {
EXPECT_FALSE(sensor->hasReading()); EXPECT_FALSE(sensor->hasReading());
} }
TEST(AmbientLightSensorTest, IlluminanceRounding) {
SensorTestContext context;
NonThrowableExceptionState exception_state;
auto* sensor = AmbientLightSensor::Create(context.GetExecutionContext(),
exception_state);
sensor->start();
SensorTestUtils::WaitForEvent(sensor, event_type_names::kActivate);
EXPECT_FALSE(sensor->hasReading());
// At this point, we have received an 'activate' event, so the sensor is
// initialized and it is connected to a SensorProxy that we can retrieve
// here. We then attach a new SensorProxy::Observer that we use to
// synchronously wait for OnSensorReadingChanged() to be called. Even though
// the order that each observer is notified is arbitrary, we know that by the
// time we get to the next call here all observers will have been called.
auto* sensor_proxy =
SensorProviderProxy::From(
To<LocalDOMWindow>(context.GetExecutionContext()))
->GetSensorProxy(device::mojom::blink::SensorType::AMBIENT_LIGHT);
ASSERT_NE(sensor_proxy, nullptr);
auto* mock_observer = MakeGarbageCollected<MockSensorProxyObserver>();
sensor_proxy->AddObserver(mock_observer);
auto* event_counter = MakeGarbageCollected<SensorTestUtils::EventCounter>();
sensor->addEventListener(event_type_names::kReading, event_counter);
// Go from no reading to 24. This will cause a new "reading" event to be
// emitted, and the rounding will cause illuminance() to return 0.
context.sensor_provider()->UpdateAmbientLightSensorData(24);
mock_observer->WaitForOnSensorReadingChanged();
SensorTestUtils::WaitForEvent(sensor, event_type_names::kReading);
EXPECT_EQ(24, sensor->latest_reading_);
ASSERT_TRUE(sensor->illuminance().has_value());
EXPECT_EQ(0, sensor->illuminance().value());
// Go from 24 to 35. The difference is not significant enough, so we will not
// emit any "reading" event or store the new raw reading, as if the new
// reading had never existed.
context.sensor_provider()->UpdateAmbientLightSensorData(35);
mock_observer->WaitForOnSensorReadingChanged();
EXPECT_EQ(24, sensor->latest_reading_);
ASSERT_TRUE(sensor->illuminance().has_value());
EXPECT_EQ(0, sensor->illuminance().value());
// Go from 24 to 49. The difference is significant enough, so we will emit a
// new "reading" event, update our raw reading and return a rounded value of
// 50 in illuminance().
context.sensor_provider()->UpdateAmbientLightSensorData(49);
mock_observer->WaitForOnSensorReadingChanged();
SensorTestUtils::WaitForEvent(sensor, event_type_names::kReading);
EXPECT_EQ(49, sensor->latest_reading_);
ASSERT_TRUE(sensor->illuminance().has_value());
EXPECT_EQ(50, sensor->illuminance().value());
// Go from 49 to 35. The difference is not significant enough, so we will not
// emit any "reading" event or store the new raw reading, as if the new
// reading had never existed.
context.sensor_provider()->UpdateAmbientLightSensorData(35);
mock_observer->WaitForOnSensorReadingChanged();
EXPECT_EQ(49, sensor->latest_reading_);
ASSERT_TRUE(sensor->illuminance().has_value());
EXPECT_EQ(50, sensor->illuminance().value());
// Go from 49 to 24. The difference is significant enough, so we will emit a
// new "reading" event, update our raw reading and return a rounded value of
// 0 in illuminance().
context.sensor_provider()->UpdateAmbientLightSensorData(24);
mock_observer->WaitForOnSensorReadingChanged();
SensorTestUtils::WaitForEvent(sensor, event_type_names::kReading);
EXPECT_EQ(24, sensor->latest_reading_);
ASSERT_TRUE(sensor->illuminance().has_value());
EXPECT_EQ(0, sensor->illuminance().value());
// Make sure there were no stray "reading" events besides those we expected
// above.
EXPECT_EQ(3U, event_counter->event_count());
}
TEST(AmbientLightSensorTest, PlatformSensorReadingsBeforeActivation) { TEST(AmbientLightSensorTest, PlatformSensorReadingsBeforeActivation) {
SensorTestContext context; SensorTestContext context;
NonThrowableExceptionState exception_state; NonThrowableExceptionState exception_state;
......
// META: script=/generic-sensor/resources/generic-sensor-helpers.js
'use strict';
sensor_test(async (t, sensorProvider) => {
const sensor = new AmbientLightSensor({
frequency: 10 // 10Hz is the maximum frequency allowed by the Blink
// implementation.
});
t.add_cleanup(() => {
sensor.stop();
});
const eventWatcher =
new EventWatcher(t, sensor, ['activate', 'reading', 'error']);
sensor.start();
await eventWatcher.wait_for('activate');
assert_false(
sensor.hasReading, 'Sensor has no readings immediately after activation');
assert_equals(
sensor.illuminance, null,
'Sensor must have no illuminance immediately after activation');
const mockSensor =
await sensorProvider.getCreatedSensor('AmbientLightSensor');
await mockSensor.setSensorReading([[24], [35], [49], [35], [24]]);
// This loop checks that illuminance rounding causes the following to happen:
// 1. The first ever reading goes from nothing to 24. A new "reading" event is
// emitted and the rounded illuminance value is 0.
// 2. Going from 24 to 35 is not significant enough. No "reading" event is
// emitted and the illuminance value remains the same.
// 3. Going from 24 to 49 is significant enough. A "reading" event is emitted
// and the rounded illuminance value is 50.
// 4. Going from 49 to 35 is not significant enough. No "reading" event is
// emitted and the illuminance value remains the same.
// 5. Going from 49 to 24 is significant enough. A "reading" event is emitted
// and the rounded illuminance value is 0.
// 6. We are back to the first raw reading value. We are at 24 and get 24, so
// nothing happens.
// 7. Go to step 3.
for (let i = 0; i < 3; i++) {
await eventWatcher.wait_for('reading');
assert_true(sensor.hasReading);
assert_equals(sensor.illuminance, 0, 'Rounded illuminance should be 0');
await eventWatcher.wait_for('reading');
assert_true(sensor.hasReading);
assert_equals(sensor.illuminance, 50, 'Rounded illuminance should be 50');
}
}, 'Illuminance rounding');
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment