blob: 81f384185fc1bb2ebaef2f95aec394cf1e3f6324 [file] [log] [blame]
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "driver/src/backlight_manager.h"
#include <fidl/fuchsia.component.runner/cpp/fidl.h>
#include <fidl/fuchsia.hardware.acpi/cpp/wire.h>
#include <lib/async-loop/cpp/loop.h>
#include <lib/async-loop/default.h>
#include <lib/driver/component/cpp/namespace.h>
#include <lib/component/incoming/cpp/service_client.h>
#include <gtest/gtest.h>
namespace backlight_manager_test {
class FakeACPIDevice final : public fidl::WireServer<fuchsia_hardware_acpi::Device> {
public:
void EvaluateObject(EvaluateObjectRequestView request, EvaluateObjectCompleter::Sync& completer) {
if (request->path.get() == backlight_driver::kLedWrite) {
current_value_ = request->parameters[0].integer_val();
completer.ReplySuccess(fuchsia_hardware_acpi::wire::EncodedObject());
} else if (request->path.get() == backlight_driver::kLedRead) {
fidl::Arena arena;
completer.ReplySuccess(fuchsia_hardware_acpi::wire::EncodedObject::WithObject(
arena, fuchsia_hardware_acpi::wire::Object::WithIntegerVal(arena, current_value_)));
} else {
completer.ReplyError(fuchsia_hardware_acpi::wire::Status::kOk);
}
}
void GetBusId(GetBusIdCompleter::Sync& completer) {}
void MapInterrupt(MapInterruptRequestView request, MapInterruptCompleter::Sync& completer) {}
void GetPio(GetPioRequestView request, GetPioCompleter::Sync& completer) {}
void GetMmio(GetMmioRequestView request, GetMmioCompleter::Sync& completer) {}
void GetBti(GetBtiRequestView request, GetBtiCompleter::Sync& completer) {}
void InstallNotifyHandler(InstallNotifyHandlerRequestView request,
InstallNotifyHandlerCompleter::Sync& completer) {}
void AcquireGlobalLock(AcquireGlobalLockCompleter::Sync& completer) {}
void InstallAddressSpaceHandler(InstallAddressSpaceHandlerRequestView request,
InstallAddressSpaceHandlerCompleter::Sync& completer) {}
void RemoveNotifyHandler(RemoveNotifyHandlerCompleter::Sync& completer) {}
void SetWakeDevice(SetWakeDeviceRequestView request, SetWakeDeviceCompleter::Sync& completer) {}
private:
uint64_t current_value_;
};
class BacklightManagerTest : public testing::Test {
public:
void SetUp() override {
auto endpoints = fidl::CreateEndpoints<fuchsia_hardware_acpi::Device>();
ASSERT_FALSE(endpoints.is_error());
fidl::BindServer(loop.dispatcher(), std::move(endpoints->server), &acpi_device);
zx::result ns = CreateDriverNamespaceWithLogSink();
ASSERT_FALSE(ns.is_error());
zx::result logger = driver::Logger::Create(*ns, loop.dispatcher(), "backlight_manager_test");
ASSERT_FALSE(logger.is_error());
logger_ = std::move(*logger);
backlight_manager_.emplace(logger_.get(), std::move(endpoints->client));
}
void TearDown() override {}
zx::result<driver::Namespace> CreateDriverNamespaceWithLogSink() {
std::vector<fuchsia_component_runner::ComponentNamespaceEntry> entries;
zx::result open_result = component::OpenServiceRoot();
if (!open_result.is_ok()) {
return open_result.take_error();
}
::fidl::ClientEnd<::fuchsia_io::Directory> svc = std::move(*open_result);
entries.emplace_back(fuchsia_component_runner::ComponentNamespaceEntry{{
.path = "/svc",
.directory = std::move(svc),
}});
return driver::Namespace::Create(entries);
}
async::Loop loop = async::Loop(&kAsyncLoopConfigNeverAttachToThread);
FakeACPIDevice acpi_device;
std::optional<backlight_driver::BacklightManager> backlight_manager_;
std::unique_ptr<driver::Logger> logger_;
};
TEST_F(BacklightManagerTest, TestSetAndGetBacklight) {
loop.StartThread("fidl-thread");
auto set_result = backlight_manager_->SetBacklight(50);
ASSERT_TRUE(set_result.is_ok());
auto get_result = backlight_manager_->GetBacklight();
ASSERT_TRUE(get_result.is_ok());
ASSERT_EQ(50, get_result.value());
set_result = backlight_manager_->SetBacklight(10);
ASSERT_TRUE(set_result.is_ok());
get_result = backlight_manager_->GetBacklight();
ASSERT_TRUE(get_result.is_ok());
ASSERT_EQ(10, get_result.value());
set_result = backlight_manager_->SetBacklight(0);
ASSERT_TRUE(set_result.is_ok());
get_result = backlight_manager_->GetBacklight();
ASSERT_TRUE(get_result.is_ok());
ASSERT_EQ(0, get_result.value());
set_result = backlight_manager_->SetBacklight(100);
ASSERT_TRUE(set_result.is_ok());
get_result = backlight_manager_->GetBacklight();
ASSERT_TRUE(get_result.is_ok());
ASSERT_EQ(100, get_result.value());
}
} // namespace backlight_manager_test