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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
// 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 "chrome/browser/ash/chromebox_for_meetings/diagnostics/diagnostics_service.h"
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/bind.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/dbus/chromebox_for_meetings/fake_cfm_hotline_client.h"
#include "chromeos/ash/components/mojo_service_manager/fake_mojo_service_manager.h"
#include "chromeos/ash/services/cros_healthd/public/cpp/fake_cros_healthd.h"
#include "chromeos/ash/services/cros_healthd/public/cpp/service_connection.h"
#include "chromeos/services/chromebox_for_meetings/public/cpp/fake_service_connection.h"
#include "chromeos/services/chromebox_for_meetings/public/cpp/fake_service_context.h"
#include "chromeos/services/chromebox_for_meetings/public/cpp/service_connection.h"
#include "chromeos/services/chromebox_for_meetings/public/mojom/cfm_service_manager.mojom.h"
#include "chromeos/services/chromebox_for_meetings/public/mojom/meet_devices_diagnostics.mojom.h"
#include "content/public/test/test_utils.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace ash::cfm {
namespace {
namespace mojom = ::chromeos::cfm::mojom;
class CfmDiagnosticsServiceTest : public ::testing::Test {
public:
CfmDiagnosticsServiceTest() = default;
CfmDiagnosticsServiceTest(const CfmDiagnosticsServiceTest&) = delete;
CfmDiagnosticsServiceTest& operator=(const CfmDiagnosticsServiceTest&) =
delete;
void SetUp() override {
cros_healthd::FakeCrosHealthd::Initialize();
CfmHotlineClient::InitializeFake();
chromeos::cfm::ServiceConnection::UseFakeServiceConnectionForTesting(
&fake_service_connection_);
DiagnosticsService::Initialize();
}
void TearDown() override {
DiagnosticsService::Shutdown();
CfmHotlineClient::Shutdown();
cros_healthd::FakeCrosHealthd::Shutdown();
}
FakeCfmHotlineClient* GetClient() {
return static_cast<FakeCfmHotlineClient*>(CfmHotlineClient::Get());
}
// Returns a mojo::Remote for the mojom::MeetDevicesDiagnostics by faking the
// way the cfm mojom binder daemon would request it through chrome.
const mojo::Remote<mojom::MeetDevicesDiagnostics>& GetDiagnosticsRemote() {
if (diagnostics_remote_.is_bound()) {
return diagnostics_remote_;
}
// if there is no valid remote create one
auto* interface_name = mojom::MeetDevicesDiagnostics::Name_;
base::RunLoop run_loop;
// Fake out CfmServiceContext
fake_service_connection_.SetCallback(base::BindLambdaForTesting(
[&](mojo::PendingReceiver<mojom::CfmServiceContext> pending_receiver,
bool success) {
ASSERT_TRUE(success);
context_receiver_set_.Add(&context_, std::move(pending_receiver));
}));
context_.SetFakeProvideAdaptorCallback(base::BindLambdaForTesting(
[&](const std::string& service_id,
mojo::PendingRemote<mojom::CfmServiceAdaptor>
pending_adaptor_remote,
mojom::CfmServiceContext::ProvideAdaptorCallback callback) {
ASSERT_EQ(interface_name, service_id);
adaptor_remote_.Bind(std::move(pending_adaptor_remote));
std::move(callback).Run(true);
}));
EXPECT_TRUE(GetClient()->FakeEmitSignal(interface_name));
run_loop.RunUntilIdle();
EXPECT_TRUE(adaptor_remote_.is_connected());
adaptor_remote_->OnBindService(
diagnostics_remote_.BindNewPipeAndPassReceiver().PassPipe());
EXPECT_TRUE(diagnostics_remote_.is_connected());
return diagnostics_remote_;
}
protected:
chromeos::cfm::FakeCfmServiceContext context_;
mojo::Remote<mojom::MeetDevicesDiagnostics> diagnostics_remote_;
mojo::ReceiverSet<mojom::CfmServiceContext> context_receiver_set_;
mojo::Remote<mojom::CfmServiceAdaptor> adaptor_remote_;
chromeos::cfm::FakeServiceConnectionImpl fake_service_connection_;
base::test::SingleThreadTaskEnvironment task_environment_;
::ash::mojo_service_manager::FakeMojoServiceManager fake_service_manager_;
};
// This test ensures that the DiagnosticsInfoService is discoverable by its
// mojom name by sending a signal received by CfmHotlineClient.
TEST_F(CfmDiagnosticsServiceTest, InfoServiceAvailable) {
ASSERT_TRUE(
GetClient()->FakeEmitSignal(mojom::MeetDevicesDiagnostics::Name_));
}
// This test ensures that the CfmDeviceInfoService correctly registers itself
// for discovery by the cfm mojom binder daemon and correctly returns a
// working mojom remote.
TEST_F(CfmDiagnosticsServiceTest, GetDiagnosticsRemote) {
ASSERT_TRUE(GetDiagnosticsRemote().is_connected());
}
TEST_F(CfmDiagnosticsServiceTest, GetDeviceInfoService) {
const auto& details_remote = GetDiagnosticsRemote();
ASSERT_TRUE(details_remote.is_connected());
}
// This test ensure that the diagnostics service can retrieve telemetry
// information from cros_healthd.
TEST_F(CfmDiagnosticsServiceTest, GetCrosHealthdTelemetry) {
auto response = cros_healthd::mojom::TelemetryInfo::New();
cros_healthd::FakeCrosHealthd::Get()->SetProbeTelemetryInfoResponseForTesting(
response);
base::RunLoop run_loop;
DiagnosticsService::Get()->GetCrosHealthdTelemetry(base::BindLambdaForTesting(
[&](cros_healthd::mojom::TelemetryInfoPtr info) {
EXPECT_EQ(info, response);
run_loop.Quit();
}));
run_loop.Run();
}
// This test ensure that the diagnostics service can retrieve process-specific
// information from cros_healthd.
TEST_F(CfmDiagnosticsServiceTest, GetCrosHealthdProcessInfo) {
auto response = cros_healthd::mojom::ProcessResult::NewProcessInfo(
cros_healthd::mojom::ProcessInfo::New());
cros_healthd::FakeCrosHealthd::Get()->SetProbeProcessInfoResponseForTesting(
response);
base::RunLoop run_loop;
DiagnosticsService::Get()->GetCrosHealthdProcessInfo(
/*pid=*/10, base::BindLambdaForTesting(
[&](cros_healthd::mojom::ProcessResultPtr info) {
EXPECT_EQ(info, response);
run_loop.Quit();
}));
run_loop.Run();
}
} // namespace
} // namespace ash::cfm
|