Home | History | Annotate | Download | only in device_sensors
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "device_motion_event_pump.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "content/common/device_sensors/device_motion_hardware_buffer.h"
     11 #include "content/public/test/test_utils.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h"
     14 
     15 namespace content {
     16 
     17 class MockDeviceMotionListener : public blink::WebDeviceMotionListener {
     18  public:
     19   MockDeviceMotionListener() : did_change_device_motion_(false) {
     20     memset(&data_, 0, sizeof(data_));
     21   }
     22   virtual ~MockDeviceMotionListener() { }
     23 
     24   virtual void didChangeDeviceMotion(
     25       const blink::WebDeviceMotionData& data) OVERRIDE {
     26     memcpy(&data_, &data, sizeof(data));
     27     did_change_device_motion_ = true;
     28   }
     29 
     30   bool did_change_device_motion() const {
     31     return did_change_device_motion_;
     32   }
     33   const blink::WebDeviceMotionData& data() const {
     34     return data_;
     35   }
     36 
     37  private:
     38   bool did_change_device_motion_;
     39   blink::WebDeviceMotionData data_;
     40 
     41   DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener);
     42 };
     43 
     44 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump {
     45  public:
     46   DeviceMotionEventPumpForTesting() { }
     47   virtual ~DeviceMotionEventPumpForTesting() { }
     48 
     49   void OnDidStart(base::SharedMemoryHandle renderer_handle) {
     50     DeviceMotionEventPump::OnDidStart(renderer_handle);
     51   }
     52   virtual bool SendStartMessage() OVERRIDE { return true; }
     53   virtual bool SendStopMessage() OVERRIDE { return true; }
     54   virtual void FireEvent() OVERRIDE {
     55     DeviceMotionEventPump::FireEvent();
     56     Stop();
     57     base::MessageLoop::current()->QuitWhenIdle();
     58   }
     59 
     60  private:
     61   DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting);
     62 };
     63 
     64 class DeviceMotionEventPumpTest : public testing::Test {
     65  public:
     66   DeviceMotionEventPumpTest() {
     67     EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous(
     68         sizeof(DeviceMotionHardwareBuffer)));
     69   }
     70 
     71  protected:
     72   virtual void SetUp() OVERRIDE {
     73     const DeviceMotionHardwareBuffer* null_buffer = NULL;
     74     listener_.reset(new MockDeviceMotionListener);
     75     motion_pump_.reset(new DeviceMotionEventPumpForTesting);
     76     buffer_ = static_cast<DeviceMotionHardwareBuffer*>(shared_memory_.memory());
     77     ASSERT_NE(null_buffer, buffer_);
     78     memset(buffer_, 0, sizeof(DeviceMotionHardwareBuffer));
     79     ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(),
     80         &handle_));
     81   }
     82 
     83   void InitBuffer(bool allAvailableSensorsActive) {
     84     blink::WebDeviceMotionData& data = buffer_->data;
     85     data.accelerationX = 1;
     86     data.hasAccelerationX = true;
     87     data.accelerationY = 2;
     88     data.hasAccelerationY = true;
     89     data.accelerationZ = 3;
     90     data.hasAccelerationZ = true;
     91     data.allAvailableSensorsAreActive = allAvailableSensorsActive;
     92   }
     93 
     94   MockDeviceMotionListener* listener() { return listener_.get(); }
     95   DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); }
     96   base::SharedMemoryHandle handle() { return handle_; }
     97 
     98  private:
     99   scoped_ptr<MockDeviceMotionListener> listener_;
    100   scoped_ptr<DeviceMotionEventPumpForTesting> motion_pump_;
    101   base::SharedMemoryHandle handle_;
    102   base::SharedMemory shared_memory_;
    103   DeviceMotionHardwareBuffer* buffer_;
    104 
    105   DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest);
    106 };
    107 
    108 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) {
    109   base::MessageLoopForUI loop;
    110 
    111   InitBuffer(true);
    112 
    113   motion_pump()->SetListener(listener());
    114   motion_pump()->OnDidStart(handle());
    115 
    116   base::MessageLoop::current()->Run();
    117 
    118   const blink::WebDeviceMotionData& received_data = listener()->data();
    119   EXPECT_TRUE(listener()->did_change_device_motion());
    120   EXPECT_TRUE(received_data.hasAccelerationX);
    121   EXPECT_EQ(1, static_cast<double>(received_data.accelerationX));
    122   EXPECT_TRUE(received_data.hasAccelerationX);
    123   EXPECT_EQ(2, static_cast<double>(received_data.accelerationY));
    124   EXPECT_TRUE(received_data.hasAccelerationY);
    125   EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ));
    126   EXPECT_TRUE(received_data.hasAccelerationZ);
    127   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX);
    128   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY);
    129   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ);
    130   EXPECT_FALSE(received_data.hasRotationRateAlpha);
    131   EXPECT_FALSE(received_data.hasRotationRateBeta);
    132   EXPECT_FALSE(received_data.hasRotationRateGamma);
    133 }
    134 
    135 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) {
    136   base::MessageLoopForUI loop;
    137 
    138   InitBuffer(false);
    139 
    140   motion_pump()->SetListener(listener());
    141   motion_pump()->OnDidStart(handle());
    142 
    143   base::MessageLoop::current()->Run();
    144 
    145   const blink::WebDeviceMotionData& received_data = listener()->data();
    146   // No change in device motion because allAvailableSensorsAreActive is false.
    147   EXPECT_FALSE(listener()->did_change_device_motion());
    148   EXPECT_FALSE(received_data.hasAccelerationX);
    149   EXPECT_FALSE(received_data.hasAccelerationX);
    150   EXPECT_FALSE(received_data.hasAccelerationY);
    151   EXPECT_FALSE(received_data.hasAccelerationZ);
    152   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX);
    153   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY);
    154   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ);
    155   EXPECT_FALSE(received_data.hasRotationRateAlpha);
    156   EXPECT_FALSE(received_data.hasRotationRateBeta);
    157   EXPECT_FALSE(received_data.hasRotationRateGamma);
    158 }
    159 
    160 }  // namespace content
    161