Commit c679a2b8 authored by Harvey Yang's avatar Harvey Yang Committed by Commit Bot

generic_sensor: Pick sensor based on location in CrOS

Following the mechanism that ARC chooses sensors:
https://source.corp.google.com/master-arc-dev/vendor/google_arc/libs/sensor_hub/sensors.cpp;rcl=3907f008bf945792e9b58ef57dd919f0d6153a1f;l=597
Choose the richer set (lid or base) of accelerometer, gyrometer, and
magnetometer; prefer the ambient light sensor on the lid than the one on
the base.

BUG=b:172414302
TEST=unit tests and run on octopus

Change-Id: I4c716c927fcb10b93eaa33735bba0ed3933867a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2494523
Commit-Queue: Cheng-Hao Yang <chenghaoyang@chromium.org>
Reviewed-by: default avatarRaphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826181}
parent 495d0662
...@@ -53,6 +53,22 @@ base::Optional<mojom::SensorType> ConvertSensorType( ...@@ -53,6 +53,22 @@ base::Optional<mojom::SensorType> ConvertSensorType(
} }
} }
bool DeviceNeedsLocationWithTypes(const std::vector<mojom::SensorType>& types) {
for (auto type : types) {
switch (type) {
case mojom::SensorType::AMBIENT_LIGHT:
case mojom::SensorType::ACCELEROMETER:
case mojom::SensorType::GYROSCOPE:
case mojom::SensorType::MAGNETOMETER:
return true;
default:
break;
}
}
return false;
}
} // namespace } // namespace
PlatformSensorProviderChromeOS::PlatformSensorProviderChromeOS() { PlatformSensorProviderChromeOS::PlatformSensorProviderChromeOS() {
...@@ -136,6 +152,28 @@ bool PlatformSensorProviderChromeOS::IsSensorTypeAvailable( ...@@ -136,6 +152,28 @@ bool PlatformSensorProviderChromeOS::IsSensorTypeAvailable(
PlatformSensorProviderChromeOS::SensorData::SensorData() = default; PlatformSensorProviderChromeOS::SensorData::SensorData() = default;
PlatformSensorProviderChromeOS::SensorData::~SensorData() = default; PlatformSensorProviderChromeOS::SensorData::~SensorData() = default;
base::Optional<PlatformSensorProviderChromeOS::SensorLocation>
PlatformSensorProviderChromeOS::ParseLocation(
const base::Optional<std::string>& raw_location) {
if (!raw_location.has_value()) {
LOG(ERROR) << "No location attribute";
return base::nullopt;
}
// These locations must be listed in the same order as the SensorLocation
// enum.
const std::vector<std::string> location_strings = {
chromeos::sensors::mojom::kLocationBase,
chromeos::sensors::mojom::kLocationLid,
chromeos::sensors::mojom::kLocationCamera};
const auto it = base::ranges::find(location_strings, raw_location.value());
if (it == std::end(location_strings))
return base::nullopt;
return static_cast<SensorLocation>(
std::distance(std::begin(location_strings), it));
}
base::Optional<int32_t> PlatformSensorProviderChromeOS::GetDeviceId( base::Optional<int32_t> PlatformSensorProviderChromeOS::GetDeviceId(
mojom::SensorType type) const { mojom::SensorType type) const {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
...@@ -206,12 +244,28 @@ void PlatformSensorProviderChromeOS::GetAllDeviceIdsCallback( ...@@ -206,12 +244,28 @@ void PlatformSensorProviderChromeOS::GetAllDeviceIdsCallback(
if (sensor.ignored) if (sensor.ignored)
continue; continue;
sensor.types = id_types.second; for (const auto& device_type : id_types.second) {
auto type_opt = ConvertSensorType(device_type);
if (!type_opt.has_value())
continue;
sensor.types.push_back(type_opt.value());
}
if (sensor.types.empty()) {
sensor.ignored = true;
continue;
}
sensor.remote.reset(); sensor.remote.reset();
std::vector<std::string> attr_names; std::vector<std::string> attr_names;
if (!sensor.scale.has_value()) if (!sensor.scale.has_value())
attr_names.push_back(chromeos::sensors::mojom::kScale); attr_names.push_back(chromeos::sensors::mojom::kScale);
if (DeviceNeedsLocationWithTypes(sensor.types) &&
!sensor.location.has_value()) {
attr_names.push_back(chromeos::sensors::mojom::kLocation);
}
if (attr_names.empty()) if (attr_names.empty())
continue; continue;
...@@ -267,6 +321,25 @@ void PlatformSensorProviderChromeOS::GetAttributesCallback( ...@@ -267,6 +321,25 @@ void PlatformSensorProviderChromeOS::GetAttributesCallback(
++index; ++index;
} }
if (DeviceNeedsLocationWithTypes(sensor.types) &&
!sensor.location.has_value()) {
if (index >= values.size()) {
LOG(ERROR) << "values doesn't contain location attribute.";
IgnoreSensor(sensor);
return;
}
sensor.location = ParseLocation(values[index]);
if (!sensor.location.has_value()) {
LOG(ERROR) << "Failed to parse location: " << values[index].value_or("")
<< ", with sensor id: " << id;
IgnoreSensor(sensor);
return;
}
++index;
}
ProcessSensorsIfPossible(); ProcessSensorsIfPossible();
} }
...@@ -286,7 +359,10 @@ bool PlatformSensorProviderChromeOS::AreAllSensorsReady() const { ...@@ -286,7 +359,10 @@ bool PlatformSensorProviderChromeOS::AreAllSensorsReady() const {
return false; return false;
return base::ranges::all_of(sensors_, [](const auto& sensor) { return base::ranges::all_of(sensors_, [](const auto& sensor) {
return sensor.second.ignored || sensor.second.scale.has_value(); return sensor.second.ignored ||
(sensor.second.scale.has_value() &&
(!DeviceNeedsLocationWithTypes(sensor.second.types) ||
sensor.second.location.has_value()));
}); });
} }
...@@ -300,26 +376,81 @@ void PlatformSensorProviderChromeOS::ProcessSensorsIfPossible() { ...@@ -300,26 +376,81 @@ void PlatformSensorProviderChromeOS::ProcessSensorsIfPossible() {
if (!AreAllSensorsReady()) if (!AreAllSensorsReady())
return; return;
DetermineSensorsByType(); DetermineMotionSensors();
DetermineLightSensor();
RemoveUnusedSensorDeviceRemotes(); RemoveUnusedSensorDeviceRemotes();
ProcessStoredRequests(); ProcessStoredRequests();
} }
void PlatformSensorProviderChromeOS::DetermineSensorsByType() { // Follow Android's and W3C's requirements of motion sensors:
// Android: https://source.android.com/devices/sensors/sensor-types
// W3C: https://w3c.github.io/sensors/#local-coordinate-system
// To implement fusion/composite sensors, accelerometer, gyroscope (and
// magnetometer) must be in the same plane, so when it comes to choosing an
// accelerometer in a convertible device, we choose the one which is in the
// same place as the gyroscope. If we still have a choice, we use the one in
// the lid.
void PlatformSensorProviderChromeOS::DetermineMotionSensors() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
struct MotionSensorInfo {
size_t count;
std::vector<std::pair<int32_t, mojom::SensorType>> sensor_info_pairs;
};
std::map<SensorLocation, MotionSensorInfo> motion_sensor_location_info;
for (const auto& sensor : sensors_) { for (const auto& sensor : sensors_) {
if (sensor.second.ignored) if (sensor.second.ignored || !sensor.second.location)
continue; continue;
int32_t id = sensor.first; SensorLocation location = sensor.second.location.value();
for (const auto& device_type : sensor.second.types) { DCHECK_LT(location, SensorLocation::kMax);
const auto type = ConvertSensorType(device_type);
if (!type.has_value()) for (auto type : sensor.second.types) {
continue; switch (type) {
case mojom::SensorType::ACCELEROMETER:
base::TryEmplace(sensor_id_by_type_, type.value(), id); case mojom::SensorType::GYROSCOPE:
case mojom::SensorType::MAGNETOMETER: {
auto& motion_sensor_info = motion_sensor_location_info[location];
motion_sensor_info.count++;
motion_sensor_info.sensor_info_pairs.push_back(
std::make_pair(sensor.first, type));
break;
}
default:
break;
}
} }
} }
const auto preferred_location =
motion_sensor_location_info[SensorLocation::kLid].count >=
motion_sensor_location_info[SensorLocation::kBase].count
? SensorLocation::kLid
: SensorLocation::kBase;
const auto& sensor_info_pairs =
motion_sensor_location_info[preferred_location].sensor_info_pairs;
for (const auto& pair : sensor_info_pairs)
sensor_id_by_type_[pair.second] = pair.first;
}
// Prefer the light sensor on the lid, as it's more meaningful to web API users.
void PlatformSensorProviderChromeOS::DetermineLightSensor() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
base::Optional<int32_t> id = base::nullopt;
for (const auto& sensor : sensors_) {
if (sensor.second.ignored ||
!base::Contains(sensor.second.types, mojom::SensorType::AMBIENT_LIGHT))
continue;
if (!id.has_value() || sensor.second.location == SensorLocation::kLid)
id = sensor.first;
}
if (id.has_value())
sensor_id_by_type_[mojom::SensorType::AMBIENT_LIGHT] = id.value();
} }
void PlatformSensorProviderChromeOS::RemoveUnusedSensorDeviceRemotes() { void PlatformSensorProviderChromeOS::RemoveUnusedSensorDeviceRemotes() {
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include "base/containers/flat_map.h" #include "base/containers/flat_map.h"
#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/optional.h" #include "base/optional.h"
#include "chromeos/components/sensors/mojom/cros_sensor_service.mojom.h" #include "chromeos/components/sensors/mojom/cros_sensor_service.mojom.h"
...@@ -47,6 +48,13 @@ class PlatformSensorProviderChromeOS ...@@ -47,6 +48,13 @@ class PlatformSensorProviderChromeOS
private: private:
friend class PlatformSensorProviderChromeOSTest; friend class PlatformSensorProviderChromeOSTest;
enum class SensorLocation {
kBase = 0,
kLid,
kCamera,
kMax,
};
using SensorIdTypesMap = using SensorIdTypesMap =
base::flat_map<int32_t, base::flat_map<int32_t,
std::vector<chromeos::sensors::mojom::DeviceType>>; std::vector<chromeos::sensors::mojom::DeviceType>>;
...@@ -55,8 +63,9 @@ class PlatformSensorProviderChromeOS ...@@ -55,8 +63,9 @@ class PlatformSensorProviderChromeOS
SensorData(); SensorData();
~SensorData(); ~SensorData();
std::vector<chromeos::sensors::mojom::DeviceType> types; std::vector<mojom::SensorType> types;
bool ignored = false; bool ignored = false;
base::Optional<SensorLocation> location;
base::Optional<double> scale; base::Optional<double> scale;
// Temporarily stores the remote, waiting for its attributes information. // Temporarily stores the remote, waiting for its attributes information.
...@@ -65,6 +74,9 @@ class PlatformSensorProviderChromeOS ...@@ -65,6 +74,9 @@ class PlatformSensorProviderChromeOS
mojo::Remote<chromeos::sensors::mojom::SensorDevice> remote; mojo::Remote<chromeos::sensors::mojom::SensorDevice> remote;
}; };
base::Optional<SensorLocation> ParseLocation(
const base::Optional<std::string>& location);
base::Optional<int32_t> GetDeviceId(mojom::SensorType type) const; base::Optional<int32_t> GetDeviceId(mojom::SensorType type) const;
void RegisterSensorClient(); void RegisterSensorClient();
...@@ -83,7 +95,10 @@ class PlatformSensorProviderChromeOS ...@@ -83,7 +95,10 @@ class PlatformSensorProviderChromeOS
void OnSensorDeviceDisconnect(int32_t id); void OnSensorDeviceDisconnect(int32_t id);
void ProcessSensorsIfPossible(); void ProcessSensorsIfPossible();
void DetermineSensorsByType();
void DetermineMotionSensors();
void DetermineLightSensor();
// Remove Mojo remotes of the unused devices, as they'll never be used. // Remove Mojo remotes of the unused devices, as they'll never be used.
void RemoveUnusedSensorDeviceRemotes(); void RemoveUnusedSensorDeviceRemotes();
void ProcessStoredRequests(); void ProcessStoredRequests();
...@@ -108,6 +123,9 @@ class PlatformSensorProviderChromeOS ...@@ -108,6 +123,9 @@ class PlatformSensorProviderChromeOS
std::map<mojom::SensorType, int32_t> sensor_id_by_type_; std::map<mojom::SensorType, int32_t> sensor_id_by_type_;
base::WeakPtrFactory<PlatformSensorProviderChromeOS> weak_ptr_factory_{this}; base::WeakPtrFactory<PlatformSensorProviderChromeOS> weak_ptr_factory_{this};
FRIEND_TEST_ALL_PREFIXES(PlatformSensorProviderChromeOSTest,
CheckUnsupportedTypes);
}; };
} // namespace device } // namespace device
......
...@@ -27,6 +27,8 @@ constexpr int kFakeDeviceId = 1; ...@@ -27,6 +27,8 @@ constexpr int kFakeDeviceId = 1;
constexpr double kScaleValue = 10.0; constexpr double kScaleValue = 10.0;
constexpr char kWrongScale[] = "10..0"; constexpr char kWrongScale[] = "10..0";
constexpr char kWrongLocation[] = "basee";
} // namespace } // namespace
class PlatformSensorProviderChromeOSTest : public ::testing::Test { class PlatformSensorProviderChromeOSTest : public ::testing::Test {
...@@ -45,15 +47,17 @@ class PlatformSensorProviderChromeOSTest : public ::testing::Test { ...@@ -45,15 +47,17 @@ class PlatformSensorProviderChromeOSTest : public ::testing::Test {
void AddDevice(int32_t iio_device_id, void AddDevice(int32_t iio_device_id,
chromeos::sensors::mojom::DeviceType type, chromeos::sensors::mojom::DeviceType type,
const base::Optional<std::string>& scale) { const base::Optional<std::string>& scale,
const base::Optional<std::string>& location) {
AddDevice(iio_device_id, AddDevice(iio_device_id,
std::set<chromeos::sensors::mojom::DeviceType>{type}, std::set<chromeos::sensors::mojom::DeviceType>{type},
std::move(scale)); std::move(scale), std::move(location));
} }
void AddDevice(int32_t iio_device_id, void AddDevice(int32_t iio_device_id,
std::set<chromeos::sensors::mojom::DeviceType> types, std::set<chromeos::sensors::mojom::DeviceType> types,
const base::Optional<std::string>& scale) { const base::Optional<std::string>& scale,
const base::Optional<std::string>& location) {
auto sensor_device = std::make_unique<chromeos::sensors::FakeSensorDevice>( auto sensor_device = std::make_unique<chromeos::sensors::FakeSensorDevice>(
std::vector<chromeos::sensors::FakeSensorDevice::ChannelData>{}); std::vector<chromeos::sensors::FakeSensorDevice::ChannelData>{});
...@@ -61,6 +65,10 @@ class PlatformSensorProviderChromeOSTest : public ::testing::Test { ...@@ -61,6 +65,10 @@ class PlatformSensorProviderChromeOSTest : public ::testing::Test {
sensor_device->SetAttribute(chromeos::sensors::mojom::kScale, sensor_device->SetAttribute(chromeos::sensors::mojom::kScale,
scale.value()); scale.value());
} }
if (location.has_value()) {
sensor_device->SetAttribute(chromeos::sensors::mojom::kLocation,
location.value());
}
sensor_devices_.push_back(sensor_device.get()); sensor_devices_.push_back(sensor_device.get());
...@@ -106,9 +114,57 @@ class PlatformSensorProviderChromeOSTest : public ::testing::Test { ...@@ -106,9 +114,57 @@ class PlatformSensorProviderChromeOSTest : public ::testing::Test {
base::test::SingleThreadTaskEnvironment task_environment_; base::test::SingleThreadTaskEnvironment task_environment_;
}; };
TEST_F(PlatformSensorProviderChromeOSTest, CheckUnsupportedTypes) {
int fake_id = 1;
// Containing at least one supported device type.
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
AddDevice(fake_id++,
std::set<chromeos::sensors::mojom::DeviceType>{
chromeos::sensors::mojom::DeviceType::ANGLVEL,
chromeos::sensors::mojom::DeviceType::COUNT},
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
// All device types are unsupported.
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::COUNT,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ANGL,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
AddDevice(fake_id++,
std::set<chromeos::sensors::mojom::DeviceType>{
chromeos::sensors::mojom::DeviceType::ANGL,
chromeos::sensors::mojom::DeviceType::BARO},
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection();
EXPECT_TRUE(CreateSensor(mojom::SensorType::ACCELEROMETER));
EXPECT_FALSE(provider_->sensors_[1].ignored);
EXPECT_FALSE(provider_->sensors_[2].ignored);
EXPECT_TRUE(provider_->sensors_[3].ignored);
EXPECT_TRUE(provider_->sensors_[4].ignored);
EXPECT_TRUE(provider_->sensors_[5].ignored);
}
TEST_F(PlatformSensorProviderChromeOSTest, MissingScale) { TEST_F(PlatformSensorProviderChromeOSTest, MissingScale) {
AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL, AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL,
/*scale=*/base::nullopt); /*scale=*/base::nullopt, chromeos::sensors::mojom::kLocationBase);
StartConnection();
EXPECT_FALSE(CreateSensor(mojom::SensorType::ACCELEROMETER));
}
TEST_F(PlatformSensorProviderChromeOSTest, MissingLocation) {
AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue),
/*location=*/base::nullopt);
StartConnection(); StartConnection();
...@@ -117,57 +173,169 @@ TEST_F(PlatformSensorProviderChromeOSTest, MissingScale) { ...@@ -117,57 +173,169 @@ TEST_F(PlatformSensorProviderChromeOSTest, MissingScale) {
TEST_F(PlatformSensorProviderChromeOSTest, WrongScale) { TEST_F(PlatformSensorProviderChromeOSTest, WrongScale) {
AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL, AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL,
kWrongScale); kWrongScale, chromeos::sensors::mojom::kLocationBase);
StartConnection(); StartConnection();
EXPECT_FALSE(CreateSensor(mojom::SensorType::ACCELEROMETER)); EXPECT_FALSE(CreateSensor(mojom::SensorType::ACCELEROMETER));
} }
TEST_F(PlatformSensorProviderChromeOSTest, ChooseFirstDeviceInTheSameType) { TEST_F(PlatformSensorProviderChromeOSTest, WrongLocation) {
int fake_id = 1; AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue), kWrongLocation);
StartConnection();
EXPECT_FALSE(CreateSensor(mojom::SensorType::ACCELEROMETER));
}
TEST_F(PlatformSensorProviderChromeOSTest, CheckMainLocationBase) {
int fake_id = 1;
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL, AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
// Will not be used. // Will not be used.
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL, AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationLid);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ANGLVEL, AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ANGLVEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
// Will not be used. chromeos::sensors::mojom::kLocationBase);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::MAGN,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection();
EXPECT_TRUE(CreateSensor(mojom::SensorType::GYROSCOPE));
// Wait until the disconnect of the gyroscope arrives at FakeSensorDevice.
base::RunLoop().RunUntilIdle();
// Remote stored in |provider_|.
EXPECT_TRUE(sensor_devices_[0]->HasReceivers());
EXPECT_TRUE(sensor_devices_[3]->HasReceivers());
// Removed in |provider_| as it'll never be used.
EXPECT_FALSE(sensor_devices_[1]->HasReceivers());
}
TEST_F(PlatformSensorProviderChromeOSTest, CheckMainLocationLid) {
int fake_id = 1;
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationLid);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ANGLVEL,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ANGLVEL, AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ANGLVEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationLid);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::MAGN,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationCamera);
StartConnection(); StartConnection();
// Wait until the disconnects of the second accelerometer and gyroscope arrive // Wait until the disconnect of the first gyroscope arrives at
// at FakeSensorDevice. // FakeSensorDevice.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
// Removed in |provider_| as they'll never be used. // Removed in |provider_| as they'll never be used.
EXPECT_FALSE(sensor_devices_[1]->HasReceivers()); EXPECT_FALSE(sensor_devices_[0]->HasReceivers());
EXPECT_FALSE(sensor_devices_[3]->HasReceivers()); EXPECT_FALSE(sensor_devices_[2]->HasReceivers());
EXPECT_FALSE(sensor_devices_[4]->HasReceivers());
// Remote stored in |provider_|.
EXPECT_TRUE(sensor_devices_[1]->HasReceivers());
EXPECT_TRUE(sensor_devices_[3]->HasReceivers());
}
TEST_F(PlatformSensorProviderChromeOSTest,
CheckMainLocationLidWithMultitypeSensor) {
int fake_id = 1;
AddDevice(fake_id++,
std::set<chromeos::sensors::mojom::DeviceType>{
chromeos::sensors::mojom::DeviceType::ACCEL,
chromeos::sensors::mojom::DeviceType::ANGLVEL},
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationLid);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ANGLVEL,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection();
// Wait until the disconnect of the gyroscope arrives at FakeSensorDevice.
base::RunLoop().RunUntilIdle();
// Remote stored in |provider_|. // Remote stored in |provider_|.
EXPECT_TRUE(sensor_devices_[0]->HasReceivers()); EXPECT_TRUE(sensor_devices_[0]->HasReceivers());
EXPECT_TRUE(sensor_devices_[2]->HasReceivers()); // Removed in |provider_| as it'll never be used.
EXPECT_FALSE(sensor_devices_[1]->HasReceivers());
EXPECT_TRUE(CreateSensor(mojom::SensorType::ACCELEROMETER));
EXPECT_TRUE(CreateSensor(mojom::SensorType::GYROSCOPE));
}
TEST_F(PlatformSensorProviderChromeOSTest, CheckAmbientLightSensorLocationLid) {
int fake_id = 1;
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::LIGHT,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::LIGHT,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationLid);
StartConnection();
// Wait until the disconnect of the first ambient light sensor arrives at
// FakeSensorDevice.
base::RunLoop().RunUntilIdle();
// Removed in |provider_| as it'll never be used.
EXPECT_FALSE(sensor_devices_[0]->HasReceivers());
// Remote stored in |provider_|.
EXPECT_TRUE(sensor_devices_[1]->HasReceivers());
}
TEST_F(PlatformSensorProviderChromeOSTest,
CheckAmbientLightSensorLocationBase) {
AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::LIGHT,
base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection();
EXPECT_TRUE(CreateSensor(mojom::SensorType::AMBIENT_LIGHT));
} }
TEST_F(PlatformSensorProviderChromeOSTest, Reconnect) { TEST_F(PlatformSensorProviderChromeOSTest, Reconnect) {
int fake_id = 1; int fake_id = 1;
// Will not be used.
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL, AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ANGLVEL, AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ANGLVEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationLid);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::LIGHT, AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::LIGHT,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection(); StartConnection();
EXPECT_TRUE(CreateSensor(mojom::SensorType::ACCELEROMETER)); EXPECT_FALSE(CreateSensor(mojom::SensorType::ACCELEROMETER));
// Simulate a disconnection between |provider_| and the dispatcher. // Simulate a disconnection between |provider_| and the dispatcher.
ResetClient(); ResetClient();
...@@ -197,7 +365,8 @@ TEST_F(PlatformSensorProviderChromeOSTest, ...@@ -197,7 +365,8 @@ TEST_F(PlatformSensorProviderChromeOSTest,
TEST_F(PlatformSensorProviderChromeOSTest, CheckLinearAcceleration) { TEST_F(PlatformSensorProviderChromeOSTest, CheckLinearAcceleration) {
AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL, AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection(); StartConnection();
...@@ -218,7 +387,8 @@ TEST_F( ...@@ -218,7 +387,8 @@ TEST_F(
TEST_F(PlatformSensorProviderChromeOSTest, TEST_F(PlatformSensorProviderChromeOSTest,
CheckAbsoluteOrientationSensorNotCreatedIfNoAccelerometer) { CheckAbsoluteOrientationSensorNotCreatedIfNoAccelerometer) {
AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::MAGN, AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::MAGN,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection(); StartConnection();
...@@ -231,7 +401,8 @@ TEST_F(PlatformSensorProviderChromeOSTest, ...@@ -231,7 +401,8 @@ TEST_F(PlatformSensorProviderChromeOSTest,
TEST_F(PlatformSensorProviderChromeOSTest, TEST_F(PlatformSensorProviderChromeOSTest,
CheckAbsoluteOrientationSensorNotCreatedIfNoMagnetometer) { CheckAbsoluteOrientationSensorNotCreatedIfNoMagnetometer) {
AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL, AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection(); StartConnection();
...@@ -245,9 +416,11 @@ TEST_F(PlatformSensorProviderChromeOSTest, CheckAbsoluteOrientationSensors) { ...@@ -245,9 +416,11 @@ TEST_F(PlatformSensorProviderChromeOSTest, CheckAbsoluteOrientationSensors) {
int fake_id = 1; int fake_id = 1;
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL, AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::MAGN, AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::MAGN,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection(); StartConnection();
...@@ -270,7 +443,8 @@ TEST_F( ...@@ -270,7 +443,8 @@ TEST_F(
TEST_F(PlatformSensorProviderChromeOSTest, TEST_F(PlatformSensorProviderChromeOSTest,
CheckRelativeOrientationSensorNotCreatedIfNoAccelerometer) { CheckRelativeOrientationSensorNotCreatedIfNoAccelerometer) {
AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ANGLVEL, AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ANGLVEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection(); StartConnection();
...@@ -283,7 +457,8 @@ TEST_F(PlatformSensorProviderChromeOSTest, ...@@ -283,7 +457,8 @@ TEST_F(PlatformSensorProviderChromeOSTest,
TEST_F(PlatformSensorProviderChromeOSTest, TEST_F(PlatformSensorProviderChromeOSTest,
CheckRelativeOrientationSensorUsingAccelerometer) { CheckRelativeOrientationSensorUsingAccelerometer) {
AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL, AddDevice(kFakeDeviceId, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection(); StartConnection();
...@@ -296,9 +471,11 @@ TEST_F(PlatformSensorProviderChromeOSTest, ...@@ -296,9 +471,11 @@ TEST_F(PlatformSensorProviderChromeOSTest,
CheckRelativeOrientationSensorUsingAccelerometerAndGyroscope) { CheckRelativeOrientationSensorUsingAccelerometerAndGyroscope) {
int fake_id = 1; int fake_id = 1;
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ANGLVEL, AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ANGLVEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL, AddDevice(fake_id++, chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kScaleValue)); base::NumberToString(kScaleValue),
chromeos::sensors::mojom::kLocationBase);
StartConnection(); StartConnection();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment