| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | #include <catch2/trompeloeil.hpp> |
| 6 | |
| 7 | #include "common/commandqueuestub.h" |
| 8 | #include "common/stringdatasourcestub.h" |
| 9 | #include "core/components/controls/amd/pm/powerstate/pmpowerstate.h" |
| 10 | |
| 11 | extern template struct trompeloeil::reporter<trompeloeil::specialized>; |
| 12 | |
| 13 | namespace Tests::AMD::PMPowerState { |
| 14 | |
| 15 | class PMPowerStateTestAdapter : public ::AMD::PMPowerState |
| 16 | { |
| 17 | public: |
| 18 | using ::AMD::PMPowerState::PMPowerState; |
| 19 | |
| 20 | using ::AMD::PMPowerState::cleanControl; |
| 21 | using ::AMD::PMPowerState::exportControl; |
| 22 | using ::AMD::PMPowerState::importControl; |
| 23 | using ::AMD::PMPowerState::mode; |
| 24 | using ::AMD::PMPowerState::modes; |
| 25 | using ::AMD::PMPowerState::syncControl; |
| 26 | }; |
| 27 | |
| 28 | class PMPowerStateImporterStub : public ::AMD::PMPowerState::Importer |
| 29 | { |
| 30 | public: |
| 31 | PMPowerStateImporterStub(std::string_view mode) |
| 32 | : mode_(mode) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 37 | provideImporter(Item const &) override |
| 38 | { |
| 39 | return {}; |
| 40 | } |
| 41 | |
| 42 | bool provideActive() const override |
| 43 | { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | std::string const &providePMPowerStateMode() const override |
| 48 | { |
| 49 | return mode_; |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | std::string mode_; |
| 54 | }; |
| 55 | |
| 56 | class PMPowerStateExporterMock : public ::AMD::PMPowerState::Exporter |
| 57 | { |
| 58 | public: |
| 59 | MAKE_MOCK1(takePMPowerStateMode, void(std::string const &), override); |
| 60 | MAKE_MOCK1(takePMPowerStateModes, void(std::vector<std::string> const &), |
| 61 | override); |
| 62 | MAKE_MOCK1(takeActive, void(bool), override); |
| 63 | MAKE_MOCK1( |
| 64 | provideExporter, |
| 65 | std::optional<std::reference_wrapper<Exportable::Exporter>>(Item const &), |
| 66 | override); |
| 67 | }; |
| 68 | |
| 69 | TEST_CASE("AMD PMPowerState tests", "[GPU][AMD][PM][PMPowerState]") |
| 70 | { |
| 71 | std::string const powerDpmStatePath{"power_dpm_state"}; |
| 72 | CommandQueueStub ctlCmds; |
| 73 | |
| 74 | SECTION("Has PMPowerState ID") |
| 75 | { |
| 76 | PMPowerStateTestAdapter ts(std::make_unique<StringDataSourceStub>()); |
| 77 | |
| 78 | REQUIRE(ts.ID() == ::AMD::PMPowerState::ItemID); |
| 79 | } |
| 80 | |
| 81 | SECTION("Is active by default") |
| 82 | { |
| 83 | PMPowerStateTestAdapter ts(std::make_unique<StringDataSourceStub>()); |
| 84 | |
| 85 | REQUIRE(ts.active()); |
| 86 | } |
| 87 | |
| 88 | SECTION("Has 'balanced' mode selected by default") |
| 89 | { |
| 90 | PMPowerStateTestAdapter ts(std::make_unique<StringDataSourceStub>()); |
| 91 | |
| 92 | REQUIRE(ts.mode() == "balanced"); |
| 93 | } |
| 94 | |
| 95 | SECTION("Has [battery, balanced, performance] modes") |
| 96 | { |
| 97 | std::vector<std::string> const modes{std::string("battery"), |
| 98 | std::string("balanced"), |
| 99 | std::string("performance")}; |
| 100 | PMPowerStateTestAdapter ts(std::make_unique<StringDataSourceStub>()); |
| 101 | |
| 102 | REQUIRE(ts.modes() == modes); |
| 103 | } |
| 104 | |
| 105 | SECTION("mode only sets known modes") |
| 106 | { |
| 107 | PMPowerStateTestAdapter ts(std::make_unique<StringDataSourceStub>()); |
| 108 | |
| 109 | ts.mode("performance"); |
| 110 | REQUIRE(ts.mode() == "performance"); |
| 111 | |
| 112 | ts.mode("unkown"); |
| 113 | REQUIRE(ts.mode() == "performance"); |
| 114 | } |
| 115 | |
| 116 | SECTION("Does not generate pre-init control commands") |
| 117 | { |
| 118 | PMPowerStateTestAdapter ts(std::make_unique<StringDataSourceStub>()); |
| 119 | |
| 120 | ts.preInit(ctlCmds); |
| 121 | |
| 122 | REQUIRE(ctlCmds.commands().empty()); |
| 123 | } |
| 124 | |
| 125 | SECTION("Does not generate post-init control commands") |
| 126 | { |
| 127 | PMPowerStateTestAdapter ts(std::make_unique<StringDataSourceStub>()); |
| 128 | |
| 129 | ts.postInit(ctlCmds); |
| 130 | |
| 131 | REQUIRE(ctlCmds.commands().empty()); |
| 132 | } |
| 133 | |
| 134 | SECTION("Imports its mode") |
| 135 | { |
| 136 | PMPowerStateTestAdapter ts(std::make_unique<StringDataSourceStub>()); |
| 137 | PMPowerStateImporterStub i("performance"); |
| 138 | ts.importControl(i); |
| 139 | |
| 140 | REQUIRE(ts.mode() == "performance"); |
| 141 | } |
| 142 | |
| 143 | SECTION("Export its mode and available modes") |
| 144 | { |
| 145 | PMPowerStateTestAdapter ts(std::make_unique<StringDataSourceStub>()); |
| 146 | PMPowerStateExporterMock e; |
| 147 | |
| 148 | trompeloeil::sequence seq; |
| 149 | REQUIRE_CALL(e, takePMPowerStateModes(trompeloeil::_)).IN_SEQUENCE(seq); |
| 150 | REQUIRE_CALL(e, takePMPowerStateMode(trompeloeil::eq("balanced"))) |
| 151 | .IN_SEQUENCE(seq); |
| 152 | |
| 153 | ts.exportControl(e); |
| 154 | } |
| 155 | |
| 156 | SECTION("Does not generate clean control commands") |
| 157 | { |
| 158 | PMPowerStateTestAdapter ts(std::make_unique<StringDataSourceStub>()); |
| 159 | ts.cleanControl(ctlCmds); |
| 160 | |
| 161 | REQUIRE(ctlCmds.commands().empty()); |
| 162 | } |
| 163 | |
| 164 | SECTION("Does not generate sync control commands when is synced") |
| 165 | { |
| 166 | PMPowerStateTestAdapter ts( |
| 167 | std::make_unique<StringDataSourceStub>(powerDpmStatePath, "balanced")); |
| 168 | ts.syncControl(ctlCmds); |
| 169 | |
| 170 | REQUIRE(ctlCmds.commands().empty()); |
| 171 | } |
| 172 | |
| 173 | SECTION("Does generate sync control commands when is out of sync") |
| 174 | { |
| 175 | PMPowerStateTestAdapter ts( |
| 176 | std::make_unique<StringDataSourceStub>(powerDpmStatePath, "_other_")); |
| 177 | ts.syncControl(ctlCmds); |
| 178 | |
| 179 | REQUIRE(ctlCmds.commands().size() == 1); |
| 180 | |
| 181 | auto &[path, value] = ctlCmds.commands().front(); |
| 182 | REQUIRE(path == powerDpmStatePath); |
| 183 | REQUIRE(value == "balanced"); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } // namespace Tests::AMD::PMPowerState |