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

Side by Side Diff: device/generic_sensor/platform_sensor_provider_unittest.cc

Issue 2434073002: [sensors] PlatformSensorProvider implementations are notified when there are no sensor instances (Closed)
Patch Set: rebased Created 4 years, 1 month 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
« no previous file with comments | « device/generic_sensor/platform_sensor_provider_base.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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_TRUE(provider_->HasSensors());
172
173 EXPECT_CALL(*provider_, AllSensorsRemoved()).Times(1);
174
175 sensor1 = nullptr;
176 sensor2 = nullptr;
177
178 EXPECT_FALSE(provider_->HasSensors());
179 }
180
163 TEST_F(PlatformSensorProviderTest, TestSensorLeaks) { 181 TEST_F(PlatformSensorProviderTest, TestSensorLeaks) {
164 PlatformSensorProvider* sensor_provider =
165 FakePlatformSensorProvider::GetInstance();
166
167 // Create Ambient Light sensor. 182 // Create Ambient Light sensor.
168 TestSensorCreateCallback callback1; 183 TestSensorCreateCallback callback1;
169 scoped_refptr<PlatformSensor> sensor1 = 184 scoped_refptr<PlatformSensor> sensor1 =
170 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1); 185 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1);
171 EXPECT_TRUE(sensor1); 186 EXPECT_TRUE(sensor1);
172 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType()); 187 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType());
173 188
174 // Sensor should be automatically destroyed. 189 // Sensor should be automatically destroyed.
175 sensor1 = nullptr; 190 sensor1 = nullptr;
176 scoped_refptr<PlatformSensor> sensor2 = 191 scoped_refptr<PlatformSensor> sensor2 =
177 sensor_provider->GetSensor(SensorType::AMBIENT_LIGHT); 192 provider_->GetSensor(SensorType::AMBIENT_LIGHT);
178 EXPECT_FALSE(sensor2); 193 EXPECT_FALSE(sensor2);
179 } 194 }
180 195
181 // This test assumes that a mock sensor has a constant maximum frequency value 196 // 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 197 // 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 198 // 60) and tests whether a mock sensor can be started with a value range from 0
184 // to 60. 199 // to 60.
185 TEST_F(PlatformSensorProviderTest, StartListeningWithDifferentParameters) { 200 TEST_F(PlatformSensorProviderTest, StartListeningWithDifferentParameters) {
186 const double too_high_frequency = 60; 201 const double too_high_frequency = 60;
187 const double normal_frequency = 39; 202 const double normal_frequency = 39;
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 sensor_client_->set_notification_suspended(false); 359 sensor_client_->set_notification_suspended(false);
345 EXPECT_FALSE(sensor_client_->IsNotificationSuspended()); 360 EXPECT_FALSE(sensor_client_->IsNotificationSuspended());
346 for (const auto& client : clients) 361 for (const auto& client : clients)
347 EXPECT_FALSE(client->IsNotificationSuspended()); 362 EXPECT_FALSE(client->IsNotificationSuspended());
348 363
349 fake_sensor->UpdateSensor(); 364 fake_sensor->UpdateSensor();
350 EXPECT_TRUE(fake_sensor->started()); 365 EXPECT_TRUE(fake_sensor->started());
351 } 366 }
352 367
353 } // namespace device 368 } // namespace device
OLDNEW
« no previous file with comments | « device/generic_sensor/platform_sensor_provider_base.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698