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

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: Rebased Created 6 years, 7 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"
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();
20 virtual ~MockDeviceLightListener() { }
21 virtual void didChangeDeviceLight(double) OVERRIDE;
22 bool did_change_device_light_;
23 double data_;
24 };
25
26 MockDeviceLightListener::MockDeviceLightListener()
27 : did_change_device_light_(false) {
28 memset(&data_, 0, sizeof(data_));
29 }
30
31 void MockDeviceLightListener::didChangeDeviceLight(double data) {
32 memcpy(&data_, &data, sizeof(data));
33 did_change_device_light_ = true;
34 }
35
36 class DeviceLightEventPumpForTesting : public DeviceLightEventPump {
37 public:
38 DeviceLightEventPumpForTesting() { }
39 virtual ~DeviceLightEventPumpForTesting() { }
40
41 void OnDidStart(base::SharedMemoryHandle renderer_handle) {
42 DeviceLightEventPump::OnDidStart(renderer_handle);
43 }
44 virtual bool SendStartMessage() OVERRIDE { return true; }
45 virtual bool SendStopMessage() OVERRIDE { return true; }
46 virtual void FireEvent() OVERRIDE {
47 DeviceLightEventPump::FireEvent();
48 Stop();
49 base::MessageLoop::current()->QuitWhenIdle();
50 }
51 };
52
53 class DeviceLightEventPumpTest : public testing::Test {
54 public:
55 DeviceLightEventPumpTest() {
56 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous(
57 sizeof(DeviceLightHardwareBuffer)));
58 }
59
60 protected:
61 virtual void SetUp() OVERRIDE {
62 const DeviceLightHardwareBuffer* null_buffer = NULL;
63 listener_.reset(new MockDeviceLightListener);
64 light_pump_.reset(new DeviceLightEventPumpForTesting);
65 buffer_ = static_cast<DeviceLightHardwareBuffer*>(shared_memory_.memory());
66 ASSERT_NE(null_buffer, buffer_);
67 memset(buffer_, 0, sizeof(DeviceLightHardwareBuffer));
68 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(),
69 &handle_));
70 }
71
72 void InitBuffer() {
73 double& data = buffer_->data;
74 data = 1.0;
75 }
76
77 scoped_ptr<MockDeviceLightListener> listener_;
78 scoped_ptr<DeviceLightEventPumpForTesting> light_pump_;
79 base::SharedMemoryHandle handle_;
80 base::SharedMemory shared_memory_;
81 DeviceLightHardwareBuffer* buffer_;
82 };
83
84 TEST_F(DeviceLightEventPumpTest, DidStartPolling) {
85 base::MessageLoopForUI loop;
86
87 InitBuffer();
88
89 light_pump_->SetListener(listener_.get());
90 light_pump_->OnDidStart(handle_);
91
92 base::MessageLoop::current()->Run();
93
94 double& received_data = listener_->data_;
95 EXPECT_TRUE(listener_->did_change_device_light_);
96 EXPECT_EQ(1, static_cast<double>(received_data));
97 }
98
99 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698