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/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "content/common/device_sensors/device_light_hardware_buffer.h" | 8 #include "content/common/device_sensors/device_light_hardware_buffer.h" |
9 #include "content/public/test/test_utils.h" | 9 #include "content/public/test/test_utils.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 | 32 |
33 private: | 33 private: |
34 bool did_change_device_light_; | 34 bool did_change_device_light_; |
35 DeviceLightData data_; | 35 DeviceLightData data_; |
36 | 36 |
37 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener); | 37 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener); |
38 }; | 38 }; |
39 | 39 |
40 class DeviceLightEventPumpForTesting : public DeviceLightEventPump { | 40 class DeviceLightEventPumpForTesting : public DeviceLightEventPump { |
41 public: | 41 public: |
42 DeviceLightEventPumpForTesting() {} | 42 DeviceLightEventPumpForTesting() |
| 43 : DeviceLightEventPump(0) {} |
43 virtual ~DeviceLightEventPumpForTesting() {} | 44 virtual ~DeviceLightEventPumpForTesting() {} |
44 | 45 |
45 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | 46 void OnDidStart(base::SharedMemoryHandle renderer_handle) { |
46 DeviceLightEventPump::OnDidStart(renderer_handle); | 47 DeviceLightEventPump::OnDidStart(renderer_handle); |
47 } | 48 } |
48 virtual bool SendStartMessage() OVERRIDE { return true; } | 49 virtual void SendStartMessage() OVERRIDE { } |
49 virtual bool SendStopMessage() OVERRIDE { return true; } | 50 virtual void SendStopMessage() OVERRIDE { } |
50 virtual void FireEvent() OVERRIDE { | 51 virtual void FireEvent() OVERRIDE { |
51 DeviceLightEventPump::FireEvent(); | 52 DeviceLightEventPump::FireEvent(); |
52 Stop(); | 53 Stop(); |
53 base::MessageLoop::current()->QuitWhenIdle(); | 54 base::MessageLoop::current()->QuitWhenIdle(); |
54 } | 55 } |
55 | 56 |
56 private: | 57 private: |
57 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting); | 58 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting); |
58 }; | 59 }; |
59 | 60 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 DeviceLightHardwareBuffer* buffer_; | 94 DeviceLightHardwareBuffer* buffer_; |
94 | 95 |
95 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); | 96 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); |
96 }; | 97 }; |
97 | 98 |
98 TEST_F(DeviceLightEventPumpTest, DidStartPolling) { | 99 TEST_F(DeviceLightEventPumpTest, DidStartPolling) { |
99 base::MessageLoopForUI loop; | 100 base::MessageLoopForUI loop; |
100 | 101 |
101 InitBuffer(); | 102 InitBuffer(); |
102 | 103 |
103 light_pump()->SetListener(listener()); | 104 light_pump()->Start(listener()); |
104 light_pump()->OnDidStart(handle()); | 105 light_pump()->OnDidStart(handle()); |
105 | 106 |
106 base::MessageLoop::current()->Run(); | 107 base::MessageLoop::current()->Run(); |
107 | 108 |
108 const DeviceLightData& received_data = listener()->data(); | 109 const DeviceLightData& received_data = listener()->data(); |
109 EXPECT_TRUE(listener()->did_change_device_light()); | 110 EXPECT_TRUE(listener()->did_change_device_light()); |
110 EXPECT_EQ(1, static_cast<double>(received_data.value)); | 111 EXPECT_EQ(1, static_cast<double>(received_data.value)); |
111 } | 112 } |
112 | 113 |
113 TEST_F(DeviceLightEventPumpTest, DidStartPollingValuesEqual) { | 114 TEST_F(DeviceLightEventPumpTest, DidStartPollingValuesEqual) { |
114 base::MessageLoopForUI loop; | 115 base::MessageLoopForUI loop; |
115 | 116 |
116 InitBuffer(); | 117 InitBuffer(); |
117 | 118 |
118 light_pump()->SetListener(listener()); | 119 light_pump()->Start(listener()); |
119 light_pump()->OnDidStart(handle()); | 120 light_pump()->OnDidStart(handle()); |
120 | 121 |
121 base::MessageLoop::current()->Run(); | 122 base::MessageLoop::current()->Run(); |
122 | 123 |
123 const DeviceLightData& received_data = listener()->data(); | 124 const DeviceLightData& received_data = listener()->data(); |
124 EXPECT_TRUE(listener()->did_change_device_light()); | 125 EXPECT_TRUE(listener()->did_change_device_light()); |
125 EXPECT_EQ(1, static_cast<double>(received_data.value)); | 126 EXPECT_EQ(1, static_cast<double>(received_data.value)); |
126 | 127 |
127 double last_seen_data = received_data.value; | 128 double last_seen_data = received_data.value; |
128 // Set next value to be same as previous value. | 129 // Set next value to be same as previous value. |
129 buffer()->data.value = 1.0; | 130 buffer()->data.value = 1.0; |
130 listener()->set_did_change_device_light(false); | 131 listener()->set_did_change_device_light(false); |
| 132 |
| 133 // Reset the pump's listener. |
| 134 light_pump()->Start(listener()); |
| 135 |
131 base::MessageLoop::current()->PostTask( | 136 base::MessageLoop::current()->PostTask( |
132 FROM_HERE, | 137 FROM_HERE, |
133 base::Bind(&DeviceLightEventPumpForTesting::FireEvent, | 138 base::Bind(&DeviceLightEventPumpForTesting::FireEvent, |
134 base::Unretained(light_pump()))); | 139 base::Unretained(light_pump()))); |
135 base::MessageLoop::current()->Run(); | 140 base::MessageLoop::current()->Run(); |
136 | 141 |
137 // No change in device light as present value is same as previous value. | 142 // No change in device light as present value is same as previous value. |
138 EXPECT_FALSE(listener()->did_change_device_light()); | 143 EXPECT_FALSE(listener()->did_change_device_light()); |
139 EXPECT_EQ(last_seen_data, static_cast<double>(received_data.value)); | 144 EXPECT_EQ(last_seen_data, static_cast<double>(received_data.value)); |
140 } | 145 } |
141 | 146 |
142 } // namespace content | 147 } // namespace content |
OLD | NEW |