| 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 "core/components/controls/controlmode.h" |
| 10 | |
| 11 | extern template struct trompeloeil::reporter<trompeloeil::specialized>; |
| 12 | |
| 13 | namespace Tests::ControlMode { |
| 14 | |
| 15 | class ControlModeTestAdapter : public ::ControlMode |
| 16 | { |
| 17 | public: |
| 18 | using ControlMode::ControlMode; |
| 19 | |
| 20 | using ControlMode::cleanControl; |
| 21 | using ControlMode::exportControl; |
| 22 | using ControlMode::importControl; |
| 23 | using ControlMode::mode; |
| 24 | using ControlMode::syncControl; |
| 25 | }; |
| 26 | |
| 27 | class ControlModeImporterStub : public ::ControlMode::Importer |
| 28 | { |
| 29 | public: |
| 30 | ControlModeImporterStub(std::string mode) |
| 31 | : mode_(std::move(mode)) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 36 | provideImporter(Item const &) override |
| 37 | { |
| 38 | return {}; |
| 39 | } |
| 40 | |
| 41 | bool provideActive() const override |
| 42 | { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | std::string const &provideMode() const override |
| 47 | { |
| 48 | return mode_; |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | std::string mode_; |
| 53 | }; |
| 54 | |
| 55 | class ControlModeExporterMock : public ::ControlMode::Exporter |
| 56 | { |
| 57 | public: |
| 58 | MAKE_MOCK1(takeMode, void(std::string const &), override); |
| 59 | MAKE_MOCK1(takeModes, void(std::vector<std::string> const &), override); |
| 60 | MAKE_MOCK1(takeActive, void(bool), override); |
| 61 | MAKE_MOCK1( |
| 62 | provideExporter, |
| 63 | std::optional<std::reference_wrapper<Exportable::Exporter>>(Item const &), |
| 64 | override); |
| 65 | }; |
| 66 | |
| 67 | TEST_CASE("ControlMode tests", "[GPU][ControlMode]") |
| 68 | { |
| 69 | std::string_view const id{"_id_"}; |
| 70 | std::vector<std::unique_ptr<IControl>> controlMocks; |
| 71 | |
| 72 | SECTION("ID is assigned") |
| 73 | { |
| 74 | ControlModeTestAdapter ts(id, std::move(controlMocks), true); |
| 75 | REQUIRE(ts.ID() == id); |
| 76 | } |
| 77 | |
| 78 | SECTION("mode are set only for known control ids") |
| 79 | { |
| 80 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 81 | ControlMock *controlMock = static_cast<ControlMock *>(controlMocks[0].get()); |
| 82 | std::string const controlMockID("_control_mock_"); |
| 83 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(controlMockID); |
| 84 | |
| 85 | ControlModeTestAdapter ts(id, std::move(controlMocks), true); |
| 86 | |
| 87 | ts.mode(controlMockID); |
| 88 | REQUIRE(ts.mode() == controlMockID); |
| 89 | |
| 90 | ts.mode("unknown"); |
| 91 | REQUIRE(ts.mode() == controlMockID); |
| 92 | } |
| 93 | |
| 94 | SECTION("Pre-init its controls") |
| 95 | { |
| 96 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 97 | ControlMock *controlMock = static_cast<ControlMock *>(controlMocks[0].get()); |
| 98 | REQUIRE_CALL(*controlMock, preInit(trompeloeil::_)); |
| 99 | |
| 100 | ControlModeTestAdapter ts(id, std::move(controlMocks), true); |
| 101 | CommandQueueStub ctlCmds; |
| 102 | ts.preInit(ctlCmds); |
| 103 | } |
| 104 | |
| 105 | SECTION("Post-init its controls") |
| 106 | { |
| 107 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 108 | ControlMock *controlMock = static_cast<ControlMock *>(controlMocks[0].get()); |
| 109 | REQUIRE_CALL(*controlMock, postInit(trompeloeil::_)); |
| 110 | |
| 111 | ControlModeTestAdapter ts(id, std::move(controlMocks), true); |
| 112 | CommandQueueStub ctlCmds; |
| 113 | ts.postInit(ctlCmds); |
| 114 | } |
| 115 | |
| 116 | SECTION("On initialization...") |
| 117 | { |
| 118 | SECTION("Initialize its controls") |
| 119 | { |
| 120 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 121 | ControlMock *controlMock = |
| 122 | static_cast<ControlMock *>(controlMocks[0].get()); |
| 123 | std::string const controlMockID("_control_mock_"); |
| 124 | REQUIRE_CALL(*controlMock, init()); |
| 125 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(controlMockID); |
| 126 | ALLOW_CALL(*controlMock, active()).RETURN(true); |
| 127 | |
| 128 | ControlModeTestAdapter ts(id, std::move(controlMocks), true); |
| 129 | ts.init(); |
| 130 | } |
| 131 | |
| 132 | SECTION("Sets as active control the first active control, deactivating the " |
| 133 | "following active controls") |
| 134 | { |
| 135 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 136 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 137 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 138 | |
| 139 | ControlMock *controlMock = |
| 140 | static_cast<ControlMock *>(controlMocks[0].get()); |
| 141 | std::string const inactiveControlMockID("_inactive_control_mock_"); |
| 142 | ALLOW_CALL(*controlMock, init()); |
| 143 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(inactiveControlMockID); |
| 144 | ALLOW_CALL(*controlMock, active()).RETURN(false); |
| 145 | |
| 146 | std::string const activeControlMockID("_active_control_mock_"); |
| 147 | |
| 148 | controlMock = static_cast<ControlMock *>(controlMocks[1].get()); |
| 149 | ALLOW_CALL(*controlMock, init()); |
| 150 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(activeControlMockID); |
| 151 | ALLOW_CALL(*controlMock, active()).RETURN(true); |
| 152 | |
| 153 | controlMock = static_cast<ControlMock *>(controlMocks[2].get()); |
| 154 | ALLOW_CALL(*controlMock, init()); |
| 155 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(activeControlMockID); |
| 156 | ALLOW_CALL(*controlMock, active()).RETURN(true); |
| 157 | REQUIRE_CALL(*controlMock, activate(false)); |
| 158 | |
| 159 | ControlModeTestAdapter ts(id, std::move(controlMocks), true); |
| 160 | ts.init(); |
| 161 | |
| 162 | REQUIRE(ts.mode() == activeControlMockID); |
| 163 | } |
| 164 | |
| 165 | SECTION("Set the first control as active when all controls are inactive") |
| 166 | { |
| 167 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 168 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 169 | |
| 170 | ControlMock *controlMock = |
| 171 | static_cast<ControlMock *>(controlMocks[0].get()); |
| 172 | std::string const inactiveControlMockID0("_inactive_control_mock_0"); |
| 173 | ALLOW_CALL(*controlMock, init()); |
| 174 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(inactiveControlMockID0); |
| 175 | ALLOW_CALL(*controlMock, active()).RETURN(false); |
| 176 | REQUIRE_CALL(*controlMock, activate(true)); |
| 177 | |
| 178 | controlMock = static_cast<ControlMock *>(controlMocks[1].get()); |
| 179 | std::string const inactiveControlMockID1("_inactive_control_mock_1"); |
| 180 | ALLOW_CALL(*controlMock, init()); |
| 181 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(inactiveControlMockID1); |
| 182 | ALLOW_CALL(*controlMock, active()).RETURN(false); |
| 183 | |
| 184 | ControlModeTestAdapter ts(id, std::move(controlMocks), true); |
| 185 | ts.init(); |
| 186 | |
| 187 | REQUIRE(ts.mode() == inactiveControlMockID0); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | SECTION("Imports controls and mode") |
| 192 | { |
| 193 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 194 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 195 | |
| 196 | ControlMock *controlMock = static_cast<ControlMock *>(controlMocks[0].get()); |
| 197 | std::string const activeControlMockID("_active_control_mock_"); |
| 198 | ALLOW_CALL(*controlMock, init()); |
| 199 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(activeControlMockID); |
| 200 | ALLOW_CALL(*controlMock, active()).RETURN(true); |
| 201 | REQUIRE_CALL(*controlMock, importWith(trompeloeil::_)); |
| 202 | REQUIRE_CALL(*controlMock, activate(false)); |
| 203 | |
| 204 | controlMock = static_cast<ControlMock *>(controlMocks[1].get()); |
| 205 | std::string const newActiveControlMockID("_new_active_control_mock_"); |
| 206 | ALLOW_CALL(*controlMock, init()); |
| 207 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(newActiveControlMockID); |
| 208 | ALLOW_CALL(*controlMock, active()).RETURN(false); |
| 209 | REQUIRE_CALL(*controlMock, importWith(trompeloeil::_)); |
| 210 | REQUIRE_CALL(*controlMock, activate(true)); |
| 211 | |
| 212 | ControlModeImporterStub i("_new_active_control_mock_"); |
| 213 | ControlModeTestAdapter ts(id, std::move(controlMocks), true); |
| 214 | ts.init(); |
| 215 | auto oldMode = ts.mode(); |
| 216 | ts.importControl(i); |
| 217 | |
| 218 | REQUIRE(ts.mode() != oldMode); |
| 219 | REQUIRE(ts.mode() == newActiveControlMockID); |
| 220 | } |
| 221 | |
| 222 | SECTION("Export controls and mode") |
| 223 | { |
| 224 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 225 | |
| 226 | ControlMock *controlMock = static_cast<ControlMock *>(controlMocks[0].get()); |
| 227 | std::string const controlMockID("_control_mock_"); |
| 228 | ALLOW_CALL(*controlMock, init()); |
| 229 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(controlMockID); |
| 230 | ALLOW_CALL(*controlMock, active()).RETURN(true); |
| 231 | REQUIRE_CALL(*controlMock, exportWith(trompeloeil::_)); |
| 232 | |
| 233 | std::vector<std::string> const modes{controlMockID}; |
| 234 | ControlModeExporterMock e; |
| 235 | REQUIRE_CALL(e, takeModes(trompeloeil::eq(modes))); |
| 236 | REQUIRE_CALL(e, takeMode(trompeloeil::eq(controlMockID))); |
| 237 | |
| 238 | ControlModeTestAdapter ts(id, std::move(controlMocks), true); |
| 239 | ts.init(); |
| 240 | ts.exportControl(e); |
| 241 | } |
| 242 | |
| 243 | SECTION("Clean controls") |
| 244 | { |
| 245 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 246 | |
| 247 | ControlMock *controlMock = static_cast<ControlMock *>(controlMocks[0].get()); |
| 248 | std::string const controlMockID("_control_mock_"); |
| 249 | ALLOW_CALL(*controlMock, init()); |
| 250 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(controlMockID); |
| 251 | ALLOW_CALL(*controlMock, active()).RETURN(true); |
| 252 | REQUIRE_CALL(*controlMock, clean(trompeloeil::_)); |
| 253 | |
| 254 | ControlModeTestAdapter ts(id, std::move(controlMocks), true); |
| 255 | ts.init(); |
| 256 | CommandQueueStub ctlCmds; |
| 257 | ts.cleanControl(ctlCmds); |
| 258 | } |
| 259 | |
| 260 | SECTION("Sync controls") |
| 261 | { |
| 262 | controlMocks.emplace_back(std::make_unique<ControlMock>()); |
| 263 | |
| 264 | ControlMock *controlMock = static_cast<ControlMock *>(controlMocks[0].get()); |
| 265 | std::string const controlMockID("_control_mock_"); |
| 266 | ALLOW_CALL(*controlMock, init()); |
| 267 | ALLOW_CALL(*controlMock, ID()).LR_RETURN(controlMockID); |
| 268 | ALLOW_CALL(*controlMock, active()).RETURN(true); |
| 269 | REQUIRE_CALL(*controlMock, sync(trompeloeil::_)); |
| 270 | |
| 271 | ControlModeTestAdapter ts(id, std::move(controlMocks), true); |
| 272 | ts.init(); |
| 273 | CommandQueueStub ctlCmds; |
| 274 | ts.syncControl(ctlCmds); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | } // namespace Tests::ControlMode |