| 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/controlmock.h" |
| 9 | #include "common/sensormock.h" |
| 10 | #include "core/components/gpu.h" |
| 11 | #include "core/info/igpuinfo.h" |
| 12 | |
| 13 | extern template struct trompeloeil::reporter<trompeloeil::specialized>; |
| 14 | |
| 15 | namespace Tests::GPU { |
| 16 | |
| 17 | class GPUInfoStub : public IGPUInfo |
| 18 | { |
| 19 | public: |
| 20 | Vendor vendor() const override |
| 21 | { |
| 22 | return Vendor::AMD; |
| 23 | } |
| 24 | |
| 25 | int index() const override |
| 26 | { |
| 27 | return 123; |
| 28 | } |
| 29 | |
| 30 | IGPUInfo::Path const &path() const override |
| 31 | { |
| 32 | return path_; |
| 33 | } |
| 34 | |
| 35 | std::vector<std::string> keys() const override |
| 36 | { |
| 37 | return std::vector<std::string>{"info_k1"}; |
| 38 | } |
| 39 | |
| 40 | std::string info(std::string_view) const override |
| 41 | { |
| 42 | return "info_1"; |
| 43 | } |
| 44 | |
| 45 | bool hasCapability(std::string_view) const override |
| 46 | { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | void initialize(std::vector<std::unique_ptr<IGPUInfo::IProvider>> const &, |
| 51 | IHWIDTranslator const &) override |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | IGPUInfo::Path path_{"_sys_", "_dev_"}; |
| 57 | }; |
| 58 | |
| 59 | class GPUImporterStub : public IGPU::Importer |
| 60 | { |
| 61 | public: |
| 62 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 63 | provideImporter(Item const &) override |
| 64 | { |
| 65 | return *this; |
| 66 | } |
| 67 | |
| 68 | bool provideActive() const override |
| 69 | { |
| 70 | return false; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | class GPUExporterMock : public IGPU::Exporter |
| 75 | { |
| 76 | public: |
| 77 | MAKE_MOCK1(takeActive, void(bool), override); |
| 78 | MAKE_MOCK1(takeInfo, void(IGPUInfo const &), override); |
| 79 | MAKE_MOCK1(takeSensor, void(ISensor const &), override); |
| 80 | MAKE_MOCK1( |
| 81 | provideExporter, |
| 82 | std::optional<std::reference_wrapper<Exportable::Exporter>>(Item const &), |
| 83 | override); |
| 84 | }; |
| 85 | |
| 86 | TEST_CASE("GPU tests", "[GPU]") |
| 87 | { |
| 88 | CommandQueueStub ctlCmds; |
| 89 | |
| 90 | auto info = std::make_unique<GPUInfoStub>(); |
| 91 | auto &infoStub = *info; |
| 92 | |
| 93 | std::vector<std::unique_ptr<IControl>> controls; |
| 94 | controls.emplace_back(std::make_unique<ControlMock>()); |
| 95 | auto &controlMock = static_cast<ControlMock &>(*controls[0]); |
| 96 | |
| 97 | std::vector<std::unique_ptr<ISensor>> sensors; |
| 98 | sensors.emplace_back(std::make_unique<SensorMock>()); |
| 99 | auto &sensorMock = static_cast<SensorMock &>(*sensors[0]); |
| 100 | |
| 101 | ::GPU ts(std::move(info), std::move(controls), std::move(sensors)); |
| 102 | |
| 103 | SECTION("Has IGPU ID") |
| 104 | { |
| 105 | REQUIRE(ts.ID() == IGPU::ItemID); |
| 106 | } |
| 107 | |
| 108 | SECTION("Is active by default") |
| 109 | { |
| 110 | REQUIRE(ts.active()); |
| 111 | |
| 112 | SECTION("Can be deactivated") |
| 113 | { |
| 114 | ts.activate(false); |
| 115 | |
| 116 | REQUIRE_FALSE(ts.active()); |
| 117 | |
| 118 | SECTION("Can be activated") |
| 119 | { |
| 120 | ts.activate(true); |
| 121 | |
| 122 | REQUIRE(ts.active()); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | SECTION("Has a unique key in a system component scope") |
| 128 | { |
| 129 | // 'GPU' + gpu index combination seems to be good enough to differentiate |
| 130 | // between system components. |
| 131 | REQUIRE_THAT(ts.key(), Catch::Matchers::ContainsSubstring("GPU") && |
| 132 | Catch::Matchers::ContainsSubstring("123")); |
| 133 | } |
| 134 | |
| 135 | SECTION("Its GPU information can be retrieved") |
| 136 | { |
| 137 | REQUIRE(&ts.info() == &infoStub); |
| 138 | } |
| 139 | |
| 140 | SECTION("GPU description and information can be retrieved") |
| 141 | { |
| 142 | auto [gpuDesc, infos] = ts.componentInfo(); |
| 143 | REQUIRE_THAT(gpuDesc, Catch::Matchers::ContainsSubstring("GPU") && |
| 144 | Catch::Matchers::ContainsSubstring("123")); |
| 145 | REQUIRE_FALSE(infos.empty()); |
| 146 | auto &[infoKey, info] = infos[0]; |
| 147 | REQUIRE(infoKey == "info_k1"); |
| 148 | REQUIRE(info == "info_1"); |
| 149 | } |
| 150 | |
| 151 | SECTION("Update its sensors") |
| 152 | { |
| 153 | std::string const sensorid("sensorid"); |
| 154 | ALLOW_CALL(sensorMock, ID()).LR_RETURN(sensorid); |
| 155 | REQUIRE_CALL(sensorMock, update()); |
| 156 | |
| 157 | std::unordered_map<std::string, std::unordered_set<std::string>> ignored; |
| 158 | ts.updateSensors(ignored); |
| 159 | } |
| 160 | |
| 161 | SECTION("Does not update ignored sensors") |
| 162 | { |
| 163 | std::string const sensorid("sensorid"); |
| 164 | std::unordered_map<std::string, std::unordered_set<std::string>> ignored; |
| 165 | ignored[ts.key()] = {}; |
| 166 | ignored[ts.key()].emplace(sensorid); |
| 167 | |
| 168 | ALLOW_CALL(sensorMock, ID()).LR_RETURN(sensorid); |
| 169 | FORBID_CALL(sensorMock, update()); |
| 170 | ts.updateSensors(ignored); |
| 171 | } |
| 172 | |
| 173 | SECTION("Pre-init its controls") |
| 174 | { |
| 175 | REQUIRE_CALL(controlMock, preInit(trompeloeil::_)); |
| 176 | ts.preInit(ctlCmds); |
| 177 | } |
| 178 | |
| 179 | SECTION("Post-init its controls") |
| 180 | { |
| 181 | REQUIRE_CALL(controlMock, postInit(trompeloeil::_)); |
| 182 | ts.postInit(ctlCmds); |
| 183 | } |
| 184 | |
| 185 | SECTION("Init controls") |
| 186 | { |
| 187 | REQUIRE_CALL(controlMock, init()); |
| 188 | ts.init(); |
| 189 | } |
| 190 | |
| 191 | SECTION("Only clear and sync controls when is active") |
| 192 | { |
| 193 | ALLOW_CALL(sensorMock, update()); |
| 194 | |
| 195 | REQUIRE_CALL(controlMock, clean(trompeloeil::_)); |
| 196 | REQUIRE_CALL(controlMock, sync(trompeloeil::_)); |
| 197 | ts.activate(true); |
| 198 | ts.sync(ctlCmds); |
| 199 | |
| 200 | FORBID_CALL(controlMock, clean(trompeloeil::_)); |
| 201 | FORBID_CALL(controlMock, sync(trompeloeil::_)); |
| 202 | ts.activate(false); |
| 203 | ts.sync(ctlCmds); |
| 204 | } |
| 205 | |
| 206 | SECTION("Imports its state and controls") |
| 207 | { |
| 208 | REQUIRE_CALL(controlMock, importWith(trompeloeil::_)); |
| 209 | |
| 210 | ts.activate(true); |
| 211 | GPUImporterStub i; |
| 212 | ts.importWith(i); |
| 213 | |
| 214 | REQUIRE_FALSE(ts.active()); |
| 215 | } |
| 216 | |
| 217 | SECTION("Exports its state, sensors and controls") |
| 218 | { |
| 219 | GPUExporterMock e; |
| 220 | ALLOW_CALL(e, provideExporter(trompeloeil::_)).LR_RETURN(e); |
| 221 | REQUIRE_CALL(e, takeActive(true)); |
| 222 | REQUIRE_CALL(e, takeInfo(trompeloeil::_)).LR_WITH(&_1 == &infoStub); |
| 223 | REQUIRE_CALL(e, takeSensor(trompeloeil::_)).LR_WITH(&_1 == &sensorMock); |
| 224 | |
| 225 | REQUIRE_CALL(controlMock, exportWith(trompeloeil::_)); |
| 226 | |
| 227 | ts.activate(true); |
| 228 | ts.exportWith(e); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | } // namespace Tests::GPU |