1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/sensors/ash/sensor_hal_dispatcher.h"
#include <memory>
#include <utility>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "chromeos/components/sensors/fake_sensor_hal_client.h"
#include "chromeos/components/sensors/fake_sensor_hal_server.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace sensors {
class SensorHalDispatcherTest : public ::testing::Test {
public:
SensorHalDispatcherTest() {}
SensorHalDispatcherTest(const SensorHalDispatcherTest&) = delete;
SensorHalDispatcherTest& operator=(const SensorHalDispatcherTest&) = delete;
~SensorHalDispatcherTest() override = default;
void SetUp() override {
SensorHalDispatcher::Initialize();
EXPECT_TRUE(SensorHalDispatcher::GetInstance());
}
void TearDown() override {
SensorHalDispatcher::Shutdown();
EXPECT_FALSE(SensorHalDispatcher::GetInstance());
}
private:
base::test::SingleThreadTaskEnvironment task_environment;
};
// Test that the SensorHalDisptcher correctly re-establishes a Mojo channel
// for the client when the server crashes.
TEST_F(SensorHalDispatcherTest, ServerConnectionError) {
// First verify that a the SensorHalDispatcher establishes a Mojo channel
// between the server and the client.
auto fake_server = std::make_unique<FakeSensorHalServer>();
auto fake_client = std::make_unique<FakeSensorHalClient>();
auto remote_server = fake_server->PassRemote();
auto remote_client = fake_client->PassRemote();
SensorHalDispatcher::GetInstance()->RegisterServer(std::move(remote_server));
SensorHalDispatcher::GetInstance()->RegisterClient(std::move(remote_client));
// Wait until the server and client get the established Mojo channel.
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_server->GetSensorService()->HasReceivers());
EXPECT_TRUE(fake_client->SensorServiceIsValid());
// Re-create a new server to simulate a server crash.
fake_server = std::make_unique<FakeSensorHalServer>();
fake_client->ResetSensorService();
// Wait until the dispatcher resets the server and client remotes.
base::RunLoop().RunUntilIdle();
remote_server = fake_server->PassRemote();
SensorHalDispatcher::GetInstance()->RegisterServer(std::move(remote_server));
// Make sure we re-create the Mojo channel from the new server to the same
// client.
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_server->GetSensorService()->HasReceivers());
EXPECT_TRUE(fake_client->SensorServiceIsValid());
}
// Test that the SensorHalDisptcher correctly re-establishes a Mojo channel
// for the client when the client reconnects after crash.
TEST_F(SensorHalDispatcherTest, ClientConnectionError) {
// First verify that a the SensorHalDispatcher establishes a Mojo channel
// between the server and the client.
auto fake_server = std::make_unique<FakeSensorHalServer>();
auto fake_client = std::make_unique<FakeSensorHalClient>();
auto remote_server = fake_server->PassRemote();
auto remote_client = fake_client->PassRemote();
SensorHalDispatcher::GetInstance()->RegisterServer(std::move(remote_server));
SensorHalDispatcher::GetInstance()->RegisterClient(std::move(remote_client));
// Wait until the server and client get the established Mojo channel.
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_server->GetSensorService()->HasReceivers());
EXPECT_TRUE(fake_client->SensorServiceIsValid());
// Re-create a new client to simulate a client crash.
fake_client = std::make_unique<FakeSensorHalClient>();
fake_server->GetSensorService()->ClearReceivers();
remote_client = fake_client->PassRemote();
SensorHalDispatcher::GetInstance()->RegisterClient(std::move(remote_client));
// Make sure we re-create the Mojo channel from the same server to the new
// client.
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_server->GetSensorService()->HasReceivers());
EXPECT_TRUE(fake_client->SensorServiceIsValid());
}
} // namespace sensors
} // namespace chromeos
|