Index: content/renderer/device_sensors/device_orientation_event_pump_unittest.cc |
diff --git a/content/renderer/device_sensors/device_orientation_event_pump_unittest.cc b/content/renderer/device_sensors/device_orientation_event_pump_unittest.cc |
index a21395123e5583832b9750476e2f9fd9ccc421c2..de1c33c858cec29a749c156f834d91a21ccbd1b3 100644 |
--- a/content/renderer/device_sensors/device_orientation_event_pump_unittest.cc |
+++ b/content/renderer/device_sensors/device_orientation_event_pump_unittest.cc |
@@ -2,7 +2,7 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "device_orientation_event_pump.h" |
+#include "content/renderer/device_sensors/device_orientation_event_pump.h" |
#include <string.h> |
@@ -14,8 +14,9 @@ |
#include "base/single_thread_task_runner.h" |
#include "base/threading/thread_task_runner_handle.h" |
#include "content/public/test/test_utils.h" |
-#include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" |
+#include "content/renderer/device_sensors/device_orientation_util.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceOrientationData.h" |
#include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceOrientationListener.h" |
namespace content { |
@@ -29,7 +30,7 @@ class MockDeviceOrientationListener |
~MockDeviceOrientationListener() override {} |
void DidChangeDeviceOrientation( |
- const device::OrientationData& data) override { |
+ const blink::WebDeviceOrientationData& data) override { |
memcpy(&data_, &data, sizeof(data)); |
did_change_device_orientation_ = true; |
} |
@@ -40,11 +41,11 @@ class MockDeviceOrientationListener |
void set_did_change_device_orientation(bool value) { |
did_change_device_orientation_ = value; |
} |
- const device::OrientationData& data() const { return data_; } |
+ const blink::WebDeviceOrientationData& data() const { return data_; } |
private: |
bool did_change_device_orientation_; |
- device::OrientationData data_; |
+ blink::WebDeviceOrientationData data_; |
DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener); |
}; |
@@ -52,21 +53,91 @@ class MockDeviceOrientationListener |
class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { |
public: |
DeviceOrientationEventPumpForTesting() |
- : DeviceOrientationEventPump(nullptr) {} |
+ : DeviceOrientationEventPump(nullptr), |
+ use_orientation_test_data_(false) {} |
~DeviceOrientationEventPumpForTesting() override {} |
- void DidStart(mojo::ScopedSharedBufferHandle renderer_handle) { |
- DeviceOrientationEventPump::DidStart(std::move(renderer_handle)); |
+ // DeviceOrientationEventPump: |
+ 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_.push_back(base::MakeUnique<SensorEntry>(this)); |
+ |
+ sensors_[0]->type = device::mojom::SensorType::RELATIVE_ORIENTATION; |
+ sensors_[1]->type = device::mojom::SensorType::ABSOLUTE_ORIENTATION; |
+ sensors_[2]->type = device::mojom::SensorType::ACCELEROMETER; |
+ sensors_[3]->type = device::mojom::SensorType::MAGNETOMETER; |
+ |
+ sensors_[0]->mode = device::mojom::ReportingMode::CONTINUOUS; |
+ sensors_[1]->mode = device::mojom::ReportingMode::ON_CHANGE; |
+ sensors_[2]->mode = device::mojom::ReportingMode::CONTINUOUS; |
+ sensors_[3]->mode = device::mojom::ReportingMode::ON_CHANGE; |
+ |
+ 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 SendStartMessage() override {} |
- void SendStopMessage() override {} |
+ |
+ void set_use_orientation_test_data(bool use_orientation_test_data) { |
+ use_orientation_test_data_ = use_orientation_test_data; |
+ } |
+ |
+ void set_orientation_test_data(blink::WebDeviceOrientationData data) { |
+ orientation_test_data_ = data; |
+ } |
+ |
+ void StartFireEvent() { DeviceSensorEventPump::DidStart(); } |
+ |
+ void SetSensorData(int index, |
+ bool active, |
+ double d0, |
+ double d1, |
+ double d2, |
+ double d3) { |
+ 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; |
+ buffers_[index]->reading.values[3].value() = d3; |
+ } |
+ } |
+ |
+ // DeviceOrientationEventPump: |
void FireEvent() override { |
DeviceOrientationEventPump::FireEvent(); |
Stop(); |
base::MessageLoop::current()->QuitWhenIdle(); |
} |
+ protected: |
+ // DeviceOrientationEventPump: |
+ bool GetDataFromSharedMemory(blink::WebDeviceOrientationData* data) override { |
+ if (!use_orientation_test_data_) |
+ return DeviceOrientationEventPump::GetDataFromSharedMemory(data); |
+ else { |
+ *data = orientation_test_data_; |
+ return true; |
+ } |
+ } |
+ |
private: |
+ mojo::ScopedSharedBufferHandle shared_memory_; |
+ std::vector<device::SensorReadingSharedBuffer*> buffers_; |
+ bool use_orientation_test_data_; |
+ blink::WebDeviceOrientationData orientation_test_data_; |
+ |
DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting); |
}; |
@@ -77,106 +148,153 @@ class DeviceOrientationEventPumpTest : public testing::Test { |
protected: |
void SetUp() override { |
listener_.reset(new MockDeviceOrientationListener); |
- orientation_pump_.reset(new DeviceOrientationEventPumpForTesting); |
- shared_memory_ = mojo::SharedBufferHandle::Create( |
- sizeof(device::DeviceOrientationHardwareBuffer)); |
- mapping_ = |
- shared_memory_->Map(sizeof(device::DeviceOrientationHardwareBuffer)); |
- ASSERT_TRUE(mapping_); |
- memset(buffer(), 0, sizeof(device::DeviceOrientationHardwareBuffer)); |
- } |
- |
- void InitBuffer() { |
- device::OrientationData& data = buffer()->data; |
- data.alpha = 1; |
- data.has_alpha = true; |
- data.beta = 2; |
- data.has_beta = true; |
- data.gamma = 3; |
- data.has_gamma = true; |
- data.all_available_sensors_are_active = true; |
- } |
- |
- void InitBufferNoData() { |
- device::OrientationData& data = buffer()->data; |
- data.all_available_sensors_are_active = true; |
+ orientation_pump_.reset(new DeviceOrientationEventPumpForTesting()); |
} |
MockDeviceOrientationListener* listener() { return listener_.get(); } |
DeviceOrientationEventPumpForTesting* orientation_pump() { |
return orientation_pump_.get(); |
} |
- mojo::ScopedSharedBufferHandle handle() { |
- return shared_memory_->Clone( |
- mojo::SharedBufferHandle::AccessMode::READ_ONLY); |
- } |
- device::DeviceOrientationHardwareBuffer* buffer() { |
- return reinterpret_cast<device::DeviceOrientationHardwareBuffer*>( |
- mapping_.get()); |
- } |
private: |
base::MessageLoop loop_; |
std::unique_ptr<MockDeviceOrientationListener> listener_; |
std::unique_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_; |
- mojo::ScopedSharedBufferHandle shared_memory_; |
- mojo::ScopedSharedBufferMapping mapping_; |
DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); |
}; |
-TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { |
- InitBuffer(); |
+TEST_F(DeviceOrientationEventPumpTest, Sensor0HasData) { |
orientation_pump()->Start(listener()); |
- orientation_pump()->DidStart(handle()); |
+ double x = 0.1; |
+ double y = 0.2; |
+ double z = 0.3; |
+ double w = 0.4; |
+ orientation_pump()->SetSensorData(0, true /* active */, x, y, z, w); |
+ orientation_pump()->StartFireEvent(); |
base::RunLoop().Run(); |
- const device::OrientationData& received_data = listener()->data(); |
+ blink::WebDeviceOrientationData received_data = listener()->data(); |
EXPECT_TRUE(listener()->did_change_device_orientation()); |
- EXPECT_TRUE(received_data.all_available_sensors_are_active); |
- EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
+ |
+ double expected_alpha, expected_beta, expected_gamma; |
+ ComputeDeviceOrientationFromQuaternion(x, y, z, w, &expected_alpha, |
+ &expected_beta, &expected_gamma); |
+ |
+ EXPECT_EQ(expected_alpha, received_data.alpha); |
EXPECT_TRUE(received_data.has_alpha); |
- EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
+ EXPECT_EQ(expected_beta, received_data.beta); |
EXPECT_TRUE(received_data.has_beta); |
- EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
+ EXPECT_EQ(expected_gamma, received_data.gamma); |
EXPECT_TRUE(received_data.has_gamma); |
+ EXPECT_FALSE(received_data.absolute); |
} |
-TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { |
- InitBufferNoData(); |
+TEST_F(DeviceOrientationEventPumpTest, Sensor1HasData) { |
orientation_pump()->Start(listener()); |
- orientation_pump()->DidStart(handle()); |
+ double x = 0.2; |
+ double y = 0.3; |
+ double z = 0.4; |
+ double w = 0.5; |
+ orientation_pump()->SetSensorData(1, true /* active */, x, y, z, w); |
+ orientation_pump()->StartFireEvent(); |
base::RunLoop().Run(); |
- const device::OrientationData& received_data = listener()->data(); |
+ blink::WebDeviceOrientationData received_data = listener()->data(); |
EXPECT_TRUE(listener()->did_change_device_orientation()); |
- EXPECT_TRUE(received_data.all_available_sensors_are_active); |
+ |
+ double expected_alpha, expected_beta, expected_gamma; |
+ ComputeDeviceOrientationFromQuaternion(x, y, z, w, &expected_alpha, |
+ &expected_beta, &expected_gamma); |
+ |
+ EXPECT_EQ(expected_alpha, received_data.alpha); |
+ EXPECT_TRUE(received_data.has_alpha); |
+ EXPECT_EQ(expected_beta, received_data.beta); |
+ EXPECT_TRUE(received_data.has_beta); |
+ EXPECT_EQ(expected_gamma, received_data.gamma); |
+ EXPECT_TRUE(received_data.has_gamma); |
+ EXPECT_TRUE(received_data.absolute); |
+} |
+ |
+TEST_F(DeviceOrientationEventPumpTest, Sensor2AndSensor3HasData) { |
+ orientation_pump()->Start(listener()); |
+ double gravity_x = 3; |
+ double gravity_y = 4; |
+ double gravity_z = 5; |
+ double geomagnetic_x = 6; |
+ double geomagnetic_y = 7; |
+ double geomagnetic_z = 8; |
+ orientation_pump()->SetSensorData(2, true /* active */, gravity_x, gravity_y, |
+ gravity_z, 0 /* this data is not used */); |
+ orientation_pump()->SetSensorData(3, true /* active */, geomagnetic_x, |
+ geomagnetic_y, geomagnetic_z, |
+ 0 /* this data is not used */); |
+ orientation_pump()->StartFireEvent(); |
+ |
+ base::RunLoop().Run(); |
+ |
+ blink::WebDeviceOrientationData received_data = listener()->data(); |
+ EXPECT_TRUE(listener()->did_change_device_orientation()); |
+ |
+ double expected_alpha, expected_beta, expected_gamma; |
+ ComputeDeviceOrientationFromGravityAndGeomagnetic( |
+ gravity_x, gravity_y, gravity_z, geomagnetic_x, geomagnetic_y, |
+ geomagnetic_z, &expected_alpha, &expected_beta, &expected_gamma); |
+ |
+ EXPECT_EQ(expected_alpha, received_data.alpha); |
+ EXPECT_TRUE(received_data.has_alpha); |
+ EXPECT_EQ(expected_beta, received_data.beta); |
+ EXPECT_TRUE(received_data.has_beta); |
+ EXPECT_EQ(expected_gamma, received_data.gamma); |
+ EXPECT_TRUE(received_data.has_gamma); |
+ EXPECT_TRUE(received_data.absolute); |
+} |
+ |
+TEST_F(DeviceOrientationEventPumpTest, NoSensorData) { |
+ orientation_pump()->Start(listener()); |
+ orientation_pump()->StartFireEvent(); |
+ |
+ base::RunLoop().Run(); |
+ |
+ blink::WebDeviceOrientationData received_data = listener()->data(); |
+ EXPECT_FALSE(listener()->did_change_device_orientation()); |
+ |
EXPECT_FALSE(received_data.has_alpha); |
EXPECT_FALSE(received_data.has_beta); |
EXPECT_FALSE(received_data.has_gamma); |
} |
TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { |
- InitBuffer(); |
orientation_pump()->Start(listener()); |
- orientation_pump()->DidStart(handle()); |
+ orientation_pump()->set_use_orientation_test_data(true); |
+ blink::WebDeviceOrientationData orientation_test_data; |
+ orientation_test_data.alpha = 1; |
+ orientation_test_data.has_alpha = true; |
+ orientation_test_data.beta = 2; |
+ orientation_test_data.has_beta = true; |
+ orientation_test_data.gamma = 3; |
+ orientation_test_data.has_gamma = true; |
+ orientation_test_data.absolute = false; |
+ orientation_pump()->set_orientation_test_data(orientation_test_data); |
+ |
+ orientation_pump()->StartFireEvent(); |
base::RunLoop().Run(); |
- const device::OrientationData& received_data = listener()->data(); |
+ blink::WebDeviceOrientationData received_data = listener()->data(); |
EXPECT_TRUE(listener()->did_change_device_orientation()); |
- EXPECT_TRUE(received_data.all_available_sensors_are_active); |
- EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
+ EXPECT_EQ(1, received_data.alpha); |
EXPECT_TRUE(received_data.has_alpha); |
- EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
+ EXPECT_EQ(2, received_data.beta); |
EXPECT_TRUE(received_data.has_beta); |
- EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
+ EXPECT_EQ(3, received_data.gamma); |
EXPECT_TRUE(received_data.has_gamma); |
+ EXPECT_FALSE(received_data.absolute); |
- buffer()->data.alpha = |
- 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0; |
+ orientation_test_data.alpha = 1 + content::kOrientationThreshold / 2.0; |
+ orientation_pump()->set_orientation_test_data(orientation_test_data); |
listener()->set_did_change_device_orientation(false); |
// Reset the pump's listener. |
@@ -187,17 +305,18 @@ TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { |
base::Unretained(orientation_pump()))); |
base::RunLoop().Run(); |
+ received_data = listener()->data(); |
EXPECT_FALSE(listener()->did_change_device_orientation()); |
- EXPECT_TRUE(received_data.all_available_sensors_are_active); |
- EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
+ EXPECT_EQ(1, received_data.alpha); |
EXPECT_TRUE(received_data.has_alpha); |
- EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
+ EXPECT_EQ(2, received_data.beta); |
EXPECT_TRUE(received_data.has_beta); |
- EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
+ EXPECT_EQ(3, received_data.gamma); |
EXPECT_TRUE(received_data.has_gamma); |
+ EXPECT_FALSE(received_data.absolute); |
- buffer()->data.alpha = |
- 1 + DeviceOrientationEventPump::kOrientationThreshold; |
+ orientation_test_data.alpha = 1 + content::kOrientationThreshold; |
+ orientation_pump()->set_orientation_test_data(orientation_test_data); |
listener()->set_did_change_device_orientation(false); |
// Reset the pump's listener. |
@@ -208,9 +327,15 @@ TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { |
base::Unretained(orientation_pump()))); |
base::RunLoop().Run(); |
+ received_data = listener()->data(); |
EXPECT_TRUE(listener()->did_change_device_orientation()); |
- EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, |
- static_cast<double>(received_data.alpha)); |
+ EXPECT_EQ(1 + content::kOrientationThreshold, received_data.alpha); |
+ EXPECT_TRUE(received_data.has_alpha); |
+ EXPECT_EQ(2, received_data.beta); |
+ EXPECT_TRUE(received_data.has_beta); |
+ EXPECT_EQ(3, received_data.gamma); |
+ EXPECT_TRUE(received_data.has_gamma); |
+ EXPECT_FALSE(received_data.absolute); |
} |
} // namespace content |