Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: content/renderer/device_sensors/device_light_event_pump_unittest.cc

Issue 286793002: [DeviceLight] Add renderer+common parts (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Try fixing for windows build Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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"
timvolodine 2014/06/11 15:30:06 probably not needed
riju_ 2014/06/12 10:34:22 Done.
8 #include "base/memory/scoped_ptr.h"
timvolodine 2014/06/11 15:30:06 probably not needed
riju_ 2014/06/12 10:34:22 Done.
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));
timvolodine 2014/06/11 15:30:06 you are copying from double into DeviceLightData?
riju_ 2014/06/12 10:34:22 Done.
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) {
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 }
117
118 TEST_F(DeviceLightEventPumpTest, DidStartPollingValueIsUpdatedFalse) {
119 base::MessageLoopForUI loop;
120
121 InitBuffer(false);
122
123 light_pump()->SetListener(listener());
124 light_pump()->OnDidStart(handle());
125
126 base::MessageLoop::current()->Run();
127
128 // No change in device light because valueIsUpdated is false.
129 EXPECT_FALSE(listener()->did_change_device_light());
130 }
131
132 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698