// SPDX-License-Identifier: GPL-3.0-or-later // Copyright 2019 Juan Palacios #include #include #include "common/commandqueuestub.h" #include "common/stringdatasourcestub.h" #include "core/components/controls/amd/pm/advanced/powerprofile/pmpowerprofile.h" #include "core/idatasource.h" extern template struct trompeloeil::reporter; namespace Tests::AMD::PMPowerProfile { class PMPowerProfileTestAdapter : public ::AMD::PMPowerProfile { public: using ::AMD::PMPowerProfile::PMPowerProfile; using PMPowerProfile::cleanControl; using PMPowerProfile::exportControl; using PMPowerProfile::importControl; using PMPowerProfile::mode; using PMPowerProfile::modes; using PMPowerProfile::syncControl; }; class PMPowerProfileImporterStub : public ::AMD::PMPowerProfile::Importer { public: PMPowerProfileImporterStub(std::string mode) : mode_(std::move(mode)) { } std::optional> provideImporter(Item const &) override { return {}; } bool provideActive() const override { return false; } std::string const &providePMPowerProfileMode() const override { return mode_; } private: std::string mode_; }; class PMPowerProfileExporterMock : public ::AMD::PMPowerProfile::Exporter { public: MAKE_MOCK1(takePMPowerProfileModes, void(std::vector const &), override); MAKE_MOCK1(takePMPowerProfileMode, void(std::string const &), override); MAKE_MOCK1(takeActive, void(bool), override); MAKE_MOCK1( provideExporter, std::optional>(Item const &), override); }; class OptionalIntDataSourceStub : public IDataSource> { public: OptionalIntDataSourceStub(std::string_view source = "", std::optional data = std::nullopt, bool success = true) noexcept : source_(source) , data_(data) , success_(success) { } std::string source() const override { return source_; } bool read(std::optional &data) override { data = data_; return success_; } std::string const source_; std::optional const data_; bool success_; }; TEST_CASE("AMD PMPowerProfile tests", "[GPU][AMD][PM][PMAdvanced][PMPowerProfile]") { CommandQueueStub ctlCmds; std::vector> modes{ std::make_pair("3D_FULL_SCREEN", 0), std::make_pair("POWER_SAVING", 1)}; SECTION("Has PMPowerProfile ID") { PMPowerProfileTestAdapter ts(std::make_unique(), std::make_unique(), modes); REQUIRE(ts.ID() == ::AMD::PMPowerProfile::ItemID); } SECTION("Is active by default") { PMPowerProfileTestAdapter ts(std::make_unique(), std::make_unique(), modes); REQUIRE(ts.active()); } SECTION("Has first mode selected by default") { PMPowerProfileTestAdapter ts( std::make_unique(), std::make_unique("pp_power_profile_mode", std::make_optional(0)), modes); REQUIRE(ts.mode() == "3D_FULL_SCREEN"); } SECTION("mode ignores unknown modes") { PMPowerProfileTestAdapter ts( std::make_unique(), std::make_unique("pp_power_profile_mode", std::make_optional(0)), modes); ts.mode("UNKNOWN"); REQUIRE(ts.mode() == "3D_FULL_SCREEN"); } SECTION("Does not generate pre-init control commands") { PMPowerProfileTestAdapter ts(std::make_unique(), std::make_unique(), modes); ts.preInit(ctlCmds); REQUIRE(ctlCmds.commands().empty()); } SECTION("Does not generate post-init control commands") { PMPowerProfileTestAdapter ts(std::make_unique(), std::make_unique(), modes); ts.postInit(ctlCmds); REQUIRE(ctlCmds.commands().empty()); } SECTION("Import its mode") { PMPowerProfileTestAdapter ts( std::make_unique(), std::make_unique("pp_power_profile_mode", std::make_optional(0)), modes); ts.init(); PMPowerProfileImporterStub i("POWER_SAVING"); ts.importControl(i); REQUIRE(ts.mode() == "POWER_SAVING"); } SECTION("Export its mode and available modes") { PMPowerProfileTestAdapter ts( std::make_unique(), std::make_unique("pp_power_profile_mode", std::make_optional(0)), modes); ts.init(); trompeloeil::sequence seq; PMPowerProfileExporterMock e; REQUIRE_CALL(e, takePMPowerProfileModes(trompeloeil::_)).IN_SEQUENCE(seq); REQUIRE_CALL(e, takePMPowerProfileMode(trompeloeil::eq("3D_FULL_SCREEN"))) .IN_SEQUENCE(seq); ts.exportControl(e); } SECTION("Generate clean control commands...") { SECTION("Including performance level command when its value is not manual") { PMPowerProfileTestAdapter ts( std::make_unique( "power_dpm_force_performance_level", "auto"), std::make_unique("pp_power_profile_mode", std::make_optional(0)), modes); ts.init(); ts.cleanControl(ctlCmds); auto &commands = ctlCmds.commands(); REQUIRE(commands.size() == 2); auto &[cmd0Path, cmd0Value] = commands[0]; REQUIRE(cmd0Path == "power_dpm_force_performance_level"); REQUIRE(cmd0Value == "manual"); auto &[cmd1Path, cmd1Value] = commands[1]; REQUIRE(cmd1Path == "pp_power_profile_mode"); REQUIRE(cmd1Value == "0"); } SECTION("Excluding performance level command when its value is manual") { PMPowerProfileTestAdapter ts( std::make_unique( "power_dpm_force_performance_level", "manual"), std::make_unique("pp_power_profile_mode", std::make_optional(0)), modes); ts.init(); ts.cleanControl(ctlCmds); auto &commands = ctlCmds.commands(); REQUIRE(commands.size() == 1); auto &[cmd0Path, cmd0Value] = commands[0]; REQUIRE(cmd0Path == "pp_power_profile_mode"); REQUIRE(cmd0Value == "0"); } } SECTION("Does not generate sync control commands when is synced") { PMPowerProfileTestAdapter ts( std::make_unique( "power_dpm_force_performance_level", "manual"), std::make_unique("pp_power_profile_mode", std::make_optional(1)), modes); ts.init(); ts.mode("POWER_SAVING"); ts.syncControl(ctlCmds); REQUIRE(ctlCmds.commands().empty()); } SECTION("Does generate sync control commands when...") { SECTION("power_dpm_force_performance_level is out of sync") { PMPowerProfileTestAdapter ts( std::make_unique( "power_dpm_force_performance_level", "auto"), std::make_unique("pp_power_profile_mode", std::make_optional(0)), modes); ts.init(); ts.mode("POWER_SAVING"); ts.syncControl(ctlCmds); auto &commands = ctlCmds.commands(); REQUIRE(commands.size() == 2); auto &[cmd0Path, cmd0Value] = commands[0]; REQUIRE(cmd0Path == "power_dpm_force_performance_level"); REQUIRE(cmd0Value == "manual"); auto &[cmd1Path, cmd1Value] = commands[1]; REQUIRE(cmd1Path == "pp_power_profile_mode"); REQUIRE(cmd1Value == "1"); } SECTION("pp_power_profile_mode is out of sync") { PMPowerProfileTestAdapter ts( std::make_unique( "power_dpm_force_performance_level", "manual"), std::make_unique("pp_power_profile_mode", std::make_optional(1)), modes); ts.init(); ts.syncControl(ctlCmds); REQUIRE(ctlCmds.commands().size() == 1); auto &[cmdPath, cmdValue] = ctlCmds.commands().front(); REQUIRE(cmdPath == "pp_power_profile_mode"); REQUIRE(cmdValue == "0"); } } } } // namespace Tests::AMD::PMPowerProfile