OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 | 8 |
9 #include "device/generic_sensor/fake_platform_sensor.h" | 9 #include "device/generic_sensor/fake_platform_sensor.h" |
10 #include "device/generic_sensor/fake_platform_sensor_provider.h" | 10 #include "device/generic_sensor/fake_platform_sensor_provider.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 private: | 80 private: |
81 scoped_refptr<PlatformSensor> sensor_; | 81 scoped_refptr<PlatformSensor> sensor_; |
82 bool notification_suspended_; | 82 bool notification_suspended_; |
83 bool sensor_reading_changed_; | 83 bool sensor_reading_changed_; |
84 bool sensor_error_; | 84 bool sensor_error_; |
85 }; | 85 }; |
86 | 86 |
87 class PlatformSensorProviderTest : public ::testing::Test { | 87 class PlatformSensorProviderTest : public ::testing::Test { |
88 public: | 88 public: |
89 PlatformSensorProviderTest() | 89 PlatformSensorProviderTest() |
90 : sensor_client_(new PlatformSensorTestClient()) { | 90 : provider_(new FakePlatformSensorProvider()), |
91 message_loop_.reset(new base::MessageLoopForIO); | 91 sensor_client_(new PlatformSensorTestClient()), |
92 } | 92 message_loop_(new base::MessageLoopForIO) {} |
93 | 93 |
94 protected: | 94 protected: |
95 scoped_refptr<PlatformSensor> CreateSensor( | 95 scoped_refptr<PlatformSensor> CreateSensor( |
96 mojom::SensorType type, | 96 mojom::SensorType type, |
97 TestSensorCreateCallback* callback) { | 97 TestSensorCreateCallback* callback) { |
98 FakePlatformSensorProvider::GetInstance()->CreateSensor( | 98 provider_->CreateSensor(type, callback->callback()); |
99 type, callback->callback()); | |
100 return callback->WaitForResult(); | 99 return callback->WaitForResult(); |
101 } | 100 } |
102 | 101 |
| 102 std::unique_ptr<FakePlatformSensorProvider> provider_; |
103 std::unique_ptr<PlatformSensorTestClient> sensor_client_; | 103 std::unique_ptr<PlatformSensorTestClient> sensor_client_; |
104 std::unique_ptr<base::MessageLoop> message_loop_; | 104 std::unique_ptr<base::MessageLoop> message_loop_; |
105 }; | 105 }; |
106 | 106 |
107 TEST_F(PlatformSensorProviderTest, CreateSensorsAndCheckType) { | 107 TEST_F(PlatformSensorProviderTest, CreateSensorsAndCheckType) { |
108 TestSensorCreateCallback callback1; | 108 TestSensorCreateCallback callback1; |
109 scoped_refptr<PlatformSensor> sensor1 = | 109 scoped_refptr<PlatformSensor> sensor1 = |
110 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1); | 110 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1); |
111 EXPECT_TRUE(sensor1); | 111 EXPECT_TRUE(sensor1); |
112 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType()); | 112 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType()); |
(...skipping 17 matching lines...) Expand all Loading... |
130 EXPECT_EQ(SensorType::GYROSCOPE, sensor4->GetType()); | 130 EXPECT_EQ(SensorType::GYROSCOPE, sensor4->GetType()); |
131 | 131 |
132 TestSensorCreateCallback callback5; | 132 TestSensorCreateCallback callback5; |
133 scoped_refptr<PlatformSensor> sensor5 = | 133 scoped_refptr<PlatformSensor> sensor5 = |
134 CreateSensor(SensorType::PRESSURE, &callback5); | 134 CreateSensor(SensorType::PRESSURE, &callback5); |
135 EXPECT_TRUE(sensor5); | 135 EXPECT_TRUE(sensor5); |
136 EXPECT_EQ(SensorType::PRESSURE, sensor5->GetType()); | 136 EXPECT_EQ(SensorType::PRESSURE, sensor5->GetType()); |
137 } | 137 } |
138 | 138 |
139 TEST_F(PlatformSensorProviderTest, CreateAndGetSensor) { | 139 TEST_F(PlatformSensorProviderTest, CreateAndGetSensor) { |
140 PlatformSensorProvider* sensor_provider = | |
141 FakePlatformSensorProvider::GetInstance(); | |
142 | |
143 // Create Ambient Light sensor. | 140 // Create Ambient Light sensor. |
144 TestSensorCreateCallback callback1; | 141 TestSensorCreateCallback callback1; |
145 scoped_refptr<PlatformSensor> sensor1 = | 142 scoped_refptr<PlatformSensor> sensor1 = |
146 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1); | 143 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1); |
147 EXPECT_TRUE(sensor1); | 144 EXPECT_TRUE(sensor1); |
148 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType()); | 145 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType()); |
149 | 146 |
150 // Try to get Gyroscope sensor, which has not been created yet. | 147 // Try to get Gyroscope sensor, which has not been created yet. |
151 scoped_refptr<PlatformSensor> sensor2 = | 148 scoped_refptr<PlatformSensor> sensor2 = |
152 sensor_provider->GetSensor(SensorType::GYROSCOPE); | 149 provider_->GetSensor(SensorType::GYROSCOPE); |
153 EXPECT_FALSE(sensor2); | 150 EXPECT_FALSE(sensor2); |
154 | 151 |
155 // Get Ambient Light sensor. | 152 // Get Ambient Light sensor. |
156 scoped_refptr<PlatformSensor> sensor3 = | 153 scoped_refptr<PlatformSensor> sensor3 = |
157 sensor_provider->GetSensor(SensorType::AMBIENT_LIGHT); | 154 provider_->GetSensor(SensorType::AMBIENT_LIGHT); |
158 EXPECT_TRUE(sensor3); | 155 EXPECT_TRUE(sensor3); |
159 | 156 |
160 EXPECT_EQ(sensor1->GetType(), sensor3->GetType()); | 157 EXPECT_EQ(sensor1->GetType(), sensor3->GetType()); |
161 } | 158 } |
162 | 159 |
| 160 TEST_F(PlatformSensorProviderTest, CreateAndRemoveSensors) { |
| 161 TestSensorCreateCallback callback1; |
| 162 scoped_refptr<PlatformSensor> sensor1 = |
| 163 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1); |
| 164 EXPECT_TRUE(sensor1); |
| 165 |
| 166 TestSensorCreateCallback callback2; |
| 167 scoped_refptr<PlatformSensor> sensor2 = |
| 168 CreateSensor(SensorType::PROXIMITY, &callback2); |
| 169 EXPECT_TRUE(sensor2); |
| 170 |
| 171 EXPECT_CALL(*provider_, AllSensorsRemoved()).Times(1); |
| 172 |
| 173 sensor1 = nullptr; |
| 174 sensor2 = nullptr; |
| 175 } |
| 176 |
163 TEST_F(PlatformSensorProviderTest, TestSensorLeaks) { | 177 TEST_F(PlatformSensorProviderTest, TestSensorLeaks) { |
164 PlatformSensorProvider* sensor_provider = | |
165 FakePlatformSensorProvider::GetInstance(); | |
166 | |
167 // Create Ambient Light sensor. | 178 // Create Ambient Light sensor. |
168 TestSensorCreateCallback callback1; | 179 TestSensorCreateCallback callback1; |
169 scoped_refptr<PlatformSensor> sensor1 = | 180 scoped_refptr<PlatformSensor> sensor1 = |
170 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1); | 181 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1); |
171 EXPECT_TRUE(sensor1); | 182 EXPECT_TRUE(sensor1); |
172 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType()); | 183 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType()); |
173 | 184 |
174 // Sensor should be automatically destroyed. | 185 // Sensor should be automatically destroyed. |
175 sensor1 = nullptr; | 186 sensor1 = nullptr; |
176 scoped_refptr<PlatformSensor> sensor2 = | 187 scoped_refptr<PlatformSensor> sensor2 = |
177 sensor_provider->GetSensor(SensorType::AMBIENT_LIGHT); | 188 provider_->GetSensor(SensorType::AMBIENT_LIGHT); |
178 EXPECT_FALSE(sensor2); | 189 EXPECT_FALSE(sensor2); |
179 } | 190 } |
180 | 191 |
181 // This test assumes that a mock sensor has a constant maximum frequency value | 192 // This test assumes that a mock sensor has a constant maximum frequency value |
182 // of 50 hz (different from the base sensor class that has a range from 0 to | 193 // of 50 hz (different from the base sensor class that has a range from 0 to |
183 // 60) and tests whether a mock sensor can be started with a value range from 0 | 194 // 60) and tests whether a mock sensor can be started with a value range from 0 |
184 // to 60. | 195 // to 60. |
185 TEST_F(PlatformSensorProviderTest, StartListeningWithDifferentParameters) { | 196 TEST_F(PlatformSensorProviderTest, StartListeningWithDifferentParameters) { |
186 const double too_high_frequency = 60; | 197 const double too_high_frequency = 60; |
187 const double normal_frequency = 39; | 198 const double normal_frequency = 39; |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 sensor_client_->set_notification_suspended(false); | 355 sensor_client_->set_notification_suspended(false); |
345 EXPECT_FALSE(sensor_client_->IsNotificationSuspended()); | 356 EXPECT_FALSE(sensor_client_->IsNotificationSuspended()); |
346 for (const auto& client : clients) | 357 for (const auto& client : clients) |
347 EXPECT_FALSE(client->IsNotificationSuspended()); | 358 EXPECT_FALSE(client->IsNotificationSuspended()); |
348 | 359 |
349 fake_sensor->UpdateSensor(); | 360 fake_sensor->UpdateSensor(); |
350 EXPECT_TRUE(fake_sensor->started()); | 361 EXPECT_TRUE(fake_sensor->started()); |
351 } | 362 } |
352 | 363 |
353 } // namespace device | 364 } // namespace device |
OLD | NEW |