ACF $AcfVersion:0$
CMultiModelDispatcherBaseTest.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtCore/QObject>
7#include <QtTest/QtTest>
8
9// ACF includes
11#include <imod/TModelWrap.h>
12#include <iser/CSerializableBase.h>
13
14
24class CMultiModelDispatcherBaseTest: public QObject
25{
26 Q_OBJECT
27
28private slots:
29 void initTestCase();
30 void cleanupTestCase();
31
32 // Basic functionality tests
33 void testRegisterModel();
34 void testUnregisterModel();
35 void testUnregisterAllModels();
36 void testGetObjectAt();
37
38 // Notification tests
39 void testOnModelChanged();
40 void testMultipleModelNotifications();
41 void testRelevantFlagsFiltering();
42 void testModelIdDispatching();
43
44 // Edge cases
45 void testRegisterSameIdTwice();
46 void testUnregisterNonExistentModel();
47 void testEmptyDispatcher();
48
49private:
50 // Helper class: Simple data model for testing
51 class TestDataModel: public iser::CSerializableBase
52 {
53 public:
54 enum ChangeFlags
55 {
56 CF_VALUE_CHANGED = 0x1,
57 CF_STATE_CHANGED = 0x2,
58 CF_CONFIG_CHANGED = 0x4
59 };
60
61 TestDataModel() : m_value(0), m_state(0) {}
62
63 void SetValue(int value)
64 {
65 if (m_value != value)
66 {
68 changeSet.Set(CF_VALUE_CHANGED);
69 BeginChanges(changeSet);
70 m_value = value;
71 EndChanges(changeSet);
72 }
73 }
74
75 int GetValue() const { return m_value; }
76
77 void SetState(int state)
78 {
79 if (m_state != state)
80 {
82 changeSet.Set(CF_STATE_CHANGED);
83 BeginChanges(changeSet);
84 m_state = state;
85 EndChanges(changeSet);
86 }
87 }
88
89 int GetState() const { return m_state; }
90
91 void SetConfig(int config)
92 {
94 changeSet.Set(CF_CONFIG_CHANGED);
95 BeginChanges(changeSet);
96 EndChanges(changeSet);
97 }
98
99 // IChangeable interface
100 virtual int GetSupportedOperations() const override
101 {
103 }
104
107 {
108 TestDataModel* clone = new TestDataModel();
109 clone->m_value = m_value;
110 clone->m_state = m_state;
112 }
113
114 virtual bool CopyFrom(const istd::IChangeable& object,
116 {
117 const TestDataModel* other = dynamic_cast<const TestDataModel*>(&object);
118 if (other)
119 {
120 m_value = other->m_value;
121 m_state = other->m_state;
122 return true;
123 }
124 return false;
125 }
126
128 {
129 m_value = 0;
130 m_state = 0;
131 return true;
132 }
133
134 virtual bool Serialize(iser::IArchive& archive) override
135 {
136 archive.SerializeValue("value", m_value);
137 archive.SerializeValue("state", m_state);
138 return true;
139 }
140
141 private:
142 int m_value;
143 int m_state;
144 };
145
146 // Helper class: Concrete dispatcher implementation for testing
147 class TestDispatcher: public imod::CMultiModelDispatcherBase
148 {
149 public:
150 TestDispatcher() : m_notificationCount(0), m_lastModelId(-1) {}
151
152 int GetNotificationCount() const { return m_notificationCount; }
153 int GetLastModelId() const { return m_lastModelId; }
154 const istd::IChangeable::ChangeSet& GetLastChangeSet() const { return m_lastChangeSet; }
155
156 void Reset()
157 {
158 m_notificationCount = 0;
159 m_lastModelId = -1;
160 m_lastChangeSet.Clear();
161 m_changeHistory.clear();
162 }
163
169
170 const QVector<ChangeRecord>& GetChangeHistory() const { return m_changeHistory; }
171
172 protected:
173 // Reimplemented from CMultiModelDispatcherBase
174 virtual void OnModelChanged(int modelId, const istd::IChangeable::ChangeSet& changeSet) override
175 {
176 m_notificationCount++;
177 m_lastModelId = modelId;
178 m_lastChangeSet = changeSet;
179
180 ChangeRecord record;
181 record.modelId = modelId;
182 record.changeSet = changeSet;
183 m_changeHistory.append(record);
184 }
185
186 private:
187 int m_notificationCount;
188 int m_lastModelId;
189 istd::IChangeable::ChangeSet m_lastChangeSet;
190 QVector<ChangeRecord> m_changeHistory;
191 };
192
193 typedef imod::TModelWrap<TestDataModel> TestModel;
194
195 TestDispatcher* m_dispatcher;
196 TestModel* m_model1;
197 TestModel* m_model2;
198 TestModel* m_model3;
199};
Test class for CMultiModelDispatcherBase functionality.
Generic implementation of a data model changes notifier.
This model wrapper provides a simple connection between a concrete istd::IChangeable implementation a...
Definition TModelWrap.h:24
Represents an input/output persistence archive for object serialization.
Definition IArchive.h:164
Set of change flags (its IDs).
Definition IChangeable.h:36
Common interface for data model objects, which can be changed.
Definition IChangeable.h:28
CompatibilityMode
Control how relationship betweeen objects are interpreted.
@ CM_WITHOUT_REFS
External references are simple ignored.
@ SO_COPY
Copying from other object.
@ SO_RESET
Resetting of object state.
@ SO_CLONE
Creating of copy of this object.
Unique ownership smart pointer for interface types.