Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / device_sensors / device_light_event_pump_unittest.cc
blobbc05b6461b5643175fca9fe0e9587a3b93944f5d
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/location.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.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"
15 namespace content {
17 class MockDeviceLightListener : public blink::WebDeviceLightListener {
18 public:
19 MockDeviceLightListener() : did_change_device_light_(false) {}
20 virtual ~MockDeviceLightListener() {}
22 virtual void didChangeDeviceLight(double value) override {
23 data_.value = value;
24 did_change_device_light_ = true;
27 bool did_change_device_light() const { return did_change_device_light_; }
29 void set_did_change_device_light(bool value) {
30 did_change_device_light_ = value;
33 const DeviceLightData& data() const { return data_; }
35 private:
36 bool did_change_device_light_;
37 DeviceLightData data_;
39 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener);
42 class DeviceLightEventPumpForTesting : public DeviceLightEventPump {
43 public:
44 DeviceLightEventPumpForTesting()
45 : DeviceLightEventPump(0) {}
46 ~DeviceLightEventPumpForTesting() override {}
48 void OnDidStart(base::SharedMemoryHandle renderer_handle) {
49 DeviceLightEventPump::OnDidStart(renderer_handle);
51 void SendStartMessage() override {}
52 void SendStopMessage() override {}
53 void FireEvent() override {
54 DeviceLightEventPump::FireEvent();
55 Stop();
56 base::MessageLoop::current()->QuitWhenIdle();
59 private:
60 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting);
63 class DeviceLightEventPumpTest : public testing::Test {
64 public:
65 DeviceLightEventPumpTest() {
66 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous(
67 sizeof(DeviceLightHardwareBuffer)));
70 protected:
71 void SetUp() override {
72 const DeviceLightHardwareBuffer* null_buffer = nullptr;
73 listener_.reset(new MockDeviceLightListener);
74 light_pump_.reset(new DeviceLightEventPumpForTesting);
75 buffer_ = static_cast<DeviceLightHardwareBuffer*>(shared_memory_.memory());
76 ASSERT_NE(null_buffer, buffer_);
77 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(),
78 &handle_));
81 void InitBuffer() {
82 DeviceLightData& data = buffer_->data;
83 data.value = 1.0;
86 MockDeviceLightListener* listener() { return listener_.get(); }
87 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); }
88 base::SharedMemoryHandle handle() { return handle_; }
89 DeviceLightHardwareBuffer* buffer() { return buffer_; }
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_;
98 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest);
101 TEST_F(DeviceLightEventPumpTest, DidStartPolling) {
102 base::MessageLoopForUI loop;
104 InitBuffer();
106 light_pump()->Start(listener());
107 light_pump()->OnDidStart(handle());
109 base::MessageLoop::current()->Run();
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));
116 TEST_F(DeviceLightEventPumpTest, FireAllNullEvent) {
117 base::MessageLoopForUI loop;
119 light_pump()->Start(listener());
120 light_pump()->OnDidStart(handle());
122 base::MessageLoop::current()->Run();
124 const DeviceLightData& received_data = listener()->data();
125 EXPECT_TRUE(listener()->did_change_device_light());
126 EXPECT_FALSE(received_data.value);
129 TEST_F(DeviceLightEventPumpTest, DidStartPollingValuesEqual) {
130 base::MessageLoopForUI loop;
132 InitBuffer();
134 light_pump()->Start(listener());
135 light_pump()->OnDidStart(handle());
137 base::MessageLoop::current()->Run();
139 const DeviceLightData& received_data = listener()->data();
140 EXPECT_TRUE(listener()->did_change_device_light());
141 EXPECT_EQ(1, static_cast<double>(received_data.value));
143 double last_seen_data = received_data.value;
144 // Set next value to be same as previous value.
145 buffer()->data.value = 1.0;
146 listener()->set_did_change_device_light(false);
148 // Reset the pump's listener.
149 light_pump()->Start(listener());
151 base::ThreadTaskRunnerHandle::Get()->PostTask(
152 FROM_HERE, base::Bind(&DeviceLightEventPumpForTesting::FireEvent,
153 base::Unretained(light_pump())));
154 base::MessageLoop::current()->Run();
156 // No change in device light as present value is same as previous value.
157 EXPECT_FALSE(listener()->did_change_device_light());
158 EXPECT_EQ(last_seen_data, static_cast<double>(received_data.value));
161 } // namespace content