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 const DeviceLightData& data() const { return data_; } |
| 28 |
| 29 private: |
| 30 bool did_change_device_light_; |
| 31 DeviceLightData data_; |
| 32 |
| 33 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener); |
| 34 }; |
| 35 |
| 36 class DeviceLightEventPumpForTesting : public DeviceLightEventPump { |
| 37 public: |
| 38 DeviceLightEventPumpForTesting() {} |
| 39 virtual ~DeviceLightEventPumpForTesting() {} |
| 40 |
| 41 void OnDidStart(base::SharedMemoryHandle renderer_handle) { |
| 42 DeviceLightEventPump::OnDidStart(renderer_handle); |
| 43 } |
| 44 virtual bool SendStartMessage() OVERRIDE { return true; } |
| 45 virtual bool SendStopMessage() OVERRIDE { return true; } |
| 46 virtual void FireEvent() OVERRIDE { |
| 47 DeviceLightEventPump::FireEvent(); |
| 48 Stop(); |
| 49 base::MessageLoop::current()->QuitWhenIdle(); |
| 50 } |
| 51 |
| 52 private: |
| 53 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting); |
| 54 }; |
| 55 |
| 56 class DeviceLightEventPumpTest : public testing::Test { |
| 57 public: |
| 58 DeviceLightEventPumpTest() { |
| 59 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( |
| 60 sizeof(DeviceLightHardwareBuffer))); |
| 61 } |
| 62 |
| 63 protected: |
| 64 virtual void SetUp() OVERRIDE { |
| 65 const DeviceLightHardwareBuffer* null_buffer = NULL; |
| 66 listener_.reset(new MockDeviceLightListener); |
| 67 light_pump_.reset(new DeviceLightEventPumpForTesting); |
| 68 buffer_ = static_cast<DeviceLightHardwareBuffer*>(shared_memory_.memory()); |
| 69 ASSERT_NE(null_buffer, buffer_); |
| 70 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(), |
| 71 &handle_)); |
| 72 } |
| 73 |
| 74 void InitBuffer(bool valueIsUpdated) { |
| 75 DeviceLightData& data = buffer_->data; |
| 76 data.value = 1.0; |
| 77 data.valueIsUpdated = valueIsUpdated; |
| 78 } |
| 79 |
| 80 MockDeviceLightListener* listener() { return listener_.get(); } |
| 81 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); } |
| 82 base::SharedMemoryHandle handle() { return handle_; } |
| 83 |
| 84 private: |
| 85 scoped_ptr<MockDeviceLightListener> listener_; |
| 86 scoped_ptr<DeviceLightEventPumpForTesting> light_pump_; |
| 87 base::SharedMemoryHandle handle_; |
| 88 base::SharedMemory shared_memory_; |
| 89 DeviceLightHardwareBuffer* buffer_; |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); |
| 92 }; |
| 93 |
| 94 TEST_F(DeviceLightEventPumpTest, DidStartPolling) { |
| 95 base::MessageLoopForUI loop; |
| 96 |
| 97 InitBuffer(true); |
| 98 |
| 99 light_pump()->SetListener(listener()); |
| 100 light_pump()->OnDidStart(handle()); |
| 101 |
| 102 base::MessageLoop::current()->Run(); |
| 103 |
| 104 const DeviceLightData& received_data = listener()->data(); |
| 105 EXPECT_TRUE(listener()->did_change_device_light()); |
| 106 EXPECT_EQ(1, static_cast<double>(received_data.value)); |
| 107 } |
| 108 |
| 109 TEST_F(DeviceLightEventPumpTest, DidStartPollingValueIsUpdatedFalse) { |
| 110 base::MessageLoopForUI loop; |
| 111 |
| 112 InitBuffer(false); |
| 113 |
| 114 light_pump()->SetListener(listener()); |
| 115 light_pump()->OnDidStart(handle()); |
| 116 |
| 117 base::MessageLoop::current()->Run(); |
| 118 |
| 119 // No change in device light because valueIsUpdated is false. |
| 120 EXPECT_FALSE(listener()->did_change_device_light()); |
| 121 } |
| 122 |
| 123 } // namespace content |
OLD | NEW |