OLD | NEW |
(Empty) | |
| 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_light_event_pump.h" |
| 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "content/common/device_sensors/device_light_hardware_buffer.h" |
| 9 #include "content/public/test/test_utils.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 class MockDeviceLightListener : public blink::WebDeviceLightListener { |
| 16 public: |
| 17 MockDeviceLightListener() : did_change_device_light_(false) {} |
| 18 virtual ~MockDeviceLightListener() {} |
| 19 |
| 20 virtual void didChangeDeviceLight(double value) OVERRIDE { |
| 21 data_.value = value; |
| 22 did_change_device_light_ = true; |
| 23 } |
| 24 |
| 25 bool did_change_device_light() const { return did_change_device_light_; } |
| 26 |
| 27 void set_did_change_device_light(bool value) { |
| 28 did_change_device_light_ = value; |
| 29 } |
| 30 |
| 31 const DeviceLightData& data() const { return data_; } |
| 32 |
| 33 private: |
| 34 bool did_change_device_light_; |
| 35 DeviceLightData data_; |
| 36 |
| 37 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener); |
| 38 }; |
| 39 |
| 40 class DeviceLightEventPumpForTesting : public DeviceLightEventPump { |
| 41 public: |
| 42 DeviceLightEventPumpForTesting() {} |
| 43 virtual ~DeviceLightEventPumpForTesting() {} |
| 44 |
| 45 void OnDidStart(base::SharedMemoryHandle renderer_handle) { |
| 46 DeviceLightEventPump::OnDidStart(renderer_handle); |
| 47 } |
| 48 virtual bool SendStartMessage() OVERRIDE { return true; } |
| 49 virtual bool SendStopMessage() OVERRIDE { return true; } |
| 50 virtual void FireEvent() OVERRIDE { |
| 51 DeviceLightEventPump::FireEvent(); |
| 52 Stop(); |
| 53 base::MessageLoop::current()->QuitWhenIdle(); |
| 54 } |
| 55 |
| 56 private: |
| 57 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting); |
| 58 }; |
| 59 |
| 60 class DeviceLightEventPumpTest : public testing::Test { |
| 61 public: |
| 62 DeviceLightEventPumpTest() { |
| 63 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( |
| 64 sizeof(DeviceLightHardwareBuffer))); |
| 65 } |
| 66 |
| 67 protected: |
| 68 virtual void SetUp() OVERRIDE { |
| 69 const DeviceLightHardwareBuffer* null_buffer = NULL; |
| 70 listener_.reset(new MockDeviceLightListener); |
| 71 light_pump_.reset(new DeviceLightEventPumpForTesting); |
| 72 buffer_ = static_cast<DeviceLightHardwareBuffer*>(shared_memory_.memory()); |
| 73 ASSERT_NE(null_buffer, buffer_); |
| 74 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(), |
| 75 &handle_)); |
| 76 } |
| 77 |
| 78 void InitBuffer() { |
| 79 DeviceLightData& data = buffer_->data; |
| 80 data.value = 1.0; |
| 81 } |
| 82 |
| 83 MockDeviceLightListener* listener() { return listener_.get(); } |
| 84 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); } |
| 85 base::SharedMemoryHandle handle() { return handle_; } |
| 86 DeviceLightHardwareBuffer* buffer() { return buffer_; } |
| 87 |
| 88 private: |
| 89 scoped_ptr<MockDeviceLightListener> listener_; |
| 90 scoped_ptr<DeviceLightEventPumpForTesting> light_pump_; |
| 91 base::SharedMemoryHandle handle_; |
| 92 base::SharedMemory shared_memory_; |
| 93 DeviceLightHardwareBuffer* buffer_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); |
| 96 }; |
| 97 |
| 98 TEST_F(DeviceLightEventPumpTest, DidStartPolling) { |
| 99 base::MessageLoopForUI loop; |
| 100 |
| 101 InitBuffer(); |
| 102 |
| 103 light_pump()->SetListener(listener()); |
| 104 light_pump()->OnDidStart(handle()); |
| 105 |
| 106 base::MessageLoop::current()->Run(); |
| 107 |
| 108 const DeviceLightData& received_data = listener()->data(); |
| 109 EXPECT_TRUE(listener()->did_change_device_light()); |
| 110 EXPECT_EQ(1, static_cast<double>(received_data.value)); |
| 111 } |
| 112 |
| 113 TEST_F(DeviceLightEventPumpTest, DidStartPollingValuesEqual) { |
| 114 base::MessageLoopForUI loop; |
| 115 |
| 116 InitBuffer(); |
| 117 |
| 118 light_pump()->SetListener(listener()); |
| 119 light_pump()->OnDidStart(handle()); |
| 120 |
| 121 base::MessageLoop::current()->Run(); |
| 122 |
| 123 const DeviceLightData& received_data = listener()->data(); |
| 124 EXPECT_TRUE(listener()->did_change_device_light()); |
| 125 EXPECT_EQ(1, static_cast<double>(received_data.value)); |
| 126 |
| 127 double last_seen_data = received_data.value; |
| 128 // Set next value to be same as previous value. |
| 129 buffer()->data.value = 1.0; |
| 130 listener()->set_did_change_device_light(false); |
| 131 base::MessageLoop::current()->PostTask( |
| 132 FROM_HERE, |
| 133 base::Bind(&DeviceLightEventPumpForTesting::FireEvent, |
| 134 base::Unretained(light_pump()))); |
| 135 base::MessageLoop::current()->Run(); |
| 136 |
| 137 // No change in device light as present value is same as previous value. |
| 138 EXPECT_FALSE(listener()->did_change_device_light()); |
| 139 EXPECT_EQ(last_seen_data, static_cast<double>(received_data.value)); |
| 140 } |
| 141 |
| 142 } // namespace content |
OLD | NEW |