| 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/location.h" | |
| 8 #include "base/macros.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "base/single_thread_task_runner.h" | |
| 11 #include "base/threading/thread_task_runner_handle.h" | |
| 12 #include "content/public/test/test_utils.h" | |
| 13 #include "device/sensors/public/cpp/device_light_hardware_buffer.h" | |
| 14 #include "mojo/public/cpp/system/buffer.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h" | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 class MockDeviceLightListener : public blink::WebDeviceLightListener { | |
| 21 public: | |
| 22 MockDeviceLightListener() : did_change_device_light_(false) {} | |
| 23 ~MockDeviceLightListener() override {} | |
| 24 | |
| 25 void DidChangeDeviceLight(double value) override { | |
| 26 data_.value = value; | |
| 27 did_change_device_light_ = true; | |
| 28 } | |
| 29 | |
| 30 bool did_change_device_light() const { return did_change_device_light_; } | |
| 31 | |
| 32 void set_did_change_device_light(bool value) { | |
| 33 did_change_device_light_ = value; | |
| 34 } | |
| 35 | |
| 36 const device::DeviceLightData& data() const { return data_; } | |
| 37 | |
| 38 private: | |
| 39 bool did_change_device_light_; | |
| 40 device::DeviceLightData data_; | |
| 41 | |
| 42 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener); | |
| 43 }; | |
| 44 | |
| 45 class DeviceLightEventPumpForTesting : public DeviceLightEventPump { | |
| 46 public: | |
| 47 DeviceLightEventPumpForTesting() | |
| 48 : DeviceLightEventPump(0) {} | |
| 49 ~DeviceLightEventPumpForTesting() override {} | |
| 50 | |
| 51 void DidStart(mojo::ScopedSharedBufferHandle renderer_handle) { | |
| 52 DeviceLightEventPump::DidStart(std::move(renderer_handle)); | |
| 53 } | |
| 54 void SendStartMessage() override {} | |
| 55 void SendStopMessage() override {} | |
| 56 void FireEvent() override { | |
| 57 DeviceLightEventPump::FireEvent(); | |
| 58 Stop(); | |
| 59 base::MessageLoop::current()->QuitWhenIdle(); | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting); | |
| 64 }; | |
| 65 | |
| 66 class DeviceLightEventPumpTest : public testing::Test { | |
| 67 public: | |
| 68 DeviceLightEventPumpTest() = default; | |
| 69 | |
| 70 protected: | |
| 71 void SetUp() override { | |
| 72 listener_.reset(new MockDeviceLightListener); | |
| 73 light_pump_.reset(new DeviceLightEventPumpForTesting); | |
| 74 shared_memory_ = mojo::SharedBufferHandle::Create( | |
| 75 sizeof(device::DeviceLightHardwareBuffer)); | |
| 76 mapping_ = shared_memory_->Map(sizeof(device::DeviceLightHardwareBuffer)); | |
| 77 ASSERT_TRUE(mapping_); | |
| 78 } | |
| 79 | |
| 80 void InitBuffer() { | |
| 81 device::DeviceLightData& data = buffer()->data; | |
| 82 data.value = 1.0; | |
| 83 } | |
| 84 | |
| 85 MockDeviceLightListener* listener() { return listener_.get(); } | |
| 86 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); } | |
| 87 mojo::ScopedSharedBufferHandle handle() { | |
| 88 return shared_memory_->Clone( | |
| 89 mojo::SharedBufferHandle::AccessMode::READ_ONLY); | |
| 90 } | |
| 91 device::DeviceLightHardwareBuffer* buffer() { | |
| 92 return reinterpret_cast<device::DeviceLightHardwareBuffer*>(mapping_.get()); | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 base::MessageLoop loop_; | |
| 97 std::unique_ptr<MockDeviceLightListener> listener_; | |
| 98 std::unique_ptr<DeviceLightEventPumpForTesting> light_pump_; | |
| 99 mojo::ScopedSharedBufferHandle shared_memory_; | |
| 100 mojo::ScopedSharedBufferMapping mapping_; | |
| 101 | |
| 102 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); | |
| 103 }; | |
| 104 | |
| 105 TEST_F(DeviceLightEventPumpTest, DidStartPolling) { | |
| 106 InitBuffer(); | |
| 107 | |
| 108 light_pump()->Start(listener()); | |
| 109 light_pump()->DidStart(handle()); | |
| 110 | |
| 111 base::RunLoop().Run(); | |
| 112 | |
| 113 const device::DeviceLightData& received_data = listener()->data(); | |
| 114 EXPECT_TRUE(listener()->did_change_device_light()); | |
| 115 EXPECT_EQ(1, static_cast<double>(received_data.value)); | |
| 116 } | |
| 117 | |
| 118 TEST_F(DeviceLightEventPumpTest, FireAllNullEvent) { | |
| 119 light_pump()->Start(listener()); | |
| 120 light_pump()->DidStart(handle()); | |
| 121 | |
| 122 base::RunLoop().Run(); | |
| 123 | |
| 124 const device::DeviceLightData& received_data = listener()->data(); | |
| 125 EXPECT_TRUE(listener()->did_change_device_light()); | |
| 126 EXPECT_FALSE(received_data.value); | |
| 127 } | |
| 128 | |
| 129 TEST_F(DeviceLightEventPumpTest, DidStartPollingValuesEqual) { | |
| 130 InitBuffer(); | |
| 131 | |
| 132 light_pump()->Start(listener()); | |
| 133 light_pump()->DidStart(handle()); | |
| 134 | |
| 135 base::RunLoop().Run(); | |
| 136 | |
| 137 const device::DeviceLightData& received_data = listener()->data(); | |
| 138 EXPECT_TRUE(listener()->did_change_device_light()); | |
| 139 EXPECT_EQ(1, static_cast<double>(received_data.value)); | |
| 140 | |
| 141 double last_seen_data = received_data.value; | |
| 142 // Set next value to be same as previous value. | |
| 143 buffer()->data.value = 1.0; | |
| 144 listener()->set_did_change_device_light(false); | |
| 145 | |
| 146 // Reset the pump's listener. | |
| 147 light_pump()->Start(listener()); | |
| 148 | |
| 149 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 150 FROM_HERE, base::Bind(&DeviceLightEventPumpForTesting::FireEvent, | |
| 151 base::Unretained(light_pump()))); | |
| 152 base::RunLoop().Run(); | |
| 153 | |
| 154 // No change in device light as present value is same as previous value. | |
| 155 EXPECT_FALSE(listener()->did_change_device_light()); | |
| 156 EXPECT_EQ(last_seen_data, static_cast<double>(received_data.value)); | |
| 157 } | |
| 158 | |
| 159 } // namespace content | |
| OLD | NEW |