| Index: content/renderer/device_sensors/device_motion_event_pump_unittest.cc
|
| diff --git a/content/renderer/device_sensors/device_motion_event_pump_unittest.cc b/content/renderer/device_sensors/device_motion_event_pump_unittest.cc
|
| index a87462441ecdef0eecbc89a3a7b9ab71195322e1..1dae04d03c8226ac60a4c74f16c5202b43e35f96 100644
|
| --- a/content/renderer/device_sensors/device_motion_event_pump_unittest.cc
|
| +++ b/content/renderer/device_sensors/device_motion_event_pump_unittest.cc
|
| @@ -15,10 +15,14 @@
|
| #include "base/run_loop.h"
|
| #include "base/single_thread_task_runner.h"
|
| #include "base/threading/thread_task_runner_handle.h"
|
| +#include "base/time/time.h"
|
| #include "content/public/test/test_utils.h"
|
| -#include "device/sensors/public/cpp/device_motion_hardware_buffer.h"
|
| +#include "content/renderer/device_sensors/device_orientation_util.h"
|
| +#include "device/generic_sensor/public/cpp/sensor_reading.h"
|
| +#include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h"
|
| #include "mojo/public/cpp/system/buffer.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| +#include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceMotionData.h"
|
| #include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceMotionListener.h"
|
|
|
| namespace content {
|
| @@ -31,7 +35,7 @@ class MockDeviceMotionListener : public blink::WebDeviceMotionListener {
|
| }
|
| ~MockDeviceMotionListener() override {}
|
|
|
| - void DidChangeDeviceMotion(const device::MotionData& data) override {
|
| + void DidChangeDeviceMotion(const blink::WebDeviceMotionData& data) override {
|
| memcpy(&data_, &data, sizeof(data));
|
| did_change_device_motion_ = true;
|
| ++number_of_events_;
|
| @@ -43,12 +47,12 @@ class MockDeviceMotionListener : public blink::WebDeviceMotionListener {
|
|
|
| int number_of_events() const { return number_of_events_; }
|
|
|
| - const device::MotionData& data() const { return data_; }
|
| + const blink::WebDeviceMotionData& data() const { return data_; }
|
|
|
| private:
|
| bool did_change_device_motion_;
|
| int number_of_events_;
|
| - device::MotionData data_;
|
| + blink::WebDeviceMotionData data_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener);
|
| };
|
| @@ -56,9 +60,53 @@ class MockDeviceMotionListener : public blink::WebDeviceMotionListener {
|
| class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump {
|
| public:
|
| DeviceMotionEventPumpForTesting()
|
| - : DeviceMotionEventPump(0), stop_on_fire_event_(true) {}
|
| + : DeviceMotionEventPump(nullptr), stop_on_fire_event_(true) {}
|
| ~DeviceMotionEventPumpForTesting() override {}
|
|
|
| + // DeviceMotionEventPump:
|
| + void SendStartMessage() override {
|
| + sensors_.push_back(base::MakeUnique<SensorEntry>(this));
|
| + sensors_.push_back(base::MakeUnique<SensorEntry>(this));
|
| + sensors_.push_back(base::MakeUnique<SensorEntry>(this));
|
| +
|
| + sensors_[0]->type = device::mojom::SensorType::ACCELEROMETER;
|
| + sensors_[1]->type = device::mojom::SensorType::LINEAR_ACCELERATION;
|
| + sensors_[2]->type = device::mojom::SensorType::GYROSCOPE;
|
| +
|
| + sensors_[0]->mode = device::mojom::ReportingMode::CONTINUOUS;
|
| + sensors_[1]->mode = device::mojom::ReportingMode::ON_CHANGE;
|
| + sensors_[2]->mode = device::mojom::ReportingMode::CONTINUOUS;
|
| +
|
| + shared_memory_ = mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes);
|
| +
|
| + buffers_.resize(sensors_.size());
|
| +
|
| + for (size_t i = 0; i < sensors_.size(); ++i) {
|
| + sensors_[i]->shared_buffer = shared_memory_->MapAtOffset(
|
| + kReadingBufferSize,
|
| + device::SensorReadingSharedBuffer::GetOffset(sensors_[i]->type));
|
| + buffers_[i] = static_cast<device::SensorReadingSharedBuffer*>(
|
| + sensors_[i]->shared_buffer.get());
|
| + }
|
| + }
|
| +
|
| + void StartFireEvent() { DeviceSensorEventPump::DidStart(); }
|
| +
|
| + void SetSensorData(int index, bool active, double d0, double d1, double d2) {
|
| + sensors_[index]->active = active;
|
| + if (active) {
|
| + buffers_[index]->reading.timestamp =
|
| + (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
|
| + buffers_[index]->reading.values[0].value() = d0;
|
| + buffers_[index]->reading.values[1].value() = d1;
|
| + buffers_[index]->reading.values[2].value() = d2;
|
| + }
|
| + }
|
| +
|
| + void set_num_available_sensors(int num_available_sensors) {
|
| + num_available_sensors_ = num_available_sensors;
|
| + }
|
| +
|
| void set_stop_on_fire_event(bool stop_on_fire_event) {
|
| stop_on_fire_event_ = stop_on_fire_event;
|
| }
|
| @@ -67,11 +115,8 @@ class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump {
|
|
|
| int pump_delay_microseconds() const { return pump_delay_microseconds_; }
|
|
|
| - void DidStart(mojo::ScopedSharedBufferHandle renderer_handle) {
|
| - DeviceMotionEventPump::DidStart(std::move(renderer_handle));
|
| - }
|
| - void SendStartMessage() override {}
|
| - void SendStopMessage() override {}
|
| + protected:
|
| + // DeviceMotionEventPump:
|
| void FireEvent() override {
|
| DeviceMotionEventPump::FireEvent();
|
| if (stop_on_fire_event_) {
|
| @@ -82,6 +127,8 @@ class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump {
|
|
|
| private:
|
| bool stop_on_fire_event_;
|
| + mojo::ScopedSharedBufferHandle shared_memory_;
|
| + std::vector<device::SensorReadingSharedBuffer*> buffers_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting);
|
| };
|
| @@ -93,90 +140,130 @@ class DeviceMotionEventPumpTest : public testing::Test {
|
| protected:
|
| void SetUp() override {
|
| listener_.reset(new MockDeviceMotionListener);
|
| - motion_pump_.reset(new DeviceMotionEventPumpForTesting);
|
| - shared_memory_ = mojo::SharedBufferHandle::Create(
|
| - sizeof(device::DeviceMotionHardwareBuffer));
|
| - mapping_ = shared_memory_->Map(sizeof(device::DeviceMotionHardwareBuffer));
|
| - ASSERT_TRUE(mapping_);
|
| - memset(buffer(), 0, sizeof(device::DeviceMotionHardwareBuffer));
|
| - }
|
| -
|
| - void InitBuffer(bool allAvailableSensorsActive) {
|
| - device::MotionData& data = buffer()->data;
|
| - data.acceleration_x = 1;
|
| - data.has_acceleration_x = true;
|
| - data.acceleration_y = 2;
|
| - data.has_acceleration_y = true;
|
| - data.acceleration_z = 3;
|
| - data.has_acceleration_z = true;
|
| - data.all_available_sensors_are_active = allAvailableSensorsActive;
|
| + motion_pump_.reset(new DeviceMotionEventPumpForTesting());
|
| }
|
|
|
| MockDeviceMotionListener* listener() { return listener_.get(); }
|
| DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); }
|
| - mojo::ScopedSharedBufferHandle handle() {
|
| - return shared_memory_->Clone(
|
| - mojo::SharedBufferHandle::AccessMode::READ_ONLY);
|
| - }
|
| - device::DeviceMotionHardwareBuffer* buffer() {
|
| - return reinterpret_cast<device::DeviceMotionHardwareBuffer*>(
|
| - mapping_.get());
|
| - }
|
|
|
| private:
|
| base::MessageLoop loop_;
|
| std::unique_ptr<MockDeviceMotionListener> listener_;
|
| std::unique_ptr<DeviceMotionEventPumpForTesting> motion_pump_;
|
| - mojo::ScopedSharedBufferHandle shared_memory_;
|
| - mojo::ScopedSharedBufferMapping mapping_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest);
|
| };
|
|
|
| -TEST_F(DeviceMotionEventPumpTest, DidStartPolling) {
|
| - InitBuffer(true);
|
| -
|
| +TEST_F(DeviceMotionEventPumpTest, AllSensorsAreActive) {
|
| motion_pump()->Start(listener());
|
| - motion_pump()->DidStart(handle());
|
| + motion_pump()->set_num_available_sensors(3);
|
| + motion_pump()->SetSensorData(0, true /* active */, 1, 2, 3);
|
| + motion_pump()->SetSensorData(1, true /* active */, 4, 5, 6);
|
| + motion_pump()->SetSensorData(2, true /* active */, 7, 8, 9);
|
| + motion_pump()->StartFireEvent();
|
|
|
| base::RunLoop().Run();
|
|
|
| - const device::MotionData& received_data = listener()->data();
|
| + blink::WebDeviceMotionData received_data = listener()->data();
|
| EXPECT_TRUE(listener()->did_change_device_motion());
|
| +
|
| + EXPECT_TRUE(received_data.has_acceleration_including_gravity_x);
|
| + EXPECT_EQ(1, received_data.acceleration_including_gravity_x);
|
| + EXPECT_TRUE(received_data.has_acceleration_including_gravity_y);
|
| + EXPECT_EQ(2, received_data.acceleration_including_gravity_y);
|
| + EXPECT_TRUE(received_data.has_acceleration_including_gravity_z);
|
| + EXPECT_EQ(3, received_data.acceleration_including_gravity_z);
|
| +
|
| EXPECT_TRUE(received_data.has_acceleration_x);
|
| - EXPECT_EQ(1, static_cast<double>(received_data.acceleration_x));
|
| - EXPECT_TRUE(received_data.has_acceleration_x);
|
| - EXPECT_EQ(2, static_cast<double>(received_data.acceleration_y));
|
| + EXPECT_EQ(4, received_data.acceleration_x);
|
| EXPECT_TRUE(received_data.has_acceleration_y);
|
| - EXPECT_EQ(3, static_cast<double>(received_data.acceleration_z));
|
| + EXPECT_EQ(5, received_data.acceleration_y);
|
| EXPECT_TRUE(received_data.has_acceleration_z);
|
| + EXPECT_EQ(6, received_data.acceleration_z);
|
| +
|
| + EXPECT_TRUE(received_data.has_rotation_rate_alpha);
|
| + EXPECT_EQ(7, received_data.rotation_rate_alpha);
|
| + EXPECT_TRUE(received_data.has_rotation_rate_beta);
|
| + EXPECT_EQ(8, received_data.rotation_rate_beta);
|
| + EXPECT_TRUE(received_data.has_rotation_rate_gamma);
|
| + EXPECT_EQ(9, received_data.rotation_rate_gamma);
|
| +}
|
| +
|
| +TEST_F(DeviceMotionEventPumpTest, AllAvailableSensorsAreActive) {
|
| + motion_pump()->Start(listener());
|
| + motion_pump()->set_num_available_sensors(2);
|
| + motion_pump()->SetSensorData(0, true /* active */, 1, 2, 3);
|
| + motion_pump()->SetSensorData(2, true /* active */, 7, 8, 9);
|
| + motion_pump()->StartFireEvent();
|
| +
|
| + base::RunLoop().Run();
|
| +
|
| + blink::WebDeviceMotionData received_data = listener()->data();
|
| + EXPECT_TRUE(listener()->did_change_device_motion());
|
| +
|
| + EXPECT_TRUE(received_data.has_acceleration_including_gravity_x);
|
| + EXPECT_EQ(1, received_data.acceleration_including_gravity_x);
|
| + EXPECT_TRUE(received_data.has_acceleration_including_gravity_y);
|
| + EXPECT_EQ(2, received_data.acceleration_including_gravity_y);
|
| + EXPECT_TRUE(received_data.has_acceleration_including_gravity_z);
|
| + EXPECT_EQ(3, received_data.acceleration_including_gravity_z);
|
| +
|
| + EXPECT_FALSE(received_data.has_acceleration_x);
|
| + EXPECT_FALSE(received_data.has_acceleration_y);
|
| + EXPECT_FALSE(received_data.has_acceleration_z);
|
| +
|
| + EXPECT_TRUE(received_data.has_rotation_rate_alpha);
|
| + EXPECT_EQ(7, received_data.rotation_rate_alpha);
|
| + EXPECT_TRUE(received_data.has_rotation_rate_beta);
|
| + EXPECT_EQ(8, received_data.rotation_rate_beta);
|
| + EXPECT_TRUE(received_data.has_rotation_rate_gamma);
|
| + EXPECT_EQ(9, received_data.rotation_rate_gamma);
|
| +}
|
| +
|
| +TEST_F(DeviceMotionEventPumpTest, NotAllAvailableSensorsAreActive) {
|
| + motion_pump()->Start(listener());
|
| + motion_pump()->set_num_available_sensors(3);
|
| + motion_pump()->SetSensorData(0, true /* active */, 1, 2, 3);
|
| + motion_pump()->SetSensorData(2, true /* active */, 7, 8, 9);
|
| + motion_pump()->StartFireEvent();
|
| +
|
| + base::RunLoop().Run();
|
| +
|
| + blink::WebDeviceMotionData received_data = listener()->data();
|
| + // No change in device motion because not all available sensors are active.
|
| + EXPECT_FALSE(listener()->did_change_device_motion());
|
| +
|
| + EXPECT_FALSE(received_data.has_acceleration_x);
|
| + EXPECT_FALSE(received_data.has_acceleration_y);
|
| + EXPECT_FALSE(received_data.has_acceleration_z);
|
| +
|
| EXPECT_FALSE(received_data.has_acceleration_including_gravity_x);
|
| EXPECT_FALSE(received_data.has_acceleration_including_gravity_y);
|
| EXPECT_FALSE(received_data.has_acceleration_including_gravity_z);
|
| +
|
| EXPECT_FALSE(received_data.has_rotation_rate_alpha);
|
| EXPECT_FALSE(received_data.has_rotation_rate_beta);
|
| EXPECT_FALSE(received_data.has_rotation_rate_gamma);
|
| }
|
|
|
| -TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) {
|
| - InitBuffer(false);
|
| -
|
| +TEST_F(DeviceMotionEventPumpTest, NoAvailableSensors) {
|
| motion_pump()->Start(listener());
|
| - motion_pump()->DidStart(handle());
|
| + motion_pump()->StartFireEvent();
|
|
|
| base::RunLoop().Run();
|
|
|
| - const device::MotionData& received_data = listener()->data();
|
| - // No change in device motion because all_available_sensors_are_active is
|
| - // false.
|
| + blink::WebDeviceMotionData received_data = listener()->data();
|
| + // No change in device motion because no sensor data was updated.
|
| EXPECT_FALSE(listener()->did_change_device_motion());
|
| - EXPECT_FALSE(received_data.has_acceleration_x);
|
| +
|
| EXPECT_FALSE(received_data.has_acceleration_x);
|
| EXPECT_FALSE(received_data.has_acceleration_y);
|
| EXPECT_FALSE(received_data.has_acceleration_z);
|
| +
|
| EXPECT_FALSE(received_data.has_acceleration_including_gravity_x);
|
| EXPECT_FALSE(received_data.has_acceleration_including_gravity_y);
|
| EXPECT_FALSE(received_data.has_acceleration_including_gravity_z);
|
| +
|
| EXPECT_FALSE(received_data.has_rotation_rate_alpha);
|
| EXPECT_FALSE(received_data.has_rotation_rate_beta);
|
| EXPECT_FALSE(received_data.has_rotation_rate_gamma);
|
| @@ -189,11 +276,12 @@ TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) {
|
| EXPECT_GE(60, base::Time::kMicrosecondsPerSecond /
|
| motion_pump()->pump_delay_microseconds());
|
|
|
| - InitBuffer(true);
|
| + motion_pump()->Start(listener());
|
| + motion_pump()->set_num_available_sensors(1);
|
| + motion_pump()->SetSensorData(1, true /* active */, 4, 5, 6);
|
|
|
| motion_pump()->set_stop_on_fire_event(false);
|
| - motion_pump()->Start(listener());
|
| - motion_pump()->DidStart(handle());
|
| + motion_pump()->StartFireEvent();
|
|
|
| base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
|
| FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
|
|
|