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

Unified Diff: content/browser/device_sensors/sensor_manager_chromeos_unittest.cc

Issue 934843002: Implement DeviceMotionEvent API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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
Index: content/browser/device_sensors/sensor_manager_chromeos_unittest.cc
diff --git a/content/browser/device_sensors/sensor_manager_chromeos_unittest.cc b/content/browser/device_sensors/sensor_manager_chromeos_unittest.cc
index 3e718392951a6a224b780b07867e809086d0f273..cd6e9971a8b4875432a89a4f4e9f0e58e983481a 100644
--- a/content/browser/device_sensors/sensor_manager_chromeos_unittest.cc
+++ b/content/browser/device_sensors/sensor_manager_chromeos_unittest.cc
@@ -6,13 +6,17 @@
#include "base/memory/scoped_ptr.h"
#include "chromeos/accelerometer/accelerometer_types.h"
+#include "content/common/device_sensors/device_motion_hardware_buffer.h"
#include "content/common/device_sensors/device_orientation_hardware_buffer.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/gfx/geometry/vector3d_f.h"
namespace {
const double kMeanGravity = 9.80665;
+const double kGravityFilterRatio = 0.8;
+
// Isolated content::SensorManagerChromeOS from the active
// chromeos::AccelerometerReader. This allows for direct control over which
// accelerometer events are provided to the sensor manager.
@@ -36,6 +40,7 @@ namespace content {
class SensorManagerChromeOSTest : public testing::Test {
public:
SensorManagerChromeOSTest() {
+ motion_buffer_.reset(new DeviceMotionHardwareBuffer);
orientation_buffer_.reset(new DeviceOrientationHardwareBuffer);
}
@@ -47,6 +52,8 @@ class SensorManagerChromeOSTest : public testing::Test {
sensor_manager_->OnAccelerometerUpdated(update);
}
+ DeviceMotionHardwareBuffer* motion_buffer() { return motion_buffer_.get(); }
+
DeviceOrientationHardwareBuffer* orientation_buffer() {
return orientation_buffer_.get();
}
@@ -57,11 +64,13 @@ class SensorManagerChromeOSTest : public testing::Test {
void SetUp() override {
testing::Test::SetUp();
sensor_manager_.reset(new TestSensorManagerChromeOS);
+ sensor_manager_->StartFetchingDeviceMotionData(motion_buffer_.get());
sensor_manager_->StartFetchingDeviceOrientationData(
orientation_buffer_.get());
}
void TearDown() override {
+ sensor_manager_->StopFetchingDeviceMotionData();
sensor_manager_->StopFetchingDeviceOrientationData();
testing::Test::TearDown();
}
@@ -69,11 +78,43 @@ class SensorManagerChromeOSTest : public testing::Test {
private:
scoped_ptr<TestSensorManagerChromeOS> sensor_manager_;
+ scoped_ptr<DeviceMotionHardwareBuffer> motion_buffer_;
+
flackr 2015/02/18 22:34:09 nit: No need to separate these.
jonross 2015/02/25 00:09:57 Done.
scoped_ptr<DeviceOrientationHardwareBuffer> orientation_buffer_;
DISALLOW_COPY_AND_ASSIGN(SensorManagerChromeOSTest);
};
+// Tests that starting to process motion data will update the associated buffer.
+TEST_F(SensorManagerChromeOSTest, MotionBuffer) {
+ DeviceMotionHardwareBuffer* buffer = motion_buffer();
+ EXPECT_FLOAT_EQ(16.0f, buffer->data.interval);
+ EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityX);
+ EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityY);
+ EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityZ);
+ EXPECT_FALSE(buffer->data.hasAccelerationX);
+ EXPECT_FALSE(buffer->data.hasAccelerationY);
+ EXPECT_FALSE(buffer->data.hasAccelerationZ);
+ EXPECT_FALSE(buffer->data.hasRotationRateAlpha);
+ EXPECT_FALSE(buffer->data.hasRotationRateBeta);
+ EXPECT_FALSE(buffer->data.hasRotationRateGamma);
+
+ OnAccelerationIncludingGravity(0.0f, 0.0f, 1.0f);
+ EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityX);
+ EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityY);
+ EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityZ);
+ EXPECT_TRUE(buffer->data.hasAccelerationX);
+ EXPECT_TRUE(buffer->data.hasAccelerationY);
+ EXPECT_TRUE(buffer->data.hasAccelerationZ);
+ EXPECT_FALSE(buffer->data.hasRotationRateAlpha);
+ EXPECT_FALSE(buffer->data.hasRotationRateBeta);
+ EXPECT_FALSE(buffer->data.hasRotationRateGamma);
+ EXPECT_TRUE(buffer->data.allAvailableSensorsAreActive);
+
+ sensor_manager()->StopFetchingDeviceMotionData();
+ EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive);
+}
+
// Tests that starting to process orientation data will update the associated
// buffer.
TEST_F(SensorManagerChromeOSTest, OrientationBuffer) {
@@ -98,44 +139,102 @@ TEST_F(SensorManagerChromeOSTest, OrientationBuffer) {
// Tests a device resting flat.
TEST_F(SensorManagerChromeOSTest, NeutralOrientation) {
OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity);
- DeviceOrientationHardwareBuffer* buffer = orientation_buffer();
- EXPECT_FLOAT_EQ(0.0f, buffer->data.beta);
- EXPECT_FLOAT_EQ(0.0f, buffer->data.gamma);
+
+ DeviceMotionHardwareBuffer* motion = motion_buffer();
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
+ EXPECT_FLOAT_EQ(-kMeanGravity, motion->data.accelerationIncludingGravityZ);
+
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationX);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationY);
+ EXPECT_FLOAT_EQ(-kMeanGravity * kGravityFilterRatio,
+ motion->data.accelerationZ);
+
+ DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
+ EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
+ EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma);
}
// Tests an upside-down device, such that the W3C boundary [-180,180) causes the
// beta value to become negative.
TEST_F(SensorManagerChromeOSTest, UpsideDown) {
OnAccelerationIncludingGravity(0.0f, 0.0f, kMeanGravity);
- DeviceOrientationHardwareBuffer* buffer = orientation_buffer();
- EXPECT_FLOAT_EQ(-180.0f, buffer->data.beta);
- EXPECT_FLOAT_EQ(0.0f, buffer->data.gamma);
+
+ DeviceMotionHardwareBuffer* motion = motion_buffer();
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
+ EXPECT_FLOAT_EQ(kMeanGravity, motion->data.accelerationIncludingGravityZ);
+
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationX);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationY);
+ EXPECT_FLOAT_EQ(kMeanGravity * kGravityFilterRatio,
+ motion->data.accelerationZ);
+
+ DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
+ EXPECT_FLOAT_EQ(-180.0f, orientation->data.beta);
+ EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma);
}
// Tests for positive beta value before the device is completely upside-down
TEST_F(SensorManagerChromeOSTest, BeforeUpsideDownBoundary) {
OnAccelerationIncludingGravity(0.0f, -kMeanGravity / 2.0f,
kMeanGravity / 2.0f);
- DeviceOrientationHardwareBuffer* buffer = orientation_buffer();
- EXPECT_FLOAT_EQ(135.0f, buffer->data.beta);
- EXPECT_FLOAT_EQ(0.0f, buffer->data.gamma);
+
+ DeviceMotionHardwareBuffer* motion = motion_buffer();
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX);
+ EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f,
+ motion->data.accelerationIncludingGravityY);
+ EXPECT_FLOAT_EQ(kMeanGravity / 2.0f,
+ motion->data.accelerationIncludingGravityZ);
+
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationX);
+ EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f * kGravityFilterRatio,
+ motion->data.accelerationY);
+ EXPECT_FLOAT_EQ(kMeanGravity / 2.0f * kGravityFilterRatio,
+ motion->data.accelerationZ);
+
+ DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
+ EXPECT_FLOAT_EQ(135.0f, orientation->data.beta);
+ EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma);
}
// Tests a device lying on its left-edge.
TEST_F(SensorManagerChromeOSTest, LeftEdge) {
OnAccelerationIncludingGravity(-kMeanGravity, 0.0f, 0.0f);
- DeviceOrientationHardwareBuffer* buffer = orientation_buffer();
- EXPECT_FLOAT_EQ(0.0f, buffer->data.beta);
- EXPECT_FLOAT_EQ(-90.0f, buffer->data.gamma);
+
+ DeviceMotionHardwareBuffer* motion = motion_buffer();
+ EXPECT_FLOAT_EQ(-kMeanGravity, motion->data.accelerationIncludingGravityX);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityZ);
+
+ EXPECT_FLOAT_EQ(-kMeanGravity * kGravityFilterRatio,
+ motion->data.accelerationX);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationY);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationZ);
+
+ DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
+ EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
+ EXPECT_FLOAT_EQ(-90.0f, orientation->data.gamma);
}
// Tests a device lying on its right-edge, such that the W3C boundary [-90,90)
// causes the gamma value to become negative.
TEST_F(SensorManagerChromeOSTest, RightEdge) {
OnAccelerationIncludingGravity(kMeanGravity, 0.0f, 0.0f);
- DeviceOrientationHardwareBuffer* buffer = orientation_buffer();
- EXPECT_FLOAT_EQ(0.0f, buffer->data.beta);
- EXPECT_FLOAT_EQ(-90.0f, buffer->data.gamma);
+
+ DeviceMotionHardwareBuffer* motion = motion_buffer();
+ EXPECT_FLOAT_EQ(kMeanGravity, motion->data.accelerationIncludingGravityX);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityZ);
+
+ EXPECT_FLOAT_EQ(kMeanGravity * kGravityFilterRatio,
+ motion->data.accelerationX);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationY);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationZ);
+
+ DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
+ EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
+ EXPECT_FLOAT_EQ(-90.0f, orientation->data.gamma);
}
// Tests for positive gamma value before the device is completely on its right
@@ -143,9 +242,75 @@ TEST_F(SensorManagerChromeOSTest, RightEdge) {
TEST_F(SensorManagerChromeOSTest, BeforeRightEdgeBoundary) {
OnAccelerationIncludingGravity(kMeanGravity / 2.0f, 0.0f,
-kMeanGravity / 2.0f);
- DeviceOrientationHardwareBuffer* buffer = orientation_buffer();
- EXPECT_FLOAT_EQ(0.0f, buffer->data.beta);
- EXPECT_FLOAT_EQ(45.0f, buffer->data.gamma);
+
+ DeviceMotionHardwareBuffer* motion = motion_buffer();
+ EXPECT_FLOAT_EQ(kMeanGravity / 2.0f,
+ motion->data.accelerationIncludingGravityX);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
+ EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f,
+ motion->data.accelerationIncludingGravityZ);
+
+ EXPECT_FLOAT_EQ(kMeanGravity / 2.0f * kGravityFilterRatio,
+ motion->data.accelerationX);
+ EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationY);
+ EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f * kGravityFilterRatio,
+ motion->data.accelerationZ);
+
+ DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
+ EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
+ EXPECT_FLOAT_EQ(45.0f, orientation->data.gamma);
+}
+
+// Tests that the gravity component of gravity is increasingly filtered out of
+// the acceleration data as the device remains stationary.
+TEST_F(SensorManagerChromeOSTest, GravityFilterComletesForStationaryDevice) {
+ OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity);
+
+ DeviceMotionHardwareBuffer* motion = motion_buffer();
+ EXPECT_FLOAT_EQ(-kMeanGravity, motion->data.accelerationIncludingGravityZ);
+ EXPECT_FLOAT_EQ(-kMeanGravity * kGravityFilterRatio,
+ motion->data.accelerationZ);
+
+ double previous_acceleration = motion->data.accelerationZ;
+ double acceleration;
+ // A third of a second, the filter will have removed enough gravity to have
+ // approached the asymptote of 0.
+ for (int i = 0; i < 20; ++i) {
+ OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity);
+ acceleration = motion->data.accelerationZ;
+ EXPECT_LT(previous_acceleration, acceleration);
+ previous_acceleration = acceleration;
+ }
+}
+
+// Tests that after gravity has been filtered out, that other acceleration
+// sources as reported.
+TEST_F(SensorManagerChromeOSTest, NonGravityAcceleration) {
+ for (int i = 0; i < 20; ++i)
+ OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity);
+ DeviceMotionHardwareBuffer* motion = motion_buffer();
+ gfx::Vector3dF stable(motion->data.accelerationX, motion->data.accelerationY,
+ motion->data.accelerationZ);
+
+ OnAccelerationIncludingGravity(10.0f, 0.0f, -kMeanGravity);
+ EXPECT_FLOAT_EQ(8.0f, motion->data.accelerationX);
+ gfx::Vector3dF accelerating(motion->data.accelerationX,
+ motion->data.accelerationY,
+ motion->data.accelerationZ);
+ EXPECT_GT(accelerating.Length(), stable.Length());
+}
+
+// Tests that after gravity has been filtered out, that acceleration opposing
+// gravity is reported, even though the instantaneous acceleration with gravity
+// is 0.
+TEST_F(SensorManagerChromeOSTest, AccelerationAgainstGravity) {
+ for (int i = 0; i < 20; ++i)
+ OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity);
+ DeviceMotionHardwareBuffer* motion = motion_buffer();
+ double stable_gravity = motion->data.accelerationZ;
+
+ OnAccelerationIncludingGravity(0.0f, 0.0f, 0.0f);
+ EXPECT_LT(stable_gravity, motion->data.accelerationZ);
}
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698