diff --git a/core/tests/Variable/TestVariableSync.cpp b/core/tests/Variable/TestVariableSync.cpp index bda2f7d..4f09d13 100644 --- a/core/tests/Variable/TestVariableSync.cpp +++ b/core/tests/Variable/TestVariableSync.cpp @@ -37,6 +37,36 @@ std::vector values(const SqpRange &range) return result; } +void validateRanges(VariableController &variableController, + const std::map &expectedRanges) +{ + for (const auto &expectedRangeEntry : expectedRanges) { + auto variableIndex = expectedRangeEntry.first; + auto expectedRange = expectedRangeEntry.second; + + // Gets the variable in the controller + auto variable = variableController.variableModel()->variable(variableIndex); + + // Compares variable's range to the expected range + QVERIFY(variable != nullptr); + auto range = variable->range(); + qInfo() << "range vs expected range" << range << expectedRange; + QCOMPARE(range, expectedRange); + + // Compares variable's data with values expected for its range + auto dataSeries = variable->dataSeries(); + QVERIFY(dataSeries != nullptr); + + auto it = dataSeries->xAxisRange(range.m_TStart, range.m_TEnd); + auto expectedValues = values(range); + qInfo() << std::distance(it.first, it.second) << expectedValues.size(); + QVERIFY(std::equal(it.first, it.second, expectedValues.cbegin(), expectedValues.cend(), + [](const auto &dataSeriesIt, const auto &expectedValue) { + return dataSeriesIt.value() == expectedValue; + })); + } +} + /// Provider used for the tests class TestProvider : public IDataProvider { std::shared_ptr clone() const { return std::make_shared(); } @@ -97,8 +127,8 @@ struct Create : public IOperation { * Variable move/shift operation in the controller */ struct Move : public IOperation { - explicit Move(int index, const SqpRange &newRange, bool shift = false) - : m_Index{index}, m_NewRange{newRange}, m_Shift{shift} + explicit Move(int index, const SqpRange &newRange, bool shift = false, int delayMS = 10) + : m_Index{index}, m_NewRange{newRange}, m_Shift{shift}, m_DelayMs{delayMS} { } @@ -106,12 +136,14 @@ struct Move : public IOperation { { if (auto variable = variableController.variableModel()->variable(m_Index)) { variableController.onRequestDataLoading({variable}, m_NewRange, !m_Shift); + QTest::qWait(m_DelayMs); } } int m_Index; ///< The index of the variable to move SqpRange m_NewRange; ///< The new range of the variable bool m_Shift; ///< Performs a shift ( + int m_DelayMs; ///< wait the delay after running the request ( }; /** @@ -169,11 +201,17 @@ private slots: /// Input data for @sa testSyncWithAborting() void testSyncWithAborting_data(); - /// Tests synchronization between variables through several operations with aborting + /// Input data for @sa testSyncOneVar() + void testSyncOneVar_data(); + + /// Tests synchronization between variables through several operations with automatic aborting void testSyncWithAborting(); /// Tests synchronization between variables through several operations void testSync(); + + /// Tests synchronization between variables through several operations + void testSyncOneVar(); }; namespace { @@ -312,12 +350,6 @@ void testSyncCase1WithAborting() {std::make_shared(0, var0NewRange), {{0, var0NewRange}, {1, var1ExpectedRange}}}); }; - // auto oldRange = newRange; - // newRange = range({14, 30}, {15, 30}); - // auto expectedRange = oldRange; - // iterations.push_back( - // {std::make_shared(0, newRange), {{0, oldRange}, {1, expectedRange}}}); - // Pan left moveVar0(range({14, 30}, {15, 30}), range({14, 30}, {15, 30})); // Pan right @@ -389,6 +421,64 @@ void testSyncCase2() QTest::newRow("sync2") << syncId << initialRange << iterations << 4000; // QTest::newRow("sync3") << syncId << initialRange << iterations << 5000; } + +void testSyncOnVarCase1() +{ + // Id used to synchronize variables in the controller + auto syncId = QUuid::createUuid(); + + /// Generates a range according to a start time and a end time (the date is the same) + auto range = [](const QTime &startTime, const QTime &endTime) { + return SqpRange{DateUtils::secondsSinceEpoch(QDateTime{{2017, 1, 1}, startTime, Qt::UTC}), + DateUtils::secondsSinceEpoch(QDateTime{{2017, 1, 1}, endTime, Qt::UTC})}; + }; + + auto initialRange = range({12, 0}, {13, 0}); + + Iterations creations{}; + // Creates variables var0, var1 and var2 + creations.push_back({std::make_shared(0), {{0, initialRange}}}); + + Iterations synchronization{}; + // Adds variables into the sync group (ranges don't need to be tested here) + synchronization.push_back({std::make_shared(0, syncId)}); + + Iterations iterations{}; + + // Moves var0 through several operations + auto moveOp = [&iterations](const auto &requestedRange, const auto &expectedRange, auto delay) { + iterations.push_back( + {std::make_shared(0, requestedRange, true, delay), {{0, expectedRange}}}); + }; + + // we assume here 300 ms is enough to finsh a operation + int delayToFinish = 300; + // jump to right, let's the operation time to finish + moveOp(range({14, 30}, {15, 30}), range({14, 30}, {15, 30}), delayToFinish); + // pan to right, let's the operation time to finish + moveOp(range({14, 45}, {15, 45}), range({14, 45}, {15, 45}), delayToFinish); + // jump to left, let's the operation time to finish + moveOp(range({03, 30}, {04, 30}), range({03, 30}, {04, 30}), delayToFinish); + // Pan to left, let's the operation time to finish + moveOp(range({03, 10}, {04, 10}), range({03, 10}, {04, 10}), delayToFinish); + // Zoom in, let's the operation time to finish + moveOp(range({03, 30}, {04, 00}), range({03, 30}, {04, 00}), delayToFinish); + // Zoom out left, let's the operation time to finish + moveOp(range({01, 10}, {18, 10}), range({01, 10}, {18, 10}), delayToFinish); + // Go back to initial range + moveOp(initialRange, initialRange, delayToFinish); + + + // jump to right, let's the operation time to finish + // moveOp(range({14, 30}, {15, 30}), initialRange, delayToFinish); + // Zoom out left, let's the operation time to finish + moveOp(range({01, 10}, {18, 10}), initialRange, delayToFinish); + // Go back to initial range + moveOp(initialRange, initialRange, 300); + + QTest::newRow("syncOnVarCase1") << syncId << initialRange << std::move(creations) + << std::move(iterations); +} } void TestVariableSync::testSync_data() @@ -429,10 +519,26 @@ void TestVariableSync::testSyncWithAborting_data() testSyncCase1WithAborting(); } -void TestVariableSync::testSync() +void TestVariableSync::testSyncOneVar_data() { - return; + // ////////////// // + // Test structure // + // ////////////// // + + QTest::addColumn("syncId"); + QTest::addColumn("initialRange"); + QTest::addColumn("creations"); + QTest::addColumn("iterations"); + // ////////// // + // Test cases // + // ////////// // + + testSyncOnVarCase1(); +} + +void TestVariableSync::testSync() +{ // Inits controllers TimeController timeController{}; VariableController variableController{}; @@ -445,33 +551,6 @@ void TestVariableSync::testSync() // Synchronization group used variableController.onAddSynchronizationGroupId(syncId); - auto validateRanges = [&variableController](const auto &expectedRanges) { - for (const auto &expectedRangeEntry : expectedRanges) { - auto variableIndex = expectedRangeEntry.first; - auto expectedRange = expectedRangeEntry.second; - - // Gets the variable in the controller - auto variable = variableController.variableModel()->variable(variableIndex); - - // Compares variable's range to the expected range - QVERIFY(variable != nullptr); - auto range = variable->range(); - QCOMPARE(range, expectedRange); - - // Compares variable's data with values expected for its range - auto dataSeries = variable->dataSeries(); - QVERIFY(dataSeries != nullptr); - - auto it = dataSeries->xAxisRange(range.m_TStart, range.m_TEnd); - auto expectedValues = values(range); - qInfo() << std::distance(it.first, it.second) << expectedValues.size(); - QVERIFY(std::equal(it.first, it.second, expectedValues.cbegin(), expectedValues.cend(), - [](const auto &dataSeriesIt, const auto &expectedValue) { - return dataSeriesIt.value() == expectedValue; - })); - } - }; - // For each iteration: // - execute operation // - compare the variables' state to the expected states @@ -481,7 +560,7 @@ void TestVariableSync::testSync() iteration.m_Operation->exec(variableController); QTest::qWait(operationDelay); - validateRanges(iteration.m_ExpectedRanges); + validateRanges(variableController, iteration.m_ExpectedRanges); } } @@ -499,34 +578,6 @@ void TestVariableSync::testSyncWithAborting() // Synchronization group used variableController.onAddSynchronizationGroupId(syncId); - auto validateRanges = [&variableController](const auto &expectedRanges) { - for (const auto &expectedRangeEntry : expectedRanges) { - auto variableIndex = expectedRangeEntry.first; - auto expectedRange = expectedRangeEntry.second; - - // Gets the variable in the controller - auto variable = variableController.variableModel()->variable(variableIndex); - - // Compares variable's range to the expected range - QVERIFY(variable != nullptr); - auto range = variable->range(); - qInfo() << "range vs expected range" << range << variable->range(); - QCOMPARE(range, expectedRange); - - // Compares variable's data with values expected for its range - auto dataSeries = variable->dataSeries(); - QVERIFY(dataSeries != nullptr); - - auto it = dataSeries->xAxisRange(range.m_TStart, range.m_TEnd); - auto expectedValues = values(range); - qInfo() << std::distance(it.first, it.second) << expectedValues.size(); - QVERIFY(std::equal(it.first, it.second, expectedValues.cbegin(), expectedValues.cend(), - [](const auto &dataSeriesIt, const auto &expectedValue) { - return dataSeriesIt.value() == expectedValue; - })); - } - }; - // For each iteration: // - execute operation // - compare the variables' state to the expected states @@ -544,9 +595,42 @@ void TestVariableSync::testSyncWithAborting() } QTest::qWait(operationDelay); - validateRanges(iterations.back().m_ExpectedRanges); + validateRanges(variableController, iterations.back().m_ExpectedRanges); } +void TestVariableSync::testSyncOneVar() +{ + // Inits controllers + TimeController timeController{}; + VariableController variableController{}; + variableController.setTimeController(&timeController); + + QFETCH(QUuid, syncId); + QFETCH(SqpRange, initialRange); + timeController.onTimeToUpdate(initialRange); + + // Synchronization group used + variableController.onAddSynchronizationGroupId(syncId); + + // For each iteration: + // - execute operation + // - compare the variables' state to the expected states + QFETCH(Iterations, iterations); + QFETCH(Iterations, creations); + + for (const auto &creation : creations) { + creation.m_Operation->exec(variableController); + QTest::qWait(300); + } + + for (const auto &iteration : iterations) { + iteration.m_Operation->exec(variableController); + } + + if (!iterations.empty()) { + validateRanges(variableController, iterations.back().m_ExpectedRanges); + } +} QTEST_MAIN(TestVariableSync)