OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
timvolodine
2015/01/26 15:29:08
nit: 2015?
jonross
2015/01/26 16:40:04
Done.
| |
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 "content/browser/device_sensors/sensor_manager_chromeos.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "content/common/device_sensors/device_orientation_hardware_buffer.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 #include "ui/accelerometer/accelerometer_types.h" | |
11 | |
12 namespace { | |
13 | |
14 const double kMeanGravity = 9.80665; | |
15 | |
16 // Isolated content::SensorManagerChromeOS from the active | |
17 // chromeos::AccelerometerReader. This allows for direct control over which | |
18 // accelerometer events are provided to the sensor manager. | |
19 class TestSensorManagerChromeOS : public content::SensorManagerChromeOS { | |
20 public: | |
21 TestSensorManagerChromeOS() {} | |
22 ~TestSensorManagerChromeOS() override{}; | |
timvolodine
2015/01/26 15:29:08
space after override
jonross
2015/01/26 16:40:04
Done.
| |
23 | |
24 protected: | |
25 void StartObservingAccelerometer() override {} | |
26 void StopObservingAccelerometer() override {} | |
27 | |
28 private: | |
29 DISALLOW_COPY_AND_ASSIGN(TestSensorManagerChromeOS); | |
30 }; | |
31 | |
32 } // namespace | |
33 | |
34 namespace content { | |
35 | |
36 class SensorManagerChromeOSTest : public testing::Test { | |
37 public: | |
38 SensorManagerChromeOSTest() { | |
39 orientation_buffer_.reset(new DeviceOrientationHardwareBuffer); | |
40 } | |
41 | |
42 virtual ~SensorManagerChromeOSTest() {} | |
43 | |
44 void OnAccelerationIncludingGravity(double x, double y, double z) { | |
45 ui::AccelerometerUpdate update; | |
46 update.Set(ui::ACCELEROMETER_SOURCE_SCREEN, x, y, z); | |
47 sensor_manager_->OnAccelerometerUpdated(update); | |
48 } | |
49 | |
50 DeviceOrientationHardwareBuffer* orientation_buffer() { | |
51 return orientation_buffer_.get(); | |
52 } | |
53 | |
54 SensorManagerChromeOS* sensor_manager() { return sensor_manager_.get(); } | |
55 | |
56 // testing::Test: | |
57 void SetUp() override { | |
58 testing::Test::SetUp(); | |
59 sensor_manager_.reset(new TestSensorManagerChromeOS); | |
60 sensor_manager_->StartFetchingDeviceOrientationData( | |
61 orientation_buffer_.get()); | |
62 } | |
63 | |
64 void TearDown() override { | |
65 sensor_manager_->StopFetchingDeviceOrientationData(); | |
66 testing::Test::TearDown(); | |
67 } | |
68 | |
69 private: | |
70 scoped_ptr<SensorManagerChromeOS> sensor_manager_; | |
timvolodine
2015/01/26 15:29:08
nit: SensorManagerChromeOS -> TestSensorManagerChr
jonross
2015/01/26 16:40:04
Done.
| |
71 | |
72 scoped_ptr<DeviceOrientationHardwareBuffer> orientation_buffer_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(SensorManagerChromeOSTest); | |
75 }; | |
76 | |
77 // Tests that starting to process orientation data will update the associated | |
78 // buffer. | |
79 TEST_F(SensorManagerChromeOSTest, OrientationBuffer) { | |
80 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); | |
81 EXPECT_TRUE(buffer->data.hasAbsolute); | |
82 EXPECT_FALSE(buffer->data.hasAlpha); | |
83 EXPECT_FALSE(buffer->data.hasBeta); | |
84 EXPECT_FALSE(buffer->data.hasGamma); | |
85 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive); | |
86 | |
87 OnAccelerationIncludingGravity(0.0f, 0.0f, 1.0f); | |
88 EXPECT_FLOAT_EQ(0.0f, buffer->data.alpha); | |
89 EXPECT_FALSE(buffer->data.hasAlpha); | |
90 EXPECT_TRUE(buffer->data.hasBeta); | |
91 EXPECT_TRUE(buffer->data.hasGamma); | |
92 EXPECT_TRUE(buffer->data.allAvailableSensorsAreActive); | |
93 | |
94 sensor_manager()->StopFetchingDeviceOrientationData(); | |
95 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive); | |
96 } | |
97 | |
98 // Tests a device resting flat. | |
99 TEST_F(SensorManagerChromeOSTest, NeutralOrientation) { | |
100 OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity); | |
101 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); | |
102 EXPECT_FLOAT_EQ(0.0f, buffer->data.beta); | |
103 EXPECT_FLOAT_EQ(0.0f, buffer->data.gamma); | |
104 } | |
105 | |
106 // Tests an upside-down device, such that the W3C boundary [-180,180) causes the | |
107 // beta value to become negative. | |
108 TEST_F(SensorManagerChromeOSTest, UpsideDown) { | |
109 OnAccelerationIncludingGravity(0.0f, 0.0f, kMeanGravity); | |
110 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); | |
111 EXPECT_FLOAT_EQ(-180.0f, buffer->data.beta); | |
112 EXPECT_FLOAT_EQ(0.0f, buffer->data.gamma); | |
113 } | |
114 | |
115 // Tests for positive beta value before the device is completely upside-down | |
116 TEST_F(SensorManagerChromeOSTest, BeforeUpsideDownBoundary) { | |
117 OnAccelerationIncludingGravity(0.0f, -kMeanGravity / 2.0f, | |
118 kMeanGravity / 2.0f); | |
119 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); | |
120 EXPECT_FLOAT_EQ(135.0f, buffer->data.beta); | |
121 EXPECT_FLOAT_EQ(0.0f, buffer->data.gamma); | |
122 } | |
123 | |
124 // Tests a device lying on its left-edge. | |
125 TEST_F(SensorManagerChromeOSTest, LeftEdge) { | |
126 OnAccelerationIncludingGravity(-kMeanGravity, 0.0f, 0.0f); | |
127 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); | |
128 EXPECT_FLOAT_EQ(0.0f, buffer->data.beta); | |
129 EXPECT_FLOAT_EQ(-90.0f, buffer->data.gamma); | |
130 } | |
131 | |
132 // Tests a device lying on its right-edge, such that the W3C boundary [-90,90) | |
133 // causes the gamma value to become negative. | |
134 TEST_F(SensorManagerChromeOSTest, RightEdge) { | |
135 OnAccelerationIncludingGravity(kMeanGravity, 0.0f, 0.0f); | |
136 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); | |
137 EXPECT_FLOAT_EQ(0.0f, buffer->data.beta); | |
138 EXPECT_FLOAT_EQ(-90.0f, buffer->data.gamma); | |
139 } | |
140 | |
141 // Tests for positive gamma value before the device is completely on its right | |
142 // side. | |
143 TEST_F(SensorManagerChromeOSTest, BeforeRightEdgeBoundary) { | |
144 OnAccelerationIncludingGravity(kMeanGravity / 2.0f, 0.0f, | |
145 -kMeanGravity / 2.0f); | |
146 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); | |
147 EXPECT_FLOAT_EQ(0.0f, buffer->data.beta); | |
148 EXPECT_FLOAT_EQ(45.0f, buffer->data.gamma); | |
149 } | |
150 | |
151 } // namespace content | |
OLD | NEW |