Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkVerboseLimitedLinearUndoTest.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkInteractionConst.h"
14 #include "mitkOperation.h"
15 #include "mitkUndoController.h"
17 
18 #include "mitkTestingMacros.h"
19 
20 #include <iostream>
21 
23 
24 namespace mitk
25 {
29  class TestOperation : public Operation
30  {
31  public:
32  TestOperation(OperationType operationType) : Operation(operationType) { g_GlobalCounter++; };
33  ~TestOperation() override { g_GlobalCounter--; };
34  };
35 } // namespace
36 
56 int mitkVerboseLimitedLinearUndoTest(int /* argc */, char * /*argv*/ [])
57 {
58  // always start with this!
59  MITK_TEST_BEGIN("VerboseLimitedLinearUndo")
60 
61  // an UndoController for the management
62  auto myUndoController = new mitk::UndoController();
63 
64  // set model, even if it is verboseLimitedLinearUndo by default; this already is tested by UndoControllerTest!
65  myUndoController->SwitchUndoModel(mitk::UndoController::VERBOSE_LIMITEDLINEARUNDO);
66 
67  for (int i = 0; i < 2; i++)
68  {
69  auto doOp = new mitk::TestOperation(mitk::OpTEST);
70  auto undoOp = new mitk::TestOperation(mitk::OpTEST);
71  mitk::OperationEvent *operationEvent = new mitk::OperationEvent(nullptr, doOp, undoOp, "Test");
72  myUndoController->SetOperationEvent(operationEvent);
73  // increase the ID to separate the operationEvents from each other. Otherwise they would be undone all together at
74  // once.
76  }
77 
78  // now 2 * 2 operation should have been instanciated
79  MITK_TEST_CONDITION_REQUIRED(g_GlobalCounter == 4, "checking initialization of mitkOperation");
80 
81  // undo one operation; 1 operationEvent element in undo list, 1 in Redo list
82  myUndoController->Undo();
83 
84  // sending two new OperationEvents: RedoList should be deleted and memory of operations freed
85  for (int i = 0; i < 2; i++)
86  {
87  auto doOp = new mitk::TestOperation(mitk::OpTEST);
88  auto undoOp = new mitk::TestOperation(mitk::OpTEST);
89  mitk::OperationEvent *operationEvent = new mitk::OperationEvent(nullptr, doOp, undoOp, "Test");
90  myUndoController->SetOperationEvent(operationEvent);
91  // increase the ID to separate the operationEvents from each other. Otherwise they would be undone all together at
92  // once.
94  }
95 
96  // 2 operations should have been deleted, 4 should have been added
97  MITK_TEST_CONDITION_REQUIRED(g_GlobalCounter == 6, "checking adding of operations");
98 
99  // two operations to RedoList
100  myUndoController->Undo();
101  myUndoController->ClearRedoList();
102 
103  // one operationEvent containing 2 operations should have been deleted
104  MITK_TEST_CONDITION_REQUIRED(g_GlobalCounter == 4, "checking deleting RedoList");
105 
106  // clear all
107  myUndoController->Clear();
108  MITK_TEST_CONDITION_REQUIRED(g_GlobalCounter == 0, "checking deleting all operations in UndoModel");
109 
110  // sending two new OperationEvents
111  for (int i = 0; i < 2; i++)
112  {
113  auto doOp = new mitk::TestOperation(mitk::OpTEST);
114  auto undoOp = new mitk::TestOperation(mitk::OpTEST);
115  mitk::OperationEvent *operationEvent = new mitk::OperationEvent(nullptr, doOp, undoOp, "Test");
116  myUndoController->SetOperationEvent(operationEvent);
117  // increase the ID to separate the operationEvents from each other. Otherwise they would be undone all together at
118  // once.
120  }
121  MITK_TEST_CONDITION_REQUIRED(g_GlobalCounter == 4, "checking added operations in UndoModel");
122 
123  delete myUndoController;
124 
125  // after deleting UndoController g_GlobalCounter will still be 4 because m_CurrentUndoModel inside myUndoModel is a
126  // static singleton
127  MITK_TEST_CONDITION_REQUIRED(g_GlobalCounter == 4, "checking singleton UndoModel");
128 
129  // always end with this!
130  MITK_TEST_END()
131  // operations will be deleted after terminating the application
132 }
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
DataCollection - Class to facilitate loading/accessing structured data.
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
int mitkVerboseLimitedLinearUndoTest(int, char *[])
Test of the LimitedLinearUndo object.
Constants for most interaction classes, due to the generic StateMachines.
int OperationType
Definition: mitkOperation.h:23
static void IncCurrObjectEventId()
Increases the current ObjectEventId For example if a button click generates operations the ObjectEven...
and MITK_TEST_END()
Represents a pair of operations: undo and the according redo.