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() { | |
75 DeviceLightData& data = buffer_->data; | |
76 data.value = 1.0; | |
77 } | |
78 | |
79 MockDeviceLightListener* listener() { return listener_.get(); } | |
80 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); } | |
81 base::SharedMemoryHandle handle() { return handle_; } | |
82 | |
83 private: | |
84 scoped_ptr<MockDeviceLightListener> listener_; | |
85 scoped_ptr<DeviceLightEventPumpForTesting> light_pump_; | |
86 base::SharedMemoryHandle handle_; | |
87 base::SharedMemory shared_memory_; | |
88 DeviceLightHardwareBuffer* buffer_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); | |
91 }; | |
92 | |
93 TEST_F(DeviceLightEventPumpTest, DidStartPolling) { | |
timvolodine
2014/06/18 17:38:36
+ test when values are equal and no change?
riju_
2014/06/19 13:15:54
Done.
| |
94 base::MessageLoopForUI loop; | |
95 | |
96 InitBuffer(); | |
97 | |
98 light_pump()->SetListener(listener()); | |
99 light_pump()->OnDidStart(handle()); | |
100 | |
101 base::MessageLoop::current()->Run(); | |
102 | |
103 const DeviceLightData& received_data = listener()->data(); | |
104 EXPECT_TRUE(listener()->did_change_device_light()); | |
105 EXPECT_EQ(1, static_cast<double>(received_data.value)); | |
106 } | |
107 | |
108 } // namespace content | |
OLD | NEW |