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