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/logging.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "content/common/device_sensors/device_light_hardware_buffer.h" | |
11 #include "content/public/test/test_utils.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h" | |
14 | |
15 namespace content { | |
16 | |
17 class MockDeviceLightListener : public blink::WebDeviceLightListener { | |
18 public: | |
19 MockDeviceLightListener() : did_change_device_light_(false) { | |
20 memset(&data_, 0, sizeof(data_)); | |
21 } | |
22 virtual ~MockDeviceLightListener() { } | |
23 | |
24 virtual void didChangeDeviceLight(double data) OVERRIDE { | |
25 memcpy(&data_, &data, sizeof(data)); | |
26 did_change_device_light_ = true; | |
27 } | |
28 | |
29 bool did_change_device_light() const { | |
30 return did_change_device_light_; | |
31 } | |
32 | |
33 const DeviceLightData& data() const { | |
34 return data_; | |
35 } | |
36 | |
37 private: | |
38 bool did_change_device_light_; | |
39 DeviceLightData data_; | |
40 | |
41 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener); | |
42 }; | |
43 | |
44 class DeviceLightEventPumpForTesting : public DeviceLightEventPump { | |
45 public: | |
46 DeviceLightEventPumpForTesting() { } | |
47 virtual ~DeviceLightEventPumpForTesting() { } | |
48 | |
49 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | |
50 DeviceLightEventPump::OnDidStart(renderer_handle); | |
51 } | |
52 virtual bool SendStartMessage() OVERRIDE { return true; } | |
53 virtual bool SendStopMessage() OVERRIDE { return true; } | |
54 virtual void FireEvent() OVERRIDE { | |
55 DeviceLightEventPump::FireEvent(); | |
56 Stop(); | |
57 base::MessageLoop::current()->QuitWhenIdle(); | |
58 } | |
59 | |
60 private: | |
61 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting); | |
62 }; | |
63 | |
64 class DeviceLightEventPumpTest : public testing::Test { | |
65 public: | |
66 DeviceLightEventPumpTest() { | |
67 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( | |
68 sizeof(DeviceLightHardwareBuffer))); | |
69 } | |
70 | |
71 protected: | |
72 virtual void SetUp() OVERRIDE { | |
73 const DeviceLightHardwareBuffer* null_buffer = NULL; | |
74 listener_.reset(new MockDeviceLightListener); | |
75 light_pump_.reset(new DeviceLightEventPumpForTesting); | |
76 buffer_ = static_cast<DeviceLightHardwareBuffer*>(shared_memory_.memory()); | |
77 ASSERT_NE(null_buffer, buffer_); | |
78 memset(buffer_, 0, sizeof(DeviceLightHardwareBuffer)); | |
79 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(), | |
80 &handle_)); | |
81 } | |
82 | |
83 void InitBuffer(bool valueIsUpdated) { | |
84 DeviceLightData& data = buffer_->data; | |
85 data.value = 1.0; | |
86 data.valueIsUpdated = valueIsUpdated; | |
87 } | |
88 | |
89 MockDeviceLightListener* listener() { return listener_.get(); } | |
90 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); } | |
91 base::SharedMemoryHandle handle() { return handle_; } | |
92 | |
93 private: | |
94 scoped_ptr<MockDeviceLightListener> listener_; | |
95 scoped_ptr<DeviceLightEventPumpForTesting> light_pump_; | |
96 base::SharedMemoryHandle handle_; | |
97 base::SharedMemory shared_memory_; | |
98 DeviceLightHardwareBuffer* buffer_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); | |
101 }; | |
102 | |
103 TEST_F(DeviceLightEventPumpTest, DidStartPolling) { | |
timvolodine
2014/06/03 16:08:12
could you include a test when valusIsUpdated==fals
riju_
2014/06/05 19:50:00
Done.
| |
104 base::MessageLoopForUI loop; | |
105 | |
106 InitBuffer(true); | |
107 | |
108 light_pump()->SetListener(listener()); | |
109 light_pump()->OnDidStart(handle()); | |
110 | |
111 base::MessageLoop::current()->Run(); | |
112 | |
113 const DeviceLightData& received_data = listener()->data(); | |
114 EXPECT_TRUE(listener()->did_change_device_light()); | |
115 EXPECT_EQ(1, static_cast<double>(received_data.value)); | |
116 } | |
timvolodine
2014/06/03 16:08:12
indent -2sp
riju_
2014/06/05 19:50:00
Done.
| |
117 | |
118 } // namespace content | |
OLD | NEW |