| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device_light_event_pump.h" | 5 #include "device_light_event_pump.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 data_.value = value; | 26 data_.value = value; |
| 27 did_change_device_light_ = true; | 27 did_change_device_light_ = true; |
| 28 } | 28 } |
| 29 | 29 |
| 30 bool did_change_device_light() const { return did_change_device_light_; } | 30 bool did_change_device_light() const { return did_change_device_light_; } |
| 31 | 31 |
| 32 void set_did_change_device_light(bool value) { | 32 void set_did_change_device_light(bool value) { |
| 33 did_change_device_light_ = value; | 33 did_change_device_light_ = value; |
| 34 } | 34 } |
| 35 | 35 |
| 36 const DeviceLightData& data() const { return data_; } | 36 const device::DeviceLightData& data() const { return data_; } |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 bool did_change_device_light_; | 39 bool did_change_device_light_; |
| 40 DeviceLightData data_; | 40 device::DeviceLightData data_; |
| 41 | 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener); | 42 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener); |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 class DeviceLightEventPumpForTesting : public DeviceLightEventPump { | 45 class DeviceLightEventPumpForTesting : public DeviceLightEventPump { |
| 46 public: | 46 public: |
| 47 DeviceLightEventPumpForTesting() | 47 DeviceLightEventPumpForTesting() |
| 48 : DeviceLightEventPump(0) {} | 48 : DeviceLightEventPump(0) {} |
| 49 ~DeviceLightEventPumpForTesting() override {} | 49 ~DeviceLightEventPumpForTesting() override {} |
| 50 | 50 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 class DeviceLightEventPumpTest : public testing::Test { | 66 class DeviceLightEventPumpTest : public testing::Test { |
| 67 public: | 67 public: |
| 68 DeviceLightEventPumpTest() = default; | 68 DeviceLightEventPumpTest() = default; |
| 69 | 69 |
| 70 protected: | 70 protected: |
| 71 void SetUp() override { | 71 void SetUp() override { |
| 72 listener_.reset(new MockDeviceLightListener); | 72 listener_.reset(new MockDeviceLightListener); |
| 73 light_pump_.reset(new DeviceLightEventPumpForTesting); | 73 light_pump_.reset(new DeviceLightEventPumpForTesting); |
| 74 shared_memory_ = | 74 shared_memory_ = mojo::SharedBufferHandle::Create( |
| 75 mojo::SharedBufferHandle::Create(sizeof(DeviceLightHardwareBuffer)); | 75 sizeof(device::DeviceLightHardwareBuffer)); |
| 76 mapping_ = shared_memory_->Map(sizeof(DeviceLightHardwareBuffer)); | 76 mapping_ = shared_memory_->Map(sizeof(device::DeviceLightHardwareBuffer)); |
| 77 ASSERT_TRUE(mapping_); | 77 ASSERT_TRUE(mapping_); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void InitBuffer() { | 80 void InitBuffer() { |
| 81 DeviceLightData& data = buffer()->data; | 81 device::DeviceLightData& data = buffer()->data; |
| 82 data.value = 1.0; | 82 data.value = 1.0; |
| 83 } | 83 } |
| 84 | 84 |
| 85 MockDeviceLightListener* listener() { return listener_.get(); } | 85 MockDeviceLightListener* listener() { return listener_.get(); } |
| 86 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); } | 86 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); } |
| 87 mojo::ScopedSharedBufferHandle handle() { | 87 mojo::ScopedSharedBufferHandle handle() { |
| 88 return shared_memory_->Clone( | 88 return shared_memory_->Clone( |
| 89 mojo::SharedBufferHandle::AccessMode::READ_ONLY); | 89 mojo::SharedBufferHandle::AccessMode::READ_ONLY); |
| 90 } | 90 } |
| 91 DeviceLightHardwareBuffer* buffer() { | 91 device::DeviceLightHardwareBuffer* buffer() { |
| 92 return reinterpret_cast<DeviceLightHardwareBuffer*>(mapping_.get()); | 92 return reinterpret_cast<device::DeviceLightHardwareBuffer*>(mapping_.get()); |
| 93 } | 93 } |
| 94 | 94 |
| 95 private: | 95 private: |
| 96 base::MessageLoop loop_; | 96 base::MessageLoop loop_; |
| 97 std::unique_ptr<MockDeviceLightListener> listener_; | 97 std::unique_ptr<MockDeviceLightListener> listener_; |
| 98 std::unique_ptr<DeviceLightEventPumpForTesting> light_pump_; | 98 std::unique_ptr<DeviceLightEventPumpForTesting> light_pump_; |
| 99 mojo::ScopedSharedBufferHandle shared_memory_; | 99 mojo::ScopedSharedBufferHandle shared_memory_; |
| 100 mojo::ScopedSharedBufferMapping mapping_; | 100 mojo::ScopedSharedBufferMapping mapping_; |
| 101 | 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); | 102 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); |
| 103 }; | 103 }; |
| 104 | 104 |
| 105 TEST_F(DeviceLightEventPumpTest, DidStartPolling) { | 105 TEST_F(DeviceLightEventPumpTest, DidStartPolling) { |
| 106 InitBuffer(); | 106 InitBuffer(); |
| 107 | 107 |
| 108 light_pump()->Start(listener()); | 108 light_pump()->Start(listener()); |
| 109 light_pump()->DidStart(handle()); | 109 light_pump()->DidStart(handle()); |
| 110 | 110 |
| 111 base::RunLoop().Run(); | 111 base::RunLoop().Run(); |
| 112 | 112 |
| 113 const DeviceLightData& received_data = listener()->data(); | 113 const device::DeviceLightData& received_data = listener()->data(); |
| 114 EXPECT_TRUE(listener()->did_change_device_light()); | 114 EXPECT_TRUE(listener()->did_change_device_light()); |
| 115 EXPECT_EQ(1, static_cast<double>(received_data.value)); | 115 EXPECT_EQ(1, static_cast<double>(received_data.value)); |
| 116 } | 116 } |
| 117 | 117 |
| 118 TEST_F(DeviceLightEventPumpTest, FireAllNullEvent) { | 118 TEST_F(DeviceLightEventPumpTest, FireAllNullEvent) { |
| 119 light_pump()->Start(listener()); | 119 light_pump()->Start(listener()); |
| 120 light_pump()->DidStart(handle()); | 120 light_pump()->DidStart(handle()); |
| 121 | 121 |
| 122 base::RunLoop().Run(); | 122 base::RunLoop().Run(); |
| 123 | 123 |
| 124 const DeviceLightData& received_data = listener()->data(); | 124 const device::DeviceLightData& received_data = listener()->data(); |
| 125 EXPECT_TRUE(listener()->did_change_device_light()); | 125 EXPECT_TRUE(listener()->did_change_device_light()); |
| 126 EXPECT_FALSE(received_data.value); | 126 EXPECT_FALSE(received_data.value); |
| 127 } | 127 } |
| 128 | 128 |
| 129 TEST_F(DeviceLightEventPumpTest, DidStartPollingValuesEqual) { | 129 TEST_F(DeviceLightEventPumpTest, DidStartPollingValuesEqual) { |
| 130 InitBuffer(); | 130 InitBuffer(); |
| 131 | 131 |
| 132 light_pump()->Start(listener()); | 132 light_pump()->Start(listener()); |
| 133 light_pump()->DidStart(handle()); | 133 light_pump()->DidStart(handle()); |
| 134 | 134 |
| 135 base::RunLoop().Run(); | 135 base::RunLoop().Run(); |
| 136 | 136 |
| 137 const DeviceLightData& received_data = listener()->data(); | 137 const device::DeviceLightData& received_data = listener()->data(); |
| 138 EXPECT_TRUE(listener()->did_change_device_light()); | 138 EXPECT_TRUE(listener()->did_change_device_light()); |
| 139 EXPECT_EQ(1, static_cast<double>(received_data.value)); | 139 EXPECT_EQ(1, static_cast<double>(received_data.value)); |
| 140 | 140 |
| 141 double last_seen_data = received_data.value; | 141 double last_seen_data = received_data.value; |
| 142 // Set next value to be same as previous value. | 142 // Set next value to be same as previous value. |
| 143 buffer()->data.value = 1.0; | 143 buffer()->data.value = 1.0; |
| 144 listener()->set_did_change_device_light(false); | 144 listener()->set_did_change_device_light(false); |
| 145 | 145 |
| 146 // Reset the pump's listener. | 146 // Reset the pump's listener. |
| 147 light_pump()->Start(listener()); | 147 light_pump()->Start(listener()); |
| 148 | 148 |
| 149 base::ThreadTaskRunnerHandle::Get()->PostTask( | 149 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 150 FROM_HERE, base::Bind(&DeviceLightEventPumpForTesting::FireEvent, | 150 FROM_HERE, base::Bind(&DeviceLightEventPumpForTesting::FireEvent, |
| 151 base::Unretained(light_pump()))); | 151 base::Unretained(light_pump()))); |
| 152 base::RunLoop().Run(); | 152 base::RunLoop().Run(); |
| 153 | 153 |
| 154 // No change in device light as present value is same as previous value. | 154 // No change in device light as present value is same as previous value. |
| 155 EXPECT_FALSE(listener()->did_change_device_light()); | 155 EXPECT_FALSE(listener()->did_change_device_light()); |
| 156 EXPECT_EQ(last_seen_data, static_cast<double>(received_data.value)); | 156 EXPECT_EQ(last_seen_data, static_cast<double>(received_data.value)); |
| 157 } | 157 } |
| 158 | 158 |
| 159 } // namespace content | 159 } // namespace content |
| OLD | NEW |