ACF $AcfVersion:0$
CModelProxyTest.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
10#include <imod/CModelProxy.h>
11#include <imod/TModelWrap.h>
13#include <iser/CSerializableBase.h>
14
15
24class CModelProxyTest: public QObject
25{
26 Q_OBJECT
27
28private slots:
29 void initTestCase();
30 void cleanupTestCase();
31
32 // Basic functionality tests
33 void testProxyCreation();
34 void testSetModelPtr();
35 void testResetModel();
36 void testObserverAttachment();
37
38 // Model switching tests
39 void testModelSwitching();
40 void testNotificationForwarding();
41 void testObserverPersistenceAcrossSwitches();
42 void testDetachObserverFromProxy();
43
44 // Edge cases
45 void testSwitchToNullModel();
46 void testMultipleObservers();
47 void testResetWithObservers();
48
49private:
50 // Helper class: Simple data model
51 class TestDataModel: public iser::CSerializableBase
52 {
53 public:
54 enum ChangeFlags
55 {
56 CF_VALUE_CHANGED = 0x1
57 };
58
59 TestDataModel(int initialValue = 0) : m_value(initialValue) {}
60
61 void SetValue(int value)
62 {
63 if (m_value != value)
64 {
66 changeSet.Set(CF_VALUE_CHANGED);
67 BeginChanges(changeSet);
68 m_value = value;
69 EndChanges(changeSet);
70 }
71 }
72
73 int GetValue() const { return m_value; }
74
75 // IChangeable interface
76 virtual int GetSupportedOperations() const override
77 {
79 }
80
83 {
84 TestDataModel* clone = new TestDataModel(m_value);
86 }
87
88 virtual bool CopyFrom(const istd::IChangeable& object,
90 {
91 const TestDataModel* other = dynamic_cast<const TestDataModel*>(&object);
92 if (other)
93 {
94 m_value = other->m_value;
95 return true;
96 }
97 return false;
98 }
99
101 {
102 m_value = 0;
103 return true;
104 }
105
106 virtual bool Serialize(iser::IArchive& archive) override
107 {
108 archive.SerializeValue("value", m_value);
109 return true;
110 }
111
112 private:
113 int m_value;
114 };
115
116 // Helper class: Test observer
117 class TestObserver: public imod::CSingleModelObserverBase
118 {
119 public:
120 TestObserver() : m_updateCount(0) {}
121
122 int GetUpdateCount() const { return m_updateCount; }
123 const istd::IChangeable::ChangeSet& GetLastChangeSet() const { return m_lastChangeSet; }
124
125 void Reset()
126 {
127 m_updateCount = 0;
128 m_lastChangeSet.Clear();
129 }
130
131 virtual void OnUpdate(const istd::IChangeable::ChangeSet& changeSet) override
132 {
133 m_updateCount++;
134 m_lastChangeSet = changeSet;
135 }
136
137 private:
138 int m_updateCount;
139 istd::IChangeable::ChangeSet m_lastChangeSet;
140 };
141
143
144 imod::CModelProxy* m_proxy;
145 TestModel* m_model1;
146 TestModel* m_model2;
147 TestObserver* m_observer;
148};
Test class for CModelProxy functionality.
Implementation of the model proxy.
Definition CModelProxy.h:24
Basic implementation for a single model observer.
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.