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 memset(&data_, 0, sizeof(data_)); | |
jochen (gone - plz use gerrit)
2014/06/12 14:33:51
why is this required?
riju_
2014/06/13 17:06:26
Done.
| |
19 } | |
20 virtual ~MockDeviceLightListener() { } | |
jochen (gone - plz use gerrit)
2014/06/12 14:33:51
iirc chromium style has no space between { }. did
riju_
2014/06/13 17:06:26
I did not know about clang format. Now I have done
| |
21 | |
22 virtual void didChangeDeviceLight(double value) OVERRIDE { | |
23 data_.value = value; | |
24 did_change_device_light_ = true; | |
25 } | |
26 | |
27 bool did_change_device_light() const { | |
28 return did_change_device_light_; | |
29 } | |
30 | |
31 const DeviceLightData& data() const { | |
32 return data_; | |
33 } | |
34 | |
35 private: | |
36 bool did_change_device_light_; | |
37 DeviceLightData data_; | |
38 | |
39 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener); | |
40 }; | |
41 | |
42 class DeviceLightEventPumpForTesting : public DeviceLightEventPump { | |
43 public: | |
44 DeviceLightEventPumpForTesting() { } | |
45 virtual ~DeviceLightEventPumpForTesting() { } | |
46 | |
47 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | |
48 DeviceLightEventPump::OnDidStart(renderer_handle); | |
49 } | |
50 virtual bool SendStartMessage() OVERRIDE { return true; } | |
51 virtual bool SendStopMessage() OVERRIDE { return true; } | |
52 virtual void FireEvent() OVERRIDE { | |
53 DeviceLightEventPump::FireEvent(); | |
54 Stop(); | |
55 base::MessageLoop::current()->QuitWhenIdle(); | |
56 } | |
57 | |
58 private: | |
59 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting); | |
60 }; | |
61 | |
62 class DeviceLightEventPumpTest : public testing::Test { | |
63 public: | |
64 DeviceLightEventPumpTest() { | |
65 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( | |
66 sizeof(DeviceLightHardwareBuffer))); | |
67 } | |
68 | |
69 protected: | |
70 virtual void SetUp() OVERRIDE { | |
71 const DeviceLightHardwareBuffer* null_buffer = NULL; | |
72 listener_.reset(new MockDeviceLightListener); | |
73 light_pump_.reset(new DeviceLightEventPumpForTesting); | |
74 buffer_ = static_cast<DeviceLightHardwareBuffer*>(shared_memory_.memory()); | |
75 ASSERT_NE(null_buffer, buffer_); | |
76 memset(buffer_, 0, sizeof(DeviceLightHardwareBuffer)); | |
jochen (gone - plz use gerrit)
2014/06/12 14:33:51
no memset plz
riju_
2014/06/13 17:06:26
Done.
| |
77 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(), | |
78 &handle_)); | |
79 } | |
80 | |
81 void InitBuffer(bool valueIsUpdated) { | |
82 DeviceLightData& data = buffer_->data; | |
83 data.value = 1.0; | |
84 data.valueIsUpdated = valueIsUpdated; | |
85 } | |
86 | |
87 MockDeviceLightListener* listener() { return listener_.get(); } | |
88 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); } | |
89 base::SharedMemoryHandle handle() { return handle_; } | |
90 | |
91 private: | |
92 scoped_ptr<MockDeviceLightListener> listener_; | |
93 scoped_ptr<DeviceLightEventPumpForTesting> light_pump_; | |
94 base::SharedMemoryHandle handle_; | |
95 base::SharedMemory shared_memory_; | |
96 DeviceLightHardwareBuffer* buffer_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); | |
99 }; | |
100 | |
101 TEST_F(DeviceLightEventPumpTest, DidStartPolling) { | |
102 base::MessageLoopForUI loop; | |
103 | |
104 InitBuffer(true); | |
105 | |
106 light_pump()->SetListener(listener()); | |
107 light_pump()->OnDidStart(handle()); | |
108 | |
109 base::MessageLoop::current()->Run(); | |
110 | |
111 const DeviceLightData& received_data = listener()->data(); | |
112 EXPECT_TRUE(listener()->did_change_device_light()); | |
113 EXPECT_EQ(1, static_cast<double>(received_data.value)); | |
114 } | |
115 | |
116 TEST_F(DeviceLightEventPumpTest, DidStartPollingValueIsUpdatedFalse) { | |
117 base::MessageLoopForUI loop; | |
118 | |
119 InitBuffer(false); | |
120 | |
121 light_pump()->SetListener(listener()); | |
122 light_pump()->OnDidStart(handle()); | |
123 | |
124 base::MessageLoop::current()->Run(); | |
125 | |
126 // No change in device light because valueIsUpdated is false. | |
127 EXPECT_FALSE(listener()->did_change_device_light()); | |
128 } | |
129 | |
130 } // namespace content | |
OLD | NEW |