Commit a245fc49 authored by Raphael Kubo da Costa's avatar Raphael Kubo da Costa Committed by Commit Bot

PlatformSensorAndProviderLinuxTest: Do not hold a raw MockSensorDeviceManager pointer.

There is no need to pass a std::unique_ptr to PlatformSensorProviderLinux
and also hold the raw pointer in PlatformSensorAndProviderLinuxTest.
Instead, just pass the std::unique_ptr and access it directly, as the test
class is already a friend of PlatformSensorProviderLinux.

R=reillyg

Change-Id: I41624ccedb9dfd886f047d0ad9550c877966af3f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2218047
Auto-Submit: Raphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#772278}
parent b4ccaf46
...@@ -196,18 +196,18 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test { ...@@ -196,18 +196,18 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test {
public: public:
void SetUp() override { void SetUp() override {
provider_ = base::WrapUnique(new PlatformSensorProviderLinux); provider_ = base::WrapUnique(new PlatformSensorProviderLinux);
provider_->SetSensorDeviceManagerForTesting(MockSensorDeviceManager::Create(
auto manager = MockSensorDeviceManager::Create( provider_->weak_ptr_factory_.GetWeakPtr()));
provider_->weak_ptr_factory_.GetWeakPtr());
manager_ = manager.get();
provider_->SetSensorDeviceManagerForTesting(std::move(manager));
} }
void TearDown() override { void TearDown() override { base::RunLoop().RunUntilIdle(); }
base::RunLoop().RunUntilIdle();
}
protected: protected:
MockSensorDeviceManager* mock_sensor_device_manager() const {
return static_cast<MockSensorDeviceManager*>(
provider_->sensor_device_manager_.get());
}
// Sensor creation is asynchronous, therefore inner loop is used to wait for // Sensor creation is asynchronous, therefore inner loop is used to wait for
// PlatformSensorProvider::CreateSensorCallback completion. // PlatformSensorProvider::CreateSensorCallback completion.
scoped_refptr<PlatformSensor> CreateSensor(mojom::SensorType type) { scoped_refptr<PlatformSensor> CreateSensor(mojom::SensorType type) {
...@@ -237,7 +237,8 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test { ...@@ -237,7 +237,8 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test {
{ {
base::ScopedAllowBlockingForTesting allow_blocking; base::ScopedAllowBlockingForTesting allow_blocking;
const base::FilePath& sensor_dir = manager_->GetSensorsBasePath(); const base::FilePath& sensor_dir =
mock_sensor_device_manager()->GetSensorsBasePath();
if (!data.sensor_scale_name.empty() && scaling != 0) { if (!data.sensor_scale_name.empty() && scaling != 0) {
base::FilePath sensor_scale_file = base::FilePath sensor_scale_file =
base::FilePath(sensor_dir).Append(data.sensor_scale_name); base::FilePath(sensor_dir).Append(data.sensor_scale_name);
...@@ -275,9 +276,10 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test { ...@@ -275,9 +276,10 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test {
// Emulates device enumerations and initial udev events. Once all // Emulates device enumerations and initial udev events. Once all
// devices are added, tells manager its ready. // devices are added, tells manager its ready.
void SetServiceStart() { void SetServiceStart() {
EXPECT_CALL(*manager_, Start()).WillOnce(Invoke([this]() { auto* manager = mock_sensor_device_manager();
manager_->DeviceAdded(); EXPECT_CALL(*manager, Start()).WillOnce(Invoke([manager]() {
manager_->EnumerationReady(); manager->DeviceAdded();
manager->EnumerationReady();
})); }));
} }
...@@ -303,8 +305,9 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test { ...@@ -303,8 +305,9 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test {
// been added. // been added.
void GenerateDeviceAddedEvent() { void GenerateDeviceAddedEvent() {
bool success = provider_->blocking_task_runner_->PostTask( bool success = provider_->blocking_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&MockSensorDeviceManager::DeviceAdded, FROM_HERE,
base::Unretained(manager_))); base::BindOnce(&MockSensorDeviceManager::DeviceAdded,
base::Unretained(mock_sensor_device_manager())));
ASSERT_TRUE(success); ASSERT_TRUE(success);
// Make sure all tasks have been delivered (including SensorDeviceManager // Make sure all tasks have been delivered (including SensorDeviceManager
// notifying PlatformSensorProviderLinux of a device addition). // notifying PlatformSensorProviderLinux of a device addition).
...@@ -319,8 +322,9 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test { ...@@ -319,8 +322,9 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test {
DeleteFile(sensor_dir); DeleteFile(sensor_dir);
} }
bool success = provider_->blocking_task_runner_->PostTask( bool success = provider_->blocking_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&MockSensorDeviceManager::DeviceRemoved, FROM_HERE,
base::Unretained(manager_))); base::BindOnce(&MockSensorDeviceManager::DeviceRemoved,
base::Unretained(mock_sensor_device_manager())));
ASSERT_TRUE(success); ASSERT_TRUE(success);
// Make sure all tasks have been delivered (including SensorDeviceManager // Make sure all tasks have been delivered (including SensorDeviceManager
// notifying PlatformSensorProviderLinux of a device removal). // notifying PlatformSensorProviderLinux of a device removal).
...@@ -329,7 +333,6 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test { ...@@ -329,7 +333,6 @@ class PlatformSensorAndProviderLinuxTest : public ::testing::Test {
base::test::TaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_;
MockSensorDeviceManager* manager_;
std::unique_ptr<PlatformSensorProviderLinux> provider_; std::unique_ptr<PlatformSensorProviderLinux> provider_;
// Used to simulate the non-test scenario where we're running in an IO thread // Used to simulate the non-test scenario where we're running in an IO thread
...@@ -417,7 +420,8 @@ TEST_F(PlatformSensorAndProviderLinuxTest, SensorRemoved) { ...@@ -417,7 +420,8 @@ TEST_F(PlatformSensorAndProviderLinuxTest, SensorRemoved) {
std::make_unique<NiceMock<LinuxMockPlatformSensorClient>>(sensor); std::make_unique<NiceMock<LinuxMockPlatformSensorClient>>(sensor);
PlatformSensorConfiguration configuration(5); PlatformSensorConfiguration configuration(5);
EXPECT_TRUE(sensor->StartListening(client.get(), configuration)); EXPECT_TRUE(sensor->StartListening(client.get(), configuration));
GenerateDeviceRemovedEvent(manager_->GetSensorsBasePath()); GenerateDeviceRemovedEvent(
mock_sensor_device_manager()->GetSensorsBasePath());
WaitOnSensorErrorEvent(client.get()); WaitOnSensorErrorEvent(client.get());
} }
......
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