ACF $AcfVersion:0$
TModelWrapTest.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/TModelWrap.h>
12#include <iser/CSerializableBase.h>
13
14
25class TModelWrapTest: public QObject
26{
27 Q_OBJECT
28
29private slots:
30 void initTestCase();
31 void cleanupTestCase();
32
33 // Basic functionality tests
34 void testModelWrapCreation();
35 void testSetBaseObject();
36 void testGetSupportedOperations();
37 void testCloneMe();
38
39 // Observer tests
40 void testObserverAttachment();
41 void testChangeNotification();
42 void testBeginEndChanges();
43 void testBeginEndChangeGroup();
44
45 // Data modification tests
46 void testModelDataAccess();
47 void testCopyFrom();
48 void testResetData();
49
50private:
51 // Helper class: Simple data class to wrap
52 class SimpleData: public iser::CSerializableBase
53 {
54 public:
55 enum ChangeFlags
56 {
57 CF_VALUE_CHANGED = 0x1,
58 CF_TEXT_CHANGED = 0x2
59 };
60
61 SimpleData() : m_value(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 SetText(const QString& text)
78 {
79 if (m_text != text)
80 {
82 changeSet.Set(CF_TEXT_CHANGED);
83 BeginChanges(changeSet);
84 m_text = text;
85 EndChanges(changeSet);
86 }
87 }
88
89 QString GetText() const { return m_text; }
90
91 // IChangeable interface
92 virtual int GetSupportedOperations() const override
93 {
95 }
96
99 {
100 SimpleData* clone = new SimpleData();
101 clone->m_value = m_value;
102 clone->m_text = m_text;
104 }
105
106 virtual bool CopyFrom(const istd::IChangeable& object,
108 {
109 const SimpleData* other = dynamic_cast<const SimpleData*>(&object);
110 if (other)
111 {
112 m_value = other->m_value;
113 m_text = other->m_text;
114 return true;
115 }
116 return false;
117 }
118
120 {
121 m_value = 0;
122 m_text.clear();
123 return true;
124 }
125
126 virtual bool Serialize(iser::IArchive& archive) override
127 {
128 archive.SerializeValue("value", m_value);
129 archive.SerializeValue("text", m_text);
130 return true;
131 }
132
133 private:
134 int m_value;
135 QString m_text;
136 };
137
138 // Helper class: Observer for testing notifications
139 class TestObserver: public imod::CSingleModelObserverBase
140 {
141 public:
142 TestObserver() : m_beforeUpdateCount(0), m_afterUpdateCount(0) {}
143
144 int GetBeforeUpdateCount() const { return m_beforeUpdateCount; }
145 int GetAfterUpdateCount() const { return m_afterUpdateCount; }
146 const istd::IChangeable::ChangeSet& GetLastChangeSet() const { return m_lastChangeSet; }
147
148 void Reset()
149 {
150 m_beforeUpdateCount = 0;
151 m_afterUpdateCount = 0;
152 m_lastChangeSet.Clear();
153 }
154
155 // Reimplemented from IObserver
156 virtual void BeforeUpdate(imod::IModel* modelPtr) override
157 {
158 CSingleModelObserverBase::BeforeUpdate(modelPtr);
159 m_beforeUpdateCount++;
160 }
161
162 virtual void OnUpdate(const istd::IChangeable::ChangeSet& changeSet) override
163 {
164 m_afterUpdateCount++;
165 m_lastChangeSet = changeSet;
166 }
167
168 private:
169 int m_beforeUpdateCount;
170 int m_afterUpdateCount;
171 istd::IChangeable::ChangeSet m_lastChangeSet;
172 };
173
175
176 WrappedModel* m_model;
177 TestObserver* m_observer;
178};
Test class for TModelWrap functionality.
Basic implementation for a single model observer.
Common interface for model objects, that supports Model/Observer design pattern.
Definition IModel.h:25
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.