third_party/re2: Remove remove-static-initializers.patch.
[chromium-blink-merge.git] / content / renderer / device_sensors / device_light_event_pump_unittest.cc
blob4f19f0db1583ddfbe32763a8ea9c6fae7d7c314d
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.
5 #include "device_light_event_pump.h"
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"
13 namespace content {
15 class MockDeviceLightListener : public blink::WebDeviceLightListener {
16 public:
17 MockDeviceLightListener() : did_change_device_light_(false) {}
18 virtual ~MockDeviceLightListener() {}
20 virtual void didChangeDeviceLight(double value) override {
21 data_.value = value;
22 did_change_device_light_ = true;
25 bool did_change_device_light() const { return did_change_device_light_; }
27 void set_did_change_device_light(bool value) {
28 did_change_device_light_ = value;
31 const DeviceLightData& data() const { return data_; }
33 private:
34 bool did_change_device_light_;
35 DeviceLightData data_;
37 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener);
40 class DeviceLightEventPumpForTesting : public DeviceLightEventPump {
41 public:
42 DeviceLightEventPumpForTesting()
43 : DeviceLightEventPump(0) {}
44 ~DeviceLightEventPumpForTesting() override {}
46 void OnDidStart(base::SharedMemoryHandle renderer_handle) {
47 DeviceLightEventPump::OnDidStart(renderer_handle);
49 void SendStartMessage() override {}
50 void SendStopMessage() override {}
51 void FireEvent() override {
52 DeviceLightEventPump::FireEvent();
53 Stop();
54 base::MessageLoop::current()->QuitWhenIdle();
57 private:
58 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting);
61 class DeviceLightEventPumpTest : public testing::Test {
62 public:
63 DeviceLightEventPumpTest() {
64 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous(
65 sizeof(DeviceLightHardwareBuffer)));
68 protected:
69 void SetUp() override {
70 const DeviceLightHardwareBuffer* null_buffer = nullptr;
71 listener_.reset(new MockDeviceLightListener);
72 light_pump_.reset(new DeviceLightEventPumpForTesting);
73 buffer_ = static_cast<DeviceLightHardwareBuffer*>(shared_memory_.memory());
74 ASSERT_NE(null_buffer, buffer_);
75 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(),
76 &handle_));
79 void InitBuffer() {
80 DeviceLightData& data = buffer_->data;
81 data.value = 1.0;
84 MockDeviceLightListener* listener() { return listener_.get(); }
85 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); }
86 base::SharedMemoryHandle handle() { return handle_; }
87 DeviceLightHardwareBuffer* buffer() { return buffer_; }
89 private:
90 scoped_ptr<MockDeviceLightListener> listener_;
91 scoped_ptr<DeviceLightEventPumpForTesting> light_pump_;
92 base::SharedMemoryHandle handle_;
93 base::SharedMemory shared_memory_;
94 DeviceLightHardwareBuffer* buffer_;
96 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest);
99 TEST_F(DeviceLightEventPumpTest, DidStartPolling) {
100 base::MessageLoopForUI loop;
102 InitBuffer();
104 light_pump()->Start(listener());
105 light_pump()->OnDidStart(handle());
107 base::MessageLoop::current()->Run();
109 const DeviceLightData& received_data = listener()->data();
110 EXPECT_TRUE(listener()->did_change_device_light());
111 EXPECT_EQ(1, static_cast<double>(received_data.value));
114 TEST_F(DeviceLightEventPumpTest, FireAllNullEvent) {
115 base::MessageLoopForUI loop;
117 light_pump()->Start(listener());
118 light_pump()->OnDidStart(handle());
120 base::MessageLoop::current()->Run();
122 const DeviceLightData& received_data = listener()->data();
123 EXPECT_TRUE(listener()->did_change_device_light());
124 EXPECT_FALSE(received_data.value);
127 TEST_F(DeviceLightEventPumpTest, DidStartPollingValuesEqual) {
128 base::MessageLoopForUI loop;
130 InitBuffer();
132 light_pump()->Start(listener());
133 light_pump()->OnDidStart(handle());
135 base::MessageLoop::current()->Run();
137 const DeviceLightData& received_data = listener()->data();
138 EXPECT_TRUE(listener()->did_change_device_light());
139 EXPECT_EQ(1, static_cast<double>(received_data.value));
141 double last_seen_data = received_data.value;
142 // Set next value to be same as previous value.
143 buffer()->data.value = 1.0;
144 listener()->set_did_change_device_light(false);
146 // Reset the pump's listener.
147 light_pump()->Start(listener());
149 base::MessageLoop::current()->PostTask(
150 FROM_HERE,
151 base::Bind(&DeviceLightEventPumpForTesting::FireEvent,
152 base::Unretained(light_pump())));
153 base::MessageLoop::current()->Run();
155 // No change in device light as present value is same as previous value.
156 EXPECT_FALSE(listener()->did_change_device_light());
157 EXPECT_EQ(last_seen_data, static_cast<double>(received_data.value));
160 } // namespace content