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

Side by Side Diff: content/renderer/device_sensors/device_orientation_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, 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
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_orientation_event_pump.h" 5 #include "device_orientation_event_pump.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 private: 45 private:
46 bool did_change_device_orientation_; 46 bool did_change_device_orientation_;
47 blink::WebDeviceOrientationData data_; 47 blink::WebDeviceOrientationData data_;
48 48
49 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener); 49 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener);
50 }; 50 };
51 51
52 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { 52 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump {
53 public: 53 public:
54 DeviceOrientationEventPumpForTesting() 54 DeviceOrientationEventPumpForTesting()
55 : DeviceOrientationEventPump(0) { } 55 : DeviceOrientationEventPump(nullptr) {}
56 ~DeviceOrientationEventPumpForTesting() override {} 56 ~DeviceOrientationEventPumpForTesting() override {}
57 57
58 void OnDidStart(base::SharedMemoryHandle renderer_handle) { 58 void DidStart(mojo::ScopedSharedBufferHandle renderer_handle) {
59 DeviceOrientationEventPump::OnDidStart(renderer_handle); 59 DeviceOrientationEventPump::DidStart(std::move(renderer_handle));
60 } 60 }
61 void SendStartMessage() override {} 61 void SendStartMessage() override {}
62 void SendStopMessage() override {} 62 void SendStopMessage() override {}
63 void FireEvent() override { 63 void FireEvent() override {
64 DeviceOrientationEventPump::FireEvent(); 64 DeviceOrientationEventPump::FireEvent();
65 Stop(); 65 Stop();
66 base::MessageLoop::current()->QuitWhenIdle(); 66 base::MessageLoop::current()->QuitWhenIdle();
67 } 67 }
68 68
69 private: 69 private:
70 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting); 70 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting);
71 }; 71 };
72 72
73 class DeviceOrientationEventPumpTest : public testing::Test { 73 class DeviceOrientationEventPumpTest : public testing::Test {
74 public: 74 public:
75 DeviceOrientationEventPumpTest() { 75 DeviceOrientationEventPumpTest() {
76 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( 76 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous(
77 sizeof(DeviceOrientationHardwareBuffer))); 77 sizeof(DeviceOrientationHardwareBuffer)));
78 } 78 }
79 79
80 protected: 80 protected:
81 void SetUp() override { 81 void SetUp() override {
82 const DeviceOrientationHardwareBuffer* null_buffer = nullptr; 82 const DeviceOrientationHardwareBuffer* null_buffer = nullptr;
83 listener_.reset(new MockDeviceOrientationListener); 83 listener_.reset(new MockDeviceOrientationListener);
84 orientation_pump_.reset(new DeviceOrientationEventPumpForTesting); 84 orientation_pump_.reset(new DeviceOrientationEventPumpForTesting);
85 buffer_ = static_cast<DeviceOrientationHardwareBuffer*>( 85 buffer_ = static_cast<DeviceOrientationHardwareBuffer*>(
86 shared_memory_.memory()); 86 shared_memory_.memory());
87 ASSERT_NE(null_buffer, buffer_); 87 ASSERT_NE(null_buffer, buffer_);
88 memset(buffer_, 0, sizeof(DeviceOrientationHardwareBuffer)); 88 memset(buffer_, 0, sizeof(DeviceOrientationHardwareBuffer));
89 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(),
90 &handle_));
91 } 89 }
92 90
93 void InitBuffer() { 91 void InitBuffer() {
94 blink::WebDeviceOrientationData& data = buffer_->data; 92 blink::WebDeviceOrientationData& data = buffer_->data;
95 data.alpha = 1; 93 data.alpha = 1;
96 data.hasAlpha = true; 94 data.hasAlpha = true;
97 data.beta = 2; 95 data.beta = 2;
98 data.hasBeta = true; 96 data.hasBeta = true;
99 data.gamma = 3; 97 data.gamma = 3;
100 data.hasGamma = true; 98 data.hasGamma = true;
101 data.allAvailableSensorsAreActive = true; 99 data.allAvailableSensorsAreActive = true;
102 } 100 }
103 101
104 void InitBufferNoData() { 102 void InitBufferNoData() {
105 blink::WebDeviceOrientationData& data = buffer_->data; 103 blink::WebDeviceOrientationData& data = buffer_->data;
106 data.allAvailableSensorsAreActive = true; 104 data.allAvailableSensorsAreActive = true;
107 } 105 }
108 106
109 MockDeviceOrientationListener* listener() { return listener_.get(); } 107 MockDeviceOrientationListener* listener() { return listener_.get(); }
110 DeviceOrientationEventPumpForTesting* orientation_pump() { 108 DeviceOrientationEventPumpForTesting* orientation_pump() {
111 return orientation_pump_.get(); 109 return orientation_pump_.get();
112 } 110 }
113 base::SharedMemoryHandle handle() { return handle_; } 111 mojo::ScopedSharedBufferHandle handle() {
112 return mojo::WrapSharedMemoryHandle(
113 base::SharedMemory::DuplicateHandle(shared_memory_.handle()),
114 sizeof(DeviceOrientationHardwareBuffer), false);
115 }
114 DeviceOrientationHardwareBuffer* buffer() { return buffer_; } 116 DeviceOrientationHardwareBuffer* buffer() { return buffer_; }
115 117
116 private: 118 private:
117 std::unique_ptr<MockDeviceOrientationListener> listener_; 119 std::unique_ptr<MockDeviceOrientationListener> listener_;
118 std::unique_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_; 120 std::unique_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_;
119 base::SharedMemoryHandle handle_;
120 base::SharedMemory shared_memory_; 121 base::SharedMemory shared_memory_;
121 DeviceOrientationHardwareBuffer* buffer_; 122 DeviceOrientationHardwareBuffer* buffer_;
122 123
123 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); 124 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest);
124 }; 125 };
125 126
126 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { 127 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) {
127 base::MessageLoop loop; 128 base::MessageLoop loop;
128 129
129 InitBuffer(); 130 InitBuffer();
130 orientation_pump()->Start(listener()); 131 orientation_pump()->Start(listener());
131 orientation_pump()->OnDidStart(handle()); 132 orientation_pump()->DidStart(handle());
132 133
133 base::MessageLoop::current()->Run(); 134 base::MessageLoop::current()->Run();
134 135
135 const blink::WebDeviceOrientationData& received_data = listener()->data(); 136 const blink::WebDeviceOrientationData& received_data = listener()->data();
136 EXPECT_TRUE(listener()->did_change_device_orientation()); 137 EXPECT_TRUE(listener()->did_change_device_orientation());
137 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); 138 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
138 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); 139 EXPECT_EQ(1, static_cast<double>(received_data.alpha));
139 EXPECT_TRUE(received_data.hasAlpha); 140 EXPECT_TRUE(received_data.hasAlpha);
140 EXPECT_EQ(2, static_cast<double>(received_data.beta)); 141 EXPECT_EQ(2, static_cast<double>(received_data.beta));
141 EXPECT_TRUE(received_data.hasBeta); 142 EXPECT_TRUE(received_data.hasBeta);
142 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); 143 EXPECT_EQ(3, static_cast<double>(received_data.gamma));
143 EXPECT_TRUE(received_data.hasGamma); 144 EXPECT_TRUE(received_data.hasGamma);
144 } 145 }
145 146
146 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { 147 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) {
147 base::MessageLoop loop; 148 base::MessageLoop loop;
148 149
149 InitBufferNoData(); 150 InitBufferNoData();
150 orientation_pump()->Start(listener()); 151 orientation_pump()->Start(listener());
151 orientation_pump()->OnDidStart(handle()); 152 orientation_pump()->DidStart(handle());
152 153
153 base::MessageLoop::current()->Run(); 154 base::MessageLoop::current()->Run();
154 155
155 const blink::WebDeviceOrientationData& received_data = listener()->data(); 156 const blink::WebDeviceOrientationData& received_data = listener()->data();
156 EXPECT_TRUE(listener()->did_change_device_orientation()); 157 EXPECT_TRUE(listener()->did_change_device_orientation());
157 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); 158 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
158 EXPECT_FALSE(received_data.hasAlpha); 159 EXPECT_FALSE(received_data.hasAlpha);
159 EXPECT_FALSE(received_data.hasBeta); 160 EXPECT_FALSE(received_data.hasBeta);
160 EXPECT_FALSE(received_data.hasGamma); 161 EXPECT_FALSE(received_data.hasGamma);
161 } 162 }
162 163
163 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { 164 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) {
164 base::MessageLoop loop; 165 base::MessageLoop loop;
165 166
166 InitBuffer(); 167 InitBuffer();
167 orientation_pump()->Start(listener()); 168 orientation_pump()->Start(listener());
168 orientation_pump()->OnDidStart(handle()); 169 orientation_pump()->DidStart(handle());
169 170
170 base::MessageLoop::current()->Run(); 171 base::MessageLoop::current()->Run();
171 172
172 const blink::WebDeviceOrientationData& received_data = listener()->data(); 173 const blink::WebDeviceOrientationData& received_data = listener()->data();
173 EXPECT_TRUE(listener()->did_change_device_orientation()); 174 EXPECT_TRUE(listener()->did_change_device_orientation());
174 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); 175 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
175 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); 176 EXPECT_EQ(1, static_cast<double>(received_data.alpha));
176 EXPECT_TRUE(received_data.hasAlpha); 177 EXPECT_TRUE(received_data.hasAlpha);
177 EXPECT_EQ(2, static_cast<double>(received_data.beta)); 178 EXPECT_EQ(2, static_cast<double>(received_data.beta));
178 EXPECT_TRUE(received_data.hasBeta); 179 EXPECT_TRUE(received_data.hasBeta);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, 212 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent,
212 base::Unretained(orientation_pump()))); 213 base::Unretained(orientation_pump())));
213 base::MessageLoop::current()->Run(); 214 base::MessageLoop::current()->Run();
214 215
215 EXPECT_TRUE(listener()->did_change_device_orientation()); 216 EXPECT_TRUE(listener()->did_change_device_orientation());
216 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, 217 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold,
217 static_cast<double>(received_data.alpha)); 218 static_cast<double>(received_data.alpha));
218 } 219 }
219 220
220 } // namespace content 221 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698