| 1 | /* |
| 2 | SPDX-Copyright: James Turner |
| 3 | SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | #include "testInputValue.hxx" |
| 7 | |
| 8 | #include <strstream> |
| 9 | |
| 10 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 11 | |
| 12 | |
| 13 | #include <Autopilot/autopilot.hxx> |
| 14 | #include <Main/fg_props.hxx> |
| 15 | #include <Main/globals.hxx> |
| 16 | #include <simgear/misc/inputvalue.hxx> |
| 17 | |
| 18 | |
| 19 | #include <simgear/math/sg_random.hxx> |
| 20 | #include <simgear/props/props_io.hxx> |
| 21 | |
| 22 | using namespace FGXMLAutopilot; |
| 23 | |
| 24 | // Set up function for each test. |
| 25 | void InputValueTests::setUp() |
| 26 | { |
| 27 | FGTestApi::setUp::initTestGlobals("ap-inputvalue"); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | // Clean up after each test. |
| 32 | void InputValueTests::tearDown() |
| 33 | { |
| 34 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | SGPropertyNode_ptr InputValueTests::configFromString(const std::string& s) |
| 39 | { |
| 40 | SGPropertyNode_ptr config = new SGPropertyNode; |
| 41 | |
| 42 | std::istringstream iss(s); |
| 43 | readProperties(iss, config); |
| 44 | return config; |
| 45 | } |
| 46 | |
| 47 | void InputValueTests::testPropertyPath() |
| 48 | { |
| 49 | |
| 50 | sg_srandom(999); |
| 51 | |
| 52 | auto config = configFromString(R"(<?xml version="1.0" encoding="UTF-8"?> |
| 53 | <PropertyList> |
| 54 | <property-path>/test/altitude-ft-node-path</property-path> |
| 55 | <value>1.23</value> |
| 56 | </PropertyList> |
| 57 | )"); |
| 58 | |
| 59 | fgSetString("/test/altitude-ft-node-path", "/test/a"); |
| 60 | fgSetDouble("/test/a", 0.5); |
| 61 | |
| 62 | simgear::Value_ptr valueA = new simgear::Value(*globals->get_props(), *config); |
| 63 | |
| 64 | CPPUNIT_ASSERT(valueA->is_enabled()); |
| 65 | // check value is not written back |
| 66 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.5, valueA->get_value(), 0.001); |
| 67 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.5, fgGetDouble("/test/a"), 0.001); |
| 68 | |
| 69 | fgSetDouble("/test/a", 2.34); |
| 70 | CPPUNIT_ASSERT_DOUBLES_EQUAL(2.34, valueA->get_value(), 0.001); |
| 71 | |
| 72 | fgSetString("/test/altitude-ft-node-path", "blah"); |
| 73 | CPPUNIT_ASSERT(!valueA->is_enabled()); |
| 74 | // <value> is used |
| 75 | CPPUNIT_ASSERT_DOUBLES_EQUAL(1.23, valueA->get_value(), 0.001); |
| 76 | |
| 77 | |
| 78 | fgSetDouble("/foo/bpath", 99); |
| 79 | fgSetString("/test/altitude-ft-node-path", "/foo/bpath"); |
| 80 | CPPUNIT_ASSERT(valueA->is_enabled()); |
| 81 | CPPUNIT_ASSERT_DOUBLES_EQUAL(99.0, valueA->get_value(), 0.001); |
| 82 | |
| 83 | fgSetDouble("/foo/bpath", -45.1); |
| 84 | CPPUNIT_ASSERT_DOUBLES_EQUAL(-45.1, valueA->get_value(), 0.001); |
| 85 | |
| 86 | // start with different config |
| 87 | auto config2 = configFromString(R"(<?xml version="1.0" encoding="UTF-8"?> |
| 88 | <PropertyList> |
| 89 | <property-path>/test/indicated-knots-node-path</property-path> |
| 90 | </PropertyList> |
| 91 | )"); |
| 92 | |
| 93 | simgear::Value_ptr valueB = new simgear::Value(*globals->get_props(), *config2); |
| 94 | CPPUNIT_ASSERT(!valueB->is_enabled()); |
| 95 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, valueB->get_value(), 0.001); |
| 96 | |
| 97 | fgSetString("/test/indicated-knots-node-path", "/instruments/airspeed/output/knots"); |
| 98 | CPPUNIT_ASSERT(!valueB->is_enabled()); |
| 99 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, valueB->get_value(), 0.001); |
| 100 | |
| 101 | // create the property, but this does not trigger the change listener, so |
| 102 | // stays invalid |
| 103 | fgSetDouble("/instruments/airspeed/output/knots", 415); |
| 104 | CPPUNIT_ASSERT(!valueB->is_enabled()); |
| 105 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, valueB->get_value(), 0.001); |
| 106 | |
| 107 | // set the path again (with some whitespace, which is trimmed) |
| 108 | fgSetString("/test/indicated-knots-node-path", " /instruments/airspeed/output/knots "); |
| 109 | CPPUNIT_ASSERT(valueB->is_enabled()); |
| 110 | CPPUNIT_ASSERT_DOUBLES_EQUAL(415.0, valueB->get_value(), 0.001); |
| 111 | |
| 112 | fgSetString("/test/indicated-knots-node-path", ""); |
| 113 | CPPUNIT_ASSERT(!valueB->is_enabled()); |
| 114 | |
| 115 | } |