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

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

Issue 2037513002: Convert device_sensors to use mojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@conversion--mime-registry
Patch Set: Created 4 years, 5 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device_light_event_pump.h" 5 #include "device_light_event_pump.h"
6 6
7 #include "base/location.h" 7 #include "base/location.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/single_thread_task_runner.h" 9 #include "base/single_thread_task_runner.h"
10 #include "base/threading/thread_task_runner_handle.h" 10 #include "base/threading/thread_task_runner_handle.h"
11 #include "content/common/device_sensors/device_light_hardware_buffer.h" 11 #include "content/common/device_sensors/device_light_hardware_buffer.h"
12 #include "content/public/test/test_utils.h" 12 #include "content/public/test/test_utils.h"
13 #include "mojo/public/cpp/system/buffer.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h" 15 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h"
15 16
16 namespace content { 17 namespace content {
17 18
18 class MockDeviceLightListener : public blink::WebDeviceLightListener { 19 class MockDeviceLightListener : public blink::WebDeviceLightListener {
19 public: 20 public:
20 MockDeviceLightListener() : did_change_device_light_(false) {} 21 MockDeviceLightListener() : did_change_device_light_(false) {}
21 ~MockDeviceLightListener() override {} 22 ~MockDeviceLightListener() override {}
22 23
(...skipping 16 matching lines...) Expand all
39 40
40 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener); 41 DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener);
41 }; 42 };
42 43
43 class DeviceLightEventPumpForTesting : public DeviceLightEventPump { 44 class DeviceLightEventPumpForTesting : public DeviceLightEventPump {
44 public: 45 public:
45 DeviceLightEventPumpForTesting() 46 DeviceLightEventPumpForTesting()
46 : DeviceLightEventPump(0) {} 47 : DeviceLightEventPump(0) {}
47 ~DeviceLightEventPumpForTesting() override {} 48 ~DeviceLightEventPumpForTesting() override {}
48 49
49 void OnDidStart(base::SharedMemoryHandle renderer_handle) { 50 void DidStart(mojo::ScopedSharedBufferHandle renderer_handle) {
50 DeviceLightEventPump::OnDidStart(renderer_handle); 51 DeviceLightEventPump::DidStart(std::move(renderer_handle));
51 } 52 }
52 void SendStartMessage() override {} 53 void SendStartMessage() override {}
53 void SendStopMessage() override {} 54 void SendStopMessage() override {}
54 void FireEvent() override { 55 void FireEvent() override {
55 DeviceLightEventPump::FireEvent(); 56 DeviceLightEventPump::FireEvent();
56 Stop(); 57 Stop();
57 base::MessageLoop::current()->QuitWhenIdle(); 58 base::MessageLoop::current()->QuitWhenIdle();
58 } 59 }
59 60
60 private: 61 private:
61 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting); 62 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting);
62 }; 63 };
63 64
64 class DeviceLightEventPumpTest : public testing::Test { 65 class DeviceLightEventPumpTest : public testing::Test {
65 public: 66 public:
66 DeviceLightEventPumpTest() { 67 DeviceLightEventPumpTest() = default;
67 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous(
68 sizeof(DeviceLightHardwareBuffer)));
69 }
70 68
71 protected: 69 protected:
72 void SetUp() override { 70 void SetUp() override {
73 const DeviceLightHardwareBuffer* null_buffer = nullptr;
74 listener_.reset(new MockDeviceLightListener); 71 listener_.reset(new MockDeviceLightListener);
75 light_pump_.reset(new DeviceLightEventPumpForTesting); 72 light_pump_.reset(new DeviceLightEventPumpForTesting);
76 buffer_ = static_cast<DeviceLightHardwareBuffer*>(shared_memory_.memory()); 73 shared_memory_ =
77 ASSERT_NE(null_buffer, buffer_); 74 mojo::SharedBufferHandle::Create(sizeof(DeviceLightHardwareBuffer));
78 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(), 75 mapping_ = shared_memory_->Map(sizeof(DeviceLightHardwareBuffer));
79 &handle_)); 76 ASSERT_TRUE(mapping_);
80 } 77 }
81 78
82 void InitBuffer() { 79 void InitBuffer() {
83 DeviceLightData& data = buffer_->data; 80 DeviceLightData& data = buffer()->data;
84 data.value = 1.0; 81 data.value = 1.0;
85 } 82 }
86 83
87 MockDeviceLightListener* listener() { return listener_.get(); } 84 MockDeviceLightListener* listener() { return listener_.get(); }
88 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); } 85 DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); }
89 base::SharedMemoryHandle handle() { return handle_; } 86 mojo::ScopedSharedBufferHandle handle() {
90 DeviceLightHardwareBuffer* buffer() { return buffer_; } 87 return shared_memory_->Clone(
88 mojo::SharedBufferHandle::AccessMode::READ_ONLY);
89 }
90 DeviceLightHardwareBuffer* buffer() {
91 return reinterpret_cast<DeviceLightHardwareBuffer*>(mapping_.get());
92 }
91 93
92 private: 94 private:
95 base::MessageLoop loop_;
93 std::unique_ptr<MockDeviceLightListener> listener_; 96 std::unique_ptr<MockDeviceLightListener> listener_;
94 std::unique_ptr<DeviceLightEventPumpForTesting> light_pump_; 97 std::unique_ptr<DeviceLightEventPumpForTesting> light_pump_;
95 base::SharedMemoryHandle handle_; 98 mojo::ScopedSharedBufferHandle shared_memory_;
96 base::SharedMemory shared_memory_; 99 mojo::ScopedSharedBufferMapping mapping_;
97 DeviceLightHardwareBuffer* buffer_;
98 100
99 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); 101 DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest);
100 }; 102 };
101 103
102 TEST_F(DeviceLightEventPumpTest, DidStartPolling) { 104 TEST_F(DeviceLightEventPumpTest, DidStartPolling) {
103 base::MessageLoopForUI loop;
104
105 InitBuffer(); 105 InitBuffer();
106 106
107 light_pump()->Start(listener()); 107 light_pump()->Start(listener());
108 light_pump()->OnDidStart(handle()); 108 light_pump()->DidStart(handle());
109 109
110 base::MessageLoop::current()->Run(); 110 base::MessageLoop::current()->Run();
111 111
112 const DeviceLightData& received_data = listener()->data(); 112 const DeviceLightData& received_data = listener()->data();
113 EXPECT_TRUE(listener()->did_change_device_light()); 113 EXPECT_TRUE(listener()->did_change_device_light());
114 EXPECT_EQ(1, static_cast<double>(received_data.value)); 114 EXPECT_EQ(1, static_cast<double>(received_data.value));
115 } 115 }
116 116
117 TEST_F(DeviceLightEventPumpTest, FireAllNullEvent) { 117 TEST_F(DeviceLightEventPumpTest, FireAllNullEvent) {
118 base::MessageLoopForUI loop;
119
120 light_pump()->Start(listener()); 118 light_pump()->Start(listener());
121 light_pump()->OnDidStart(handle()); 119 light_pump()->DidStart(handle());
122 120
123 base::MessageLoop::current()->Run(); 121 base::MessageLoop::current()->Run();
124 122
125 const DeviceLightData& received_data = listener()->data(); 123 const DeviceLightData& received_data = listener()->data();
126 EXPECT_TRUE(listener()->did_change_device_light()); 124 EXPECT_TRUE(listener()->did_change_device_light());
127 EXPECT_FALSE(received_data.value); 125 EXPECT_FALSE(received_data.value);
128 } 126 }
129 127
130 TEST_F(DeviceLightEventPumpTest, DidStartPollingValuesEqual) { 128 TEST_F(DeviceLightEventPumpTest, DidStartPollingValuesEqual) {
131 base::MessageLoopForUI loop;
132
133 InitBuffer(); 129 InitBuffer();
134 130
135 light_pump()->Start(listener()); 131 light_pump()->Start(listener());
136 light_pump()->OnDidStart(handle()); 132 light_pump()->DidStart(handle());
137 133
138 base::MessageLoop::current()->Run(); 134 base::MessageLoop::current()->Run();
139 135
140 const DeviceLightData& received_data = listener()->data(); 136 const DeviceLightData& received_data = listener()->data();
141 EXPECT_TRUE(listener()->did_change_device_light()); 137 EXPECT_TRUE(listener()->did_change_device_light());
142 EXPECT_EQ(1, static_cast<double>(received_data.value)); 138 EXPECT_EQ(1, static_cast<double>(received_data.value));
143 139
144 double last_seen_data = received_data.value; 140 double last_seen_data = received_data.value;
145 // Set next value to be same as previous value. 141 // Set next value to be same as previous value.
146 buffer()->data.value = 1.0; 142 buffer()->data.value = 1.0;
147 listener()->set_did_change_device_light(false); 143 listener()->set_did_change_device_light(false);
148 144
149 // Reset the pump's listener. 145 // Reset the pump's listener.
150 light_pump()->Start(listener()); 146 light_pump()->Start(listener());
151 147
152 base::ThreadTaskRunnerHandle::Get()->PostTask( 148 base::ThreadTaskRunnerHandle::Get()->PostTask(
153 FROM_HERE, base::Bind(&DeviceLightEventPumpForTesting::FireEvent, 149 FROM_HERE, base::Bind(&DeviceLightEventPumpForTesting::FireEvent,
154 base::Unretained(light_pump()))); 150 base::Unretained(light_pump())));
155 base::MessageLoop::current()->Run(); 151 base::MessageLoop::current()->Run();
156 152
157 // No change in device light as present value is same as previous value. 153 // No change in device light as present value is same as previous value.
158 EXPECT_FALSE(listener()->did_change_device_light()); 154 EXPECT_FALSE(listener()->did_change_device_light());
159 EXPECT_EQ(last_seen_data, static_cast<double>(received_data.value)); 155 EXPECT_EQ(last_seen_data, static_cast<double>(received_data.value));
160 } 156 }
161 157
162 } // namespace content 158 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698