Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Unified Diff: content/renderer/device_sensors/device_motion_event_pump_unittest.cc

Issue 2953263002: Revert of Reland: Refactor DeviceMotionEventPump to use //device/generic_sensor instead of //device/sensors (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/renderer/device_sensors/device_motion_event_pump.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 00a82fdf839cec6e2d9a762aaee321a9d61d4776..a87462441ecdef0eecbc89a3a7b9ab71195322e1 100644
--- a/content/renderer/device_sensors/device_motion_event_pump_unittest.cc
+++ b/content/renderer/device_sensors/device_motion_event_pump_unittest.cc
@@ -11,31 +11,15 @@
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
-#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#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/generic_sensor/public/cpp/sensor_reading.h"
-#include "device/generic_sensor/public/interfaces/sensor.mojom.h"
-#include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h"
-#include "device/sensors/public/cpp/motion_data.h"
-#include "mojo/public/cpp/bindings/interface_request.h"
+#include "device/sensors/public/cpp/device_motion_hardware_buffer.h"
#include "mojo/public/cpp/system/buffer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceMotionListener.h"
-
-namespace {
-
-constexpr uint64_t kReadingBufferSize =
- sizeof(device::SensorReadingSharedBuffer);
-
-constexpr uint64_t kSharedBufferSizeInBytes =
- kReadingBufferSize * static_cast<uint64_t>(device::mojom::SensorType::LAST);
-
-} // namespace
namespace content {
@@ -72,83 +56,8 @@
class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump {
public:
DeviceMotionEventPumpForTesting()
- : DeviceMotionEventPump(nullptr), stop_on_fire_event_(true) {}
+ : DeviceMotionEventPump(0), stop_on_fire_event_(true) {}
~DeviceMotionEventPumpForTesting() override {}
-
- // DeviceMotionEventPump:
- void SendStartMessage() override {
- accelerometer_.mode = device::mojom::ReportingMode::CONTINUOUS;
- linear_acceleration_sensor_.mode = device::mojom::ReportingMode::ON_CHANGE;
- gyroscope_.mode = device::mojom::ReportingMode::CONTINUOUS;
-
- shared_memory_ = mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes);
-
- accelerometer_.shared_buffer = shared_memory_->MapAtOffset(
- kReadingBufferSize,
- device::SensorReadingSharedBuffer::GetOffset(accelerometer_.type));
- accelerometer_buffer_ = static_cast<device::SensorReadingSharedBuffer*>(
- accelerometer_.shared_buffer.get());
-
- linear_acceleration_sensor_.shared_buffer = shared_memory_->MapAtOffset(
- kReadingBufferSize, device::SensorReadingSharedBuffer::GetOffset(
- linear_acceleration_sensor_.type));
- linear_acceleration_sensor_buffer_ =
- static_cast<device::SensorReadingSharedBuffer*>(
- linear_acceleration_sensor_.shared_buffer.get());
-
- gyroscope_.shared_buffer = shared_memory_->MapAtOffset(
- kReadingBufferSize,
- device::SensorReadingSharedBuffer::GetOffset(gyroscope_.type));
- gyroscope_buffer_ = static_cast<device::SensorReadingSharedBuffer*>(
- gyroscope_.shared_buffer.get());
- }
-
- void StartFireEvent() { DeviceMotionEventPump::DidStart(); }
-
- void SetAccelerometerSensorData(bool active,
- double d0,
- double d1,
- double d2) {
- if (active) {
- mojo::MakeRequest(&accelerometer_.sensor);
- accelerometer_buffer_->reading.timestamp =
- (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
- accelerometer_buffer_->reading.values[0].value() = d0;
- accelerometer_buffer_->reading.values[1].value() = d1;
- accelerometer_buffer_->reading.values[2].value() = d2;
- } else {
- accelerometer_.sensor.reset();
- }
- }
-
- void SetLinearAccelerationSensorData(bool active,
- double d0,
- double d1,
- double d2) {
- if (active) {
- mojo::MakeRequest(&linear_acceleration_sensor_.sensor);
- linear_acceleration_sensor_buffer_->reading.timestamp =
- (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
- linear_acceleration_sensor_buffer_->reading.values[0].value() = d0;
- linear_acceleration_sensor_buffer_->reading.values[1].value() = d1;
- linear_acceleration_sensor_buffer_->reading.values[2].value() = d2;
- } else {
- linear_acceleration_sensor_.sensor.reset();
- }
- }
-
- void SetGyroscopeSensorData(bool active, double d0, double d1, double d2) {
- if (active) {
- mojo::MakeRequest(&gyroscope_.sensor);
- gyroscope_buffer_->reading.timestamp =
- (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
- gyroscope_buffer_->reading.values[0].value() = d0;
- gyroscope_buffer_->reading.values[1].value() = d1;
- gyroscope_buffer_->reading.values[2].value() = d2;
- } else {
- gyroscope_.sensor.reset();
- }
- }
void set_stop_on_fire_event(bool stop_on_fire_event) {
stop_on_fire_event_ = stop_on_fire_event;
@@ -156,10 +65,13 @@
bool stop_on_fire_event() { return stop_on_fire_event_; }
- int pump_delay_microseconds() const { return kDefaultPumpDelayMicroseconds; }
-
- protected:
- // 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 {}
void FireEvent() override {
DeviceMotionEventPump::FireEvent();
if (stop_on_fire_event_) {
@@ -170,10 +82,6 @@
private:
bool stop_on_fire_event_;
- mojo::ScopedSharedBufferHandle shared_memory_;
- device::SensorReadingSharedBuffer* accelerometer_buffer_;
- device::SensorReadingSharedBuffer* linear_acceleration_sensor_buffer_;
- device::SensorReadingSharedBuffer* gyroscope_buffer_;
DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting);
};
@@ -185,102 +93,90 @@
protected:
void SetUp() override {
listener_.reset(new MockDeviceMotionListener);
- motion_pump_.reset(new DeviceMotionEventPumpForTesting());
+ 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;
}
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, AllSensorsAreActive) {
+TEST_F(DeviceMotionEventPumpTest, DidStartPolling) {
+ InitBuffer(true);
+
motion_pump()->Start(listener());
- motion_pump()->SetAccelerometerSensorData(true /* active */, 1, 2, 3);
- motion_pump()->SetLinearAccelerationSensorData(true /* active */, 4, 5, 6);
- motion_pump()->SetGyroscopeSensorData(true /* active */, 7, 8, 9);
- motion_pump()->StartFireEvent();
+ motion_pump()->DidStart(handle());
base::RunLoop().Run();
- device::MotionData received_data = listener()->data();
+ const device::MotionData& 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(4, received_data.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_TRUE(received_data.has_acceleration_y);
- EXPECT_EQ(5, received_data.acceleration_y);
+ EXPECT_EQ(3, static_cast<double>(received_data.acceleration_z));
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);
+ 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, TwoSensorsAreActive) {
+TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) {
+ InitBuffer(false);
+
motion_pump()->Start(listener());
- motion_pump()->SetAccelerometerSensorData(true /* active */, 1, 2, 3);
- motion_pump()->SetLinearAccelerationSensorData(false /* active */, 4, 5, 6);
- motion_pump()->SetGyroscopeSensorData(true /* active */, 7, 8, 9);
- motion_pump()->StartFireEvent();
+ motion_pump()->DidStart(handle());
base::RunLoop().Run();
- device::MotionData 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);
-
+ const device::MotionData& received_data = listener()->data();
+ // No change in device motion because all_available_sensors_are_active is
+ // false.
+ 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_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, NoActiveSensors) {
- motion_pump()->Start(listener());
- motion_pump()->StartFireEvent();
-
- base::RunLoop().Run();
-
- device::MotionData received_data = listener()->data();
- EXPECT_TRUE(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);
@@ -293,11 +189,11 @@
EXPECT_GE(60, base::Time::kMicrosecondsPerSecond /
motion_pump()->pump_delay_microseconds());
+ InitBuffer(true);
+
+ motion_pump()->set_stop_on_fire_event(false);
motion_pump()->Start(listener());
- motion_pump()->SetLinearAccelerationSensorData(true /* active */, 4, 5, 6);
-
- motion_pump()->set_stop_on_fire_event(false);
- motion_pump()->StartFireEvent();
+ motion_pump()->DidStart(handle());
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
« no previous file with comments | « content/renderer/device_sensors/device_motion_event_pump.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698