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" |
11 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h" | 11 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h" |
12 | 12 |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace device { | 15 namespace device { |
16 | 16 |
17 using mojom::SensorInitParams; | 17 using mojom::SensorInitParams; |
18 using mojom::SensorType; | 18 using mojom::SensorType; |
19 | 19 |
20 namespace { | |
21 | |
22 uint64_t GetBufferOffset(mojom::SensorType type) { | |
23 return (static_cast<uint64_t>(SensorType::LAST) - | |
24 static_cast<uint64_t>(type)) * | |
25 SensorInitParams::kReadBufferSize; | |
26 } | |
27 | |
28 using CreateSensorCallback = | |
29 base::Callback<void(scoped_refptr<PlatformSensor>)>; | |
30 ; | |
31 | |
32 } // namespace | |
33 | |
34 class TestSensorCreateCallback { | 20 class TestSensorCreateCallback { |
35 public: | 21 public: |
36 TestSensorCreateCallback() | 22 TestSensorCreateCallback() |
37 : callback_(base::Bind(&TestSensorCreateCallback::SetResult, | 23 : callback_(base::Bind(&TestSensorCreateCallback::SetResult, |
38 base::Unretained(this))) {} | 24 base::Unretained(this))) {} |
39 | 25 |
40 scoped_refptr<PlatformSensor> WaitForResult() { | 26 scoped_refptr<PlatformSensor> WaitForResult() { |
41 run_loop_.Run(); | 27 run_loop_.Run(); |
42 scoped_refptr<PlatformSensor> sensor = sensor_; | 28 scoped_refptr<PlatformSensor> sensor = sensor_; |
43 sensor_ = nullptr; | 29 sensor_ = nullptr; |
44 return sensor; | 30 return sensor; |
45 } | 31 } |
46 | 32 |
47 const CreateSensorCallback& callback() const { return callback_; } | 33 const PlatformSensorProvider::CreateSensorCallback& callback() const { |
| 34 return callback_; |
| 35 } |
48 | 36 |
49 private: | 37 private: |
50 void SetResult(scoped_refptr<PlatformSensor> sensor) { | 38 void SetResult(scoped_refptr<PlatformSensor> sensor) { |
51 sensor_ = sensor; | 39 sensor_ = sensor; |
52 run_loop_.Quit(); | 40 run_loop_.Quit(); |
53 } | 41 } |
54 | 42 |
55 const CreateSensorCallback callback_; | 43 const PlatformSensorProvider::CreateSensorCallback callback_; |
56 base::RunLoop run_loop_; | 44 base::RunLoop run_loop_; |
57 scoped_refptr<PlatformSensor> sensor_; | 45 scoped_refptr<PlatformSensor> sensor_; |
58 }; | 46 }; |
59 | 47 |
60 class PlatformSensorTestClient : public PlatformSensor::Client { | 48 class PlatformSensorTestClient : public PlatformSensor::Client { |
61 public: | 49 public: |
62 PlatformSensorTestClient() | 50 PlatformSensorTestClient() |
63 : notification_suspended_(false), | 51 : notification_suspended_(false), |
64 sensor_reading_changed_(false), | 52 sensor_reading_changed_(false), |
65 sensor_error_(false) {} | 53 sensor_error_(false) {} |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 PlatformSensorProviderTest() | 89 PlatformSensorProviderTest() |
102 : sensor_client_(new PlatformSensorTestClient()) { | 90 : sensor_client_(new PlatformSensorTestClient()) { |
103 message_loop_.reset(new base::MessageLoopForIO); | 91 message_loop_.reset(new base::MessageLoopForIO); |
104 } | 92 } |
105 | 93 |
106 protected: | 94 protected: |
107 scoped_refptr<PlatformSensor> CreateSensor( | 95 scoped_refptr<PlatformSensor> CreateSensor( |
108 mojom::SensorType type, | 96 mojom::SensorType type, |
109 TestSensorCreateCallback* callback) { | 97 TestSensorCreateCallback* callback) { |
110 FakePlatformSensorProvider::GetInstance()->CreateSensor( | 98 FakePlatformSensorProvider::GetInstance()->CreateSensor( |
111 type, SensorInitParams::kReadBufferSize, GetBufferOffset(type), | 99 type, callback->callback()); |
112 callback->callback()); | |
113 return callback->WaitForResult(); | 100 return callback->WaitForResult(); |
114 } | 101 } |
115 | 102 |
116 std::unique_ptr<PlatformSensorTestClient> sensor_client_; | 103 std::unique_ptr<PlatformSensorTestClient> sensor_client_; |
117 std::unique_ptr<base::MessageLoop> message_loop_; | 104 std::unique_ptr<base::MessageLoop> message_loop_; |
118 }; | 105 }; |
119 | 106 |
120 TEST_F(PlatformSensorProviderTest, CreateSensorsAndCheckType) { | 107 TEST_F(PlatformSensorProviderTest, CreateSensorsAndCheckType) { |
121 TestSensorCreateCallback callback1; | 108 TestSensorCreateCallback callback1; |
122 scoped_refptr<PlatformSensor> sensor1 = | 109 scoped_refptr<PlatformSensor> sensor1 = |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 scoped_refptr<PlatformSensor> sensor2 = | 151 scoped_refptr<PlatformSensor> sensor2 = |
165 sensor_provider->GetSensor(SensorType::GYROSCOPE); | 152 sensor_provider->GetSensor(SensorType::GYROSCOPE); |
166 EXPECT_FALSE(sensor2); | 153 EXPECT_FALSE(sensor2); |
167 | 154 |
168 // Get Ambient Light sensor. | 155 // Get Ambient Light sensor. |
169 scoped_refptr<PlatformSensor> sensor3 = | 156 scoped_refptr<PlatformSensor> sensor3 = |
170 sensor_provider->GetSensor(SensorType::AMBIENT_LIGHT); | 157 sensor_provider->GetSensor(SensorType::AMBIENT_LIGHT); |
171 EXPECT_TRUE(sensor3); | 158 EXPECT_TRUE(sensor3); |
172 | 159 |
173 EXPECT_EQ(sensor1->GetType(), sensor3->GetType()); | 160 EXPECT_EQ(sensor1->GetType(), sensor3->GetType()); |
174 | |
175 // Try to create a sensor with zero buffer and offset. | |
176 TestSensorCreateCallback callback4; | |
177 sensor_provider->CreateSensor(SensorType::GYROSCOPE, 0, 0, | |
178 callback4.callback()); | |
179 scoped_refptr<PlatformSensor> sensor4 = callback4.WaitForResult(); | |
180 EXPECT_FALSE(sensor4); | |
181 | |
182 scoped_refptr<PlatformSensor> sensor5 = | |
183 sensor_provider->GetSensor(SensorType::GYROSCOPE); | |
184 EXPECT_FALSE(sensor5); | |
185 } | 161 } |
186 | 162 |
187 TEST_F(PlatformSensorProviderTest, TestSensorLeaks) { | 163 TEST_F(PlatformSensorProviderTest, TestSensorLeaks) { |
188 PlatformSensorProvider* sensor_provider = | 164 PlatformSensorProvider* sensor_provider = |
189 FakePlatformSensorProvider::GetInstance(); | 165 FakePlatformSensorProvider::GetInstance(); |
190 | 166 |
191 // Create Ambient Light sensor. | 167 // Create Ambient Light sensor. |
192 TestSensorCreateCallback callback1; | 168 TestSensorCreateCallback callback1; |
193 scoped_refptr<PlatformSensor> sensor1 = | 169 scoped_refptr<PlatformSensor> sensor1 = |
194 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1); | 170 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 sensor_client_->set_notification_suspended(false); | 344 sensor_client_->set_notification_suspended(false); |
369 EXPECT_FALSE(sensor_client_->IsNotificationSuspended()); | 345 EXPECT_FALSE(sensor_client_->IsNotificationSuspended()); |
370 for (const auto& client : clients) | 346 for (const auto& client : clients) |
371 EXPECT_FALSE(client->IsNotificationSuspended()); | 347 EXPECT_FALSE(client->IsNotificationSuspended()); |
372 | 348 |
373 fake_sensor->UpdateSensor(); | 349 fake_sensor->UpdateSensor(); |
374 EXPECT_TRUE(fake_sensor->started()); | 350 EXPECT_TRUE(fake_sensor->started()); |
375 } | 351 } |
376 | 352 |
377 } // namespace device | 353 } // namespace device |
OLD | NEW |